Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. #define HASHMAP_CAPACITY 100
  5.  
  6. // =============================================
  7.  
  8. class Entry {
  9. public:
  10.     int value;
  11.     int hash;
  12.     int key;
  13.  
  14.     Entry(int, int);
  15. };
  16.  
  17. Entry::Entry(int key, int value) {
  18.     this->key = key;
  19.     this->value = value;
  20.     this->hash = this->key % HASHMAP_CAPACITY;
  21. }
  22.  
  23. // =============================================
  24.  
  25. class EntriesStack {
  26.     Entry *data;
  27.     int size;
  28.     int hash;
  29.  
  30. public:
  31.     EntriesStack(int);
  32.     ~EntriesStack();
  33.     Entry *get(int);
  34.     void push(Entry);
  35.     void add(Entry);
  36.  
  37.     inline int getHash() {
  38.         return this->hash;
  39.     };
  40. };
  41.  
  42. EntriesStack::EntriesStack(int hash) {
  43.     this->data = (Entry *)calloc(0, sizeof(Entry));
  44.     this->size = 0;
  45.     this->hash = hash;
  46. }
  47.  
  48. EntriesStack::~EntriesStack() {
  49.     free(this->data);
  50. }
  51.  
  52. Entry *EntriesStack::get(int key) {
  53.     for (int i = 0; i < this->size; i++) {
  54.         if (this->data[i].key == key) {
  55.             return &this->data[i];
  56.         }
  57.     }
  58.  
  59.     return NULL;
  60. }
  61.  
  62. void EntriesStack::push(Entry e) {
  63.     this->data = (Entry *)realloc(this->data, ++this->size * sizeof(Entry));
  64.     this->data[this->size - 1] = e;
  65.  
  66. }
  67.  
  68. void EntriesStack::add(Entry e) {
  69.     Entry *entry = this->get(e.key);
  70.     if (entry == NULL) {
  71.         this->push(e);
  72.     } else {
  73.         entry->value = e.value;
  74.     }
  75. }
  76.  
  77. // =============================================
  78.  
  79. class HashMap {
  80.     EntriesStack *data;
  81.     int size;
  82.  
  83. public:
  84.     HashMap();
  85.     ~HashMap();
  86.     int set(int, int);
  87.     int get(int);
  88.     int hasItem(int);
  89. };
  90.  
  91. HashMap::HashMap() {
  92.     this->data = (EntriesStack *)calloc(0, sizeof(EntriesStack));
  93.     this->size = 0;
  94. }
  95.  
  96. HashMap::~HashMap() {
  97.     free(this->data);
  98. }
  99.  
  100. int HashMap::set(int key, int value) {
  101.     Entry e(key, value);
  102.     for (int i = 0; i < this->size; i++) {
  103.         if (this->data[i].getHash() == e.hash) {
  104.             this->data[i].add(e);
  105.             return value;
  106.         }
  107.     }
  108.  
  109.     this->data = (EntriesStack *)realloc(this->data, ++this->size * sizeof(EntriesStack));
  110.     this->data[this->size - 1] = EntriesStack(e.hash);
  111.     this->data[this->size - 1].push(e);
  112. }
  113.  
  114. int HashMap::get(int key) {
  115.     Entry e(key, 0);
  116.     for (int i = 0; i < this->size; i++) {
  117.         if (this->data[i].getHash() == e.hash) {
  118.             return this->data[i].get(key)->value;
  119.         }
  120.     }
  121.  
  122.     return 0;
  123. }
  124.  
  125. int HashMap::hasItem(int key) {
  126.     Entry e(key, 0);
  127.     for (int i = 0; i < this->size; i++) {
  128.         if (this->data[i].getHash() == e.hash) {
  129.             return this->data[i].get(key) != NULL;
  130.         }
  131.     }
  132.  
  133.     return false;
  134. }
  135.  
  136. // =============================================
  137.  
  138. int main() {
  139.     HashMap *map = new HashMap();
  140.  
  141.     bool quit = false;
  142.     do {
  143.         char command;
  144.         int key, value;
  145.         std::cout << "Enter command (s = set, g = get, q = quit): ";
  146.         std::cin >> command;
  147.  
  148.         switch (command) {
  149.         case 's':
  150.             std::cout << "Enter key: ";
  151.             std::cin >> key;
  152.             std::cout << "Enter value: ";
  153.             std::cin >> value;
  154.             map->set(key, value);
  155.             break;
  156.         case 'g':
  157.             std::cout << "Enter key: ";
  158.             std::cin >> key;
  159.             if (map->hasItem(key)) {
  160.                 std::cout << "map[" << key << "] = " << map->get(key);
  161.             } else {
  162.                 std::cout << "There is no item with that hash" << std::endl;
  163.             }
  164.  
  165.             break;
  166.  
  167.         case 'q':
  168.             quit = true;
  169.         }
  170.  
  171.         std::cout << std::endl;
  172.     } while (!quit);
  173.  
  174.     delete map;
  175.  
  176.     return 0;
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement