]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/prioq: add prioq_peek_item()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 26 Jan 2019 10:27:18 +0000 (11:27 +0100)
committerLukáš Nykrýn <lnykryn@redhat.com>
Thu, 7 Feb 2019 11:57:43 +0000 (12:57 +0100)
(cherry-picked from commit ef21b3b5bf824e652addf850bcfd9374c7b33ce8)

Related: #1664976

src/basic/prioq.c
src/basic/prioq.h
src/test/test-prioq.c

index ef28a086d149ad48e090974e4435b91faec1d046..0bf58c1f16ac49cafeec5fc6a6089ef3fe668fb3 100644 (file)
@@ -259,15 +259,14 @@ int prioq_reshuffle(Prioq *q, void *data, unsigned *idx) {
         return 1;
 }
 
-void *prioq_peek(Prioq *q) {
-
+void *prioq_peek_by_index(Prioq *q, unsigned idx) {
         if (!q)
                 return NULL;
 
-        if (q->n_items <= 0)
+        if (idx >= q->n_items)
                 return NULL;
 
-        return q->items[0].data;
+        return q->items[idx].data;
 }
 
 void *prioq_pop(Prioq *q) {
index e0361752603f91ed38f75262dd99fac4e1d5a140..c3815235254bc6a56b2ac69e4211748c84761d6a 100644 (file)
@@ -18,8 +18,14 @@ int prioq_put(Prioq *q, void *data, unsigned *idx);
 int prioq_remove(Prioq *q, void *data, unsigned *idx);
 int prioq_reshuffle(Prioq *q, void *data, unsigned *idx);
 
-void *prioq_peek(Prioq *q) _pure_;
+void *prioq_peek_by_index(Prioq *q, unsigned idx) _pure_;
+static inline void *prioq_peek(Prioq *q) {
+        return prioq_peek_by_index(q, 0);
+}
 void *prioq_pop(Prioq *q);
 
+#define PRIOQ_FOREACH_ITEM(q, p)                                \
+        for (unsigned _i = 0; (p = prioq_peek_by_index(q, _i)); _i++)
+
 unsigned prioq_size(Prioq *q) _pure_;
 bool prioq_isempty(Prioq *q) _pure_;
index 89c41d8ce744b51922b3afecf181ce0e86437366..ece13808ed61f8cc6afa0388e9802bd1fccf3c95 100644 (file)
@@ -87,6 +87,7 @@ static void test_struct(void) {
         Set *s;
         unsigned previous = 0, i;
         int r;
+        struct test *t;
 
         srand(0);
 
@@ -96,9 +97,12 @@ static void test_struct(void) {
         s = set_new(&test_hash_ops);
         assert_se(s);
 
-        for (i = 0; i < SET_SIZE; i++) {
-                struct test *t;
+        assert_se(prioq_peek(q) == NULL);
+        assert_se(prioq_peek_by_index(q, 0) == NULL);
+        assert_se(prioq_peek_by_index(q, 1) == NULL);
+        assert_se(prioq_peek_by_index(q, (unsigned) -1) == NULL);
 
+        for (i = 0; i < SET_SIZE; i++) {
                 t = new0(struct test, 1);
                 assert_se(t);
                 t->value = (unsigned) rand();
@@ -112,9 +116,18 @@ static void test_struct(void) {
                 }
         }
 
-        for (;;) {
-                struct test *t;
+        for (i = 0; i < SET_SIZE; i++)
+                assert_se(prioq_peek_by_index(q, i));
+        assert_se(prioq_peek_by_index(q, SET_SIZE) == NULL);
+
+        unsigned count = 0;
+        PRIOQ_FOREACH_ITEM(q, t) {
+                assert_se(t);
+                count++;
+        }
+        assert_se(count == SET_SIZE);
 
+        for (;;) {
                 t = set_steal_first(s);
                 if (!t)
                         break;
@@ -126,8 +139,6 @@ static void test_struct(void) {
         }
 
         for (i = 0; i < SET_SIZE * 3 / 4; i++) {
-                struct test *t;
-
                 assert_se(prioq_size(q) == (SET_SIZE * 3 / 4) - i);
 
                 t = prioq_pop(q);