Back to home page

Redis cross reference

 
 

    


0001 /* adlist.h - A generic doubly linked list implementation
0002  *
0003  * Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com>
0004  * All rights reserved.
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions are met:
0008  *
0009  *   * Redistributions of source code must retain the above copyright notice,
0010  *     this list of conditions and the following disclaimer.
0011  *   * Redistributions in binary form must reproduce the above copyright
0012  *     notice, this list of conditions and the following disclaimer in the
0013  *     documentation and/or other materials provided with the distribution.
0014  *   * Neither the name of Redis nor the names of its contributors may be used
0015  *     to endorse or promote products derived from this software without
0016  *     specific prior written permission.
0017  *
0018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0019  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0021  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0022  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0023  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0024  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0026  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0027  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0028  * POSSIBILITY OF SUCH DAMAGE.
0029  */
0030 
0031 #ifndef __ADLIST_H__
0032 #define __ADLIST_H__
0033 
0034 /* Node, List, and Iterator are the only data structures used currently. */
0035 
0036 typedef struct listNode {
0037     struct listNode *prev;
0038     struct listNode *next;
0039     void *value;
0040 } listNode;
0041 
0042 typedef struct listIter {
0043     listNode *next;
0044     int direction;
0045 } listIter;
0046 
0047 typedef struct list {
0048     listNode *head;
0049     listNode *tail;
0050     void *(*dup)(void *ptr);
0051     void (*free)(void *ptr);
0052     int (*match)(void *ptr, void *key);
0053     unsigned long len;
0054 } list;
0055 
0056 /* Functions implemented as macros */
0057 #define listLength(l) ((l)->len)
0058 #define listFirst(l) ((l)->head)
0059 #define listLast(l) ((l)->tail)
0060 #define listPrevNode(n) ((n)->prev)
0061 #define listNextNode(n) ((n)->next)
0062 #define listNodeValue(n) ((n)->value)
0063 
0064 #define listSetDupMethod(l,m) ((l)->dup = (m))
0065 #define listSetFreeMethod(l,m) ((l)->free = (m))
0066 #define listSetMatchMethod(l,m) ((l)->match = (m))
0067 
0068 #define listGetDupMethod(l) ((l)->dup)
0069 #define listGetFree(l) ((l)->free)
0070 #define listGetMatchMethod(l) ((l)->match)
0071 
0072 /* Prototypes */
0073 list *listCreate(void);
0074 void listRelease(list *list);
0075 list *listAddNodeHead(list *list, void *value);
0076 list *listAddNodeTail(list *list, void *value);
0077 list *listInsertNode(list *list, listNode *old_node, void *value, int after);
0078 void listDelNode(list *list, listNode *node);
0079 listIter *listGetIterator(list *list, int direction);
0080 listNode *listNext(listIter *iter);
0081 void listReleaseIterator(listIter *iter);
0082 list *listDup(list *orig);
0083 listNode *listSearchKey(list *list, void *key);
0084 listNode *listIndex(list *list, long index);
0085 void listRewind(list *list, listIter *li);
0086 void listRewindTail(list *list, listIter *li);
0087 void listRotate(list *list);
0088 
0089 /* Directions for iterators */
0090 #define AL_START_HEAD 0
0091 #define AL_START_TAIL 1
0092 
0093 #endif /* __ADLIST_H__ */