summaryrefslogtreecommitdiff
path: root/hashmap.hpp
blob: 952bf8654288a85c6c2d7155f8fea687a1acd9b9 (plain) (blame)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef hashmap_hpp
#define hashmap_hpp

extern "C" {
#include "plat.h"
}

#include <tuple>
#include <stddef.h>

template <typename T>
struct Hash_Function {};

template <typename Key, typename Value, int size>
struct Hash_Map {
	enum {
		flags_tombstone = 1 << 0,
		flags_null      = 1 << 1
	};

	Key keys[size];
	Value values[size];
	uint8_t flags[size];

	void init() {
		int i;
		for (i = 0; i < size; i++) flags[i] = flags_null;
	}

	int find(const Key& to_find) const {
		int tombstone = -1, i;
		int bucket = (int)(Hash_Function<Key>{}(to_find) % (size_t)size);
		for (i = 0; i < size; i++) {
			const Key& k = keys[bucket];
			uint8_t flag = flags[bucket];
			if (flag & flags_null) {
				if (flag & flags_tombstone) {
					if (tombstone < 0) tombstone = bucket;
				} else return tombstone >= 0? tombstone: bucket;
			} else if (k == to_find) return bucket;
			bucket = (bucket + 1) % size;
		}
		if (tombstone >= 0) return tombstone;
		return -1;
	}

	Value& set(const Key& k, const Value& v) {
		int bucket = find(k);
		assert(bucket >= 0); /* full */
		flags[bucket] = 0;
		keys[bucket] = k;
		values[bucket] = v;
		return values[bucket];
	}

	Value* get(const Key& k) {
		int bucket = find(k);
		if (bucket < 0 || flags[bucket] & flags_null) return 0;
		return &values[bucket];
	}

	Value& operator[](const Key& k) {
		int bucket = find(k);
		assert(bucket >= 0);
		return values[bucket];
	}

	const Value& operator[](const Key& k) const {
		int bucket = find(k);
		assert(bucket >= 0);
		return values[bucket];
	}

	Key* kaddr(const Key& k) {
		int bucket = find(k);
		if (bucket < 0 || flags[bucket] & flags_null) return 0;
		return &keys[bucket];
	}

	void remove(const Key& k) {
		int bucket = find(k);
		assert(bucket >= 0);
		flags[bucket] = flags_null | flags_tombstone;
	}

	int has(const Key& k) {
		int bucket = find(k);
		return bucket >= 0 && ~flags[bucket] & flags_null;
	}

	template <typename Table>
	struct iterator {
		Table* table;
		int bucket;

		void init_begin(Table* t) {
			bucket = 0;
			table = t;
			while (
				bucket < size &&
				table->flags[bucket] & flags_null
			) bucket++;
		}
		void init_end(Table* t) {
			bucket = size;
			table = t;
		}
		bool equals(const iterator<Table>& other) {
			return bucket == other.bucket && table == other.table;
		}
		bool operator==(const iterator<Table>& other) {
			return equals(other);
		}
		bool operator!=(const iterator<Table>& other) {
			return !equals(other);
		}
		iterator<Table> operator++() {
			bucket++;
			while (
				bucket < size &&
				table->flags[bucket] & flags_null
			) bucket++;
			return *this;
		}
		std::pair<Key&, Value&> operator*() {
			return { table->keys[bucket], table->values[bucket] };
		}
		std::pair<const Key&, const Value&> operator*() const {
			return { table->keys[bucket], table->values[bucket] };
		}
	};

	iterator<Hash_Map<Key, Value, size>> begin() {
		iterator<Hash_Map<Key, Value, size>> r;
		r.init_begin(this);
		return r;
	}

	iterator<Hash_Map<Key, Value, size>> end() {
		iterator<Hash_Map<Key, Value, size>> r;
		r.init_end(this);
		return r;
	}

	iterator<const Hash_Map<Key, Value, size>> begin() const {
		iterator<const Hash_Map<Key, Value, size>> r;
		r.init_begin(this);
		return r;
	}

	iterator<const Hash_Map<Key, Value, size>> end() const {
		iterator<const Hash_Map<Key, Value, size>> r;
		r.init_end(this);
		return r;
	}
};

#endif