Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаб3.docx
Скачиваний:
12
Добавлен:
14.12.2018
Размер:
22.82 Кб
Скачать

HashTable.H

#pragma once

#include<iostream>

#include<vector>

using namespace std;

template<typename TKey, typename TValue>

class HashTable{

private:

int capacity;

vector<vector<pair<TKey, TValue>>> hashtable;

int (*getHash)(TKey);

public:

HashTable(int capacity, int (*getHash)(TKey));

void add(pair<TKey, TValue> p);

void pop(TKey key);

void print();

vector<pair<TKey,TValue>> getArray();

pair<TKey, TValue> getByKey(TKey key);

};

template<typename TKey, typename TValue>

HashTable<TKey, TValue>::HashTable(int capacity, int (*getHash)(TKey)):hashtable(capacity, vector<pair<TKey, TValue>>()){

this->capacity = capacity;

this->getHash = getHash;

}

template<typename TKey, typename TValue>

void HashTable<TKey, TValue>::add(pair<TKey, TValue> p)

{

hashtable[getHash(p.first)%capacity].push_back(p);

}

template<typename TKey, typename TValue>

void HashTable<TKey, TValue>::pop(TKey key)

{

int pos = getHash(key)%capacity;

for(int i = 0; i < hashtable[pos].size(); i++)

if(key == hashtable[pos][i].first)

hashtable[pos].erase(hashtable[pos].begin() + i);

}

template<typename TKey, typename TValue>

void HashTable<TKey, TValue>::print()

{

for(int i = 0; i < capacity; i++)

{

cout << "I:" << i << endl;

for (size_t j = 0; j < hashtable[i].size(); j++)

cout << " :"<< j << " " <<hashtable[i][j].first << " " << hashtable[i][j].second << endl;

}

}

template<typename TKey, typename TValue>

vector<pair<TKey,TValue>> HashTable<TKey, TValue>::getArray()

{

vector<pair<TKey,TValue>> listOfPairs;

for(int i = 0; i < capacity; i++)

for (size_t j = 0; j < hashtable[i].size(); j++)

listOfPairs.push_back(hashtable[i][j]);

return listOfPairs;

}

template<typename TKey, typename TValue>

pair<TKey, TValue> HashTable<TKey, TValue>::getByKey(TKey key)

{

vector<pair<TKey, TValue>> v = hashtable[getHash(key)%capacity];

for(int i = 0; i < v.size(); i++)

if(key == v[i].first)

return v[i];

throw 1;

}

DbTable.H

#pragma once

#include "HashTable.h"

#include "Field.h"

using namespace std;

class DBTable{

private:

HashTable<int,Field*>* table;

public:

DBTable(int(*getHash)(int));

void add(Field* f);

void pop(int key);

void rewrite(Field* f);

Field* getByKey(int key);

string toString();

};

DBTable::DBTable(int(*getHash)(int))

{

table = new HashTable<int,Field*>(10,getHash);

}

void DBTable::add(Field* f)

{

table->add(make_pair(f->key, f));

}

void DBTable::pop(int key)

{

table->pop(key);

}

Field* DBTable::getByKey(int key)

{

try{

return table->getByKey(key).second;

}catch(int e){

throw -1;

}

}

void DBTable::rewrite(Field* f)

{

table->pop(f->key);

table->add(make_pair(f->key, f));

}

string DBTable::toString()

{

string result = "";

for(auto row :table->getArray())

result += row.second->toString() + "\n";

return result;

}

Соседние файлы в предмете Теория алгоритмов и автоматов