c字典dictionary集合操作

#ifndef DICTIONARY_H
#define DICTIONARY_H

#include "hashtable.h"

typedef struct {
    HashTable *table;
} Dictionary;

Dictionary *dictionary_construct();
void dictionary_destroy(Dictionary *dictionary);
void dictionary_add_entry(Dictionary *dictionary, 
                          const char *key, const char *value);
const char *dictionary_get_entry(Dictionary *dictionary, const char *key);

#endif

 

-----------------------------------------------------

 

 

#include "dictionary.h"
#include "hashtable.h"
#include "logger.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#ifdef DMALLOC
#include "dmalloc.h"
#endif

Dictionary *dictionary_construct() {

	Dictionary *dictionary = NULL;

	/*
     * Allocate space for the object and set all fields to zero.
     */
	dictionary = calloc(1, sizeof(Dictionary));
	
	if (dictionary == NULL) {
        return NULL;
    }

    /*
     * Instantiate an internal hash table to hold the dictionary entries.
     *
     * FIXME: This number at the moment is rather arbitrary (but large).
     *        When I implement automatic growing in the hash table, this
     *        will be less important (and should probably be smaller)
     */
    dictionary->table = hash_table_construct(2000);

    if (dictionary->table == NULL) {
        return NULL;
    }
		
	return dictionary;
}

void dictionary_destroy(Dictionary *dictionary) {
     
	if (dictionary == NULL) {
        return;
    }
		
    /*
     * FIXME: Remove all elements from the hash table and free them, 
     *        before deleting the hash table.
     */
    if (dictionary->table != NULL) {
        hash_table_destroy(dictionary->table);
    }

	free(dictionary);

}

void dictionary_add_entry(Dictionary *dictionary, 
                          const char *key, const char *value) {

    int hash_code = -1;

	if (dictionary == NULL) {
        return;
    }

    if (key == NULL || key[0] == 0) {
        return;
    }

    /*
     * Generate a unique hash code for the key
     */
    hash_code = hash_table_get_hash_code_from_string(key);

    /*
     * Make a copy of the entry value so that it won't get changed
     */
    if (value != NULL) {
        value = strdup(value);
    }

    /*
     * Add the copy of the entry value to the hash table
     */
    hash_table_add_element(dictionary->table, value, hash_code);

}

const char *dictionary_get_entry(Dictionary *dictionary, const char *key) {

    const char *value = NULL;
    int hash_code = -1;

    if (dictionary == NULL) {
        return NULL;
    }

    if (key == NULL || key[0] == 0) {
        return NULL;
    }

    /*
     * Generate a unique hash code for the key
     */
    hash_code = hash_table_get_hash_code_from_string(key);

    /*
     * Retrieve the entry value from the hash table
     */
    value = hash_table_get_element(dictionary->table, hash_code);

    return value;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值