⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 list.c

📁 cache库 别人写的
💻 C
字号:
/********************************************************************
	created:	2008/01/23
	filename: 	list.c
	author:		Lichuang
                
	purpose:    
*********************************************************************/

#include "list.h"
#include "node.h"
#include "hash.h"
#include <string.h>

static void initnode(int index, int hashindex, const void* key, const void* data, cache_t* cache);

int list_findnode(int hashindex, const void* key, void* data, cache_t* cache, comfun cmp)
{
    hashitem_t *hashitem = HASH_ITEM(cache, hashindex);

    int index = hashitem->first;
    int keysize = cache->keysize;
    node_t *node;
    void* nodekey;
    while (0 <= index)
    {
        node = NODE(cache, index);
        nodekey = NODE_KEY(node);

        if (!cmp(key, nodekey, keysize))
        {
            if (NULL != data)                
                memcpy(data, NODE_DATA(node), cache->datasize);
            break;
        }

        index = node->next;
    }

    return index;
}

int list_insertnode(int hashindex, const void* key, const void* data, cache_t* cache, comfun cmp, delfun fun)
{
    int index = list_findnode(hashindex, key, NULL, 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, cache_t* cache, comfun cmp)
{
    int index = list_findnode(hashindex, key, NULL, 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, cache_t* cache, comfun 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, cache_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(cache_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;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -