stdmap_shims.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_stdmap_shims_h
2 #define fhiclcpp_stdmap_shims_h
3 
4 #include <algorithm>
5 #include <iostream>
6 #include <iterator>
7 #include <list>
8 #include <map>
9 #include <stdexcept>
10 #include <type_traits>
11 #include <utility>
12 
14 
15 namespace shims {
16 
17  template <class Key,
18  class T,
19  class Compare = std::less<Key>,
20  class Allocator = std::allocator<std::pair<const Key, T>>>
21  class map {
22  public:
23  using mapmap_t = std::map<const Key, T, Compare, Allocator>;
24  using listmap_t = std::list<std::pair<const Key, T>, Allocator>;
25 
26  static_assert(std::is_same_v<typename mapmap_t::key_type,
27  typename listmap_t::value_type::first_type>,
28  "type mismatch for key_type");
29  static_assert(std::is_same_v<typename mapmap_t::mapped_type,
30  typename listmap_t::value_type::second_type>,
31  "type mismatch for mapped_type");
32  static_assert(std::is_same_v<typename mapmap_t::value_type,
33  typename listmap_t::value_type>,
34  "type mismatch for value_type");
35  static_assert(std::is_same_v<typename mapmap_t::size_type,
36  typename listmap_t::size_type>,
37  "type mismatch for size_type");
38 
39  using size_type = typename mapmap_t::size_type;
40 
41  using iterator_tag = std::input_iterator_tag;
42  struct iterator_tuple {
45  };
46 
47  template <class Category,
48  class TT,
49  class Distance = std::ptrdiff_t,
50  class Pointer = TT*,
51  class Reference = TT&>
52  struct iter : std::iterator<Category, TT, Distance, Pointer, Reference> {
53  using type = TT;
54 
55  iter(typename mapmap_t::iterator it) { _iters.mapmap_iter = it; }
56  iter(typename listmap_t::iterator it) { _iters.listmap_iter = it; }
57 
58  TT& operator*()
59  {
60  return isSnippetMode() ? *_iters.listmap_iter : *_iters.mapmap_iter;
61  }
62 
63  TT* operator->()
64  {
65  return isSnippetMode() ? &*_iters.listmap_iter : &*_iters.mapmap_iter;
66  }
67  TT const* operator->() const
68  {
69  return isSnippetMode() ? &*_iters.listmap_iter : &*_iters.mapmap_iter;
70  }
71 
72  TT&
74  {
75  return isSnippetMode() ? *(_iters.listmap_iter++) :
76  *(_iters.mapmap_iter++);
77  }
78 
79  bool
81  {
82  return isSnippetMode() ?
83  _iters.listmap_iter == other._iters.listmap_iter :
84  _iters.mapmap_iter == other._iters.mapmap_iter;
85  }
86 
87  bool
89  {
90  return !operator==(other);
91  }
92 
93  template <typename II>
94  std::enable_if_t<std::is_same_v<typename mapmap_t::iterator, II>, II> get(
95  II)
96  {
97  return _iters.mapmap_iter;
98  }
99 
100  template <typename II>
101  std::enable_if_t<std::is_same_v<typename listmap_t::iterator, II>, II>
102  get(II)
103  {
104  return _iters.listmap_iter;
105  }
106 
107  template <typename IIL, typename IIR>
108  friend std::enable_if_t<
109  !std::is_same_v<IIL, IIR> &&
110  std::is_same_v<std::remove_const_t<typename IIL::type>,
111  std::remove_const_t<typename IIR::type>>,
112  bool>
113  operator==(IIL, IIR);
114 
115  template <typename IIL, typename IIR>
116  friend std::enable_if_t<
117  !std::is_same_v<IIL, IIR> &&
118  std::is_same_v<std::remove_const_t<typename IIL::type>,
119  std::remove_const_t<typename IIR::type>>,
120  bool>
121  operator!=(IIL, IIR);
122 
123  private:
125  };
126 
129 
130  struct maps_tuple {
133  };
134 
135  T& operator[](Key const& key)
136  {
137  if (isSnippetMode()) {
138  for (auto& element : _maps.listmap) {
139  if (element.first == key)
140  return element.second;
141  }
142  _maps.listmap.emplace_back(std::make_pair(key, T{}));
143  return _maps.listmap.back().second;
144  } else {
145  return _maps.mapmap[key];
146  }
147  }
148 
149  iterator
151  {
154  }
155 
157  begin() const
158  {
159  maps_tuple& maps = *const_cast<maps_tuple*>(&_maps);
160  return isSnippetMode() ? const_iterator{std::begin(maps.listmap)} :
162  }
163 
165  cbegin() const
166  {
167  return begin();
168  }
169 
170  iterator
171  end()
172  {
175  }
176 
178  end() const
179  {
180  maps_tuple& maps = *const_cast<maps_tuple*>(&_maps);
181 
182  return isSnippetMode() ? const_iterator{std::end(maps.listmap)} :
184  }
185 
187  cend() const
188  {
189  return end();
190  }
191 
192  T&
193  at(Key const& key)
194  {
195  if (isSnippetMode()) {
196  auto it =
197  std::find_if(_maps.listmap.begin(),
198  _maps.listmap.end(),
199  [&key](auto& element) { return element.first == key; });
200  if (it == _maps.listmap.end())
201  throw std::out_of_range("Key <" + key + "> not found.");
202  return it->second;
203  } else {
204  return _maps.mapmap.at(key);
205  }
206  }
207 
208  T const&
209  at(Key const& key) const
210  {
211  if (isSnippetMode()) {
212  auto it = std::find_if(
213  _maps.listmap.cbegin(),
214  _maps.listmap.cend(),
215  [&key](auto const& element) { return element.first == key; });
216  if (it == _maps.listmap.cend())
217  throw std::out_of_range("Key <" + key + "> not found.");
218  return it->second;
219  } else {
220  return _maps.mapmap.at(key);
221  }
222  }
223 
224  iterator
225  find(Key const& key)
226  {
227  if (isSnippetMode()) {
228  return std::find_if(
229  _maps.listmap.begin(), _maps.listmap.end(), [&key](auto& element) {
230  return element.first == key;
231  });
232  } else {
233  return _maps.mapmap.find(key);
234  }
235  }
236 
238  find(Key const& key) const
239  {
240  maps_tuple& maps = *const_cast<maps_tuple*>(&_maps);
241 
242  if (isSnippetMode()) {
243  return std::find_if(
244  maps.listmap.begin(),
245  maps.listmap.end(),
246  [&key](auto const& element) { return element.first == key; });
247  } else {
248  return maps.mapmap.find(key);
249  }
250  }
251 
252  size_t
253  erase(Key const& key)
254  {
255  if (isSnippetMode()) {
256  auto erase_count = size_t{0};
257  auto i = _maps.listmap.begin();
258  auto e = _maps.listmap.end();
259 
260  while (i != e) {
261  if (key == i->first) {
262  i = _maps.listmap.erase(i);
263  ++erase_count;
264  } else {
265  i++;
266  }
267  }
268 
269  return erase_count;
270  } else {
271  return _maps.mapmap.erase(key);
272  }
273  }
274 
275  bool
276  empty() const
277  {
278  return isSnippetMode() ? _maps.listmap.empty() : _maps.mapmap.empty();
279  }
280 
281  size_type
282  size() const
283  {
284  return isSnippetMode() ? _maps.listmap.size() : _maps.mapmap.size();
285  }
286 
287  iterator
289  {
290  if (isSnippetMode()) {
291  return _maps.listmap.erase(it.get(typename listmap_t::iterator{}));
292  } else {
293  return _maps.mapmap.erase(it.get(typename mapmap_t::iterator{}));
294  }
295  }
296 
297  iterator
299  {
300  if (isSnippetMode()) {
301  return _maps.listmap.erase(it.get(typename listmap_t::iterator{}));
302  } else {
303  return _maps.mapmap.erase(it.get(typename mapmap_t::iterator{}));
304  }
305  }
306 
307  template <class... Args>
308  std::pair<iterator, bool>
309  emplace(Args&&... args)
310  {
311  if (isSnippetMode()) {
312  _maps.listmap.emplace_back(std::forward<Args>(args)...);
313  return std::make_pair(iterator{std::prev(_maps.listmap.end())}, true);
314  } else {
315  auto result = _maps.mapmap.emplace(std::forward<Args>(args)...);
316  return std::make_pair(iterator{result.first}, result.second);
317  }
318  }
319 
321  };
322  template <typename IIL, typename IIR>
323  std::enable_if_t<!std::is_same_v<IIL, IIR> &&
324  std::is_same_v<std::remove_const_t<typename IIL::type>,
325  std::remove_const_t<typename IIR::type>>,
326  bool>
328  {
329  return isSnippetMode() ?
330  left._iters.listmap_iter == right._iters.listmap_iter :
331  left._iters.mapmap_iter == right._iters.mapmap_iter;
332  }
333 
334  template <typename IIL, typename IIR>
335  std::enable_if_t<!std::is_same_v<IIL, IIR> &&
336  std::is_same_v<std::remove_const_t<typename IIL::type>,
337  std::remove_const_t<typename IIR::type>>,
338  bool>
340  {
341  return !operator==(left, right);
342  }
343 }
344 
345 #endif /* fhiclcpp_stdmap_shims_h */
346 
347 // Local Variables:
348 // mode: c++
349 // End:
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
intermediate_table::iterator iterator
size_t erase(Key const &key)
Definition: stdmap_shims.h:253
iterator begin()
Definition: stdmap_shims.h:150
bool isSnippetMode(bool m)
Definition: parse_shims.cc:9
size_type size() const
Definition: stdmap_shims.h:282
static QCString result
bool empty() const
Definition: stdmap_shims.h:276
iterator_tuple _iters
Definition: stdmap_shims.h:124
maps_tuple _maps
Definition: stdmap_shims.h:320
listmap_t::iterator listmap_iter
Definition: stdmap_shims.h:44
static QCString args
Definition: declinfo.cpp:674
bool operator!=(iter other) const
Definition: stdmap_shims.h:88
std::pair< iterator, bool > emplace(Args &&...args)
Definition: stdmap_shims.h:309
iter(typename listmap_t::iterator it)
Definition: stdmap_shims.h:56
const_iterator begin() const
Definition: stdmap_shims.h:157
TT const * operator->() const
Definition: stdmap_shims.h:67
iterator end()
Definition: stdmap_shims.h:171
const double e
def key(type, name=None)
Definition: graph.py:13
std::enable_if_t< std::is_same_v< typename mapmap_t::iterator, II >, II > get(II)
Definition: stdmap_shims.h:94
iter(typename mapmap_t::iterator it)
Definition: stdmap_shims.h:55
const_iterator cbegin() const
Definition: stdmap_shims.h:165
mapmap_t::iterator mapmap_iter
Definition: stdmap_shims.h:43
T const & at(Key const &key) const
Definition: stdmap_shims.h:209
std::enable_if_t<!std::is_same_v< IIL, IIR > &&std::is_same_v< std::remove_const_t< typename IIL::type >, std::remove_const_t< typename IIR::type > >, bool > operator!=(IIL left, IIR right)
Definition: stdmap_shims.h:339
std::enable_if_t<!std::is_same_v< IIL, IIR > &&std::is_same_v< std::remove_const_t< typename IIL::type >, std::remove_const_t< typename IIR::type > >, bool > operator==(IIL left, IIR right)
Definition: stdmap_shims.h:327
bool operator==(iter other) const
Definition: stdmap_shims.h:80
iterator erase(iterator it)
Definition: stdmap_shims.h:288
T & operator[](Key const &key)
Definition: stdmap_shims.h:135
T & at(Key const &key)
Definition: stdmap_shims.h:193
const_iterator end() const
Definition: stdmap_shims.h:178
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
std::list< std::pair< const Key, T >, Allocator > listmap_t
Definition: stdmap_shims.h:24
const_iterator cend() const
Definition: stdmap_shims.h:187
iterator find(Key const &key)
Definition: stdmap_shims.h:225
typename mapmap_t::size_type size_type
Definition: stdmap_shims.h:39
std::input_iterator_tag iterator_tag
Definition: stdmap_shims.h:41
std::map< const Key, T, Compare, Allocator > mapmap_t
Definition: stdmap_shims.h:23
iterator erase(const_iterator &it)
Definition: stdmap_shims.h:298
const_iterator find(Key const &key) const
Definition: stdmap_shims.h:238