C ++ Rehashing Algorithm Vectors

时间:2017-03-30 12:14:43

标签: c++ algorithm vector hashmap hashtable

我被要求使用说明创建一个重复算法:

将旧矢量表备份到临时矢量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;

}

我成功创建了&amp;测试了newTableSize功能&amp;现在我正在使用rehash算法函数。 向量的使用使我感到困惑,我对此不熟悉。 我在正确的轨道上吗?我的重新哈希算法的部分是否有效? 感谢

2 个答案:

答案 0 :(得分:0)

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

  1. 制作新的HashTable<X>
  2. 将所有项目插入其中。
  3. 致电std::swap(*this,new_hashTable);
  4. 如果您希望对代码进行最小化更改,

    变化 Table.insert(tempTable[i]);this->insert(*tempTable[i]);可能有用。

    不要忘记当前使用的rehash会产生碰撞,你需要处理这个。

答案 1 :(得分:0)

最好直接使用新大小构建tempTable向量,并在完成移动它到表向量时,因为它涉及较少的复制操作:

  1. 保存表格,清理,复制:

    • save是向量的副本:涉及复制所有元素(确定它们只是指针)
    • 清除原始载体
    • 调整原始矢量的大小
    • 复制所有元素
    • 清除临时矢量
  2. 创建临时和移动:

    • 使用新尺寸
    • 创建临时矢量
    • 复制所有元素
    • 将临时矢量移动到原始矢量:直接窃取即将被销毁的临时矢量的内部数组,而不是复制其所有元素。
  3. 操作越来越简单......