hash table(开放寻址法-线性探查实现的哈希表)

hash table(开放寻址法-线性探查实现的哈希表)

//
// Created by 许加权 on 2021/7/17.
//#ifndef C11LEARN_HASHLINER_H
#define C11LEARN_HASHLINER_H
#include "KeyNode.h"
template<typename T>
class HashLiner
{
public:HashLiner();HashLiner(const HashLiner<T> & hashLiner);~HashLiner();const HashLiner<T>& operator=(const HashLiner<T> & hashLiner);T & operator[](int key);
protected:long long capacity;KeyNode<T>** array;long long w;long long p;long long s;long long two_32;
protected:virtual int auxiliary_hashing(int key);virtual int hashing(int key,int index);void insert(KeyNode<T>* node);KeyNode<T>*search(int key);void clear();void copy(const HashLiner<T> & hashLiner);
};template<typename T>
HashLiner<T>::HashLiner(){s = 2654435769;w = 32;p = 14;two_32 = 1;two_32 = two_32<<32;capacity = 1<<p;array = new KeyNode<T>*[capacity];
}
template<typename T>
HashLiner<T>::HashLiner(const HashLiner<T> & hashLiner){s = 2654435769;w = 32;p = 14;two_32 = 1;two_32 = two_32<<32;capacity = 1<<p;array = new KeyNode<T>*[capacity];copy(hashLiner);
}
template<typename T>
HashLiner<T>::~HashLiner(){clear();if(array!= nullptr){delete [] array;array = nullptr;}
}
template<typename T>
void HashLiner<T>::copy(const HashLiner<T> & hashLiner){for (int i = 0; i < capacity; ++i) {if(hashLiner.array[i]!= nullptr){array[i] = new KeyNode<T>(hashLiner.array[i]->key,hashLiner.array[i]->value);}}
}
template<typename T>
const HashLiner<T>& HashLiner<T>::operator=(const HashLiner<T> & hashLiner){if(this == &hashLiner) return *this;clear();copy(hashLiner);return *this;
}
template<typename T>
T & HashLiner<T>::operator[](int key){KeyNode<T>* node = search(key);if(node == nullptr){node = new KeyNode<T>();node->key = key;insert(node);}return node->value;
}
template<typename T>
int HashLiner<T>::auxiliary_hashing(int key){return ((key*s)%two_32)>>(w-p);
}
template<typename T>
int HashLiner<T>::hashing(int key,int index){return (auxiliary_hashing(key)+index) % capacity;
}
template<typename T>
void HashLiner<T>::insert(KeyNode<T>* node){int i = -1;int j;while (++i<capacity){j = hashing(node->key,i);if(array[j] == nullptr) {array[j] = node;return;}}throw "hash table overflow";
}
template<typename T>
KeyNode<T>* HashLiner<T>::search(int key){int i = -1;int j;while (++i<capacity){j = hashing(key,i);if(array[j] == nullptr)return nullptr;if(array[j]->key == key)return array[j];}return nullptr;
}
template<typename T>
void HashLiner<T>::clear(){for (int i = 0; i < capacity; ++i) {if(array[i]!= nullptr){delete array[i];array[i] = nullptr;}}
}
#endif //C11LEARN_HASHLINER_H

测试代码

    HashLiner<string> hashLiner;hashLiner[2] = "hello";hashLiner[123456] = "world";cout << hashLiner[2] << endl;cout << hashLiner[123456] << endl;HashLiner<string> hashLiner1 = hashLiner;cout << hashLiner1[2] << endl;cout << hashLiner1[123456] << endl;HashLiner<string> hashLiner2;hashLiner2 = hashLiner;cout << hashLiner2[2] << endl;cout << hashLiner2[123456] << endl;

辅助类
KeyNode地址
辅助auxiliary_hashing函数是利用乘法散列法实现的

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/308957.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring5 jar包下载

下载地址 https://repo.spring.io/simple/libs-release-local/org/springframework/spring/ Spring5最新版本的下载 选择最新版本5.2.3 下载前两项&#xff0c;解压放入文件夹中 项目中导包 ps&#xff1a;我使用的开发工具是idea 第一步&#xff1a;在file中选择project st…

hash table(开放寻址法-二次探查实现的哈希表)

hash table(开放寻址法-二次探查实现的哈希表&#xff09; #ifndef C11LEARN_HASHQUADRATIC_H #define C11LEARN_HASHQUADRATIC_H #include "HashLiner.h" template<typename T> class HashQuadratic:public HashLiner<T> { public:HashQuadratic(int c1…

优化 Azure 成本,实现财务目标

点击上方蓝字关注“汪宇杰博客”原文&#xff1a;Omar Khan General Manager, Microsoft Azure翻译&#xff1a;汪宇杰导语我们的许多客户都面临着如何满足关键 IT 项目的资金需求的困难决策。我们在此共同帮助您实现财务目标。确保 Azure 工作负载的成本得到优化有助于释放资金…

集合的定义与并查操作(C语言)

代码如下&#xff1a; #define MAXN 1000 /* 集合最大元素个数 */ typedef int ElementType; /* 默认元素可以用非负整数表示 */ typedef int SetName; /* 默认用根结点的下标作为集合名称 */ typedef ElementType SetType[MAXN]; /…

采用config方式灵活配置我们的Quarz.net中的Job,Trigger

经常在项目中遇到定时任务的时候&#xff0c;通常第一个想到的是Timer定时器&#xff0c;但是这玩意功能太弱鸡&#xff0c;实际上通常采用的是专业化的第三方调度框架&#xff0c;比如说Quartz&#xff0c;它具有功能强大和应用的灵活性&#xff0c;我想使用过的人都非常了解&…

hash table(开放寻址法-双重散列实现的哈希表)

hash table(开放寻址法-双重散列实现的哈希表&#xff09; #ifndef C11LEARN_HASHDOUBLE_H #define C11LEARN_HASHDOUBLE_H #include "HashLiner.h" template<typename T> class HashDouble:public HashLiner<T>{ protected:virtual int hashing(int key…

对于任给的一张无向带权连通图,求出其最小生成树(C++)

对于任给的一张无向带权连通图&#xff0c;求出其最小生成树。 题目要求: (1)编程创建一幅图 (2)输出创建的图 (3)编写Prim算法代码&#xff0c;实现图的最小生成树求解&#xff0c;且输出最小生成树 (4)编写Kruskal算法代码&#xff0c;实现图的最小生成树求解&#xff0c;且…

使用.Net Core实现的一个图形验证码

SimpleCaptcha是一个使用简单&#xff0c;基于.Net Standard 2.0的图形验证码模块。它的灵感来源于Edi.Wang的这篇文章https://edi.wang/post/2018/10/13/generate-captcha-code-aspnet-core&#xff0c;我将其中生成验证码的代码抽取出来进行封装得到了这个模块。下面介绍一下…

hash table(全域散列法实现的哈希表)

hash table(全域散列法实现的哈希表&#xff09; 利用每次重建哈希表时随机生成散列函数 #ifndef C11LEARN_HASHUNIVERSAL_H #define C11LEARN_HASHUNIVERSAL_H #include "Chain.h" #include "../tools/random.h" template<typename T> class HashU…

Maven编译项目时报错:不再支持源选项 5。请使用 6 或更高版本。 不再支持目标选项 1.5。请使用 1.6 或更高版本。

在使用Maven编译项目时报错&#xff1a; 不再支持源选项 5。请使用 6 或更高版本。 不再支持目标选项 1.5。请使用 1.6 或更高版本。 在项目pom.xml文件中增加maven编译的jdk版本设置&#xff0c;maven.compiler.source和maven.compiler.target&#xff1a; <properties&…

random(随机函数生成)

random(随机函数生成&#xff09; #ifndef C11LEARN_RANDOM_H #define C11LEARN_RANDOM_H #include <time.h> #include <stdlib.h> int random_include_left_right(int left,int right); int random_only_include_left(int left,int right); int random_only_incl…

ABP框架 v3.0 已发布!

我们很高兴地宣布,ABP框架和ABP商业版3.0版已经发布.与常规的2周发布一个版本不同的是, 这个版本用了4周的时间.关闭了119个issue,合并了89个pull request 和主框架仓库中的798次提交.由于这是一个主要版本,它也包括了一些重大更改.不要害怕,这些变化都容易对应,并且下面会详细…

C++实现dijkstra单源最短路径

代码如下: #include <iostream> using namespace std; const int N 30; typedef char ElemType; const double noEdge 99999;class Graph { private:double G[N][N];int vertexN, edgeN;double dist[N];bool vis[N];int path[N];int sv;ElemType data[N];int findMinD…

hash table(完全散列实现的哈希表)

hash table(完全散列实现的哈希表) 完全散列 特点&#xff1a;静态的&#xff0c;创建时候完成了散列表的生成。 不可以删&#xff0c;也不可以增加数据。只可以修改数据。 内部用全域散列生成 #ifndef C11LEARN_HASHPERFECT_H #define C11LEARN_HASHPERFECT_H #include &quo…

WPF 框架全构建环境虚拟机硬盘分享

现在 WPF 完全开源了&#xff0c;咱可以构建自己私有的版本。我分享一个虚拟机硬盘给你&#xff0c;只要你下载下来&#xff0c;通过 VMWare 导入&#xff0c;即可无需任何配置&#xff0c;拿到一个能构建 WPF 官方源代码的全构建环境。可以用来只做你的定制版的 WPF 框架现在 …

C++从vector中删除指定元素

①只删除一个元素 vector<int> num;for(vector<int>::iterator iternum.begin();iter!num.end();iter){ //从vector中删除指定的某一个元素 if(*iterk){num.erase(iter);break;} }②删除指定的多个重复元素 erase函数的返回的是指向被删除元素的下一个元素…

二叉树的遍历(算法导论第三版12.1-4)(包含先序遍历,后序遍历和中序遍历)

二叉树的遍历(算法导论第三版12.1-4) 1⃣️先序遍历 template<typename T> void preorder_tree_wald(BinaryTreeNode<T> *node) {if(node! nullptr){std::cout<<node->key<<" ";preorder_tree_wald(node->left);preorder_tree_wald(…

基于.NetCore3.1搭建项目系列 —— 使用Swagger做Api文档 (下篇)

前言回顾上一篇文章《使用Swagger做Api文档 》&#xff0c;文中介绍了在.net core 3.1中&#xff0c;利用Swagger轻量级框架&#xff0c;如何引入程序包&#xff0c;配置服务&#xff0c;注册中间件&#xff0c;一步一步的实现&#xff0c;最终实现生产自动生产API接口说明文档…

C++ vector容器中常见的三种遍历方式

1.迭代器 for (std::vector<int>::iterator it vecTest.begin(); it ! vecTest.end(); it){tempNum *it;}2.C11 新增关键字auto for (auto it : vecTest){tempNum it;}3.对C念念不舍的童鞋们习惯的数组写法 for (size_t i 0; i < maxCount; i){tempNum vecTest[i…

二叉树的中序遍历非递归方法(算法导论第三版12.1-3)

二叉树的中序遍历非递归方法&#xff08;算法导论第三版12.1-3&#xff09; 1⃣️用栈实现 template<typename T> void inorder_tree_walk_non_recursion_by_stack(BinaryTreeNode<T> *node) {Stack<BinaryTreeNode<T>*> stack;stack.push(node);whi…