X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/ed9b544e10b84cd43348ddfab7068b610a5df1f7..4774a53b:/adlist.c diff --git a/adlist.c b/adlist.c index 1f978c7b..015012f5 100644 --- a/adlist.c +++ b/adlist.c @@ -1,6 +1,6 @@ /* adlist.c - A generic doubly linked list implementation * - * Copyright (c) 2006-2009, Salvatore Sanfilippo + * Copyright (c) 2006-2010, Salvatore Sanfilippo * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -123,6 +123,35 @@ list *listAddNodeTail(list *list, void *value) return list; } +list *listInsertNode(list *list, listNode *old_node, void *value, int after) { + listNode *node; + + if ((node = zmalloc(sizeof(*node))) == NULL) + return NULL; + node->value = value; + if (after) { + node->prev = old_node; + node->next = old_node->next; + if (list->tail == old_node) { + list->tail = node; + } + } else { + node->next = old_node; + node->prev = old_node->prev; + if (list->head == old_node) { + list->head = node; + } + } + if (node->prev != NULL) { + node->prev->next = node; + } + if (node->next != NULL) { + node->next->prev = node; + } + list->len++; + return list; +} + /* Remove the specified node from the specified list. * It's up to the caller to free the private value of the node. * @@ -143,7 +172,7 @@ void listDelNode(list *list, listNode *node) } /* Returns a list iterator 'iter'. After the initialization every - * call to listNextElement() will return the next element of the list. + * call to listNext() will return the next element of the list. * * This function can't fail. */ listIter *listGetIterator(list *list, int direction) @@ -164,6 +193,17 @@ void listReleaseIterator(listIter *iter) { zfree(iter); } +/* Create an iterator in the list private iterator structure */ +void listRewind(list *list, listIter *li) { + li->next = list->head; + li->direction = AL_START_HEAD; +} + +void listRewindTail(list *list, listIter *li) { + li->next = list->tail; + li->direction = AL_START_TAIL; +} + /* Return the next element of an iterator. * It's valid to remove the currently returned element using * listDelNode(), but not to remove other elements. @@ -172,13 +212,13 @@ void listReleaseIterator(listIter *iter) { * or NULL if there are no more elements, so the classical usage patter * is: * - * iter = listGetItarotr(list,); - * while ((node = listNextIterator(iter)) != NULL) { - * DoSomethingWith(listNodeValue(node)); + * iter = listGetIterator(list,); + * while ((node = listNext(iter)) != NULL) { + * doSomethingWith(listNodeValue(node)); * } * * */ -listNode *listNextElement(listIter *iter) +listNode *listNext(listIter *iter) { listNode *current = iter->next; @@ -211,7 +251,7 @@ list *listDup(list *orig) copy->free = orig->free; copy->match = orig->match; iter = listGetIterator(orig, AL_START_HEAD); - while((node = listNextElement(iter)) != NULL) { + while((node = listNext(iter)) != NULL) { void *value; if (copy->dup) { @@ -248,7 +288,7 @@ listNode *listSearchKey(list *list, void *key) listNode *node; iter = listGetIterator(list, AL_START_HEAD); - while((node = listNextElement(iter)) != NULL) { + while((node = listNext(iter)) != NULL) { if (list->match) { if (list->match(node->value, key)) { listReleaseIterator(iter);