LArSoft  v08_61_00
Liquid Argon Software toolkit - http://larsoft.org/
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 = typename std::map<const Key, T, Compare, Allocator>;
24  using listmap_t = typename std::list<std::pair<const Key, T>, Allocator>;
25 
26  static_assert(
27  std::is_same<typename mapmap_t::key_type,
28  typename listmap_t::value_type::first_type>::value,
29  "type mismatch for key_type");
30  static_assert(
31  std::is_same<typename mapmap_t::mapped_type,
32  typename listmap_t::value_type::second_type>::value,
33  "type mismatch for mapped_type");
34  static_assert(std::is_same<typename mapmap_t::value_type,
35  typename listmap_t::value_type>::value,
36  "type mismatch for value_type");
37  static_assert(std::is_same<typename mapmap_t::size_type,
38  typename listmap_t::size_type>::value,
39  "type mismatch for size_type");
40 
41  using size_type = typename mapmap_t::size_type;
42 
43  using iterator_tag = std::input_iterator_tag;
44  struct iterator_tuple {
47  };
48 
49  template <class Category,
50  class TT,
51  class Distance = std::ptrdiff_t,
52  class Pointer = TT*,
53  class Reference = TT&>
54  struct iter : std::iterator<Category, TT, Distance, Pointer, Reference> {
55  using type = TT;
56 
57  iter(typename mapmap_t::iterator it) { _iters.mapmap_iter = it; }
58  iter(typename listmap_t::iterator it) { _iters.listmap_iter = it; }
59 
60  TT& operator*()
61  {
62  return isSnippetMode() ? *_iters.listmap_iter : *_iters.mapmap_iter;
63  }
64 
65  TT* operator->()
66  {
67  return isSnippetMode() ? &*_iters.listmap_iter : &*_iters.mapmap_iter;
68  }
69  TT const* operator->() const
70  {
71  return isSnippetMode() ? &*_iters.listmap_iter : &*_iters.mapmap_iter;
72  }
73 
74  TT&
76  {
77  return isSnippetMode() ? *(_iters.listmap_iter++) :
78  *(_iters.mapmap_iter++);
79  }
80 
81  bool
83  {
84  return isSnippetMode() ?
85  _iters.listmap_iter == other._iters.listmap_iter :
86  _iters.mapmap_iter == other._iters.mapmap_iter;
87  }
88 
89  bool
91  {
92  return !operator==(other);
93  }
94 
95  template <typename II>
96  typename std::enable_if<
98  II>::type get(II)
99  {
100  return _iters.mapmap_iter;
101  }
102 
103  template <typename II>
104  typename std::enable_if<
106  II>::type get(II)
107  {
108  return _iters.listmap_iter;
109  }
110 
111  template <typename IIL, typename IIR>
112  friend typename std::enable_if<
114  std::is_same<
115  typename std::remove_const<typename IIL::type>::type,
116  typename std::remove_const<typename IIR::type>::type>::value,
117  bool>::type
118  operator==(IIL, IIR);
119 
120  template <typename IIL, typename IIR>
121  friend typename std::enable_if<
123  std::is_same<
124  typename std::remove_const<typename IIL::type>::type,
125  typename std::remove_const<typename IIR::type>::type>::value,
126  bool>::type
127  operator!=(IIL, IIR);
128 
129  private:
131  };
132 
135 
136  struct maps_tuple {
139  };
140 
141  T& operator[](Key const& key)
142  {
143  if (isSnippetMode()) {
144  for (auto& element : _maps.listmap) {
145  if (element.first == key)
146  return element.second;
147  }
148  _maps.listmap.emplace_back(std::make_pair(key, T{}));
149  return _maps.listmap.back().second;
150  } else {
151  return _maps.mapmap[key];
152  }
153  }
154 
155  iterator
157  {
160  }
161 
163  begin() const
164  {
165  maps_tuple& maps = *const_cast<maps_tuple*>(&_maps);
166 
167  return isSnippetMode() ? const_iterator{std::begin(maps.listmap)} :
169  }
170 
172  cbegin() const
173  {
174  return begin();
175  }
176 
177  iterator
178  end()
179  {
182  }
183 
185  end() const
186  {
187  maps_tuple& maps = *const_cast<maps_tuple*>(&_maps);
188 
189  return isSnippetMode() ? const_iterator{std::end(maps.listmap)} :
191  }
192 
194  cend() const
195  {
196  return end();
197  }
198 
199  T&
200  at(Key const& key)
201  {
202  if (isSnippetMode()) {
203  auto it =
204  std::find_if(_maps.listmap.begin(),
205  _maps.listmap.end(),
206  [&key](auto& element) { return element.first == key; });
207  if (it == _maps.listmap.end())
208  throw std::out_of_range("Key <" + key + "> not found.");
209  return it->second;
210  } else {
211  return _maps.mapmap.at(key);
212  }
213  }
214 
215  T const&
216  at(Key const& key) const
217  {
218  if (isSnippetMode()) {
219  auto it = std::find_if(
220  _maps.listmap.cbegin(),
221  _maps.listmap.cend(),
222  [&key](auto const& element) { return element.first == key; });
223  if (it == _maps.listmap.cend())
224  throw std::out_of_range("Key <" + key + "> not found.");
225  return it->second;
226  } else {
227  return _maps.mapmap.at(key);
228  }
229  }
230 
231  iterator
232  find(Key const& key)
233  {
234  if (isSnippetMode()) {
235  return std::find_if(
236  _maps.listmap.begin(), _maps.listmap.end(), [&key](auto& element) {
237  return element.first == key;
238  });
239  } else {
240  return _maps.mapmap.find(key);
241  }
242  }
243 
245  find(Key const& key) const
246  {
247  maps_tuple& maps = *const_cast<maps_tuple*>(&_maps);
248 
249  if (isSnippetMode()) {
250  return std::find_if(
251  maps.listmap.begin(),
252  maps.listmap.end(),
253  [&key](auto const& element) { return element.first == key; });
254  } else {
255  return maps.mapmap.find(key);
256  }
257  }
258 
259  size_t
260  erase(Key const& key)
261  {
262  if (isSnippetMode()) {
263  auto erase_count = size_t{0};
264  auto i = _maps.listmap.begin();
265  auto e = _maps.listmap.end();
266 
267  while (i != e) {
268  if (key == i->first) {
269  i = _maps.listmap.erase(i);
270  ++erase_count;
271  } else {
272  i++;
273  }
274  }
275 
276  return erase_count;
277  } else {
278  return _maps.mapmap.erase(key);
279  }
280  }
281 
282  bool
283  empty() const
284  {
285  return isSnippetMode() ? _maps.listmap.empty() : _maps.mapmap.empty();
286  }
287 
288  size_type
289  size() const
290  {
291  return isSnippetMode() ? _maps.listmap.size() : _maps.mapmap.size();
292  }
293 
294  iterator
296  {
297  if (isSnippetMode()) {
298  return _maps.listmap.erase(it.get(typename listmap_t::iterator{}));
299  } else {
300  return _maps.mapmap.erase(it.get(typename mapmap_t::iterator{}));
301  }
302  }
303 
304  iterator
306  {
307  if (isSnippetMode()) {
308  return _maps.listmap.erase(it.get(typename listmap_t::iterator{}));
309  } else {
310  return _maps.mapmap.erase(it.get(typename mapmap_t::iterator{}));
311  }
312  }
313 
314  template <class... Args>
315  std::pair<iterator, bool>
316  emplace(Args&&... args)
317  {
318  if (isSnippetMode()) {
319  _maps.listmap.emplace_back(std::forward<Args>(args)...);
320  return std::make_pair(iterator{std::prev(_maps.listmap.end())}, true);
321  } else {
322  auto result = _maps.mapmap.emplace(std::forward<Args>(args)...);
323  return std::make_pair(iterator{result.first}, result.second);
324  }
325  }
326 
328  };
329  template <typename IIL, typename IIR>
330  typename std::enable_if<
332  std::is_same<typename std::remove_const<typename IIL::type>::type,
333  typename std::remove_const<typename IIR::type>::type>::value,
334  bool>::type
336  {
337  return isSnippetMode() ?
338  left._iters.listmap_iter == right._iters.listmap_iter :
339  left._iters.mapmap_iter == right._iters.mapmap_iter;
340  }
341 
342  template <typename IIL, typename IIR>
343  typename std::enable_if<
345  std::is_same<typename std::remove_const<typename IIL::type>::type,
346  typename std::remove_const<typename IIR::type>::type>::value,
347  bool>::type
349  {
350  return !operator==(left, right);
351  }
352 }
353 
354 #endif /* fhiclcpp_stdmap_shims_h */
355 
356 // Local Variables:
357 // mode: c++
358 // End:
intermediate_table::iterator iterator
typename std::map< const Key, T, Compare, Allocator > mapmap_t
Definition: stdmap_shims.h:23
size_t erase(Key const &key)
Definition: stdmap_shims.h:260
iterator begin()
Definition: stdmap_shims.h:156
bool isSnippetMode(bool m)
Definition: parse_shims.cc:9
constexpr auto const & right(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:104
size_type size() const
Definition: stdmap_shims.h:289
bool empty() const
Definition: stdmap_shims.h:283
iterator_tuple _iters
Definition: stdmap_shims.h:130
maps_tuple _maps
Definition: stdmap_shims.h:327
listmap_t::iterator listmap_iter
Definition: stdmap_shims.h:46
bool operator!=(iter other) const
Definition: stdmap_shims.h:90
std::pair< iterator, bool > emplace(Args &&...args)
Definition: stdmap_shims.h:316
iter(typename listmap_t::iterator it)
Definition: stdmap_shims.h:58
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
const_iterator begin() const
Definition: stdmap_shims.h:163
TT const * operator->() const
Definition: stdmap_shims.h:69
iterator end()
Definition: stdmap_shims.h:178
std::enable_if< std::is_same< typename mapmap_t::iterator, II >::value, II >::type get(II)
Definition: stdmap_shims.h:98
std::enable_if< !std::is_same< IIL, IIR >::value &&std::is_same< typename std::remove_const< typename IIL::type >::type, typename std::remove_const< typename IIR::type >::type >::value, bool >::type operator!=(IIL left, IIR right)
Definition: stdmap_shims.h:348
typename std::list< std::pair< const Key, T >, Allocator > listmap_t
Definition: stdmap_shims.h:24
iter(typename mapmap_t::iterator it)
Definition: stdmap_shims.h:57
const_iterator cbegin() const
Definition: stdmap_shims.h:172
mapmap_t::iterator mapmap_iter
Definition: stdmap_shims.h:45
std::string value(std::any const &)
T const & at(Key const &key) const
Definition: stdmap_shims.h:216
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:96
bool operator==(iter other) const
Definition: stdmap_shims.h:82
iterator erase(iterator it)
Definition: stdmap_shims.h:295
T & operator[](Key const &key)
Definition: stdmap_shims.h:141
T & at(Key const &key)
Definition: stdmap_shims.h:200
std::enable_if< !std::is_same< IIL, IIR >::value &&std::is_same< typename std::remove_const< typename IIL::type >::type, typename std::remove_const< typename IIR::type >::type >::value, bool >::type operator==(IIL left, IIR right)
Definition: stdmap_shims.h:335
const_iterator end() const
Definition: stdmap_shims.h:185
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:72
const_iterator cend() const
Definition: stdmap_shims.h:194
Float_t e
Definition: plot.C:34
iterator find(Key const &key)
Definition: stdmap_shims.h:232
typename mapmap_t::size_type size_type
Definition: stdmap_shims.h:41
std::input_iterator_tag iterator_tag
Definition: stdmap_shims.h:43
iterator erase(const_iterator &it)
Definition: stdmap_shims.h:305
const_iterator find(Key const &key) const
Definition: stdmap_shims.h:245