#include "common.h" #define nullptr 0 #define u64 unsigned long long int #define u32 unsigned int #define maxstringsize 10 typedef struct LinkNode { u64 hash_value; u32 depth; // 这个 node 所在的深度, 根目录是0 u32 mNum; u32 create_tm; bool is_del; char str[maxstringsize]; LinkNode *father; LinkNode *sonlist; LinkNode *next; // 用于 sonlist 中链接各个子 node }LinkNode; #define maxmemsize 1007 //散列表大小, 根据题目要求, 一般设置为原数据大小的 2-4 倍 + 7 (一个质数, 这样取模后更加均匀散列) static LinkNode g_n_mem[maxmemsize]; static int g_n_mem_cnt = 0; typedef struct HashNode { u64 hash_value; LinkNode *filePtr; HashNode *next; }HashNode; #define maxhashsize 107 static HashNode *m_hashmap[maxhashsize]; static HashNode g_hn_mem[maxmemsize * 2]; static int g_hn_mem_cnt = 0; static char filename[20][maxstringsize] { {"root"}, {"aaaa"}, {"bbbb"}, {"april"}, {"friday"}, {"monday"}, {"love"}, {"kiss"}, {"autum"}, {"weather"}, {"water"}, {"leak"}, {"mouth"}, {"leader"}, {"gohome"}, {"shafa"}, {"season"}, {"global"}, {"see"}, {"sea"}, }; static LinkNode *root = nullptr; static u32 g_create_tm = 0; #define m_max_depth 5 // there is 5 level only u64 my_hash(const char str[]) //因为题目说只有 小写字母,因此做一个 26 进制的进位就可以满足, 如果是小写+数字,则进位改成 26+10, hash 函数可以根据实际需求构造 { u64 h = 0; while (*str != '\0') { h = h * 26 + (*str++) - 'a' + 1; // 字符串全是小写的情况, 可根据情况改成 31 / 131 } return h & 0xFFFFFFFFFFFFFFFF; } u64 my_hash2(const char str[]) // 这个hash 实现方法,在几个题目上机试了都没有问题,还是挺实用的 { u64 h = 0; u64 p = 1; while (*str != '\0') { h += p * (*str++); p *= 2531011; } return h & 0xFFFFFFFFFFFFFFFF; } void add_to_hash_list(HashNode **phead, HashNode *pNode) //改变一个指针, 必须要传指针的地址进来 { pNode->next = *phead; *phead = pNode; } LinkNode *is_exist_in_hash_list(u64 hash_value) // 检查 hash 表是否已经存在对应的节点 { HashNode *p = m_hashmap[hash_value % maxhashsize]; while (p != nullptr) { if (p->hash_value == hash_value) { return p->filePtr; } p = p->next; } return nullptr; } HashNode *get_hash_node(u64 hash_value) //获取 hash 节点对应的保存的 指针(文件信息) { HashNode *p = m_hashmap[hash_value % maxhashsize]; while (p != nullptr) { if (p->hash_value == hash_value) { return p; } p = p->next; } return nullptr; } void del_from_hash_list(u64 hash_value) //从 hash 表中移除, 如果给 hash 表增加头节点, 则删除起来代码更简洁 { HashNode *p = m_hashmap[hash_value % maxhashsize]; if (p->hash_value == hash_value) { m_hashmap[hash_value % maxhashsize] = p->next; } else { HashNode *pre = p; p = p->next; while (p != nullptr) { if (p->hash_value == hash_value) { pre->next = p->next; break; //must has break } pre = p; p = p->next; } } p->hash_value = 0; p->filePtr = nullptr; p->next = nullptr; } void add_to_father_son_list(LinkNode *father, LinkNode *pNode) //将文件添加到对应的父节点 { pNode->next = father->sonlist; father->sonlist = pNode; pNode->father = father; } void init(int mNum) { LOGE("mNum = %d", mNum); g_create_tm = 0; for (int i = 0; i < maxhashsize; i++) { m_hashmap[i] = nullptr; } g_n_mem_cnt = 0; for (int i = 0; i < maxhashsize * 2; i++) { g_hn_mem[i].filePtr = nullptr; g_hn_mem[i].hash_value = 0; g_hn_mem[i].next = nullptr; } g_hn_mem_cnt = 0; //add root node u64 hash_value = my_hash(filename[0]); root = &g_n_mem[g_n_mem_cnt++]; strcpy_s(root->str, filename[0]); root->hash_value = hash_value; root->sonlist = nullptr; root->next = nullptr; root->father = nullptr; root->depth = 0; root->mNum = mNum; root->create_tm = g_create_tm++; root->is_del = false; //add to hash list HashNode *pNode = &g_hn_mem[g_hn_mem_cnt++]; pNode->hash_value = hash_value; pNode->filePtr = root; pNode->next = nullptr; add_to_hash_list(&m_hashmap[pNode->hash_value % maxhashsize], pNode); } void add_file(char upper[], char newFile[], int mNum) // 添加新的文件到已经存在的节点下 { u64 hash_value = my_hash(upper); LinkNode * father = is_exist_in_hash_list(hash_value); if (father == nullptr) { LOGE("there is no upper= %s mNum=%d return %s", upper, mNum, newFile); return; } else if (mNum >= father->mNum) { LOGE("can not add to upper= %s mNum=%d > %d return %s", upper, mNum, father->mNum, newFile); return; } father->mNum -= mNum; LOGE("father = %s %s , son = %s", father->str, upper, newFile); //add node u64 son_hash_value = my_hash(newFile); LinkNode *son = &g_n_mem[g_n_mem_cnt++]; strcpy_s(son->str, newFile); son->hash_value = son_hash_value; son->sonlist = nullptr; son->next = nullptr; son->mNum = mNum; son->depth = father->depth + 1; son->create_tm = g_create_tm++; son->is_del = false; //add to hash list HashNode *pNode = &g_hn_mem[g_hn_mem_cnt++]; pNode->hash_value = son_hash_value; pNode->filePtr = son; pNode->next = nullptr; add_to_hash_list(&m_hashmap[pNode->hash_value % maxhashsize], pNode); //add to father son list add_to_father_son_list(father, son); } void update_depth(LinkNode *father, u32 *max_depth) //更新每个文件所在的 层 (深度) , 并返回当前节点所在链的最大深度 (也可以计算当前节点的子节点的深度) { if (father->sonlist == nullptr) { if (father->depth > *max_depth) { *max_depth = father->depth; } return; } LinkNode *p = father->sonlist; while (p != nullptr) { p->depth = father->depth + 1; update_depth(p, max_depth); p = p->next; } } //如果file 已经存在于 upper 下, 直接返回 //假设深度最大是5, 如果并入 upper 后,总的深度超过5,则直接返回 //合并规则: file 的 mNum 并入 upper 节点, file 的子链表变成 upper 的子链表, 同时需要删除 file 节点 ( node 本身 和 hash 表都要更新) void merge_file(char upper[], char file[]) { u64 hash_value = my_hash(upper); LinkNode * father = is_exist_in_hash_list(hash_value); u64 son_hash_value = my_hash(file); LinkNode * son = is_exist_in_hash_list(son_hash_value); if (father == nullptr || son == nullptr) return; LOGE("father = %s %s , son = %s %s", father->str, upper, son->str, file); u32 max_depth = 0; update_depth(son, &max_depth); if (father->depth + max_depth - son->depth > m_max_depth) { LOGE("depth is beyond limit, return depth: %s(%d) %s(%d) max_depth(%d)", father->str, father->depth, son->str, son->depth, max_depth); return; } LOGE("max_depth of %s = %d", son->str, max_depth); //add to father son list LinkNode *p = son->sonlist; while (p != nullptr) { // 这里有 BUG, 因为添加到父节点的子链表会导致 next指向改变, 再指行 p=p->next 会错误 LinkNode *q = p; p = p->next; add_to_father_son_list(father, q); } max_depth = 0; update_depth(father, &max_depth); //remove from hash list del_from_hash_list(son->hash_value); //remove node son->sonlist = nullptr; son->father = nullptr; son->next = nullptr; son->hash_value = 0; son->mNum = 0; son->depth = 0; son->is_del = true; } //给排名前 depthNum 的文件增加 mNum 个文件 (除了 root 之外) //排名规则: 人数最少,优先级最高, 如果人数一样,创建时间越晚,优先级越高 //这里有一个问题, 当直接对 g_n_mem 数组进行冒泡排序, 将会改下对于数组里面的内容 //但由于这个数组对应的 hash 节点里的 *filePtr 还是指向之前的内存, 因此需要同步改 hash 对应的 *filePtr void recuit(u32 depthNum, u32 mNum) { LOGE("depthNum = %d mNum = %d", depthNum, mNum); int count = 0; for (int i = 1; i < g_n_mem_cnt - 1; i++) { if (g_n_mem[i].is_del == false) { count++; for (int j = i + 1; j < g_n_mem_cnt; j++) { if (g_n_mem[j].is_del == false && (g_n_mem[i].mNum > g_n_mem[j].mNum || (g_n_mem[i].mNum > g_n_mem[j].mNum && g_n_mem[i].create_tm < g_n_mem[j].create_tm))) { //因为 hashnode 保存的指针 filePtr 指向 g_n_mem[i], 而内存里面的数据发生交换,需要同步修改 hash list HashNode *pi = get_hash_node(g_n_mem[i].hash_value); HashNode *pj = get_hash_node(g_n_mem[j].hash_value); pi->filePtr = &g_n_mem[j]; pj->filePtr = &g_n_mem[i]; LinkNode tmp = g_n_mem[i]; g_n_mem[i] = g_n_mem[j]; g_n_mem[j] = tmp; } } } if (count >= depthNum) { break; } } count = 0; for (int i = 1; i < g_n_mem_cnt - 1; i++) { if (g_n_mem[i].is_del == false) { g_n_mem[i].mNum += mNum; count++; } if (count >= depthNum) { break; } } } //root->1->3(april)->5(monday)->6(love)->7(kiss) //root->1->4(friday) //root->2(bbb)->8(autum)->9(weather)->10(water) extern void test_hash() { init(100); add_file(filename[0], filename[1], 50); add_file(filename[0], filename[2], 40); add_file(filename[1], filename[3], 10); add_file(filename[1], filename[4], 15); add_file(filename[3], filename[5], 5); add_file(filename[5], filename[6], 3); add_file(filename[6], filename[7], 2); add_file(filename[2], filename[8], 7); //add fail add_file(filename[8], filename[9], 9); //父只有7个,添加失败 add_file(filename[9], filename[10], 4); //没有父节点,添加失败 add_file(filename[8], filename[9], 6); //父有7个 add_file(filename[9], filename[10], 4); // okay //recuit(3, 10); merge_file(filename[6], filename[8]); merge_file(filename[6], filename[9]); //检查 hash 链表 filePtr 是否已经被修改 for (int i = 0; i < g_n_mem_cnt; i++) { u64 hash_value = my_hash(filename[i]); LinkNode * pNode = is_exist_in_hash_list(hash_value); if (pNode != nullptr) LOGE("filename[%d] = %s, node str = %s", i, filename[i], pNode->str); } }