teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0074-basic-prioq-add-prioq_peek_item.patch

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