Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar10 / src / hashmap / linear-probing.hpp
blob: 04a541fa41253ab8aebcb3d023f5f519f02941d0 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#pragma once
#include <stdexcept>
#include <vector>

template <class Key, class Value, class Hash = std::hash<Key>>
class linear_probing_hashmap
{
  private:
	using pair_type = std::pair<Key, Value>;

	struct node
	{
		pair_type data;
		bool empty = true;
		bool deleted = false;

		// TODO: This might be wasteful.
		node() = default;

		node(const Key& key, const Value& value)
			: data(std::make_pair(key, value))
		{}
	};

	std::vector<node> _data;
	Hash _hash;
	double _load_factor;
	size_t _size = 0;
	size_t _jump_value = 1;

	size_t get_hash_index(const Key& key) const
	{
		return _hash(key) % bucket_count();
	}

	void increment_index(size_t& index, size_t max_size) const
	{
		index += _jump_value;

		// TODO: empty?
		if (index >= max_size)
			index %= max_size;
	}

	size_t get_iterations_needed() const
	{
		return _data.size();
	}

  public:
	linear_probing_hashmap(size_t initial_capacity, double load_factor, size_t jump_value = 1)
		: _data(initial_capacity)
		, _load_factor(load_factor)
		, _jump_value(jump_value)
	{}

	linear_probing_hashmap()
		: linear_probing_hashmap(10, 0.75)
	{}

	void insert(const Key& key, const Value& value);

	const Value& find(const Key& key) const;
	Value& modify(const Key& key);

	void erase(const Key& key);

	void resize(size_t count);

	size_t size() const
	{
		return _size;
	}

	bool empty() const
	{
		return _size == 0;
	}

	size_t bucket_count() const
	{
		return _data.size();
	}

	double max_load_factor() const
	{
		return _load_factor;
	}
};

template <class Key, class Value, class Hash>
void linear_probing_hashmap<Key, Value, Hash>::insert(const Key& key, const Value& value)
{
	size_t current_index = get_hash_index(key);
	size_t iterations_needed = get_iterations_needed();

	for (size_t i = 0; i < iterations_needed; i++)
	{
		node& current_node = _data[current_index];

		// Element is contained in the collection.
		if (!current_node.empty && !current_node.deleted && current_node.data.first == key)
		{
			current_node.data.second = value;
			return;
		}

		// We don't need to check for deleted flag here.
		if (current_node.empty)
		{
			current_node = node(key, value);
			current_node.empty = false;
			current_node.deleted = false;

			++_size;

			if (size() >= bucket_count() * max_load_factor())
			{
				resize(2 * bucket_count());
			}

			return;
		}
		increment_index(current_index, _data.size());
	}
	throw std::runtime_error("Numbers are not co-prime!");
}

template <class Key, class Value, class Hash>
const Value& linear_probing_hashmap<Key, Value, Hash>::find(const Key& key) const
{
	size_t current_index = get_hash_index(key);
	size_t iterations_needed = get_iterations_needed();

	for (size_t i = 0; i < iterations_needed; i++)
	{
		const node& current_node = _data[current_index];

		if (current_node.empty && !current_node.deleted)
		{
			break;
		}

		if (!current_node.empty && !current_node.deleted && current_node.data.first == key)
		{
			return current_node.data.second;
		}

		increment_index(current_index, _data.size());
	}

	throw std::out_of_range("[linear_probing_hashmap]: Element not found.");
}

template <class Key, class Value, class Hash>
Value& linear_probing_hashmap<Key, Value, Hash>::modify(const Key& key)
{
	size_t current_index = get_hash_index(key);
	size_t iterations_needed = get_iterations_needed();

	for (size_t i = 0; i < iterations_needed; i++)
	{
		node& current_node = _data[current_index];

		if (current_node.empty && !current_node.deleted)
		{
			break;
		}

		if (!current_node.empty && !current_node.deleted && current_node.data.first == key)
		{
			return current_node.data.second;
		}

		increment_index(current_index, _data.size());
	}

	throw std::out_of_range("[linear_probing_hashmap]: Element not found.");
}

template <class Key, class Value, class Hash>
void linear_probing_hashmap<Key, Value, Hash>::erase(const Key& key)
{
	size_t current_index = get_hash_index(key);
	size_t iterations_needed = get_iterations_needed();

	for (size_t i = 0; i < iterations_needed; i++)
	{
		node& current_node = _data[current_index];
		
		if (current_node.empty && !current_node.deleted)
		{
			break;
		}

		if (current_node.data.first == key)
		{
			current_node.deleted = true;
			current_node.empty = true;
			_size--;
			break;
		}

		increment_index(current_index, _data.size());
	}
}

template <class Key, class Value, class Hash>
void linear_probing_hashmap<Key, Value, Hash>::resize(size_t count)
{
	// TODO: Ensure count is big enough.
	std::vector<node> new_data(count);

	for (const auto& current_element : _data)
	{
		if (current_element.empty)
			continue;

		size_t current_index = _hash(current_element.data.first) % new_data.size();
		size_t iterations_needed = new_data.size();

		for (size_t i = 0; i < iterations_needed; i++)
		{
			node& current_node = new_data[current_index];

			if (current_node.empty)
			{
				current_node = current_element;
				break;
			}

			increment_index(current_index, new_data.size());
		}
	}

	_data = std::move(new_data);
}