-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkm_container.h
101 lines (77 loc) · 2.9 KB
/
km_container.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once
#include "km_array.h"
#include "km_defines.h"
#include "km_memory.h"
static const uint32 DYNAMIC_ARRAY_START_CAPACITY = 16;
// TODO pretty high, maybe do lower
static const uint32 HASH_TABLE_START_CAPACITY = 89;
// NOTE: Adding things to this container might invalidate pointers to elements.
// Subtle case that confused me: getting pointers through Append 3 times in a row, and only afterward
// setting the 3 values through the pointers. Some values would be unset if a resize was triggered.
template <typename T, typename Allocator = StandardAllocator>
struct DynamicArray
{
uint32 size;
T* data;
uint32 capacity;
Allocator* allocator;
DynamicArray(Allocator* allocator = nullptr, uint32 capacity = DYNAMIC_ARRAY_START_CAPACITY);
DynamicArray(const Array<T>& array, Allocator* allocator = nullptr);
DynamicArray(const DynamicArray<T>& other) = delete;
Array<T> ToArray() const;
void FromArray(const Array<T>& array);
T* Append();
T* Append(const T& element);
void Append(const Array<T>& array);
void Append(const Array<const T>& array);
void RemoveLast();
uint32 IndexOf(const T& value);
void Clear();
void Initialize(Allocator* allocator = nullptr, uint32 capacity = DYNAMIC_ARRAY_START_CAPACITY);
void Free();
inline T& operator[](uint32 index);
inline const T& operator[](uint32 index) const;
DynamicArray<T, Allocator>& operator=(const DynamicArray<T, Allocator>& other);
bool UpdateCapacity(uint32 newCapacity);
};
struct HashKey
{
static const uint32 MAX_LENGTH = 64;
FixedArray<char, MAX_LENGTH> s;
HashKey();
HashKey(Array<char> str); // TODO move somewhere else to use const_string?
HashKey(const Array<const char> str); // TODO move somewhere else to use const_string?
HashKey(const char* str);
bool WriteString(const Array<const char> str);
bool WriteString(const char* str);
};
template <typename V>
struct KeyValuePair
{
HashKey key;
V value;
};
template <typename V, typename Allocator = StandardAllocator>
struct HashTable
{
uint32 size;
uint32 capacity;
KeyValuePair<V>* pairs;
Allocator* allocator;
HashTable(Allocator* allocator = nullptr, uint32 capacity = HASH_TABLE_START_CAPACITY);
HashTable(const HashTable<V, Allocator>& other) = delete;
~HashTable();
void Add(const HashKey& key, const V& value);
V* Add(const HashKey& key);
V* GetValue(const HashKey& key);
const V* GetValue(const HashKey& key) const;
bool Remove(const HashKey& key);
void Clear();
void Initialize(Allocator* allocator = nullptr, uint32 capacity = HASH_TABLE_START_CAPACITY);
void Free();
HashTable<V, Allocator>& operator=(const HashTable<V, Allocator>& other);
private:
KeyValuePair<V>* GetPair(const HashKey& key) const;
KeyValuePair<V>* GetFreeSlot(const HashKey& key);
};
bool KeyCompare(const HashKey& key1, const HashKey& key2);