SPH
UnorderedMap.h
Go to the documentation of this file.
1 #pragma once
2 
7 
10 
12 
13 template <typename TKey, typename TValue>
14 class UnorderedMap : public Noncopyable {
15 public:
17  struct Element {
18  TKey key;
19  TValue value;
20  };
21 
22 private:
23  Array<Element> data;
24 
25 public:
26  UnorderedMap() = default;
27 
29  UnorderedMap(std::initializer_list<Element> list)
30  : data(list) {}
31 
35  INLINE TValue& operator[](const TKey& key) {
36  Element* element = this->find(key);
37  SPH_ASSERT(element);
38  return element->value;
39  }
40 
44  INLINE const TValue& operator[](const TKey& key) const {
45  const Element* element = this->find(key);
46  SPH_ASSERT(element);
47  return element->value;
48  }
49 
51  INLINE TValue& insert(const TKey& key, const TValue& value) {
52  Element* element = this->find(key);
53  if (!element) {
54  return this->add(key, value);
55  } else {
56  element->value = value;
57  return element->value;
58  }
59  }
60 
62  INLINE TValue& insert(const TKey& key, TValue&& value) {
63  Element* element = this->find(key);
64  if (!element) {
65  return this->add(key, std::move(value));
66  } else {
67  element->value = std::move(value);
68  return element->value;
69  }
70  }
71 
75  INLINE void remove(const TKey& key) {
76  Element* element = this->find(key);
77  SPH_ASSERT(element);
78  const Size index = element - &data[0];
79  data.remove(index);
80  }
81 
85  INLINE bool tryRemove(const TKey& key) {
86  Element* element = this->find(key);
87  if (!element) {
88  return false;
89  } else {
90  const Size index = element - &data[0];
91  data.remove(index);
92  return true;
93  }
94  }
95 
97  INLINE void clear() {
98  data.clear();
99  }
100 
104  INLINE Optional<TValue&> tryGet(const TKey& key) {
105  Element* element = this->find(key);
106  if (!element) {
107  return NOTHING;
108  } else {
109  return element->value;
110  }
111  }
112 
114  INLINE Optional<const TValue&> tryGet(const TKey& key) const {
115  const Element* element = this->find(key);
116  if (!element) {
117  return NOTHING;
118  } else {
119  return element->value;
120  }
121  }
122 
126  INLINE bool contains(const TKey& key) const {
127  return this->find(key) != nullptr;
128  }
129 
131  INLINE Size size() const {
132  return data.size();
133  }
134 
136  INLINE Size empty() const {
137  return data.empty();
138  }
139 
142  return data.begin();
143  }
144 
147  return data.begin();
148  }
149 
152  return data.end();
153  }
154 
157  return data.end();
158  }
159 
161  return data;
162  }
163 
164  INLINE operator ArrayView<const Element>() const {
165  return data;
166  }
167 
168  UnorderedMap clone() const {
169  UnorderedMap cloned;
170  cloned.data = data.clone();
171  return cloned;
172  }
173 
174 private:
176  INLINE Element* find(const TKey& key) {
177  for (Element& element : data) {
178  if (element.key == key) {
179  return &element;
180  }
181  }
182  return nullptr;
183  }
184 
185  INLINE const Element* find(const TKey& key) const {
186  return const_cast<UnorderedMap*>(this)->find(key);
187  }
188 
190  template <typename T>
191  INLINE TValue& add(const TKey& key, T&& value) {
192  data.push(Element{ key, std::forward<T>(value) });
193  return data.back().value;
194  }
195 };
196 
197 
Generic dynamically allocated resizable storage.
#define SPH_ASSERT(x,...)
Definition: Assert.h:94
NAMESPACE_SPH_BEGIN
Definition: BarnesHut.cpp:13
uint32_t Size
Integral type used to index arrays (by default).
Definition: Globals.h:16
#define INLINE
Macros for conditional compilation based on selected compiler.
Definition: Object.h:31
#define NAMESPACE_SPH_END
Definition: Object.h:12
Wrapper of type value of which may or may not be present.
const NothingType NOTHING
Definition: Optional.h:16
Object providing safe access to continuous memory of data.
Definition: ArrayView.h:17
INLINE Iterator< StorageType > end() noexcept
Definition: Array.h:462
void remove(const TCounter idx)
Removes an element with given index from the array.
Definition: Array.h:383
void clear()
Removes all elements from the array, but does NOT release the memory.
Definition: Array.h:434
INLINE TCounter size() const noexcept
Definition: Array.h:193
INLINE bool empty() const noexcept
Definition: Array.h:201
INLINE Iterator< StorageType > begin() noexcept
Definition: Array.h:450
Array clone() const
Performs a deep copy of all elements of the array.
Definition: Array.h:147
Simple (forward) iterator over continuous array of objects of type T.
Definition: Iterator.h:18
Wrapper of type value of which may or may not be present.
Definition: Optional.h:23
INLINE Size size() const
Returns the number of elements in the map.
Definition: UnorderedMap.h:131
UnorderedMap(std::initializer_list< Element > list)
Constructs the map fromm initializer list of elements.
Definition: UnorderedMap.h:29
UnorderedMap()=default
INLINE Iterator< const Element > end() const
Returns the iterator pointing to the one-past-last element.
Definition: UnorderedMap.h:156
INLINE bool contains(const TKey &key) const
Returns true if the map contains element of given key.
Definition: UnorderedMap.h:126
INLINE TValue & insert(const TKey &key, TValue &&value)
Adds a new element into the map or sets new value of element with the same key.
Definition: UnorderedMap.h:62
INLINE Optional< const TValue & > tryGet(const TKey &key) const
Returns a reference to the value matching the given key, or NOTHING if no such value exists.
Definition: UnorderedMap.h:114
INLINE void clear()
Removes all elements from the map.
Definition: UnorderedMap.h:97
UnorderedMap clone() const
Definition: UnorderedMap.h:168
INLINE Iterator< Element > end()
Returns the iterator pointing to the one-past-last element.
Definition: UnorderedMap.h:151
INLINE TValue & insert(const TKey &key, const TValue &value)
Adds a new element into the map or sets new value of element with the same key.
Definition: UnorderedMap.h:51
INLINE bool tryRemove(const TKey &key)
Removes element with given key if present, otherwise it does nothing.
Definition: UnorderedMap.h:85
INLINE Iterator< Element > begin()
Returns the iterator pointing to the first element.
Definition: UnorderedMap.h:141
INLINE void remove(const TKey &key)
Removes element with given key from the map.
Definition: UnorderedMap.h:75
INLINE const TValue & operator[](const TKey &key) const
Returns a reference to the element, given its key.
Definition: UnorderedMap.h:44
INLINE Optional< TValue & > tryGet(const TKey &key)
Returns a reference to the value matching the given key, or NOTHING if no such value exists.
Definition: UnorderedMap.h:104
INLINE Iterator< const Element > begin() const
Returns the iterator pointing to the first element.
Definition: UnorderedMap.h:146
INLINE TValue & operator[](const TKey &key)
Returns a reference to the element, given its key.
Definition: UnorderedMap.h:35
INLINE Size empty() const
Returns true if the map contains no elements, false otherwise.
Definition: UnorderedMap.h:136
Object with deleted copy constructor and copy operator.
Definition: Object.h:54
Element of the container.
Definition: UnorderedMap.h:17