list_t *list_dup(list_t *orig) { list_iter *iter = list_get_iterator(orig, LIST_START_HEAD); if (iter == NULL) { return NULL; } list_t *copy = list_create(&orig->type); if (copy == NULL) { list_release_iterator(iter); return NULL; } list_node *node; while ((node = list_next(iter)) != NULL) { void *value = node->value; if (copy->type.dup) { value = copy->type.dup(value); if (value == NULL) { list_release_iterator(iter); list_release(copy); return NULL; } } if (list_add_node_tail(copy, value) == NULL) { list_release_iterator(iter); list_release(copy); return NULL; } } list_release_iterator(iter); return copy; }
int main(int argc, char *argv[]) { list_type type; type.dup = node_dup; type.free = node_free; type.compare = node_compare; list_t *list = list_create(&type); for (int i = 0; i < 10; ++i) { sds value = sdsempty(); value = sdscatprintf(value, "%d", i); list_add_node_tail(list, value); printf("add %s\n", value); sdsfree(value); } for (int i = 0; i < 10; ++i) { sds value = sdsempty(); value = sdscatprintf(value, "%d", 10 + i); list_add_node_head(list, value); printf("add %s\n", value); sdsfree(value); } list_node *node = list_index(list, 10); sds value = sdsempty(); value = sdscatprintf(value, "%d", 100); list_insert_node(list, node, value, 1); printf("insert %s\n", value); node = list_find(list, value); printf("search: %s\n", (char *)node->value); sdsfree(value); for (int i = -10; i < 10; ++i) { node = list_index(list, i); if (node) { printf("%d: %s\n", i, (char *)node->value); } } list_t *copy = list_dup(list); list_release(list); printf("len: %ld\n", list_len(copy)); list_rotate(copy); list_rotate(copy); list_rotate(copy); list_iter *iter = list_get_iterator(copy, LIST_START_HEAD); while ((node = list_next(iter)) != NULL) { printf("%s\n", (char *)node->value); list_del(copy, node); } list_release_iterator(iter); list_release(copy); printf("len: %ld\n", list_len(copy)); printf("head: %p, tail: %p\n", copy->head, copy->tail); return 0; }
list_node *list_find(list_t *list, void *value) { if (list->type.compare == NULL) return NULL; list_iter *iter = list_get_iterator(list, LIST_START_HEAD); if (iter == NULL) { return NULL; } list_node *node; while ((node = list_next(iter)) != NULL) { if (list->type.compare(node->value, value) == 0) { list_release_iterator(iter); return node; } } list_release_iterator(iter); return NULL; }
/* Search the list for a node matching a given key. * The match is performed using the 'match' method * set with listSetMatchMethod(). If no 'match' method * is set, the 'value' pointer of every node is directly * compared with the 'key' pointer. * * On success the first matching node pointer is returned * (search starts from head). If no matching node exists * NULL is returned. */ list_node *list_search_key(list *list, void *key, int len) { list_iter *iter; list_node *node; iter = list_get_iterator(list, AL_START_HEAD); while((node = list_next(iter)) != NULL) { if (len > 0) { if (memcmp(node->value, key, len) == 0) { list_release_iterator(iter); return node; } } else { if (key == node->value) { list_release_iterator(iter); return node; } } } list_release_iterator(iter); return NULL; }