summaryrefslogtreecommitdiff
path: root/osi
diff options
context:
space:
mode:
authorJakub Pawlowski <jpawlowski@google.com>2016-02-01 11:51:44 -0800
committerJakub Pawlowski <jpawlowski@google.com>2016-02-01 17:18:00 -0800
commit67f57c639e6dc2f302115017eb92c40dd306c15d (patch)
tree6275968ec67786a5d9eb24b9edb8148315442f07 /osi
parent700b163a3e5673a47df8de9509e971bb10f0580e (diff)
downloadandroid-system-bt-67f57c639e6dc2f302115017eb92c40dd306c15d.tar.gz
android-system-bt-67f57c639e6dc2f302115017eb92c40dd306c15d.tar.xz
Make list_foreach() even more useful
- Changed |callback| return type to list_node_t to be able to interrupt iteration (to find specific elements for example). Change-Id: I00eb83725d03e6f1aec239ae11eb19cf59af35a9
Diffstat (limited to 'osi')
-rw-r--r--osi/include/list.h8
-rw-r--r--osi/src/list.c6
-rw-r--r--osi/test/list_test.cpp20
3 files changed, 20 insertions, 14 deletions
diff --git a/osi/include/list.h b/osi/include/list.h
index b1513c0..54daf99 100644
--- a/osi/include/list.h
+++ b/osi/include/list.h
@@ -79,14 +79,14 @@ bool list_remove(list_t *list, void *data);
void list_clear(list_t *list);
// Iterates through the |list| and calls |callback| for each data element. Iteration
-// continues until |callback| returns false. The function returns the result of the
-// last executed |callback| or true if the list is empty. |context| is passed to
-// |callback| on each iteration.
+// continues until |callback| returns false. The function returns the pointer to last
+// processed element, or NULL if the list is empty, or all calls to |callback| returned
+// true. |context| is passed to |callback| on each iteration.
// If the list is empty, |callback| will never be called. It is safe to mutate the
// list inside the callback. If an element is added before the node being visited,
// there will be no callback for the newly-inserted node. Neither |list| nor
// |callback| may be NULL.
-bool list_foreach(const list_t *list, list_iter_cb callback, void *context);
+list_node_t *list_foreach(const list_t *list, list_iter_cb callback, void *context);
// Returns an iterator to the first element in |list|. |list| may not be NULL.
// The returned iterator is valid as long as it does not equal the value returned
diff --git a/osi/src/list.c b/osi/src/list.c
index 61d19f0..79eb76f 100644
--- a/osi/src/list.c
+++ b/osi/src/list.c
@@ -168,17 +168,17 @@ void list_clear(list_t *list) {
list->length = 0;
}
-bool list_foreach(const list_t *list, list_iter_cb callback, void *context) {
+list_node_t *list_foreach(const list_t *list, list_iter_cb callback, void *context) {
assert(list != NULL);
assert(callback != NULL);
for (list_node_t *node = list->head; node; ) {
list_node_t *next = node->next;
if (!callback(node->data, context))
- return false;
+ return node;
node = next;
}
- return true;
+ return NULL;
}
list_node_t *list_begin(const list_t *list) {
diff --git a/osi/test/list_test.cpp b/osi/test/list_test.cpp
index de01b19..fcb6590 100644
--- a/osi/test/list_test.cpp
+++ b/osi/test/list_test.cpp
@@ -173,9 +173,9 @@ TEST_F(ListTest, test_list_foreach_full) {
// Test complete iteration
int sum = 0;
- bool rc = list_foreach(list, list_callback_sum, &sum);
+ list_node_t *rc = list_foreach(list, list_callback_sum, &sum);
EXPECT_EQ(sum, 15);
- EXPECT_TRUE(rc);
+ EXPECT_TRUE(rc == NULL);
list_free(list);
}
@@ -191,20 +191,26 @@ TEST_F(ListTest, test_list_foreach_partial) {
// Test partial iteration
int find = 4;
- bool rc = list_foreach(list, list_callback_find_int, &find);
- EXPECT_FALSE(rc);
+ list_node_t *rc = list_foreach(list, list_callback_find_int, &find);
+ EXPECT_TRUE(rc != NULL);
+ int *rc_val = (int *)list_node(rc);
+ EXPECT_TRUE(*rc_val == 4);
find = 1;
rc = list_foreach(list, list_callback_find_int, &find);
- EXPECT_FALSE(rc);
+ EXPECT_TRUE(rc != NULL);
+ rc_val = (int *)list_node(rc);
+ EXPECT_TRUE(*rc_val == 1);
find = 5;
rc = list_foreach(list, list_callback_find_int, &find);
- EXPECT_FALSE(rc);
+ EXPECT_TRUE(rc != NULL);
+ rc_val = (int *)list_node(rc);
+ EXPECT_TRUE(*rc_val == 5);
find = 0;
rc = list_foreach(list, list_callback_find_int, &find);
- EXPECT_TRUE(rc);
+ EXPECT_TRUE(rc == NULL);
list_free(list);
}