Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- const int DEFAULT_SIZE = 16;
- template<typename V>
- class HashTable
- {
- private:
- template<typename V>
- struct Node {
- int key;
- V value;
- Node(int k, V v) : key(k), value(v) {}
- Node() : key(-1) {}
- };
- bool isUnEmpty() {
- return !(length == 0);
- }
- Node<V>* *table;
- size_t length;
- int (*func)(const V&);
- public:
- HashTable(int (*f)(const V&)) : table(new Node<V>*[DEFAULT_SIZE]), func(f), length(0) {
- for (int i = 0; i < DEFAULT_SIZE; i++) {
- table[i] = (Node<V>*)nullptr;
- }
- };
- ~HashTable() {
- for (int i = 0; i < DEFAULT_SIZE * length; i++) {
- delete table[i];
- }
- delete[] table;
- };
- void add(V elem) {
- int hash = hashCode(elem);
- if (table[hash] == (Node<V>*)nullptr) {
- length++;
- }
- table[hash] = new Node<V>(hash, elem);
- }
- int hashCode(V elem) {
- if (length < DEFAULT_SIZE) {
- return func(elem) % DEFAULT_SIZE;
- }
- return func(elem) % length;
- }
- bool isEmpty() {
- return !isUnEmpty();
- }
- int findElem(V elem) {
- int hash = hashCode(elem);
- for (int i = 0; i < DEFAULT_SIZE * length; i++) {
- if (table[i]->hash == hash) return hash;
- }
- return -1;
- }
- void remove(V elem) {
- int hash = findElem(elem);
- table[hash] = nullptr;
- length--;
- }
- void makeEmpty() {
- int i = 0;
- while (!isEmpty()) {
- remove(table[i++]);
- }
- }
- /*V get(int key) {
- }*/
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement