LArSoft  v06_85_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& operator++()
75  {
76  return isSnippetMode() ? *(_iters.listmap_iter++) :
77  *(_iters.mapmap_iter++);
78  }
79 
80  bool
82  {
83  return isSnippetMode() ?
84  _iters.listmap_iter == other._iters.listmap_iter :
85  _iters.mapmap_iter == other._iters.mapmap_iter;
86  }
87 
88  bool
90  {
91  return !operator==(other);
92  }
93 
94  template <typename II>
95  typename std::enable_if<
97  II>::type get(II)
98  {
99  return _iters.mapmap_iter;
100  }
101 
102  template <typename II>
103  typename std::enable_if<
105  II>::type get(II)
106  {
107  return _iters.listmap_iter;
108  }
109 
110  template <typename IIL, typename IIR>
111  friend typename std::enable_if<
113  std::is_same<
114  typename std::remove_const<typename IIL::type>::type,
115  typename std::remove_const<typename IIR::type>::type>::value,
116  bool>::type
117  operator==(IIL, IIR);
118 
119  template <typename IIL, typename IIR>
120  friend typename std::enable_if<
122  std::is_same<
123  typename std::remove_const<typename IIL::type>::type,
124  typename std::remove_const<typename IIR::type>::type>::value,
125  bool>::type
126  operator!=(IIL, IIR);
127 
128  private:
130  };
131 
134 
135  struct maps_tuple {
138  };
139 
140  T& operator[](Key const& key)
141  {
142  if (isSnippetMode()) {
143  for (auto& element : _maps.listmap) {
144  if (element.first == key)
145  return element.second;
146  }
147  _maps.listmap.emplace_back(std::make_pair(key, T{}));
148  return _maps.listmap.back().second;
149  } else {
150  return _maps.mapmap[key];
151  }
152  }
153 
154  iterator
156  {
159  }
160 
162  begin() const
163  {
164  maps_tuple& maps = *const_cast<maps_tuple*>(&_maps);
165 
166  return isSnippetMode() ? const_iterator{std::begin(maps.listmap)} :
168  }
169 
171  cbegin() const
172  {
173  return begin();
174  }
175 
176  iterator
177  end()
178  {
181  }
182 
184  end() const
185  {
186  maps_tuple& maps = *const_cast<maps_tuple*>(&_maps);
187 
188  return isSnippetMode() ? const_iterator{std::end(maps.listmap)} :
190  }
191 
193  cend() const
194  {
195  return end();
196  }
197 
198  T&
199  at(Key const& key)
200  {
201  if (isSnippetMode()) {
202  auto it =
203  std::find_if(_maps.listmap.begin(),
204  _maps.listmap.end(),
205  [&key](auto& element) { return element.first == key; });
206  if (it == _maps.listmap.end())
207  throw std::out_of_range("Key <" + key + "> not found.");
208  return it->second;
209  } else {
210  return _maps.mapmap.at(key);
211  }
212  }
213 
214  T const&
215  at(Key const& key) const
216  {
217  if (isSnippetMode()) {
218  auto it = std::find_if(
219  _maps.listmap.cbegin(),
220  _maps.listmap.cend(),
221  [&key](auto const& element) { return element.first == key; });
222  if (it == _maps.listmap.cend())
223  throw std::out_of_range("Key <" + key + "> not found.");
224  return it->second;
225  } else {
226  return _maps.mapmap.at(key);
227  }
228  }
229 
230  iterator
231  find(Key const& key)
232  {
233  if (isSnippetMode()) {
234  return std::find_if(
235  _maps.listmap.begin(), _maps.listmap.end(), [&key](auto& element) {
236  return element.first == key;
237  });
238  } else {
239  return _maps.mapmap.find(key);
240  }
241  }
242 
244  find(Key const& key) const
245  {
246  maps_tuple& maps = *const_cast<maps_tuple*>(&_maps);
247 
248  if (isSnippetMode()) {
249  return std::find_if(
250  maps.listmap.begin(),
251  maps.listmap.end(),
252  [&key](auto const& element) { return element.first == key; });
253  } else {
254  return maps.mapmap.find(key);
255  }
256  }
257 
258  size_t
259  erase(Key const& key)
260  {
261  if (isSnippetMode()) {
262  auto erase_count = size_t{0};
263  auto i = _maps.listmap.begin();
264  auto e = _maps.listmap.end();
265 
266  while (i != e) {
267  if (key == i->first) {
268  i = _maps.listmap.erase(i);
269  ++erase_count;
270  } else {
271  i++;
272  }
273  }
274 
275  return erase_count;
276  } else {
277  return _maps.mapmap.erase(key);
278  }
279  }
280 
281  bool
282  empty() const
283  {
284  return isSnippetMode() ? _maps.listmap.empty() : _maps.mapmap.empty();
285  }
286 
287  size_type
288  size() const
289  {
290  return isSnippetMode() ? _maps.listmap.size() : _maps.mapmap.size();
291  }
292 
293  iterator
295  {
296  if (isSnippetMode()) {
297  return _maps.listmap.erase(it.get(typename listmap_t::iterator{}));
298  } else {
299  return _maps.mapmap.erase(it.get(typename mapmap_t::iterator{}));
300  }
301  }
302 
303  iterator
305  {
306  if (isSnippetMode()) {
307  return _maps.listmap.erase(it.get(typename listmap_t::iterator{}));
308  } else {
309  return _maps.mapmap.erase(it.get(typename mapmap_t::iterator{}));
310  }
311  }
312 
313  template <class... Args>
314  std::pair<iterator, bool>
315  emplace(Args&&... args)
316  {
317  if (isSnippetMode()) {
318  _maps.listmap.emplace_back(std::forward<Args>(args)...);
319  return std::make_pair(iterator{std::prev(_maps.listmap.end())}, true);
320  } else {
321  auto result = _maps.mapmap.emplace(std::forward<Args>(args)...);
322  return std::make_pair(iterator{result.first}, result.second);
323  }
324  }
325 
327  };
328  template <typename IIL, typename IIR>
329  typename std::enable_if<
331  std::is_same<typename std::remove_const<typename IIL::type>::type,
332  typename std::remove_const<typename IIR::type>::type>::value,
333  bool>::type
335  {
336  return isSnippetMode() ?
337  left._iters.listmap_iter == right._iters.listmap_iter :
338  left._iters.mapmap_iter == right._iters.mapmap_iter;
339  }
340 
341  template <typename IIL, typename IIR>
342  typename std::enable_if<
344  std::is_same<typename std::remove_const<typename IIL::type>::type,
345  typename std::remove_const<typename IIR::type>::type>::value,
346  bool>::type
348  {
349  return !operator==(left, right);
350  }
351 }
352 
353 #endif /* fhiclcpp_stdmap_shims_h */
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:259
iterator begin()
Definition: stdmap_shims.h:155
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:112
size_type size() const
Definition: stdmap_shims.h:288
intermediate_table::iterator iterator
bool empty() const
Definition: stdmap_shims.h:282
iterator_tuple _iters
Definition: stdmap_shims.h:129
maps_tuple _maps
Definition: stdmap_shims.h:326
listmap_t::iterator listmap_iter
Definition: stdmap_shims.h:46
bool operator!=(iter other) const
Definition: stdmap_shims.h:89
std::pair< iterator, bool > emplace(Args &&...args)
Definition: stdmap_shims.h:315
iter(typename listmap_t::iterator it)
Definition: stdmap_shims.h:58
const_iterator begin() const
Definition: stdmap_shims.h:162
TT const * operator->() const
Definition: stdmap_shims.h:69
iterator end()
Definition: stdmap_shims.h:177
std::enable_if< std::is_same< typename mapmap_t::iterator, II >::value, II >::type get(II)
Definition: stdmap_shims.h:97
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:347
typename std::list< std::pair< const Key, T >, Allocator > listmap_t
Definition: stdmap_shims.h:24
std::vector< evd::details::RawDigitInfo_t >::const_iterator begin(RawDigitCacheDataClass const &cache)
iter(typename mapmap_t::iterator it)
Definition: stdmap_shims.h:57
const_iterator cbegin() const
Definition: stdmap_shims.h:171
mapmap_t::iterator mapmap_iter
Definition: stdmap_shims.h:45
T const & at(Key const &key) const
Definition: stdmap_shims.h:215
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:104
std::string value(boost::any const &)
bool operator==(iter other) const
Definition: stdmap_shims.h:81
iterator erase(iterator it)
Definition: stdmap_shims.h:294
T & operator[](Key const &key)
Definition: stdmap_shims.h:140
T & at(Key const &key)
Definition: stdmap_shims.h:199
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:334
const_iterator end() const
Definition: stdmap_shims.h:184
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
const_iterator cend() const
Definition: stdmap_shims.h:193
Float_t e
Definition: plot.C:34
iterator find(Key const &key)
Definition: stdmap_shims.h:231
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:304
const_iterator find(Key const &key) const
Definition: stdmap_shims.h:244