|
|
a3e2b5 |
From ee14a2bd3d95b5d15e4d72ee2582b366e5009a86 Mon Sep 17 00:00:00 2001
|
|
|
a3e2b5 |
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
|
|
|
a3e2b5 |
Date: Sat, 26 Jan 2019 11:27:18 +0100
|
|
|
a3e2b5 |
Subject: [PATCH] basic/prioq: add prioq_peek_item()
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
(cherry-picked from commit ef21b3b5bf824e652addf850bcfd9374c7b33ce8)
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
Related: #1664976
|
|
|
a3e2b5 |
---
|
|
|
a3e2b5 |
src/basic/prioq.c | 7 +++----
|
|
|
a3e2b5 |
src/basic/prioq.h | 8 +++++++-
|
|
|
a3e2b5 |
src/test/test-prioq.c | 23 +++++++++++++++++------
|
|
|
a3e2b5 |
3 files changed, 27 insertions(+), 11 deletions(-)
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
diff --git a/src/basic/prioq.c b/src/basic/prioq.c
|
|
|
a3e2b5 |
index ef28a086d1..0bf58c1f16 100644
|
|
|
a3e2b5 |
--- a/src/basic/prioq.c
|
|
|
a3e2b5 |
+++ b/src/basic/prioq.c
|
|
|
a3e2b5 |
@@ -259,15 +259,14 @@ int prioq_reshuffle(Prioq *q, void *data, unsigned *idx) {
|
|
|
a3e2b5 |
return 1;
|
|
|
a3e2b5 |
}
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
-void *prioq_peek(Prioq *q) {
|
|
|
a3e2b5 |
-
|
|
|
a3e2b5 |
+void *prioq_peek_by_index(Prioq *q, unsigned idx) {
|
|
|
a3e2b5 |
if (!q)
|
|
|
a3e2b5 |
return NULL;
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
- if (q->n_items <= 0)
|
|
|
a3e2b5 |
+ if (idx >= q->n_items)
|
|
|
a3e2b5 |
return NULL;
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
- return q->items[0].data;
|
|
|
a3e2b5 |
+ return q->items[idx].data;
|
|
|
a3e2b5 |
}
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
void *prioq_pop(Prioq *q) {
|
|
|
a3e2b5 |
diff --git a/src/basic/prioq.h b/src/basic/prioq.h
|
|
|
a3e2b5 |
index e036175260..c381523525 100644
|
|
|
a3e2b5 |
--- a/src/basic/prioq.h
|
|
|
a3e2b5 |
+++ b/src/basic/prioq.h
|
|
|
a3e2b5 |
@@ -18,8 +18,14 @@ int prioq_put(Prioq *q, void *data, unsigned *idx);
|
|
|
a3e2b5 |
int prioq_remove(Prioq *q, void *data, unsigned *idx);
|
|
|
a3e2b5 |
int prioq_reshuffle(Prioq *q, void *data, unsigned *idx);
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
-void *prioq_peek(Prioq *q) _pure_;
|
|
|
a3e2b5 |
+void *prioq_peek_by_index(Prioq *q, unsigned idx) _pure_;
|
|
|
a3e2b5 |
+static inline void *prioq_peek(Prioq *q) {
|
|
|
a3e2b5 |
+ return prioq_peek_by_index(q, 0);
|
|
|
a3e2b5 |
+}
|
|
|
a3e2b5 |
void *prioq_pop(Prioq *q);
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
+#define PRIOQ_FOREACH_ITEM(q, p) \
|
|
|
a3e2b5 |
+ for (unsigned _i = 0; (p = prioq_peek_by_index(q, _i)); _i++)
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
unsigned prioq_size(Prioq *q) _pure_;
|
|
|
a3e2b5 |
bool prioq_isempty(Prioq *q) _pure_;
|
|
|
a3e2b5 |
diff --git a/src/test/test-prioq.c b/src/test/test-prioq.c
|
|
|
a3e2b5 |
index 89c41d8ce7..ece13808ed 100644
|
|
|
a3e2b5 |
--- a/src/test/test-prioq.c
|
|
|
a3e2b5 |
+++ b/src/test/test-prioq.c
|
|
|
a3e2b5 |
@@ -87,6 +87,7 @@ static void test_struct(void) {
|
|
|
a3e2b5 |
Set *s;
|
|
|
a3e2b5 |
unsigned previous = 0, i;
|
|
|
a3e2b5 |
int r;
|
|
|
a3e2b5 |
+ struct test *t;
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
srand(0);
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
@@ -96,9 +97,12 @@ static void test_struct(void) {
|
|
|
a3e2b5 |
s = set_new(&test_hash_ops);
|
|
|
a3e2b5 |
assert_se(s);
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
- for (i = 0; i < SET_SIZE; i++) {
|
|
|
a3e2b5 |
- struct test *t;
|
|
|
a3e2b5 |
+ assert_se(prioq_peek(q) == NULL);
|
|
|
a3e2b5 |
+ assert_se(prioq_peek_by_index(q, 0) == NULL);
|
|
|
a3e2b5 |
+ assert_se(prioq_peek_by_index(q, 1) == NULL);
|
|
|
a3e2b5 |
+ assert_se(prioq_peek_by_index(q, (unsigned) -1) == NULL);
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
+ for (i = 0; i < SET_SIZE; i++) {
|
|
|
a3e2b5 |
t = new0(struct test, 1);
|
|
|
a3e2b5 |
assert_se(t);
|
|
|
a3e2b5 |
t->value = (unsigned) rand();
|
|
|
a3e2b5 |
@@ -112,9 +116,18 @@ static void test_struct(void) {
|
|
|
a3e2b5 |
}
|
|
|
a3e2b5 |
}
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
- for (;;) {
|
|
|
a3e2b5 |
- struct test *t;
|
|
|
a3e2b5 |
+ for (i = 0; i < SET_SIZE; i++)
|
|
|
a3e2b5 |
+ assert_se(prioq_peek_by_index(q, i));
|
|
|
a3e2b5 |
+ assert_se(prioq_peek_by_index(q, SET_SIZE) == NULL);
|
|
|
a3e2b5 |
+
|
|
|
a3e2b5 |
+ unsigned count = 0;
|
|
|
a3e2b5 |
+ PRIOQ_FOREACH_ITEM(q, t) {
|
|
|
a3e2b5 |
+ assert_se(t);
|
|
|
a3e2b5 |
+ count++;
|
|
|
a3e2b5 |
+ }
|
|
|
a3e2b5 |
+ assert_se(count == SET_SIZE);
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
+ for (;;) {
|
|
|
a3e2b5 |
t = set_steal_first(s);
|
|
|
a3e2b5 |
if (!t)
|
|
|
a3e2b5 |
break;
|
|
|
a3e2b5 |
@@ -126,8 +139,6 @@ static void test_struct(void) {
|
|
|
a3e2b5 |
}
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
for (i = 0; i < SET_SIZE * 3 / 4; i++) {
|
|
|
a3e2b5 |
- struct test *t;
|
|
|
a3e2b5 |
-
|
|
|
a3e2b5 |
assert_se(prioq_size(q) == (SET_SIZE * 3 / 4) - i);
|
|
|
a3e2b5 |
|
|
|
a3e2b5 |
t = prioq_pop(q);
|