C-HashMap实现

typedef struct _Entry {
    String key;
    void* value;
    struct _Entry* next;
} *Entry;

typedef struct _Map {
    Entry* table;
    int cap;
    int sz;
    int slotSz;
} *Map;

Map newMap(int cap);
void putElem(Map map, String key, void* value);
void* getElem(Map map, String key);
int containsKey(Map map, String key);
void* deleteElem(Map map, String key);
void releaseMap(Map map);

unsigned long hashcode(String key) {
    unsigned long h = 0;
    unsigned long g;
    for (int i = 0; i < key->len; i++) {
        h = (h << 4) + key->val[i];
        g = h & 0xF0000000L;
        if(g) h ^= g >> 24;
        h &= ~g;
    }
    return h ^ (h >> 16);
}

Entry newEntry(String key, void *value) {
    Entry e = (Entry)malloc(sizeof(struct _Entry));
    e->key = cloneString(key);
    e->value = value;
    e->next = NULL;
    return e;
}

Map newMap(int cap) {
    Map map = (Map) malloc(sizeof(struct _Map));
    map->table = (Entry*)malloc(cap * sizeof(Entry));
    memset(map->table, 0, cap * sizeof(Entry));
    map->cap = cap;
    map->sz = 0;
    map->slotSz = 0;
    return map;
}

void putElem(Map map, String key, void* value) {
    int idx = hashcode(key) % map->cap;
    Entry entry = map->table[idx], cur;
    if (entry == NULL) {
        entry = newEntry(key, value);
        map->table[idx] = entry;
        map->slotSz++;
    } else {
        do {
            if (equalString(entry->key, key)) {
                entry->value = value;
                return;
            }
            cur = entry;
        } while ((entry = entry->next) != NULL);
        cur->next = newEntry(key, value);
        cur->value = value;
    }
    map->sz++;
}

void* getElem(Map map, String key) {
    int idx = hashcode(key) % map->cap;
    Entry entry = map->table[idx];
    while (entry != NULL && !equalString(entry->key, key)) {
        entry = entry->next;
    }
    return entry == NULL ? NULL : entry->value;
}

int containsKey(Map map, String key) {
    int idx = hashcode(key) % map->cap;
    return map->table[idx] != NULL;
}

void* deleteElem(Map map, String key) {
    int idx = hashcode(key) % map->cap;
    Entry entry = map->table[idx], cur = NULL;
    while (entry != NULL && !equalString(entry->key, key)) {
        cur = entry;
        entry = entry->next;
    }
    if (entry != NULL) {
        void* ret = entry->value;
        if (cur != NULL) {
            cur->next = NULL;
        } else {
            map->table[idx] = NULL;
            map->slotSz--;
        }
        free(entry);
        map->sz--;
        return ret;
    } else {
        return NULL;
    }
}

void releaseMap(Map map) {
    Entry entry, next;
    for (int i = 0; i < map->cap; i++) {
        entry = map->table[i];
        while (entry != NULL) {
            next = entry->next;
            releaseString(entry->key);
            entry->next = NULL;
            entry->value = NULL;
            free(entry);
            entry = next;
        }
        map->table[i] = NULL;
    }
    free(map->table);
    map->table = NULL;
    free(map);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值