内核rbtree使用示例

/**
 * @author lijk@.infosec.com.cn
 * @version 0.0.1
 * @date 2016-9-20 11:52:06
 */
#include <stdio.h>
#include <stdlib.h>
#include "rbtree.h"

typedef struct roc_node_s
{
    struct rb_node node;
    void *ctx;
    int key;
}roc_node_t;

typedef void (*roc_cb)(void*);

roc_node_t* roc_search(struct rb_root *root, int key)
{
    struct rb_node *node = root->rb_node;
    while(node)
    {
        roc_node_t *data = rb_entry(node, roc_node_t, node);
        int result = key - data->key;

        if (result < 0)
            node = node->rb_left;
        else if (result > 0)
            node = node->rb_right;
        else
            return data;
    }
    return NULL;
}

int roc_insert(struct rb_root *root, roc_node_t *data)
{
    struct rb_node **new = &(root->rb_node), *parent = NULL;
    while(*new)
    {
        roc_node_t *this = rb_entry(*new, roc_node_t, node);
        int result = data->key - this->key;

        parent = *new;
        if (result < 0)
            new = &((*new)->rb_left);
        else if (result > 0)
            new = &((*new)->rb_right);
        else
            return 0;
    }

    rb_link_node(&data->node, parent, new);
    rb_insert_color(&data->node, root);

    return 1;
}

void roc_erase(struct rb_root *root, int key, roc_cb cb)
{
    roc_node_t *data = roc_search(root, key);
    if(data)
    {
        rb_erase(&data->node, root);
        RB_CLEAR_NODE(&data->node);
        if(cb) cb(data);
    }
}

void roc_destroy(struct rb_root *root, roc_cb cb)
{
    roc_node_t *pos = NULL;
    struct rb_node *node = NULL;
    while((node = rb_first(root)))
    {
        pos = rb_entry(node, roc_node_t, node);
    #ifdef _DEBUG
        fprintf(stdout, "key = %d\n", pos->key);
    #endif
        rb_erase(&pos->node, root);
        RB_CLEAR_NODE(&pos->node);
        if(cb) cb(pos);
    }
}

void roc_dump(struct rb_root *root)
{
    struct rb_node *node = NULL;
    for(node = rb_first(root); node != NULL; node = rb_next(node))
        fprintf(stdout, "key = %d\n", rb_entry(node, roc_node_t, node)->key);
}

void roc_free(void *ptr)
{
    roc_node_t *node = (roc_node_t*)ptr;
    if(node)
    {
        if(node->ctx) free(node->ctx);
        free(node);
    }
}

int main(int argc, char const *argv[])
{
    struct rb_root root = RB_ROOT;

    int loop = 0;
    roc_node_t *node = NULL;
    for(loop = 0; loop < 100; loop ++)
    {
        node = (roc_node_t*)malloc(sizeof(roc_node_t));
        if(node == NULL)
            break;
        node->ctx = NULL;
        node->key = loop;
        roc_insert(&root, node);
    }

#if 0
    for(loop = 0; loop < 100; loop ++)
        roc_erase(&root, loop, roc_free);
#endif

    roc_dump(&root);
    roc_destroy(&root, roc_free);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值