内核rbtree使用示例
来源:互联网 发布:nginx htaccess配置 编辑:程序博客网 时间:2024/05/16 19:28
/** * @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
- 内核rbtree使用示例
- 使用内核数据结构:红黑树 rbtree
- linux内核分析之rbtree的使用
- Linux内核中rbtree的使用
- linux内核分析之rbtree的使用
- 理解linux内核中的rbtree
- nginx--rbtree使用详解
- RBTree
- RBTree
- RBTree
- RBTree
- RBTree
- rbtree
- rbtree
- RBTree
- rbtree原理及应用--使用
- Linux内核文档之rbtree.txt
- Linux内核文档之rbtree.txt
- nil/Nil/NULL/NSNull的来历
- Android Launcher3一些默认修改
- 数组去重
- 程序设计中为什么要解耦?
- JNDI学习总结(三)——Tomcat下使用Druid配置JNDI数据源
- 内核rbtree使用示例
- EditText 当设置为action为搜索的时候,设置点击了搜索以后直接搜索的逻辑
- 什么方面的资料是玩家的需求点和喜欢看的游戏攻略
- php魔术方法(一)
- SWUSTOJ2475 字典树
- JDK自带内存及线程分析工具小记
- android 注册成功后返回账号和密码到登录界面,精辟讲解
- 《Higher-order organization of complex networks》-论文学习笔记
- android标题栏中添加返回按钮