Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d2372ba4 authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

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
parent d2de7086
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -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
+3 −3
Original line number Diff line number Diff line
@@ -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) {
+13 −7
Original line number Diff line number Diff line
@@ -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);
}