mn_basic_light_map.hpp
Go to the documentation of this file.
1 /*
2 *This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
3 *Copyright (c) 2018 Amber-Sophia Schroeck
4 *
5 *The Mini Thread Library is free software; you can redistribute it and/or modify
6 *it under the terms of the GNU Lesser General Public License as published by
7 *the Free Software Foundation, version 3, or (at your option) any later version.
8 
9 *The Mini Thread Library is distributed in the hope that it will be useful, but
10 *WITHOUT ANY WARRANTY; without even the implied warranty of
11 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 *General Public License for more details.
13 *
14 *You should have received a copy of the GNU Lesser General Public
15 *License along with the Mini Thread Library; if not, see
16 *<https://www.gnu.org/licenses/>.
17 */
18 #ifndef __MINILIB_BASIC_LIGHT_MAP_H__
19 #define __MINILIB_BASIC_LIGHT_MAP_H__
20 
21 #include "../mn_config.hpp"
22 
23 #include "../mn_algorithm.hpp"
24 #include "../mn_iterator.hpp"
25 #include "../mn_typetraits.hpp"
26 
27 #include "mn_pair.hpp"
28 #include "mn_vector.hpp"
29 
30 
31 namespace mn {
32  namespace container {
33 
42  template <class TKey, class TValue,
43  class TALLOCATOR = memory::default_allocator,
44  class TPairType = mn::container::pair<TKey, TValue>,
47  public:
48  using mapped_type = TValue;
49  using key_type = TKey;
50 
51  using value_type = TPairType;
52  using reference = TPairType&;
53  using pointer = TPairType*;
54  using const_reference = const TPairType&;
55  using const_pointer = const TPairType*;
56 
59 
60  using iterator = TValue*;
61  using const_iterator = const TValue*;
62 
64 
65  basic_light_map(const size_type start_size = 32) noexcept
66  : m_ayKeyValue(start_size) { }
67 
69  m_ayKeyValue.clear();
70  }
71 
72 
73  void clear() {
74  m_ayKeyValue.clear();
75  }
76 
77  template< class... Args >
78  mn::container::pair<iterator, bool> assign(const key_type& key, Args && ... args) {
79  value_type _value(key, mn::forward<Args>(args)...);
80 
81  return assign(_value);
82  }
83 
85  mn::container::pair<iterator, bool> _ret(vValue.seconde, false);
86 
87  for(typename TContainer::iterator it = m_ayKeyValue.begin();
88  it != m_ayKeyValue.end(); it++) {
89  value_type _pair = *it;
90 
91  if(_pair.first == vValue.first) {
92  m_ayKeyValue.erase(it);
93  m_ayKeyValue.insert(vValue);
94  _ret->second = true;
95  }
96  }
97  return _ret;
98  }
99 
109  template< class... Args >
110  mn::container::pair<iterator, bool> emplace(const key_type& key, Args && ... args) {
111  value_type _value = value_type(key, mn::forward<Args>(args)...);
112 
113  return insert(_value);
114  }
115 
123  mn::container::pair<iterator, bool> result(&value, false);
124 
125  if(find(value.first) == nullptr) {
126  m_ayKeyValue.push_back(value);
127  result.second = find(value.first) != nullptr;
128  }
129 
130  return result;
131  }
132 
133  template< class... Args >
134  void insert_or_assign(const key_type& key, Args && ... args) {
135  value_type _value(key, mn::forward<Args>(args)...);
136 
137  auto iter = find(key);
138  if(iter == nullptr)
139  insert(_value);
140  else
141  assign(_value);
142 
143  }
144 
151  bool insert(const key_type& key, const mapped_type& value) {
152  return insert(value_type(key, value)).second;
153  }
154 
161  bool insert(key_type&& key, mapped_type&& value) {
162  return insert(value_type( mn::forward(key), mn::forward(value)) ).second;
163  }
164 
165 
166 
167 
174  return m_ayKeyValue.erase(pos);
175  }
176 
184  return m_ayKeyValue.erase(first, last);
185  }
191  size_type erase( const key_type& tKey ) {
192  if(empty()) return 0;
193 
194  size_type _ret = 0;
195 
196  for(typename TContainer::iterator it = m_ayKeyValue.begin();
197  it != m_ayKeyValue.end(); it++) {
198  value_type _pair = *it;
199 
200  if(_pair.first == tKey) {
201  m_ayKeyValue.erase(it); _ret = 1;
202  }
203  }
204 
205  return _ret;
206  }
212  iterator find(const key_type& tKey) noexcept {
213  if(empty()) return nullptr;
214 
215  for(typename TContainer::iterator it = m_ayKeyValue.begin();
216  it != m_ayKeyValue.end(); it++) {
217  value_type _pair = *it;
218 
219  if(_pair.first == tKey) {
220  return &(_pair.second);
221  }
222  }
223 
224  return nullptr;
225  }
232  const_iterator find(const key_type& tKey) const noexcept {
233  if(empty()) return nullptr;
234 
235  for(typename TContainer::iterator it = m_ayKeyValue.begin();
236  it != m_ayKeyValue.end(); it++) {
237  value_type _pair = *it;
238 
239  if(_pair.first == tKey) {
240  return &(_pair.second);
241  }
242  }
243 
244  return nullptr;
245  }
246 
251  constexpr bool empty() const noexcept {
252  return m_ayKeyValue.is_empty();
253  }
254 
259  constexpr size_type size() const noexcept {
260  return m_ayKeyValue.get_used();
261  }
262 
268  size_type count( const key_type& key ) const {
269  return (find(key) != nullptr) ? 1 : 0;
270  }
271 
272 
277  void swap( self_type& other ) {
279  }
280 
287  iterator operator[](const key_type& tKey) noexcept {
288  return find(tKey);
289  }
296  const_iterator operator[](const key_type& tKey) const noexcept {
297  return find(tKey);
298  }
299  private:
300  TContainer m_ayKeyValue;
301  };
302 
311  template <class TKey, class TValue>
313 
314  }
315 }
316 
317 #endif // __MINILIB_BASIC_LIGHT_MAP_H__
Lightweight c++11 dictionary map implementation.
Definition: mn_basic_light_map.hpp:46
void swap(self_type &other)
Exchanges the contents of the container with those of other.
Definition: mn_basic_light_map.hpp:277
mn::container::pair< iterator, bool > assign(const key_type &key, Args &&... args)
Definition: mn_basic_light_map.hpp:78
TValue * iterator
Definition: mn_basic_light_map.hpp:60
constexpr size_type size() const noexcept
Get the number of map-members.
Definition: mn_basic_light_map.hpp:259
mn::container::pair< iterator, bool > insert(const value_type &value)
Inserts value.
Definition: mn_basic_light_map.hpp:122
size_type count(const key_type &key) const
Returns the number of elements with key key.
Definition: mn_basic_light_map.hpp:268
TKey key_type
Definition: mn_basic_light_map.hpp:49
mn::container::pair< iterator, bool > emplace(const key_type &key, Args &&... args)
Inserts a new element into the container constructed in-place with the given args if there is no elem...
Definition: mn_basic_light_map.hpp:110
void clear()
Definition: mn_basic_light_map.hpp:73
void insert_or_assign(const key_type &key, Args &&... args)
Definition: mn_basic_light_map.hpp:134
iterator operator[](const key_type &tKey) noexcept
Read value of map for given key.
Definition: mn_basic_light_map.hpp:287
iterator find(const key_type &tKey) noexcept
Finds an element with key equivalent to key.
Definition: mn_basic_light_map.hpp:212
~basic_light_map()
Definition: mn_basic_light_map.hpp:68
TValue mapped_type
Definition: mn_basic_light_map.hpp:48
TPairType value_type
Definition: mn_basic_light_map.hpp:51
bool insert(const key_type &key, const mapped_type &value)
insert key_type key with mapped_type value.
Definition: mn_basic_light_map.hpp:151
const_iterator operator[](const key_type &tKey) const noexcept
Read value of map for given key.
Definition: mn_basic_light_map.hpp:296
const TPairType * const_pointer
Definition: mn_basic_light_map.hpp:55
const TPairType & const_reference
Definition: mn_basic_light_map.hpp:54
mn::container::pair< iterator, bool > assign(const value_type &vValue)
Definition: mn_basic_light_map.hpp:84
TPairType & reference
Definition: mn_basic_light_map.hpp:52
mn::size_t size_type
Definition: mn_basic_light_map.hpp:58
basic_light_map(const size_type start_size=32) noexcept
Definition: mn_basic_light_map.hpp:65
bool insert(key_type &&key, mapped_type &&value)
insert key_type key with mapped_type value.
Definition: mn_basic_light_map.hpp:161
const_iterator find(const key_type &tKey) const noexcept
Finds an element with key equivalent to key.
Definition: mn_basic_light_map.hpp:232
mn::ptrdiff_t difference_type
Definition: mn_basic_light_map.hpp:57
TPairType * pointer
Definition: mn_basic_light_map.hpp:53
TContainer m_ayKeyValue
Definition: mn_basic_light_map.hpp:300
size_type erase(const key_type &tKey)
Removes the element with the key equivalent to tKey.
Definition: mn_basic_light_map.hpp:191
const TValue * const_iterator
Definition: mn_basic_light_map.hpp:61
iterator erase(const_iterator pos)
Removes specified elements from the container.
Definition: mn_basic_light_map.hpp:173
constexpr bool empty() const noexcept
Is the map empty.
Definition: mn_basic_light_map.hpp:251
iterator erase(const_iterator first, const_iterator last)
Removes specified elements from the container.
Definition: mn_basic_light_map.hpp:183
Definition: mn_vector.hpp:117
Basic vector container This file is part of the Mini Thread Library (https://github....
malloc_allocator< basic_allocator_filter > default_allocator
Definition: mn_default_allocator.hpp:30
Definition: mn_allocator_typetraits.hpp:25
long ptrdiff_t
Definition: mn_def.hpp:49
T && forward(remove_reference_t< T > &t)
Definition: mn_functional.hpp:74
MN_THREAD_CONFIG_SIZE_TYPE size_t
Definition: mn_def.hpp:48
Definition: mn_pair.hpp:28
second_type second
Definition: mn_pair.hpp:73