Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #define HASHMAP_CAPACITY 100
- // =============================================
- class Entry {
- public:
- int value;
- int hash;
- int key;
- Entry(int, int);
- };
- Entry::Entry(int key, int value) {
- this->key = key;
- this->value = value;
- this->hash = this->key % HASHMAP_CAPACITY;
- }
- // =============================================
- class EntriesStack {
- Entry *data;
- int size;
- int hash;
- public:
- EntriesStack(int);
- ~EntriesStack();
- Entry *get(int);
- void push(Entry);
- void add(Entry);
- inline int getHash() {
- return this->hash;
- };
- };
- EntriesStack::EntriesStack(int hash) {
- this->data = (Entry *)calloc(0, sizeof(Entry));
- this->size = 0;
- this->hash = hash;
- }
- EntriesStack::~EntriesStack() {
- free(this->data);
- }
- Entry *EntriesStack::get(int key) {
- for (int i = 0; i < this->size; i++) {
- if (this->data[i].key == key) {
- return &this->data[i];
- }
- }
- return NULL;
- }
- void EntriesStack::push(Entry e) {
- this->data = (Entry *)realloc(this->data, ++this->size * sizeof(Entry));
- this->data[this->size - 1] = e;
- }
- void EntriesStack::add(Entry e) {
- Entry *entry = this->get(e.key);
- if (entry == NULL) {
- this->push(e);
- } else {
- entry->value = e.value;
- }
- }
- // =============================================
- class HashMap {
- EntriesStack *data;
- int size;
- public:
- HashMap();
- ~HashMap();
- int set(int, int);
- int get(int);
- int hasItem(int);
- };
- HashMap::HashMap() {
- this->data = (EntriesStack *)calloc(0, sizeof(EntriesStack));
- this->size = 0;
- }
- HashMap::~HashMap() {
- free(this->data);
- }
- int HashMap::set(int key, int value) {
- Entry e(key, value);
- for (int i = 0; i < this->size; i++) {
- if (this->data[i].getHash() == e.hash) {
- this->data[i].add(e);
- return value;
- }
- }
- this->data = (EntriesStack *)realloc(this->data, ++this->size * sizeof(EntriesStack));
- this->data[this->size - 1] = EntriesStack(e.hash);
- this->data[this->size - 1].push(e);
- }
- int HashMap::get(int key) {
- Entry e(key, 0);
- for (int i = 0; i < this->size; i++) {
- if (this->data[i].getHash() == e.hash) {
- return this->data[i].get(key)->value;
- }
- }
- return 0;
- }
- int HashMap::hasItem(int key) {
- Entry e(key, 0);
- for (int i = 0; i < this->size; i++) {
- if (this->data[i].getHash() == e.hash) {
- return this->data[i].get(key) != NULL;
- }
- }
- return false;
- }
- // =============================================
- int main() {
- HashMap *map = new HashMap();
- bool quit = false;
- do {
- char command;
- int key, value;
- std::cout << "Enter command (s = set, g = get, q = quit): ";
- std::cin >> command;
- switch (command) {
- case 's':
- std::cout << "Enter key: ";
- std::cin >> key;
- std::cout << "Enter value: ";
- std::cin >> value;
- map->set(key, value);
- break;
- case 'g':
- std::cout << "Enter key: ";
- std::cin >> key;
- if (map->hasItem(key)) {
- std::cout << "map[" << key << "] = " << map->get(key);
- } else {
- std::cout << "There is no item with that hash" << std::endl;
- }
- break;
- case 'q':
- quit = true;
- }
- std::cout << std::endl;
- } while (!quit);
- delete map;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement