c语言自定义散列结构,c语言实现通用数据结构(五):通用映射(HashMap)

#include "myHashMap.h"

#include 

//某条Entry链表上是否包含某个key值。

Entry* listContainsEntry(MyList * list, void* key,

int(*equal)(void*key1,void*key2)) {

MyListIterator* it = createMyListIterator(list);

while(myListIteratorHasNext(it)) {

Entry * entry = (Entry *) (myListIteratorNext(it));

if(entry->key == key || (equal != NULL && (*equal)(entry->key, key))) {

returnentry;

}

}

freeMyListIterator(it);

returnNULL;

}

voidrebuildMyHashMap(MyHashMap * map) {

intnewSize = map->initialCapacity * 2;

MyList **newentryList = (MyList **) malloc(sizeof(MyList*) * newSize);

for(inti = 0; i 

newentryList[i] = createMyList();

}

MyHashMapEntryIterator* it = createMyHashMapEntryIterator(map);

while(myHashMapEntryIteratorHasNext(it)) {

Entry * entry = myHashMapEntryIteratorNext(it);

inthasCode = (*(map->hashCode))(entry->key);

hasCode %= newSize;

if(hasCode 

hasCode += newSize;

myListInsertDataAtLast(newentryList[hasCode], entry);

}

freeMyHashMapEntryIterator(it);

for(inti = 0; i initialCapacity; i++) {

freeMyList(map->entryList[i]);

}

free(map->entryList);

map->entryList = newentryList;

map->initialCapacity = newSize;

}

//创建HashMap

MyHashMap *createMyHashMap(int(*hashCode)(void*key),

int(*equal)(void*key1,void*key2)) {

MyHashMap *re = (MyHashMap *) malloc(sizeof(MyHashMap));

re->size = 0;

re->loadFactor = DEFAULT_LOAD_FACTOR;

re->initialCapacity = DEFAULT_INITIAL_CAPACITY;

re->entryList = (MyList **) malloc(sizeof(MyList*) * re->initialCapacity);

re->hashCode = hashCode;

re->equal = equal;

for(inti = 0; i initialCapacity; i++) {

re->entryList[i] = createMyList();

}

returnre;

}

//使用全部参数创建HashMap

MyHashMap *createMyHashMapForAll(intinitialCapacity,floatloadFactor,

int(*hashCode)(void*key),int(*equal)(void*key1,void*key2)) {

MyHashMap *re = createMyHashMap(hashCode, equal);

re->initialCapacity = initialCapacity;

re->loadFactor = loadFactor;

returnre;

}

//是否包含某个key

intmyHashMapContainsKey(MyHashMap *constmap,void*constkey) {

inthasCode = (*(map->hashCode))(key);

hasCode %= map->initialCapacity;

if(hasCode 

hasCode += map->initialCapacity;

Entry * re = listContainsEntry(map->entryList[hasCode], key, map->equal);

returnre != NULL;

}

//增加一条映射

voidmyHashMapPutData(MyHashMap *constmap,void*constkey,

void*constvalue) {

inthasCode = (*(map->hashCode))(key);

hasCode %= map->initialCapacity;

if(hasCode 

hasCode += map->initialCapacity;

Entry * re = listContainsEntry(map->entryList[hasCode], key, map->equal);

if(re == NULL) {

Entry * entry = (Entry*) malloc(sizeof(Entry));

entry->key = key;

entry->value = value;

myListInsertDataAtLast(map->entryList[hasCode], entry);

(map->size)++;

if(map->size > map->initialCapacity * map->loadFactor) {

rebuildMyHashMap(map);

}

} else{

re->value = value;

}

}

//通过key得到数据,如果没有数据则返回null

void* myHashMapGetDataByKey(MyHashMap *constmap,void*constkey) {

inthasCode = (*(map->hashCode))(key);

hasCode %= map->initialCapacity;

if(hasCode 

hasCode += map->initialCapacity;

Entry * re = listContainsEntry(map->entryList[hasCode], key, map->equal);

if(re == NULL) {

returnNULL;

}

returnre->value;

}

//数据的容量

intmyHashMapGetSize(constMyHashMap *constmap) {

returnmap->size;

}

//创建Entry迭代器

MyHashMapEntryIterator* createMyHashMapEntryIterator(MyHashMap * constmap) {

MyHashMapEntryIterator* re = (MyHashMapEntryIterator*) malloc(

sizeof(MyHashMapEntryIterator));

re->count = 0;

re->index = 0;

re->map = map;

re->current = map->entryList[0]->first;

returnre;

}

//释放Entry迭代器

voidfreeMyHashMapEntryIterator(MyHashMapEntryIterator* iterator) {

free(iterator);

}

//Entry迭代器是否有下一个

intmyHashMapEntryIteratorHasNext(MyHashMapEntryIterator* iterator) {

returniterator->count map->size;

}

//遍历下一个Entry元素

Entry* myHashMapEntryIteratorNext(MyHashMapEntryIterator* iterator) {

(iterator->count)++;

while(!(iterator->current)) {

(iterator->index)++;

iterator->current = iterator->map->entryList[iterator->index]->first;

}

Entry * re = (Entry *) iterator->current->data;

iterator->current = iterator->current->next;

returnre;

}

//删除一条数据,返回是否删除成功

intmyHashMapRemoveDataByKey(MyHashMap *constmap,void*constkey) {

inthasCode = (*(map->hashCode))(key);

hasCode %= map->initialCapacity;

if(hasCode 

hasCode += map->initialCapacity;

MyListIterator* it = createMyListIterator(map->entryList[hasCode]);

intre = 0;

while(myListIteratorHasNext(it)) {

Entry * entry = (Entry *) (myListIteratorNext(it));

if((*(map->equal))(entry->key, key)) {

myListRemoveDataAt(map->entryList[hasCode], it->count - 1);

re = 1;

(map->size)--;

break;

}

}

freeMyListIterator(it);

returnre;

}

voidmyFree(Entry * p){

free(p);

}

//释放HashMap

voidfreeMyHashMap(MyHashMap * map) {

myHashMapOutput(map, myFree);

for(inti = 0; i initialCapacity; i++) {

freeMyList(map->entryList[i]);

}

free(map->entryList);

free(map);

}

//遍历

voidmyHashMapOutput(MyHashMap *map,void(*pt)(Entry*)) {

MyHashMapEntryIterator* iterator = createMyHashMapEntryIterator(map);

while(myHashMapEntryIteratorHasNext(iterator)) {

pt(myHashMapEntryIteratorNext(iterator));

}

freeMyHashMapEntryIterator(iterator);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值