博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一种hashmap的简单实现
阅读量:5256 次
发布时间:2019-06-14

本文共 3757 字,大约阅读时间需要 12 分钟。

//template 
//利用id来索引到对应的对象struct HashNode { int id; void* obj; struct list_head list_item;}struct list_head { list_head* prev; list_head* next;}#define LIST_HEAD_INIT(name) { &(name), &(name) }#define INIT_LIST_HEAD(ptr) do { (ptr)->next = (ptr); (ptr)->prev = (ptr); } while (0)#define list_for_each(pos, head) for (pos = (head)->next; pos != (head); pos = pos->next)#define list_for_each_safe(pos, n, head) for (pos = (head)->next, n = pos->next; pos != (head); pos = n, n = pos->next)static inline void list_add(struct list_head *p, struct list_head *prev, struct list_head *next){ next->prev = p; p->next = next; p->prev = prev; prev->next = p;}static inline void list_add_in(struct list_head *p, struct list_head *head){ list_add(p, head, head->next);}static inline void list_add_tail(struct list_head *p, struct list_head *head){ list_add(p, head->prev, head);}static inline void __list_del(struct list_head *prev, struct list_head *next){ next->prev = prev; prev->next = next;}static inline void list_del(struct list_head *entry){ __list_del(entry->prev, entry->next); entry->next = 0; entry->prev = 0;}#ifndef offsetof#if __GNUC__ >= 4#define offsetof(type, member) __builtin_offsetof (type, member);printf("this is gcc > 4\n");#else#define offsetof(type, member) (unsigned long)(&((type *)0)->member);printf("this is gcc < 4\n");#endif#endif#define list_entry(ptr, type, member) ((type *)((char *)(ptr)-offsetof(type, member)))class CHashMap {private: struct HashNode* hash_table; int hash_table_size;public: CHashMap(); ~CHashMap(); int CreateMap(int map_size); int AddMapObj(int id, void* obj); int DelMapObj(int id); void* GetMapObj(int id); int ClearMap(); }CHashMap() { hash_table = NULL; hash_table_size = 0;}~CHashMap() { if (hash_table) { ClearMap(); } hash_table_size = 0;}int CreateMap(int map_size) { try { hash_table = new HashNode[map_size]; } catch(&bad_alloc) { return -1; } //对每个hash桶的头节点进行初始化 for (int i = 0; i < map_size; i++) { hash_table[i].id = 0; hash_table[i].obj = NULL; INIT_LIST_HEAD(&hash_table[i].list_item); } hash_table_size = map_size; return 0;}int AddMapObj(int id, void* obj) { int bucket_id = id%hash_table_size; HashNode* temp = new struct HashNode(); temp->id = id; temp->obj = obj; list_add_tail(&temp->list_item, &hash_table[bucket_id].list_item); return 0;}int DelMapObj(int id){ int bucket_id = id%hash_table_size; struct list_head* temp1; struct list_head* temp2; struct list_head* head = &hash_table[bucket_id].list_item; struct HashNode* node; list_for_each_safe(temp1, temp2, head) { node = list_entry(temp1, struct HashNode, list_item); if (node->id == id) { list_del(temp1); delete node; return 0; } } return -1; }void* GetMapObj(int id) { int bucket_id = id%hash_table_size; struct list_head* head = &hash_table[bucket_id].list_item; struct list_head* temp ; struct HashNode* node; list_for_each(temp, head) { node = list_entry(temp, struct HashNode, list_item); if (node->id == id) { return node->obj; } } return NULL;}int ClearMap(){ struct list_head* _head; struct list_head* _tmp1; struct list_head* _tmp2; struct OBJECTMAP* item; for( int i=0; i

 

template
struct HashListNode{ NodeType obj; struct list_head list_item;};template
int idx = Traits::GetNodeID(obj)% hash_table_size;

 

转载于:https://www.cnblogs.com/share-ideas/p/11221399.html

你可能感兴趣的文章
Dreamweaver cc新版本css单行显示
查看>>
【android】安卓的权限提示及版本相关
查看>>
Java基础教程——网络基础知识
查看>>
Kruskal基础最小生成树
查看>>
【hdu 1429】胜利大逃亡(续)
查看>>
关于收费软件
查看>>
javascript之Style物
查看>>
Factory Design Pattern
查看>>
P1192-台阶问题
查看>>
一、使用pip安装Python包
查看>>
Duilib扩展《01》— 双击、右键消息扩展
查看>>
网站产品设计
查看>>
go 学习笔记(4) ---项目结构
查看>>
java中静态代码块的用法 static用法详解
查看>>
Java线程面试题
查看>>
day22 01 初识面向对象----简单的人狗大战小游戏
查看>>
mybatis源代码分析:深入了解mybatis延迟加载机制
查看>>
Flask三剑客
查看>>
Hibernate-缓存
查看>>
【BZOJ4516】生成魔咒(后缀自动机)
查看>>