gpt4 book ai didi

C++ 重新散列算法 vector

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:09 25 4
gpt4 key购买 nike

我被要求创建一个带有指令的重新散列算法:

 将旧 vector 表备份到临时 vector 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 算法函数。 vector 的使用让我感到困惑,我对此很陌生。我在正确的轨道上吗?我的重新哈希算法的各个部分会起作用吗?谢谢

最佳答案

最简单的方法之一是创建一个新的 HashTable并交换两者。

  1. 创建一个新的 HashTable<X>
  2. 将所有项目插入其中。
  3. 调用std::swap(*this,new_hashTable);

如果你想对你的代码做最少的改动,

改变 Table.insert(tempTable[i]);this->insert(*tempTable[i]);可能会奏效。


不要忘记重新哈希,因为您当前使用的会产生冲突,您需要处理它。

关于C++ 重新散列算法 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43117592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com