18 template <
typename TKey,
typename TValue,
typename TLess = std::less<TKey>>
39 FlatMap(std::initializer_list<Element> list)
42 return less(e1.key, e2.key);
50 Element* element = this->find(key);
52 return element->
value;
59 const Element* element = this->find(key);
61 return element->
value;
66 Element* element = this->find(key);
68 return this->add(key, value);
70 element->
value = value;
71 return element->
value;
77 Element* element = this->find(key);
79 return this->add(key, std::move(value));
81 element->
value = std::move(value);
82 return element->
value;
90 Element* element = this->find(key);
92 const Size index = element - &data[0];
100 Element* element = this->find(key);
104 const Size index = element - &data[0];
119 Element* element = this->find(key);
123 return element->
value;
129 const Element* element = this->find(key);
133 return element->
value;
141 return this->find(key) !=
nullptr;
184 cloned.data = data.
clone();
189 INLINE bool less(
const TKey& key1,
const TKey& key2)
const {
190 return TLess::operator()(key1, key2);
194 INLINE Element* find(
const TKey& key) {
195 auto compare = [
this](
const Element& element,
const TKey& key) {
return less(element.key, key); };
196 auto iter = std::lower_bound(data.
begin(), data.
end(), key, compare);
197 if (iter != data.
end() && iter->key == key) {
204 INLINE const Element* find(
const TKey& key)
const {
205 return const_cast<FlatMap*
>(
this)->find(key);
209 template <
typename T>
210 INLINE TValue& add(
const TKey& key, T&& value) {
215 while (from < to && from != mid) {
216 mid = (from + to) / 2;
217 SPH_ASSERT(less(data[mid].key, key) || less(key, data[mid].key));
218 if (less(data[mid].key, key)) {
224 data.
insert(from, Element{ key, std::forward<T>(value) });
225 return data[from].value;
Generic dynamically allocated resizable storage.
#define SPH_ASSERT(x,...)
uint32_t Size
Integral type used to index arrays (by default).
#define INLINE
Macros for conditional compilation based on selected compiler.
#define NAMESPACE_SPH_END
Wrapper of type value of which may or may not be present.
const NothingType NOTHING
Object providing safe access to continuous memory of data.
INLINE Iterator< StorageType > end() noexcept
void remove(const TCounter idx)
Removes an element with given index from the array.
void insert(const TCounter position, U &&value)
Inserts a new element to given position in the array.
void clear()
Removes all elements from the array, but does NOT release the memory.
INLINE TCounter size() const noexcept
INLINE bool empty() const noexcept
INLINE Iterator< StorageType > begin() noexcept
Array clone() const
Performs a deep copy of all elements of the array.
Container of key-value pairs.
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.
INLINE Iterator< Element > end()
Returns the iterator pointing to the one-past-last element.
INLINE Iterator< const Element > end() const
Returns the iterator pointing to the one-past-last element.
INLINE Size empty() const
Returns true if the map contains no elements, false otherwise.
INLINE bool tryRemove(const TKey &key)
Removes element with given key if present, otherwise it does nothing.
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.
INLINE bool contains(const TKey &key) const
Returns true if the map contains element of given key.
INLINE void remove(const TKey &key)
Removes element with given key from the map.
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.
INLINE TValue & operator[](const TKey &key)
Returns a reference to the element, given its key.
FlatMap(std::initializer_list< Element > list)
Constructs the map fromm initializer list of elements.
INLINE Iterator< Element > begin()
Returns the iterator pointing to the first element.
INLINE void clear()
Removes all elements from the map.
INLINE Optional< TValue & > tryGet(const TKey &key)
Returns a reference to the value matching the given key, or NOTHING if no such value exists.
INLINE const TValue & operator[](const TKey &key) const
Returns a reference to the element, given its key.
INLINE Size size() const
Returns the number of elements in the map.
INLINE Iterator< const Element > begin() const
Returns the iterator pointing to the first element.
Simple (forward) iterator over continuous array of objects of type T.
Wrapper of type value of which may or may not be present.
Element of the container.
Object with deleted copy constructor and copy operator.