【问题标题】:C++ Rehashing Algorithm VectorsC++ 重新散列算法向量
【发布时间】: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&amp;)很奇怪,应该是const X&amp;或者X&amp;&amp; 2.不需要备份,建新表替换旧表即可。
  • insert 函数给了我们我们只被要求创建一个 newTableSize 函数和 re-hash 函数.. 它特别说明使用临时表,绝对没有必要吗?
  • 可以在临时表上建新表,使用table = temp_table;
  • 这对重新散列有用吗?因为新的关键索引等?
  • 等等... 你的HashTable 假装它是一个模板类但使用PhoneDir 的所有功能?此外,您的 reHash 不会做任何哈希!

标签: c++ algorithm vector hashmap hashtable


【解决方案1】:

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

  1. 新建一个HashTable&lt;X&gt;
  2. 将所有项目插入其中。
  3. 致电std::swap(*this,new_hashTable);

如果您希望对代码进行最小更改,

改变 Table.insert(tempTable[i]);this-&gt;insert(*tempTable[i]); 可能会起作用。


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

【讨论】:

    【解决方案2】:

    最好直接构建一个新大小的tempTable向量,完成后移动到Table向量,因为它涉及的复制操作更少:

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

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

      • 创建一个具有新大小的临时向量
      • 复制所有元素
      • 将临时向量移动到原始向量:您直接窃取即将被销毁的临时向量的内部数组,而不是复制其所有元素。

    更少更简单的操作...

    【讨论】:

      猜你喜欢
      • 2012-09-03
      • 2016-01-23
      • 2012-05-26
      • 1970-01-01
      • 2016-04-14
      • 2023-03-30
      • 2011-07-03
      • 2017-11-14
      • 2017-07-18
      相关资源
      最近更新 更多