redis listチェーンテーブルソース分析

5365 ワード

//     
typedef struct listNode {
    struct listNode *prev;
    struct listNode *next;
    void *value;
} listNode;



typedef struct list {
    //     
    listNode *head;
    //     
    listNode *tail;
    //       
    void *(*dup)(void *ptr);
    //       
    void (*free)(void *ptr);
    //        
    int (*match)(void *ptr, void *key);
    //           
    unsigned long len;
} list;


//           value   
list *listAddNodeHead(list *list, void *value)
{
    listNode *node;

    if ((node = zmalloc(sizeof(*node))) == NULL)
        return NULL;
    node->value = value;
    if (list->len == 0) {
        //      list    
        list->head = list->tail = node; //               
        node->prev = node->next = NULL; //               
                                        //              
    } else {
        node->prev = NULL;
        node->next = list->head;
        list->head->prev = node;
        list->head = node;              //              
    }
    list->len++;        // list     
    return list;
}


//         value   
list *listAddNodeTail(list *list, void *value)
{
    listNode *node;

    if ((node = zmalloc(sizeof(*node))) == NULL)
        return NULL;
    node->value = value;
    if (list->len == 0) {
        list->head = list->tail = node;
        node->prev = node->next = NULL;
    } else {
        node->prev = list->tail;
        node->next = NULL;    //             NULL
        list->tail->next = node;
        list->tail = node;
    }
    list->len++;        //      
    return list;
}



//  value              
// after 1 :   
// after 0 :   
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;
}


//       list
list *listCreate(void)
{
    struct list *list;

    if ((list = zmalloc(sizeof(*list))) == NULL)
        return NULL;
    list->head = list->tail = NULL;
    list->len = 0;
    list->dup = NULL;
    list->free = NULL;
    list->match = NULL;
    return list;
}


// listIter       ,      list
typedef struct listIter {
    listNode *next;
    int direction;
} listIter;


//         
void listReleaseIterator(listIter *iter) {
    zfree(iter);
}


// listIter       ,      list
//  direction     
listIter *listGetIterator(list *list, int direction)
{
    listIter *iter;

    if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL;
    if (direction == AL_START_HEAD)  //         
        iter->next = list->head;
    else
        iter->next = list->tail;    //       
    iter->direction = direction;    //     
    return iter;
}


//         
void listReleaseIterator(listIter *iter) {
    zfree(iter);
}



//  list      key   
//      NULL
listNode *listSearchKey(list *list, void *key)
{
    listIter *iter;
    listNode *node;
    //                
    iter = listGetIterator(list, AL_START_HEAD);
    while((node = listNext(iter)) != NULL) {
        if (list->match) {
            //             
            if (list->match(node->value, key)) {
                listReleaseIterator(iter);
                return node;
            }
        } else {
            //         
            if (key == node->value) {
                listReleaseIterator(iter);
                return node;
            }
        }
    }
    //                      
    listReleaseIterator(iter);
    return NULL;
}


//   index    list   node
// index 0       ,     
// index -1        ,    
listNode *listIndex(list *list, long index) {
    listNode *n;

    if (index < 0) {
        index = (-index)-1;
        n = list->tail;
        //    index 
        while(index-- && n) n = n->prev;
    } else {
        n = list->head;
        //    index 
        while(index-- && n) n = n->next;
    }
    return n;
}


//  list           
void listRotate(list *list) {
    listNode *tail = list->tail;
    //          1     
    if (listLength(list) <= 1) return;

    /* Detach current tail */
    list->tail = tail->prev;
    list->tail->next = NULL;
    /* Move it as head */
    list->head->prev = tail;
    tail->prev = NULL;
    tail->next = list->head;
    list->head = tail;
}


//   list
void listRelease(list *list)
{
    unsigned long len;
    listNode *current, *next;

    current = list->head;
    len = list->len;
    while(len--) {
        next = current->next;
        //           ,     
        if (list->free) list->free(current->value);
        //       
        zfree(current);
        current = next;
    }
    zfree(list);
}