Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ list_at(list, -3); // third last
list_remove(list, node);
```

## void list_destroy(list_t *self)
## void list_destroy(list_t **self)

Free the list and all nodes.

```c
list_destroy(list);
list_destroy(&list);
```

## list_iterator_t \*list_iterator_new(list_t *list, list_direction_t direction)
Expand All @@ -102,12 +102,12 @@ while ((node = list_iterator_next(it))) {

Return the next `list_node_t` or __NULL__.

## void list_iterator_destroy(list_iterator_t *self);
## void list_iterator_destroy(list_iterator_t **self);

Free the iterator only.

```c
list_iterator_destroy(it);
list_iterator_destroy(&it);
```

## Examples
Expand All @@ -127,8 +127,8 @@ while ((node = list_iterator_next(it))) {
puts(node->val);
}

list_iterator_destroy(it);
list_destroy(langs);
list_iterator_destroy(&it);
list_destroy(&langs);
```

stdout:
Expand Down
21 changes: 11 additions & 10 deletions src/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,24 @@ list_new(void) {

/*
* Free the list.
* @self: Pointer to the list
* @self: Pointer to the list pointer.
*/

void
list_destroy(list_t *self) {
unsigned int len = self->len;
list_destroy(list_t **self) {
unsigned int len = (*self)->len;
list_node_t *next;
list_node_t *curr = self->head;
list_node_t *curr = (*self)->head;

while (len--) {
next = curr->next;
if (self->free) self->free(curr->val);
if ((*self)->free) (*self)->free(curr->val);
LIST_FREE(curr);
curr = next;
}

LIST_FREE(self);
LIST_FREE(*self);
*self = NULL;
}

/*
Expand Down Expand Up @@ -151,18 +152,18 @@ list_find(list_t *self, void *val) {
while ((node = list_iterator_next(it))) {
if (self->match) {
if (self->match(val, node->val)) {
list_iterator_destroy(it);
list_iterator_destroy(&it);
return node;
}
} else {
if (val == node->val) {
list_iterator_destroy(it);
list_iterator_destroy(&it);
return node;
}
}
}

list_iterator_destroy(it);
list_iterator_destroy(&it);
return NULL;
}

Expand All @@ -185,7 +186,7 @@ list_at(list_t *self, int index) {
list_iterator_t *it = list_iterator_new(self, direction);
list_node_t *node = list_iterator_next(it);
while (index--) node = list_iterator_next(it);
list_iterator_destroy(it);
list_iterator_destroy(&it);
return node;
}

Expand Down
4 changes: 2 additions & 2 deletions src/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void
list_remove(list_t *self, list_node_t *node);

void
list_destroy(list_t *self);
list_destroy(list_t **self);

// list_t iterator prototypes.

Expand All @@ -121,7 +121,7 @@ list_node_t *
list_iterator_next(list_iterator_t *self);

void
list_iterator_destroy(list_iterator_t *self);
list_iterator_destroy(list_iterator_t **self);

#ifdef __cplusplus
}
Expand Down
6 changes: 3 additions & 3 deletions src/list_iterator.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ list_iterator_next(list_iterator_t *self) {
*/

void
list_iterator_destroy(list_iterator_t *self) {
LIST_FREE(self);
self = NULL;
list_iterator_destroy(list_iterator_t **self) {
LIST_FREE(*self);
*self = NULL;
}
38 changes: 19 additions & 19 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ test_list_rpush() {
assert(NULL == c->next);
assert(b == c->prev);

list_destroy(list);
list_destroy(&list);
}

static void
Expand All @@ -90,7 +90,7 @@ test_list_lpush() {
assert(b == c->next);
assert(NULL == c->prev);

list_destroy(list);
list_destroy(&list);
}

static void
Expand All @@ -117,29 +117,29 @@ test_list_at() {
assert(a == list_at(list, -3));
assert(NULL == list_at(list, -4));

list_destroy(list);
list_destroy(&list);
}

static void
test_list_destroy() {
// Setup
list_t *a = list_new();
list_destroy(a);
list_destroy(&a);

// a b c
list_t *b = list_new();
list_rpush(b, list_node_new("a"));
list_rpush(b, list_node_new("b"));
list_rpush(b, list_node_new("c"));
list_destroy(b);
list_destroy(&b);

// Assertions
list_t *c = list_new();
c->free = freeProxy;
list_rpush(c, list_node_new(list_node_new("a")));
list_rpush(c, list_node_new(list_node_new("b")));
list_rpush(c, list_node_new(list_node_new("c")));
list_destroy(c);
list_destroy(&c);
assert(3 == freeProxyCalls);
freeProxyCalls=0;
}
Expand All @@ -148,37 +148,37 @@ static void
test_list_destroy_complexver() {
// Setup
list_t *a = list_new();
list_destroy(a);
list_destroy(&a);

// a b c
list_t *b = list_new();
list_rpush(b, list_node_new("a"));
list_rpush(b, list_node_new("b"));
list_rpush(b, list_node_new("c"));
list_destroy(b);
list_destroy(&b);

// Assertions
list_t *c = list_new();
c->free = freeProxy;
list_rpush(c, list_node_new(list_node_new("a")));
list_rpush(c, list_node_new(list_node_new("b")));
list_rpush(c, list_node_new(list_node_new("c")));
list_destroy(c);
list_destroy(&c);
assert(3 == freeProxyCalls);
freeProxyCalls=0;
list_t *d = list_new();
d->free = freeProxy;
list_rpush(d, list_node_new(list_node_new("a")));
list_rpush(d, list_node_new(list_node_new("b")));
list_rpush(d, list_node_new(list_node_new("c")));
list_destroy(d);
list_destroy(&d);
assert(3 == freeProxyCalls);
freeProxyCalls=0;
}
static void
test_list_empty_list_destroy() {
list_t *list = list_new();
list_destroy(list);
list_destroy(&list);
freeProxyCalls=0;
}
static void
Expand All @@ -204,7 +204,7 @@ test_list_find() {
assert(ruby == b);
assert(NULL == c);

list_destroy(langs);
list_destroy(&langs);

a = list_find(users, &userTJ);
b = list_find(users, &userSimon);
Expand All @@ -213,7 +213,7 @@ test_list_find() {
assert(simon == b);
assert(NULL == c);

list_destroy(users);
list_destroy(&users);
}

static void
Expand Down Expand Up @@ -248,7 +248,7 @@ test_list_remove() {
assert(NULL == list->head);
assert(NULL == list->tail);

list_destroy(list);
list_destroy(&list);
}

static void
Expand Down Expand Up @@ -290,7 +290,7 @@ test_list_rpop() {
assert(NULL == list_rpop(list));
assert(0 == list->len);

list_destroy(list);
list_destroy(&list);
}

static void
Expand Down Expand Up @@ -328,7 +328,7 @@ test_list_lpop() {
assert(NULL == list_lpop(list));
assert(0 == list->len);

list_destroy(list);
list_destroy(&list);
}

static void
Expand Down Expand Up @@ -358,7 +358,7 @@ test_list_iterator_t() {
assert(c == simon);
assert(NULL == d);

list_iterator_destroy(it);
list_iterator_destroy(&it);

// From tail
it = list_iterator_new(list, LIST_TAIL);
Expand All @@ -371,9 +371,9 @@ test_list_iterator_t() {
assert(b2 == taylor);
assert(c2 == tj);
assert(NULL == d2);
list_iterator_destroy(it);
list_iterator_destroy(&it);

list_destroy(list);
list_destroy(&list);
}

int
Expand Down