NetDEM v1.0
Loading...
Searching...
No Matches
mini_map.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "utils_io.hpp"
4#include "utils_macros.hpp"
5
6namespace netdem {
7
8template <typename T_key, typename T_val> struct Pair {
9public:
10 T_key first;
11 T_val second;
12
13 Pair() {}
14 Pair(const T_key &key, const T_key &val) : first(key), second(val) {}
15};
16
17template <typename T_key, typename T_val> class MiniMap {
18public:
19 const Pair<T_key, T_val> *begin() const { return &(pair_list.front()); }
20
21 Pair<T_key, T_val> *begin() { return &(pair_list.front()); }
22
23 const Pair<T_key, T_val> *end() const { return &(pair_list.back()) + 1; }
24
25 Pair<T_key, T_val> *end() { return &(pair_list.back()) + 1; }
26
28 *it = pair_list.back();
29 pair_list.pop_back();
30 }
31
32 void erase(const T_key &key) {
33 auto it = find(key);
34 erase(it);
35 }
36
37 const Pair<T_key, T_val> *find(const T_key &key) const {
38 for (auto &pair_item : pair_list) {
39 if (pair_item.first == key) {
40 return &pair_item;
41 }
42 }
43 return end();
44 }
45
46 Pair<T_key, T_val> *find(const T_key &key) {
47 for (auto &pair_item : pair_list) {
48 if (pair_item.first == key) {
49 return &pair_item;
50 }
51 }
52 return end();
53 }
54
55 int size() const { return pair_list.size(); };
56
57 int size() { return pair_list.size(); };
58
59 const T_val &operator[](const T_key &key) const {
60 auto it = find(key);
61 if (it != end()) {
62 return it->second;
63 } else {
64 IO::PrintError("in MiniMap[], key not exist");
65 return pair_list.back().second;
66 }
67 }
68
69 T_val &operator[](const T_key &key) {
70 auto it = find(key);
71 if (it != end()) {
72 return it->second;
73 } else {
74 pair_list.emplace_back();
75 pair_list.back().first = key;
76 pair_list.back().second = T_val();
77 return pair_list.back().second;
78 }
79 }
80
81 const T_val &at(const T_key &key) const {
82 auto it = find(key);
83 if (it != end()) {
84 return it->second;
85 } else {
86 IO::PrintError("in MiniMap[], key not exist");
87 return pair_list.back().second;
88 }
89 }
90
91 T_val &at(const T_key &key) {
92 auto it = find(key);
93 if (it != end()) {
94 return it->second;
95 } else {
96 IO::PrintError("in MiniMap[], key not exist");
97 return pair_list.back().second;
98 }
99 }
100
101 void clear() { pair_list.clear(); }
102
103private:
104 VecXT<Pair<T_key, T_val>> pair_list;
105};
106
107} // namespace netdem
static void PrintError(std::string const &info)
Definition utils_io.cpp:18
Definition mini_map.hpp:17
Pair< T_key, T_val > * find(const T_key &key)
Definition mini_map.hpp:46
const Pair< T_key, T_val > * find(const T_key &key) const
Definition mini_map.hpp:37
int size() const
Definition mini_map.hpp:55
const T_val & operator[](const T_key &key) const
Definition mini_map.hpp:59
void erase(Pair< T_key, T_val > *it)
Definition mini_map.hpp:27
T_val & at(const T_key &key)
Definition mini_map.hpp:91
T_val & operator[](const T_key &key)
Definition mini_map.hpp:69
const Pair< T_key, T_val > * end() const
Definition mini_map.hpp:23
Pair< T_key, T_val > * begin()
Definition mini_map.hpp:21
const T_val & at(const T_key &key) const
Definition mini_map.hpp:81
void clear()
Definition mini_map.hpp:101
const Pair< T_key, T_val > * begin() const
Definition mini_map.hpp:19
void erase(const T_key &key)
Definition mini_map.hpp:32
Pair< T_key, T_val > * end()
Definition mini_map.hpp:25
int size()
Definition mini_map.hpp:57
Definition bond_entry.hpp:7
std::vector< T > VecXT
Definition utils_macros.hpp:31
Definition mini_map.hpp:8
Pair(const T_key &key, const T_key &val)
Definition mini_map.hpp:14
Pair()
Definition mini_map.hpp:13
T_key first
Definition mini_map.hpp:10
T_val second
Definition mini_map.hpp:11