Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

12пми / MyHashTable / MyHashTable

.txt
Скачиваний:
18
Добавлен:
02.06.2015
Размер:
2.69 Кб
Скачать
#ifndef MYHASHTABLE_H_
#define MYHASHTABLE_H_

#include <iostream>
#include <climits>
using namespace std;

template <typename T>
class MyHashTable
{
public:
    MyHashTable(const unsigned size);
    virtual ~MyHashTable();
    void clear();
    bool empty();
    unsigned size();
    unsigned maxSize();
    bool get(const string& key, T& value);
    bool set(const string& key, const T value);
    void printLog();
private:
    class Element
    {
    public:
        Element(const string& key, const T value);
        virtual ~Element();
        string key;
        T value;
        Element *next;
    };

    unsigned mSize; // size of the hash table
    unsigned mCount; // number elements which are contained in the hash table
    Element **mArray;
    unsigned hashFunction(const string& key);
};

template <typename T>
MyHashTable<T>::MyHashTable(const unsigned size)
{
    mSize = size;
    //Create table of pointers
    mArray = new Element*[mSize];
    mCount = 0;
	for(unsigned i=0; i<mSize; i++)
	{
		mArray[i]=NULL;
	}
}

template <typename T>
MyHashTable<T>::~MyHashTable()
{
    if (mArray != NULL)
    {
        //Clean the hash table
        clear();
        //Erase table of pointers
        delete[] mArray;
        mArray = NULL;
    }
}


__________________
//вам нужно дописать set().get().clear();




__________________
template <typename T>
bool MyHashTable<T>::empty()
{
    return (!mSize);
}

template <typename T>
unsigned MyHashTable<T>::size()
{
    return mCount;
}

template <typename T>
unsigned MyHashTable<T>::maxSize()
{
    return UINT_MAX;
}


template <typename T>
void MyHashTable<T>::printLog()
{
    if (mArray != NULL)
    {
        for (int i = 0; i < mSize; i++)
        {
            cout << "printLog index " << i << endl;
            Element* el = mArray[i];
            while(el != NULL)
            {
                cout << "printLog key = " << el->key;
                cout << endl;
                el = el->next;
            }
        }
    }
}



template <typename T>
unsigned MyHashTable<T>::hashFunction(const string& key)
{
    unsigned value = 0;
    for(string::const_iterator cit = key.begin(); cit != key.end(); ++cit)
    {
        value += *cit;
        value %= mSize;
    }
    return value;
}


template <typename T>
MyHashTable<T>::Element::Element(const string& newKey, const T newValue):key(newKey), value(newValue), next(NULL)
{
}

template <typename T>
MyHashTable<T>::Element::~Element()
{
    cout << "delete key " << key << endl;
}

#endif /* MYHASHTABLE_H_ */

______
Соседние файлы в папке MyHashTable