0. Preface
The linked list in Redis is implemented in the form of a universal linked list, and for the purpose of the list, the main function is to delete and change the search, so for the lookup, Redis provides a match function pointer, the user is responsible for the implementation of its specific matching operation, so as to achieve generalization.
Files Involved: adlist.h/adlist.c
1. Data structure
typedefstructListNode {structListNode *prev; structListNode *Next; void*value;//Universal implementation that can store any type of data} listnode;typedefstructListiter {ListNode*Next; intDirection//used to control the direction of a linked list traversal} listiter;typedefstructList {ListNode*head;//Table HeaderListNode *tail;//End of Table void* (*dup) (void*PTR);//used to replicate PTR values for deep replication void(* Free)(void*PTR);//releasing memory of the corresponding type structure int(*match) (void*ptr,void*key);//Custom Match KeyUnsignedLongLen//Number of nodes} list;
#define Listsetdupmethod (l,m) ((l)->dup = (m))#define Listsetfreemethod (l,m) ((l)->free = (m)) #define Listsetmatchmethod (l,m) ((l)->match = (m))
A Dup,free,match function pointer is provided, which allows the user to access a specific type of data by setting the function pointer.
2. API implementation:
Extract only a few major APIs, the full comment of the file on Githud (username: jabnih)
A. Listrelease
For operations that release a list, the release of each node determines whether the user has set the free function, and if so, executes the user's action to release the specific type of data. For example, value is a pointer to an array of characters allocated from the heap, and when the node is freed, the value memory needs to be released first
For free can be implemented as:
1 void Free (void * value) 2 {3 if (value) 4 Free ((Char *) value); 5 }
1 //release linked list2 voidListrelease (List *list)3 {4UnsignedLongLen;5ListNode *current, *Next;6 7Current = List->head;8Len = list->Len;9 while(len--) {TenNext = current->Next; One //if the free function is set, the custom function is called A if(list-> Free) list-> Free(current->value); - - Zfree (current); theCurrent =Next; - } - Zfree (list); -}
B. Listdup
When replication is performed, the ability to set up the DUP function allows for deep copying or custom replication.
1 //copy Linked list, if the list has a DUP, call the function for deep copy, or copy the value of the node directly (shallow copy)2List *listdup (list *orig)3 {4List *copy;5Listiter *iter;6ListNode *node;7 8 if(copy = Listcreate ()) = =NULL)9 returnNULL;TenCopy->dup = orig->DUP; OneCopy-> Free= orig-> Free; ACopy->match = orig->match; -ITER =Listgetiterator (orig, al_start_head); - while(node = Listnext (iter))! =NULL) { the //traverse the entire linked list - void*value; - - if(copy->DUP) { + //Deep Copy -Value = Copy->dup (node->value); + if(Value = =NULL) { A //replication Error at listrelease (copy); - Listreleaseiterator (ITER); - returnNULL; - } -}Else - //Shallow Copy inValue = node->value; - to //to add the copied node to the tail of the copy list + if(Listaddnodetail (copy, value) = =NULL) { - listrelease (copy); the Listreleaseiterator (ITER); * returnNULL; $ }Panax Notoginseng } - Listreleaseiterator (ITER); the returncopy; +}
C. listsearchkey
1 //find nodes, if the match method is set, compare by using the match method, or simply compare the value of the node2ListNode *listsearchkey (List *list,void*key)3 {4Listiter *iter;5ListNode *node;6 7ITER =listgetiterator (list, al_start_head);8 while(node = Listnext (iter))! =NULL) {9 if(list->match) {Ten if(List->match (node->value, key)) { One //Here you can change the following two statements to break (same as below) and return null to return to node A Listreleaseiterator (ITER); - returnnode; - } the}Else { - if(Key = = Node->value) { - Listreleaseiterator (ITER); - returnnode; + } - } + } A Listreleaseiterator (ITER); at returnNULL; -}
3. Summary
1. Universal Linked List implementation
2. External extension, users can customize the search, copy, release functions.
Redis Learning--linked list implementation