Tramway SDK
hashmap.h
Go to the documentation of this file.
1// TRAMWAY DRIFT AND DUNGEON EXPLORATION SIMULATOR 2022
2// All rights reserved.
3
4#ifndef TRAM_SDK_TEMPLATES_HASHMAP_H
5#define TRAM_SDK_TEMPLATES_HASHMAP_H
6
7#include <framework/uid.h>
8#include <iostream> // error message
9
10namespace tram {
11
12template <typename T>
13class Hashmap {
14public:
15 Hashmap(std::string name, size_t max_size) {
16 this->name = name;
17 this->max_size = max_size;
18
19 size_t memory_size = ((max_size * 2) + padding) * sizeof(PAIR);
20
21 char* memory = (char*)::operator new(memory_size);
22
23 first = (PAIR*) memory;
24 last = (PAIR*) (memory + memory_size);
25
26 for (PAIR* it = first; it != last; it++) new (it) PAIR(); // wait what the fuck
27 }
28
29 Hashmap(std::string name, size_t max_size, std::initializer_list<std::pair<uint64_t, T>> list) : Hashmap(name, max_size) {
30 for (const auto& entry : list) {
31 Insert(entry.first, entry.second);
32 }
33 }
34
35 T Find(UID key) {
36 return Find(key.key);
37 }
38
39 T Find(uint64_t key) {
40 uint64_t hash = key % max_size;
41
42 PAIR* candidate = first + hash;
43
44 while (candidate != last) {
45 if (candidate->first == key) {
46 return candidate->second;
47 }
48
49 if (candidate->first == 0) {
50 break;
51 }
52
53 candidate++;
54 }
55
56 return T();
57 }
58
59 void Insert(UID key, T value) {
60 Insert(key.key, value);
61 }
62
63 void Insert(uint64_t key, T value) {
64 if(size == max_size){
65 std::cout << "Hashmap " << name << " density reached!" << std::endl;
66 }
67
68 uint64_t hash = key % max_size;
69
70 PAIR* candidate = first + hash;
71
72 while (candidate != last) {
73 if (candidate->first == key) {
74 candidate->second.~T();
75 break;
76 }
77
78 if (candidate->first == 0) {
79 break;
80 }
81
82 candidate++;
83 }
84
85 if (candidate == last) {
86 std::cout << "Hashmap " << name << " overflow!" << std::endl;
87 abort();
88 }
89
90 candidate->first = key;
91 candidate->second = value;
92 }
93protected:
94 typedef std::pair<uint64_t, T> PAIR;
95 const size_t padding = 10;
96
97 std::string name;
98 size_t size = 0;
99 size_t max_size = 0;
100 PAIR* first = nullptr;
101 PAIR* last = nullptr;
102};
103
104}
105
106#endif // TRAM_SDK_TEMPLATES_HASHMAP_H
Definition: hashmap.h:13
size_t max_size
Definition: hashmap.h:99
Hashmap(std::string name, size_t max_size)
Definition: hashmap.h:15
const size_t padding
Definition: hashmap.h:95
size_t size
Definition: hashmap.h:98
std::pair< uint64_t, T > PAIR
Definition: hashmap.h:94
void Insert(UID key, T value)
Definition: hashmap.h:59
T Find(UID key)
Definition: hashmap.h:35
PAIR * last
Definition: hashmap.h:101
std::string name
Definition: hashmap.h:97
T Find(uint64_t key)
Definition: hashmap.h:39
Hashmap(std::string name, size_t max_size, std::initializer_list< std::pair< uint64_t, T > > list)
Definition: hashmap.h:29
PAIR * first
Definition: hashmap.h:100
void Insert(uint64_t key, T value)
Definition: hashmap.h:63
Definition: api.h:9
Definition: uid.h:11
uint32_t key
Definition: uid.h:37