哈希表的规则为5

时间:2017-03-31 04:21:06

标签: c++ hashtable copy-constructor assignment-operator

我已经为使用线性探测的哈希表实现了更新,搜索和删除功能。我的问题是,我该如何为此实施5的规则?我熟悉这些概念,但似乎无法掌握如何完成这项任务。感谢任何帮助,谢谢。我将发布到目前为止我所做的事情,这不起作用,因为程序在测试复制构造函数时崩溃了。

规则为5的哈希表

template <class TYPE>
class LPTable :public Table<TYPE> {

    struct Record {
            TYPE data_;
            string key_;
            bool isDeleted = false;

            Record() {
                    key_ = "";
                    data_ = 0;
                    isDeleted = false;
            }

            Record(const string& key, const TYPE& data) {
                    key_ = key;
                    data_ = data;
                    isDeleted = false;
            }

    };

    Record** records_;   //the table
    int LargerMax;       // *1.35 max_
    int max_;           //capacity of the array
    int size_;          //current number of records held
    int MyHash(string key); // custom hash function 
    int numRecords() const { return this.size_; }
    bool isEmpty()         { return size_ = 0; }

public:
    LPTable(int maxExpected);
    LPTable(const LPTable& other);
    LPTable(LPTable&& other);
    virtual bool update(const string& key, const TYPE& value);
    virtual bool remove(const string& key);
    virtual bool find(const string& key, TYPE& value);
    virtual const LPTable& operator=(const LPTable& other);
    virtual const LPTable& operator=(LPTable&& other);
    virtual ~LPTable();
};
/* none of the code in the function definitions below are correct.  You can replace what you need
*/
template <class TYPE>
LPTable<TYPE>::LPTable(int maxExpected) : Table<TYPE>() {
    LargerMax = maxExpected * 1.35;

    records_ = new Record*[LargerMax];  

    for (int i = 0; i < LargerMax; i++)
    {
            records_[i] = nullptr;
    }

    size_ = 0;
}


//custom hash function using std::hash
template <class TYPE>
int LPTable<TYPE>::MyHash(string key) {

    size_t idx = hash<string>()(key) % LargerMax;
    return idx;
}


//copy ctor
template <class TYPE>
LPTable<TYPE>::LPTable(const LPTable<TYPE>& other) {

    if (size_ != 0)
    {
            delete[] records_;
    }

    size_ = other.size_;
    LargerMax = other.LargerMax;
    max_ = other.max_;

    for (int i = 0; i < LargerMax; i++)
    {
            records_[i] = other.records_[i];
    }

 }

template <class TYPE>
LPTable<TYPE>::LPTable(LPTable<TYPE>&& other) {

}

//use copy/swap idiom
template <class TYPE>
const LPTable<TYPE>& LPTable<TYPE>::operator=(const LPTable<TYPE>& other)   {

    LPTable temp(other);
    std::swap(temp.records_, records_);
    std::swap(temp.max_, max_);
    std::swap(temp.size_, size_);
    return *this;

}

template <class TYPE>
const LPTable<TYPE>& LPTable<TYPE>::operator=(LPTable<TYPE>&& other) {
    return *this;

}
template <class TYPE>
LPTable<TYPE>::~LPTable() {

     delete[] records_;
}

0 个答案:

没有答案