MapKeyIterator.h
Go to the documentation of this file.
1 /**
2  * @file MapKeyIterator.h
3  * @brief Provides a key iterator from a map-type object
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date February 18th, 2015
6  *
7  * The classes provided here are templates.
8  */
9 #ifndef NUTOOLS_RANDOMUTILS_PROVIDERS_MAPKEYITERATOR_H
10 #define NUTOOLS_RANDOMUTILS_PROVIDERS_MAPKEYITERATOR_H 1
11 
12 // C++ standard libraries
13 #include <iterator>
14 #include <tuple>
15 #include <type_traits>
16 
17 
18 namespace rndm {
19  namespace NuRandomServiceHelper {
20 
21 
22  /** ************************************************************************
23  * @brief Wraps a map-like object iterator to iterate on its first element
24  * @tparam Iter the iterator type
25  *
26  * This object can wrap a map-like iterator type to iterate through its key
27  * in a for loop.
28  *
29  * The Iter::value_type must support a std::get<0>() call, that is what's
30  * used to extract the key.
31  *
32  * Example of usage:
33  *
34  * std::map<int, double> my_map;
35  * // ... fill the map ...
36  * // print all the kets
37  * for (MapKeyIterator<map_id_t::const_iterator> it = map_id.cbegin();
38  * it != map_id.cend(); ++it)
39  * std::cout << "Key: " << *it << std::endl;
40  *
41  * Although the non-constant iterators are supported, this is of little use
42  * since typically the key is declared constant anyway (in the example
43  * above, the key is `const int`).
44  */
45  template <typename Iter>
46  class MapKeyIterator: public Iter {
47  public:
48  using map_iterator_t = Iter;
49  using iterator_traits_t = std::iterator_traits<map_iterator_t>;
50 
51  // iterator traits
52  using difference_type = typename iterator_traits_t::difference_type;
53  using value_type
57  using iterator_category = typename iterator_traits_t::iterator_category;
58 
59  /// Default constructor is really default
60  MapKeyIterator() = default;
61 
62  // accept quietly all the other constructery C++ provides
63 
64  /// Initialize from a original iterator type
66  map_iterator_t(from) {}
67 
68  /// Return the key of the original iterator value
70 
71  /// Return a pointer to the key of the original iterator value
72  pointer operator->() { return &get_key(get_value()); }
73 
74  protected:
75  using pair_reference = typename iterator_traits_t::reference;
76 
78 
80  { return std::get<0>(value); }
81 
82  }; // MapKeyIterator<>
83 
84 
85 
86  /**
87  * @brief Provides iterators for std::begin() and std::end()
88  * @tparam Map type of the map to be wrapped
89  *
90  * This object can wrap a map-like type to iterate through its key in a
91  * range-based for loop.
92  *
93  * Example of usage:
94  *
95  * std::map<int, double> my_map;
96  * // ... fill the map ...
97  * // print all the kets
98  * for (int const& key: MapKeyConstIteratorBox<std::map<int, double>>(my_map))
99  * std::cout << "Key: " << key << std::endl;
100  *
101  * Only constant iteration on keys is provided.
102  */
103  template <typename Map>
105  public:
106  using map_t = Map;
107 
109 
111  beginKey(map.cbegin()), endKey(map.cend())
112  {}
113 
114  const_iterator begin() { return beginKey; }
115  const_iterator end() { return endKey; }
116  const_iterator cbegin() { return beginKey; }
117  const_iterator cend() { return endKey; }
118 
119  protected:
120  const_iterator beginKey; ///< iterator pointing to the begin key
121  const_iterator endKey; ///< iterator pointing to the endkey
122 
123  }; // class MapKeyConstIteratorBox<>
124 
125  } // namespace NuRandomServiceHelper
126 } // namespace rndm
127 
128 #endif // NUTOOLS_RANDOMUTILS_PROVIDERS_MAPKEYITERATOR_H
MapKeyIterator(map_iterator_t const &from)
Initialize from a original iterator type.
decltype(auto) constexpr cend(T &&obj)
ADL-aware version of std::cend.
Definition: StdUtils.h:87
typename std::add_lvalue_reference< value_type >::type reference
Wraps a map-like object iterator to iterate on its first element.
pointer operator->()
Return a pointer to the key of the original iterator value.
static reference get_key(pair_reference value)
const_iterator beginKey
iterator pointing to the begin key
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
static QCString type
Definition: declinfo.cpp:672
reference operator*()
Return the key of the original iterator value.
decltype(auto) constexpr cbegin(T &&obj)
ADL-aware version of std::cbegin.
Definition: StdUtils.h:82
MapKeyIterator()=default
Default constructor is really default.
QuadExpr operator*(double v, const QuadExpr &e)
Definition: QuadExpr.h:39
Provides iterators for std::begin() and std::end()
#define Map
Definition: vhdlcode.cpp:19862
const_iterator endKey
iterator pointing to the endkey
typename std::tuple_element< 0, typename iterator_traits_t::value_type >::type value_type