www.pudn.com > ccache.rar > list.c


/******************************************************************** 
	created:	2008/01/23 
	filename: 	list.c 
	author:		Lichuang 
                 
	purpose:     
*********************************************************************/ 
 
#include "list.h" 
#include "node.h" 
#include "hash.h" 
#include  
 
static void initnode(int index, int hashindex, const void* key, const void* data, ccache_t* cache); 
 
int list_findnode(int hashindex, const void* key, ccache_t* cache, cmpfun cmp) 
{ 
    hashitem_t *hashitem = HASH_ITEM(cache, hashindex); 
 
    int index = hashitem->first; 
    node_t *node; 
    void* nodekey; 
    while (0 <= index) 
    { 
        node = NODE(cache, index); 
        nodekey = NODE_KEY(node); 
 
        if (!cmp(key, nodekey)) 
        { 
            break; 
        } 
 
        index = node->next; 
    } 
 
    return index; 
} 
 
int list_insertnode(int hashindex, const void* key, const void* data, ccache_t* cache, cmpfun cmp, delfun fun) 
{ 
    int index = list_findnode(hashindex, key, cache, cmp); 
    if (0 <= index) 
    { 
        return -1; 
    } 
 
    index = getfreenode(cache, cmp, fun); 
    if (0 > index) 
    { 
        return -1; 
    } 
 
    initnode(index, hashindex, key, data, cache); 
 
    return index; 
} 
 
int list_updatenode(int hashindex, const void* key, const void* data, ccache_t* cache, cmpfun cmp) 
{ 
    int index = list_findnode(hashindex, key, cache, cmp); 
 
    if (0 > index) 
    { 
        return -1; 
    } 
 
    node_t* node = NODE(cache, index); 
 
    void *nodekey, *nodedata; 
    nodekey = NODE_KEY(node); 
    nodedata = NODE_DATA(node); 
 
    memcpy(nodedata, data, node->datasize); 
 
    return index; 
} 
 
int list_deletenode(int hashindex, int index, ccache_t* cache, cmpfun cmp) 
{ 
    (void)cmp; 
 
    int prev, next; 
    node_t *node = NODE(cache, index), *tmp; 
 
    prev = node->prev, next = node->next; 
    if (0 <= prev) 
    { 
        tmp = NODE(cache, prev); 
        tmp->next = next; 
    } 
    if (0 <= next) 
    { 
        tmp = NODE(cache, next); 
        tmp->prev = prev; 
    } 
 
    hashitem_t* hashitem = HASH_ITEM(cache, hashindex); 
    if (index == hashitem->first) 
    { 
        hashitem->first = next;  
    } 
 
    hashitem->nodenum--; 
 
    node->prev = node->next = -1; 
     
    return index; 
} 
 
static void initnode(int index, int hashindex, const void* key, const void* data, ccache_t* cache) 
{ 
    node_t* node = NODE(cache, index), *tmp; 
    node->keysize = cache->keysize; 
    node->datasize = cache->datasize; 
    node->hashindex = hashindex; 
 
    void *nodekey, *nodedata; 
    nodekey = NODE_KEY(node); 
    nodedata = NODE_DATA(node); 
 
    memcpy(nodekey, key, node->keysize); 
    memcpy(nodedata, data, node->datasize); 
 
    hashitem_t* hash = HASH_ITEM(cache, hashindex); 
    hash->nodenum++; 
 
    int first = hash->first; 
    if (0 <= first) 
    { 
        tmp = NODE(cache, first); 
        tmp->prev = index; 
    } 
    node->next = first; 
    node->prev = -1; 
 
    hash->first = index; 
} 
 
void list_initnodes(ccache_t* cache) 
{ 
    int nodenum = cache->nodenum; 
    node_t* node; 
    int keysize = cache->keysize; 
    int datasize = cache->datasize; 
    int i; 
 
    for (i = 0; i < nodenum; ++i) 
    { 
        node = NODE(cache, i); 
        node->next = i + 1; 
        node->prev = i - 1; 
        node->keysize = keysize; 
        node->datasize = datasize; 
        node->hashindex = -1; 
        node->index = i; 
        node->lrunext = node->lruprev = -1; 
    } 
 
    node->next = -1; 
} 
 
void list_visitnodes(ccache_t* cache, int hashindex, visitfun visit) 
{ 
    void *key, *data; 
    hashitem_t *hash; 
    node_t *node; 
    int i; 
 
    hash = HASH_ITEM(cache, hashindex); 
    for (i = hash->first; i >= 0; ) 
    { 
        node = NODE(cache, i); 
        data = NODE_DATA(node); 
        key  = NODE_KEY(node); 
 
        visit(key, data); 
 
        i = node->next; 
    } 
}