CBuild
C++ build system with scripts written in c++
Loading...
Searching...
No Matches
map.hpp
Go to the documentation of this file.
1
23#ifndef __MYMAP_HPP__
24#define __MYMAP_HPP__
25// C++ libraries
26#include "stdexcept"
27#include "vector"
28// Code
29namespace lib {
38template <class _K, class _D> class mapData {
39 public:
40 _K key;
41 _D data;
42
43 public:
55 mapData(_K key, _D data) {
56 this->key = key;
57 this->data = data;
58 }
66 if ((this->key == val.key) && (this->data == val.data)) {
67 return true;
68 } else {
69 return false;
70 }
71 }
72};
79template <class _K, class _D> class map {
80 private:
81 std::vector<mapData<_K, _D>> content;
82
83 public:
87 map() {
88 this->content.clear();
89 }
93 map(map const&) = default;
99 map(std::initializer_list<lib::mapData<_K, _D>> init_list) {
100 this->content.reserve(init_list.size()); // Reserve space for efficiency
101 for (const auto& element : init_list) {
102 this->content.push_back(element);
103 }
104 }
112 this->content.push_back(element);
113 }
120 void push_back(_K key, _D data) {
121 lib::mapData<_K, _D> element(key, data);
122 this->content.push_back(element);
123 }
130 if (this->get(element.key) != NULL)
131 throw std::runtime_error("lib::map - Key already exist!");
132 this->push_back(element);
133 }
140 void push_back_check(_K key, _D data) {
141 if (this->get(key) != NULL)
142 throw std::runtime_error("lib::map - Key already exist!");
143 this->push_back(key, data);
144 }
150 lib::mapData<_K, _D> ret = this->content.at(this->content.size() - 1);
151 this->content.pop_back();
152 return ret;
153 }
159 const _D* get(_K key) {
160 if (this->content.empty())
161 return NULL;
162 for (unsigned int i = 0; i < this->content.size(); i++) {
163 lib::mapData<_K, _D>* elem = &(this->content.at(i));
164 if (elem->key == key) {
165 return &(elem->data);
166 }
167 }
168 return NULL;
169 }
176 if (this->content.empty())
177 return NULL;
178 for (unsigned int i = 0; i < this->content.size(); i++) {
179 lib::mapData<_K, _D>* elem = &(this->content.at(i));
180 if (elem->key == key) {
181 return elem;
182 }
183 }
184 return NULL;
185 }
192 bool contains_key(_K key) {
193 for (lib::mapData<_K, _D> data : this->content) {
194 if (data.key == key)
195 return true;
196 }
197 return false;
198 }
205 bool contains(_D value) {
206 for (lib::mapData<_K, _D> data : this->content) {
207 if (data.data == value)
208 return true;
209 }
210 return false;
211 }
215 void clear() {
216 this->content.clear();
217 }
223 lib::mapData<_K, _D> at(__SIZE_TYPE__ i) {
224 return this->content.at(i);
225 }
232 lib::mapData<_K, _D>* ptr_at(__SIZE_TYPE__ i) {
233 return &(this->content.at(i));
234 }
239 void remove(__SIZE_TYPE__ idx) {
240 if (idx >= this->content.size())
241 return;
242 this->content.erase(std::next(this->content.begin(), idx));
243 }
248 void remove(_K key) {
249 __SIZE_TYPE__ idx = this->content.size();
250 for (unsigned int i = 0; i < this->content.size(); i++) {
251 lib::mapData<_K, _D> elem = this->content.at(i);
252 if (elem.key == key) {
253 idx = i;
254 }
255 }
256 if (idx >= this->content.size())
257 return;
258 this->content.erase(std::next(this->content.begin(), idx));
259 }
264 std::vector<_K> keys() {
265 std::vector<_K> ret;
266 for (auto elem : this->content) {
267 ret.push_back(elem.key);
268 }
269 return ret;
270 }
275 __SIZE_TYPE__ size() {
276 return this->content.size();
277 }
284 return this->content.at(i);
285 }
293 map.content = this->content;
294 map.content.push_back(val);
295 return map;
296 }
304 map.content = this->content;
305 map.content.push_back(val);
306 this->content = map.content;
307 return map;
308 }
316 map.content = this->content;
317 for (lib::mapData<_K, _D> elem : val.content) {
318 map.push(elem);
319 }
320 return map;
321 }
329 map.content = this->content;
330 for (lib::mapData<_K, _D> elem : val.content) {
331 map.push(elem);
332 }
333 this->content = map.content;
334 return map;
335 }
342 this->content = val.content;
343 return *this;
344 }
351 this->content.clear();
352 this->content.push_back(val);
353 return *this;
354 }
361 if (this->content.size() != val.content.size())
362 return false;
363 for (__SIZE_TYPE__ i = 0; i < this->content.size(); i++) {
364 if (this->content.at(i) != val.content.at(i))
365 return false;
366 }
367 return true;
368 }
373 typename std::vector<mapData<_K, _D>>::iterator begin() {
374 return this->content.begin();
375 }
380 typename std::vector<mapData<_K, _D>>::iterator end() {
381 return this->content.end();
382 }
388 typename std::vector<mapData<_K, _D>>::const_iterator begin() const {
389 return this->content.begin();
390 }
396 typename std::vector<mapData<_K, _D>>::const_iterator end() const {
397 return this->content.end();
398 }
402 bool empty() {
403 return this->content.empty();
404 }
405 void erase(_K key) {
406 for (unsigned int i = 0; i < this->content.size(); i++) {
407 if (this->content.at(i).key == key) {
408 this->content.erase(this->content.begin() + i);
409 }
410 }
411 }
412};
413} // namespace lib
414#endif // __MYMAP_HPP__
Simple pair of values, have "==" operator implemented.
Definition map.hpp:38
mapData(_K key, _D data)
Construct a new map data object.
Definition map.hpp:55
mapData()
Construct a new map data object, ! Key and data variables is not initialize and contains garbage.
Definition map.hpp:48
bool operator==(lib::mapData< _K, _D > val)
If ((this.key == val.key) && (this.data == val.data))
Definition map.hpp:65
Simple map implementation with some stack operation added.
Definition map.hpp:79
void push_back(_K key, _D data)
Push new element to map, stack operation ! Dangerous not perform chak if this elemnt exists in map.
Definition map.hpp:120
bool empty()
Simmilar to std::vector::empty.
Definition map.hpp:402
bool contains_key(_K key)
Does map contains some key.
Definition map.hpp:192
void remove(__SIZE_TYPE__ idx)
Remove element from map and return copy of this element.
Definition map.hpp:239
std::vector< mapData< _K, _D > > content
Definition map.hpp:81
void push_back_check(lib::mapData< _K, _D > element)
Add data, but check if key exists, if exist - trow exception.
Definition map.hpp:129
lib::mapData< _K, _D > pop_back()
Pop last element, remove it and return back it's copy.
Definition map.hpp:149
std::vector< mapData< _K, _D > >::iterator begin()
Begin iterator.
Definition map.hpp:373
std::vector< _K > keys()
Get keys list.
Definition map.hpp:264
lib::map< _K, _D > operator+=(lib::mapData< _K, _D > val)
Add one element to map, modify it.
Definition map.hpp:302
lib::mapData< _K, _D > * ptr_at(__SIZE_TYPE__ i)
Array operation, get pointer to an element at specified index.
Definition map.hpp:232
map(map const &)=default
Copy constructor.
__SIZE_TYPE__ size()
Size of map.
Definition map.hpp:275
map(std::initializer_list< lib::mapData< _K, _D > > init_list)
Consruct a new map object and initialize it.
Definition map.hpp:99
void remove(_K key)
Remove element with given key.
Definition map.hpp:248
std::vector< mapData< _K, _D > >::const_iterator end() const
End const iterator.
Definition map.hpp:396
lib::map< _K, _D > operator=(lib::map< _K, _D > val)
Copy content of second map to first.
Definition map.hpp:341
void clear()
Erase all elements (simmilar to std::vector::clear())
Definition map.hpp:215
bool contains(_D value)
Does map contains some value.
Definition map.hpp:205
lib::map< _K, _D > operator+(lib::mapData< _K, _D > val)
Add one elment to map.
Definition map.hpp:291
map()
Construct a new map object.
Definition map.hpp:87
lib::map< _K, _D > operator+(lib::map< _K, _D > val)
Add another map to this map.
Definition map.hpp:314
void push_back(lib::mapData< _K, _D > element)
Push new element to map, stack operation ! Dangerous not perform chak if this elemnt exists in map.
Definition map.hpp:111
const _D * get(_K key)
Get element by it's key.
Definition map.hpp:159
lib::mapData< _K, _D > operator[](__SIZE_TYPE__ i)
Typical array operation, simmilar to at but using operator [].
Definition map.hpp:283
lib::map< _K, _D > operator+=(lib::map< _K, _D > val)
Add another map to this, modify it.
Definition map.hpp:327
bool operator==(lib::map< _K, _D > val)
Check if content of two maps is equal.
Definition map.hpp:360
std::vector< mapData< _K, _D > >::iterator end()
End iterator.
Definition map.hpp:380
void push_back_check(_K key, _D data)
Add data, but check if key exists, if exist - trow exception.
Definition map.hpp:140
lib::mapData< _K, _D > at(__SIZE_TYPE__ i)
Array operation, get element at index.
Definition map.hpp:223
lib::mapData< _K, _D > * get_ptr(_K key)
Get reference to key and value of map.
Definition map.hpp:175
lib::map< _K, _D > operator=(lib::mapData< _K, _D > val)
Reset map, set first it's element to val.
Definition map.hpp:350
std::vector< mapData< _K, _D > >::const_iterator begin() const
Begin const iterator.
Definition map.hpp:388
void erase(_K key)
Definition map.hpp:405
Optional datatype.
Definition map.hpp:29