anitazha / rpms / systemd

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