【发布时间】:2017-08-24 08:42:07
【问题描述】:
我被要求创建一个带有说明的重新散列算法:
将旧向量表备份到临时向量oldTable
删除旧表中的元素
获取新的表大小
将表格扩展到新的大小
将 oldTable 中的元素重新插入扩展的新表中
删除 oldTable 中的元素
Hash Table.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 算法函数。 向量的使用让我感到困惑,我对此很陌生。 我在正确的轨道上吗?我的重新散列算法的各个部分会起作用吗? 谢谢
【问题讨论】:
-
不是你问的,但这里有几点。 1.你的
insert(X&)很奇怪,应该是const X&或者X&&2.不需要备份,建新表替换旧表即可。 -
insert 函数给了我们我们只被要求创建一个 newTableSize 函数和 re-hash 函数.. 它特别说明使用临时表,绝对没有必要吗?
-
可以在临时表上建新表,使用
table = temp_table; -
这对重新散列有用吗?因为新的关键索引等?
-
等等... 你的
HashTable假装它是一个模板类但使用PhoneDir的所有功能?此外,您的reHash不会做任何哈希!
标签: c++ algorithm vector hashmap hashtable