** 文 件 名: data_struct.c
**
** 创 建 人: 齐鲁桐
**
** 文件创建日期: 2018 年 2 月 22 日
**
** 描 述: 数据结构实现
*********************************************************************************************************/
相关头文件
*********************************************************************************************************/
#include "data_struct.h"
宏定义
*********************************************************************************************************/
全局变量
*********************************************************************************************************/
** 函数名称: string_copy
** 功能描述: 字符串拷贝
** 输 入 :
** 输 出 :
** 全局变量:
** 调用模块:
*********************************************************************************************************/
STRING string_copy(STRING str)
{
char *result = malloc(strlen(str) + 1);
strcpy(result, str);
return result;
}
** 函数名称: list_create
** 功能描述: 创建一个双向链表
** 输 入 : 字符串
** 输 出 : 链表
** 全局变量:
** 调用模块:
*********************************************************************************************************/
List list_create()
{
List list = NEW(struct list);
list->value = NULL;
list->last = NULL;
list->next = NULL;
return list;
}
** 函数名称: list_head
** 功能描述: 移动到链表头节点
** 输 入 : 字符串
** 输 出 : 链表
** 全局变量:
** 调用模块:
*********************************************************************************************************/
List list_head(List list)
{
List head = list;
while (head->last != NULL) {
head = head->last;
}
return head;
}
** 函数名称: list_create
** 功能描述: 移动到链表尾节点
** 输 入 : 字符串
** 输 出 : 链表
** 全局变量:
** 调用模块:
*********************************************************************************************************/
List list_tail(List list)
{
List tail = list;
while (tail->next != NULL) {
tail = tail->next;
}
return tail;
}
** 函数名称: list_index
** 功能描述:
** 输 入 :
** 输 出 :
** 全局变量:
** 调用模块:
*********************************************************************************************************/
List list_index(List list, int index)
{
List node = list;
int i = 0;
while (i < index && node != NULL) {
node = node->next;
i++;
}
if (node == NULL) {
printf("Index Out Of Bounds\n");
return NULL;
}
return node;
}
** 函数名称: list_append
** 功能描述: 为链表增加新项
** 输 入 : list 链表
** str 字符串
** 输 出 : 新链表
** 全局变量:
** 调用模块:
*********************************************************************************************************/
List list_append(List list, STRING str)
{
List head = list;
List node = head;
List new_node = list_create(str);
while (node->next != NULL) {
node = node->next;
}
new_node->last = node;
node->next = new_node;
return head;
}
** 函数名称: list_remove
** 功能描述:
** 输 入 :
** 输 出 :
** 全局变量:
** 调用模块:
*********************************************************************************************************/
List list_remove(List list, STRING str)
{
List head = list_head(list);
List node = head;
while (node != NULL) {
if (strcmp(node->value, str) == 0) {
if (node->last != NULL) {
if (node->next != NULL) {
node->next->last = node->last;
node->last->next = node->next;
} else {
node->last->next = NULL;
}
} else {
node->next->last = NULL;
head = node->next;
}
free(node);
break;
}
node = node->next;
}
if (node == NULL) {
printf("node %s is not exist\n", str);
return head;
}
return head;
}
** 函数名称: list_remove
** 功能描述:
** 输 入 :
** 输 出 :
** 全局变量:
** 调用模块:
*********************************************************************************************************/
void list_delete(List list)
{
List head = list_head(list);
List node = head;
while (head != NULL) {
node = head;
XFREE(head);
head = node->next;
}
}
** 函数名称: hashmap_delete_entry
** 功能描述: 删除节点,释放内存
** 输 入 :
** 输 出 :
** 全局变量:
** 调用模块:
*********************************************************************************************************/
void hashmap_delete_entry(HashEntry entry)
{
XFREE(entry->key);
XFREE(entry->value);
XFREE(entry);
return;
}
** 函数名称: hashmap_create
** 功能描述: 创建HashMap
** 输 入 :
** 输 出 : 一个HashMap
** 全局变量:
** 调用模块:
*********************************************************************************************************/
HashMap hashmap_create()
{
HashMap hashMap = NEW(struct hashmap);
hashMap->size = 0;
hashMap->listSize = 8;
hashMap->hashCode = default_HashCode;
hashMap->equal = hashmap_equal;
hashMap->put = hashmap_put;
hashMap->get = hashmap_get;
hashMap->remove = hashmap_remove;
hashMap->clear = hashmap_clear;
hashMap->exists = hashmap_exists;
hashMap->list = (HashEntry)malloc(hashMap->listSize * sizeof(struct hashentry));
int i = 0;
for (i = 0; i < hashMap->listSize; ++i) {
hashMap->list[i].key = NULL;
hashMap->list[i].value = NULL;
hashMap->list[i].next = NULL;
}
return hashMap;
}
** 函数名称: hashmap_iterator
** 功能描述: 生成HashMap的迭代器
** 输 入 : hashMap: HashMap
**
**
** 输 出 : HashMap的迭代器
** 全局变量:
** 调用模块:
*********************************************************************************************************/
HashMapIterator hashmap_iterator(HashMap hashMap)
{
HashMapIterator iterator = NEW(struct hashmapiterator);
iterator->hashMap = hashMap;
iterator->entry = NULL;
iterator->count = 0;
iterator->hashCode = -1;
return iterator;
}
** 函数名称: hashmap_hasNext
** 功能描述: 判断HashMap是否迭代完
** 输 入 : hashMap: HashMap
**
**
** 输 出 : 若迭代次数小于HashMap元素个数,返回True,否则返回False
** 全局变量:
** 调用模块:
*********************************************************************************************************/
Bool hashmap_hasNext(HashMapIterator iterator)
{
return iterator->count < iterator->hashMap->size ? True : False;
}
** 函数名称: hashmap_next
** 功能描述: 获取HashMap的下一项,保存在迭代器的entry属性里
** 输 入 : hashMap: HashMap
**
**
** 输 出 : 返回带有迭代器
** 全局变量:
** 调用模块:
*********************************************************************************************************/
HashMapIterator hashmap_next(HashMapIterator iterator)
{
if (hashmap_hasNext(iterator)) {
if (iterator->entry != NULL && iterator->entry->next != NULL) {
iterator->count++;
iterator->entry = iterator->entry->next;
return iterator;
}
while (++iterator->hashCode < iterator->hashMap->listSize) {
HashEntry entry = &iterator->hashMap->list[iterator->hashCode];
if (entry->key != NULL) {
iterator->count++;
iterator->entry = entry;
break;
}
}
}
return iterator;
}
** 函数名称: hashmap_reset
** 功能描述: 重新设置HashMap的内存大小,不够时扩容,过大时缩小
** 输 入 : hashMap: HashMap
** key: 键
**
**
** 输 出 : 整数型哈希码
** 全局变量:
** 调用模块:
*********************************************************************************************************/
void hashmap_reset(HashMap hashMap, int listSize)
{
if (listSize < 8) {
return;
}
HashEntry tmpList = (HashEntry)malloc(hashMap->size * sizeof(struct hashentry));
HashMapIterator iterator = hashmap_iterator(hashMap);
int length = hashMap->size;
int i = 0;
for (i = 0; hashmap_hasNext(iterator); i++) {
iterator = hashmap_next(iterator);
tmpList[i].key = iterator->entry->key;
tmpList[i].value = iterator->entry->value;
tmpList[i].next = NULL;
}
XFREE(iterator);
hashMap->size = 0;
for (i = 0; i < hashMap->listSize; i++) {
HashEntry current = &hashMap->list[i];
current->key = NULL;
current->value = NULL;
if (current->next != NULL) {
while (current->next != NULL) {
HashEntry temp = current->next->next;
free(current->next);
current->next = temp;
}
}
}
hashMap->listSize = listSize;
HashEntry relist = (HashEntry)realloc(hashMap->list, hashMap->listSize * sizeof(struct hashentry));
if (relist != NULL) {
hashMap->list = relist;
relist = NULL;
}
for (i = 0; i < hashMap->listSize; i++) {
hashMap->list[i].key = NULL;
hashMap->list[i].value = NULL;
hashMap->list[i].next = NULL;
}
for (i = 0; i < length; i++) {
hashMap->put(hashMap, tmpList[i].key, tmpList[i].value);
XFREE(tmpList[i].key);
XFREE(tmpList[i].value);
}
XFREE(tmpList);
}
** 函数名称: default_HashCode
** 功能描述: 生成哈希码
** 输 入 : hashMap: HashMap
** key: 键
**
**
** 输 出 : 整数型哈希码
** 全局变量:
** 调用模块:
*********************************************************************************************************/
int default_HashCode(HashMap hashMap, void *key)
{
char * str = (STRING)key;
unsigned int seed = 131;
unsigned int hash = 0;
while (*str) {
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF) % hashMap->listSize;
}
** 函数名称: hashmap_equal
** 功能描述: 判断输入的字符串是否相等
** 输 入 : key1:
** key2:
**
** 输 出 : 布尔值
** 全局变量:
** 调用模块:
*********************************************************************************************************/
Bool hashmap_equal(void *key1, void *key2)
{
return strcmp((STRING)key1, (STRING)key2) ? False : True;
}
** 函数名称: hashmap_put
** 功能描述: 将键值对存到HashMap中
** 输 入 : hashMap:
** key:
** value:
**
** 输 出 : 布尔值
** 全局变量:
** 调用模块:
*********************************************************************************************************/
void hashmap_put(HashMap hashMap, void *key1, void *value1)
{
if(!key1 || !value1){
return;
}
STRING key = string_copy(key1);
STRING value = string_copy(value1);
if (hashMap->size >= hashMap->listSize) {
hashmap_reset(hashMap, hashMap->listSize * 2);
}
int index = hashMap->hashCode(hashMap, key);
if (hashMap->list[index].key == NULL) {
hashMap->list[index].key = key;
hashMap->list[index].value = value;
hashMap->size++;
return;
} else {
HashEntry current = &hashMap->list[index];
while (current != NULL) {
if (hashMap->equal(key, current->key)) {
hashMap->list[index].value = value;
return;
}
current = current->next;
};
HashEntry entry = NEW(struct hashentry);
entry->key = key;
entry->value = value;
entry->next = hashMap->list[index].next;
hashMap->list[index].next = entry;
hashMap->size++;
}
}
** 函数名称: hashmap_get
** 功能描述:
** 输 入 : hashMap
** key 字符串
**
**
** 输 出 : key所对应的value
** 全局变量:
** 调用模块:
*********************************************************************************************************/
void *hashmap_get(HashMap hashMap, void *key)
{
int index = hashMap->hashCode(hashMap, key);
HashEntry entry = &hashMap->list[index];
if (entry->key == NULL) {
printf("This key %s does not exist.\n", (STRING)key);
return NULL;
}
while (entry != NULL && entry->key != NULL && !hashMap->equal(entry->key, key)) {
entry = entry->next;
}
return entry->value;
}
** 函数名称: hashmap_exists
** 功能描述: 判断输入的字符串是否相等
** 输 入 : key1:
** key2:
**
** 输 出 : 布尔值
** 全局变量:
** 调用模块:
*********************************************************************************************************/
Bool hashmap_exists(HashMap map, void *key)
{
int index = map->hashCode(map, key);
HashEntry entry = &map->list[index];
while (entry != NULL && entry->key != NULL) {
if (map->equal(entry->key, key)) {
return True;
}
entry = entry->next;
}
return False;
}
** 函数名称: hashmap_remove
** 功能描述: 判断输入的字符串是否相等
** 输 入 : key1:
** key2:
**
** 输 出 : 布尔值
** 全局变量:
** 调用模块:
*********************************************************************************************************/
Bool hashmap_remove(HashMap hashMap, void *key)
{
int index = hashMap->hashCode(hashMap, key);
HashEntry entry = &hashMap->list[index];
if (entry->key == NULL) {
return False;
}
Bool result = False;
if (hashMap->equal(entry->key, key)) {
if (entry->next != NULL) {
HashEntry temp = entry->next;
XFREE(entry->key);
XFREE(entry->value);
entry->key = temp->key;
entry->value = temp->value;
entry->next = temp->next;
XFREE(temp);
} else {
XFREE(entry->key);
XFREE(entry->value);
}
hashMap->size--;
result = True;
} else {
HashEntry p = entry;
entry = entry->next;
while (entry != NULL) {
if (hashMap->equal(entry->key, key)) {
p->next = entry->next;
hashmap_delete_entry(entry);
hashMap->size--;
result = True;
break;
}
p = entry;
entry = entry->next;
};
}
if (hashMap->size < hashMap->listSize / 2) {
hashmap_reset(hashMap, hashMap->listSize / 2);
}
return result;
}
** 函数名称: hashmap_clear
** 功能描述: 清空HashMap,执行完后HashMap变为刚创建时的形态
** 输 入 : hashMap:
**
** 输 出 : 布尔值
** 全局变量:
** 调用模块:
*********************************************************************************************************/
void hashmap_clear(HashMap hashMap)
{
int i = 0;
for (i = 0; i < hashMap->listSize; i++) {
HashEntry entry = hashMap->list[i].next;
while (entry != NULL) {
HashEntry next = entry->next;
hashmap_delete_entry(entry);
entry = next;
}
hashMap->list[i].next = NULL;
}
for (i = 0; i < hashMap->listSize; ++i) {
XFREE(hashMap->list[i].key);
XFREE(hashMap->list[i].value);
}
free(hashMap->list);
hashMap->size = 0;
hashMap->listSize = 8;
hashMap->list = (HashEntry)malloc(hashMap->listSize * sizeof(struct hashentry));
for (i = 0; i < hashMap->listSize; ++i) {
hashMap->list[i].key = NULL;
hashMap->list[i].value = NULL;
hashMap->list[i].next = NULL;
}
}
** 函数名称: hashmap_delete
** 功能描述: 判断输入的字符串是否相等
** 输 入 : key1:
** key2:
**
** 输 出 : 布尔值
** 全局变量:
** 调用模块:
*********************************************************************************************************/
void hashmap_delete(HashMap hashMap)
{
hashmap_clear(hashMap);
XFREE(hashMap->list);
XFREE(hashMap);
}
END
*********************************************************************************************************/