Molassembler  3.0.0
Molecule graph and conformer library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
DynamicMap.h
Go to the documentation of this file.
1 
11 #ifndef INLCUDE_MOLASSEMBLER_TEMPLE_CONSTEXPR_DYNAMIC_MAP_H
12 #define INLCUDE_MOLASSEMBLER_TEMPLE_CONSTEXPR_DYNAMIC_MAP_H
13 
16 
17 namespace Scine {
18 namespace Molassembler {
19 namespace Temple {
20 
29 template<typename KeyType, typename MappedType, size_t N>
30 class DynamicMap {
31 private:
32  /* std::pair is NOT constexpr move or copy assignable, and this does not
33  * appear to change with C++17
34  */
36 
37  /* A map is just a tree set where each node stores both a key and a value.
38  * Comparisons and equality merely reference the keys. Value type lookup by
39  * key is just searching for a node by key and returning the stored value
40  * type there.
41  */
43  constexpr static auto keyComparator_ = std::less<KeyType>();
44 
45  constexpr OnlyFirstComparator() = default;
46 
47  constexpr bool operator() (
48  const PairType& a,
49  const PairType& b
50  ) const {
51  return keyComparator_(a.first, b.first);
52  }
53  };
54 
56  constexpr static auto keyComparator_ = std::equal_to<KeyType>();
57 
58  constexpr OnlyFirstEquality() = default;
59 
60  constexpr bool operator() (
61  const PairType& a,
62  const PairType& b
63  ) const {
64  return keyComparator_(a.first, b.first);
65  }
66  };
67 
69 
70  SetType items_;
71 
72 public:
75  constexpr DynamicMap() = default;
76 
77  constexpr DynamicMap(DynamicMap&& other) noexcept
78  : items_(other.items_)
79  {}
80 
81  constexpr DynamicMap(const DynamicMap& other)
82  : items_(other.items_)
83  {}
84 
85  constexpr DynamicMap& operator = (const DynamicMap& other) {
86  items_ = other.items_;
87  return *this;
88  }
89 
90  constexpr DynamicMap& operator = (DynamicMap&& other) noexcept {
91  items_ = other.items_;
92  return *this;
93  }
95 
98 
102  constexpr void insert(
103  KeyType key,
104  MappedType item
105  ) {
106  PairType pair { std::move(key), std::move(item) };
107 
108  if(items_.contains(pair)) {
109  throw "Map already contains an item for this key!";
110  }
111 
112  items_.insert(pair);
113  }
114 
120  constexpr void insertOrUpdate(
121  KeyType key,
122  MappedType item
123  ) {
124  PairType pair { std::move(key), std::move(item) };
125 
126  auto searchIter = items_.find(pair);
127 
128  if(searchIter == items_.end()) {
129  items_.insert(pair);
130  } else {
131  // searchIter is a const_iterator unfortunately, so need to go via index
132  auto indexOfElement = static_cast<unsigned>(
133  searchIter - items_.begin()
134  );
135 
136  // Overwrite pair with same key
137  *(items_.begin() + indexOfElement) = pair;
138  }
139  }
140 
141  constexpr void clear() {
142  items_.clear();
143  }
145 
148 
152  constexpr const MappedType& at(const KeyType& key) const {
153  PairType pair {key, MappedType {}};
154  auto keyOptional = items_.getOption(pair);
155 
156  if(!keyOptional.hasValue()) {
157  throw "No such key in this DynamicMap!";
158  }
159 
160  return keyOptional.value().second;
161  }
162 
163 
168  constexpr unsigned size() const {
169  return items_.size();
170  }
172 
175  using const_iterator = typename SetType::const_iterator;
176 
177  constexpr const_iterator begin() const {
178  return items_.begin();
179  }
180 
181  constexpr const_iterator end() const {
182  return items_.end();
183  }
185 
188  constexpr bool operator == (const DynamicMap& other) const {
189  return items_ == other.items_;
190  }
191 
192  constexpr bool operator != (const DynamicMap& other) const {
193  return !(*this == other);
194  }
195 
196  constexpr bool operator < (const DynamicMap& other) const {
197  return items_ < other.items_;
198  }
199 
200  constexpr bool operator > (const DynamicMap& other) const {
201  return other.items_ < items_;
202  }
204 };
205 
206 } // namespace Temple
207 } // namespace Molassembler
208 } // namespace Scine
209 
210 #endif
PURITY_WEAK constexpr bool contains(const T &item) const
Check if the set contains an element.
Definition: DynamicSet.h:61
A constexpr associative container with reasonably fast key-based lookup.
Definition: DynamicMap.h:30
constexpr void clear()
Inserts an element into the map.
Definition: DynamicMap.h:141
Heterogeneous pair type.
Definition: Pair.h:27
constexpr void insertOrUpdate(KeyType key, MappedType item)
Inserts a key-value pair into the map or updates the mapped value if the key exists.
Definition: DynamicMap.h:120
constexpr void insert(KeyType key, MappedType item)
Inserts an element into the map.
Definition: DynamicMap.h:102
constexpr unsigned size() const
Number of items in the map.
Definition: DynamicMap.h:168
BTree-based std::set-like container (but max size is space allocated)
constexpr const MappedType & at(const KeyType &key) const
Value lookup.
Definition: DynamicMap.h:152
constexpr void clear()
Remove all elements of the set.
Definition: DynamicSet.h:78
std::pair-like container
constexpr void insert(const T &item)
Insertion an element into the set.
Definition: DynamicSet.h:69