C++ 重新散列算法向量
C++ Rehashing Algorithm Vectors
我被要求创建一个带有说明的重新哈希算法:
将旧矢量表备份为临时矢量旧表
删除旧表中的元素
获取新的表大小
将表格展开为新大小
将旧表中的元素重新插入到展开的新表中
删除旧表中的元素
哈希表.h 类:
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include "Math.h"
// hash table storing class X objects using linear probing
template <class X>
class HashTable {
public:
// constructor sets the hash table size & load threshold
HashTable(int table_size, double load_threshold = 0.75);
// destructor
~HashTable() { for (int i = 0; i < Table.size(); i++) if (Table[i]) delete Table[i]; }
// search for object a in the table
size_t find(X& a); // size_t = unsigned int
// insert new object a in the table, return true if done
bool insert(X& a);
//function to return a new prime table size
size_t newTableSize();
//rehash func
void reHash();
private:
// the hash table & number of objects stored
vector<X*> Table;
size_t num_x;
// maximum load threshold
double LOAD_TH;
};
template <class X>
HashTable<X>::HashTable(int table_size, double load_threshold)
{
for (int i = 0; i < table_size; i++) Table.push_back(NULL);
num_x = 0;
LOAD_TH = load_threshold;
}
template <class X>
size_t HashTable<X>::newTableSize() {
bool found = true;
int newSize = 2 * Table.size() + 1; // = now odd because oldSize is a prime
do {
int x = sqrt(newSize);
for (int i = 3; i <= x; i += 2) {
if (newSize % i == 0) {
newSize = newSize + 2;
x = sqrt(newSize);
break;
}
else
{
found = true;
}
}
} while (!found);
return newSize;
}
template<class X>
void HashTable<X>::reHash() {
//Backup the old vector<X*> Table to a temporary vector<X*> oldTable
vector<X*> tempTable;
tempTable = Table;
//Delete the elements in the old Table
Table.clear();
//Obtain the new table size
int newPrimeSize = newTableSize();
//Expand Table to the new size
Table.resize(newPrimeSize);
//Reinsert elements from oldTable into the expanded new Table
//Table = tempTable;
//or method below??
for (int i = 0; i < tempTable.size; i++) {
if (tempTable[i] != NULL){
Table.insert(tempTable[i]);
}
}
//Delete the elements in the oldTable
tempTable.clear();
}
template <class X>
size_t HashTable<X>::find(X& a)
{
// calculate the hash index
size_t index = a.hash_index() % Table.size();
// search - find index of matching key or the 1st empty slot
while (Table[index] != NULL && Table[index]->get_key() != a.get_key())
index = (index + 1) % Table.size();
// retrieve matching value to a if found
if (Table[index] != NULL) a.set_value(Table[index]->get_value());
return index;
}
template <class X>
bool HashTable<X>::insert(X& a)
{
// calculate the load factor of the table
double load_factor = (double)num_x / (double)Table.size();
if (load_factor > LOAD_TH) {
// replace the following return by rehashing - practical work
return 0;
}
// search a in the able
size_t index = find(a);
// not found, create a new entry in the table
if (Table[index] == NULL) {
Table[index] = new X(a);
num_x++;
return 1;
}
// object already in table, do nothing
return 0;
}
#endif
测试类:
#include <iostream>
#include <vector>
#include <string>
#include <time.h>
#include "Math.h"
using namespace std;
#include "HashTable.h"
// a class of phone records
class PhoneDir {
public:
PhoneDir(string name, int number = -1)
: name(name), number(number) {};
string get_key() { return name; }
int get_value() { return number; }
void set_value(int num) { number = num; }
size_t hash_index(); // return hash index of key: name
private:
string name; // key
int number; // value
};
size_t PhoneDir::hash_index()
{
size_t hash_index = 0;
for (int i = 0; i < name.size(); i++) {
char c = name[i];
hash_index = 37 * hash_index + c;
}
return hash_index;
}
int main()
{
int oldSize = 5;
// store phone records in hash table with size 11
HashTable<PhoneDir> HTable(7);
HTable.insert(PhoneDir("Tom", 123456));
HTable.insert(PhoneDir("Sam", 346834));
HTable.insert(PhoneDir("Pete", 347980));
HTable.insert(PhoneDir("Jack", 328709));
HTable.insert(PhoneDir("David", 335566));
// serach using name for phone number over the hash table
char yn = 'y';
do {
//test function part 1
cout << " Test " << endl;
cout << HTable.newTableSize();
cout << "Whose number are you looking for? ";
string name; cin >> name;
// form enquiry and search
PhoneDir enquiry(name);
clock_t t0 = clock();
size_t index = HTable.find(enquiry);
clock_t t1 = clock();
cout << "index = " << index;
cout << ", name = " << enquiry.get_key();
cout << ", number = " << enquiry.get_value() << endl;
cout << "time taken = " << t1 - t0 << endl << endl;
cout << "Another (y/n)? "; cin >> yn;
} while (yn == 'y');
return 0;
}
我成功地创建并测试了newTableSize函数,现在我在rehash算法函数上。矢量的使用让我感到困惑,我是新手。我走在正确的轨道上吗?我的重新哈希算法的某些部分会起作用吗?谢谢
最简单的
方法之一是创建一个新HashTable
并交换两者。
- 创建新
HashTable<X>
- 将所有项目插入其中。
- 呼叫
std::swap(*this,new_hashTable);
如果你想对你的代码进行最小的更改,
改变 Table.insert(tempTable[i]);
至 this->insert(*tempTable[i]);
可能会起作用。
不要忘记您当前使用的重新哈希会产生冲突,您需要处理这个问题。
最好直接构建一个具有新大小的 tempTable 向量,完成后将其移动到 Table 向量,因为它涉及较少的复制操作:
-
保存表,清理,复制:
- Save 是向量的副本:涉及复制所有元素(好吧,它们只是指针(
- 清除原始矢量
- 调整原始矢量的大小
- 复制所有元素
- 清除温度矢量
-
创建临时和移动:
- 创建具有新大小的临时矢量
- 复制所有元素 将 temp 向量
- 移动到原始向量:您直接窃取即将被销毁的 temp 向量的内部数组,而不是复制其所有元素。
操作更少更简单...
相关文章:
- 为什么在此排序算法实现中,向量明显比数组慢?
- 如何使用 STL 算法将整数向量转换为字符串向量?
- C++向量初始化遗传算法的代理向量
- 使用 Rcpp 加速替换迭代算法中的列表和向量元素是否合法?
- 检查向量是否使用除法和阻抗算法进行排序
- 从整数向量向量搜索整数向量的最佳算法
- 如何通过 stl 容器和算法库计算两个向量的内积?
- 如何在不使用标准算法的情况下在排序向量中添加 c 元素?
- C++ std::向量插入两个元素替代算法失败
- 五个中三个最大参数的平均值,不使用排序算法或向量/数组
- 使用STL算法合并2个向量
- adjacent_difference算法,back_inserter向量<向量<int>>
- 并行算法将向量的元素分配到另一个元素的元素
- 双重释放或损坏(输出):使用向量的组合算法0x0000000001a880a0***
- C++ 重新散列算法向量
- 使用QuickSort算法以降序排序向量
- 使用算法查找自定义数据向量中的最大值和最小值
- 为对象向量实现快速排序算法
- 在不使用默认算法的情况下对字符串的 std 向量进行排序
- 向量返回/加法算法的问题