Main MRPT website > C++ reference for MRPT 1.9.9
map_as_vector.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef mrpt_map_as_vector_H
10 #define mrpt_map_as_vector_H
11 
13 #include <map>
14 #include <vector>
15 
16 namespace mrpt
17 {
18 namespace utils
19 {
20 /** A STL-like container which looks and behaves (almost exactly) like a
21  * std::map<> but is implemented as a linear std::vector<> indexed by KEY.
22  * Note that KEY must be integer types only (size_t, uint32_t, etc.)
23  * This implementation is much more efficient than std::map<> when the most
24  * common operation is accesing elements
25  * by KEY with find() or [], and the range of KEY values starts at 0 (or a
26  * reasonable low number).
27  *
28  * This container is internally implemented as a linear array (std::vector) of
29  * the same fundamental type than the equivalent std::map<K,V>,
30  * that is, elements are <code> std::pair<K,V> </code> (note that K is NOT
31  * const as in std::map).
32  * I know, I know... this implementation wastes a lot of useless key elements
33  * in the pair.first when indices
34  * are already implicit in the std::vector<> order... but I promise I'll pay a
35  * beer to whoever show me an
36  * *efficient* alternative. If failed, update this comment: COUNTER OF WASTED
37  * HOURS WITH THIS: 3h
38  *
39  * Note that there is one <b>fundamental difference</b> wrt std::map<>: if you
40  * start with an empty map_as_vector<> and
41  * insert one element at the i'th position (with either [] or insert), the
42  * elements [0,i-1] will also exist then, but both
43  * their first & second entries (for the corresponding std::pair) will be
44  * <b>undefined</b>. This was intentional in order to
45  * gain efficiency (in particular, each std::pair<> doesn't have a
46  * constructor when resizing the underlying std::vector).
47  *
48  * The default underlying non-associative container is a "memory-aligned
49  * std::vector<>", but it can be changed to a
50  * standard vector<> or to a deque<> (to avoid memory reallocations) by
51  * changing the template parameter \a VECTOR_T.
52  *
53  * \note Defined in #include <mrpt/utils/map_as_vector.h>
54  * \ingroup stlext_grp
55  */
56 template <typename KEY, typename VALUE,
57  typename VECTOR_T = typename mrpt::aligned_containers<
58  std::pair<KEY, VALUE>>::vector_t>
60 {
61  public:
62  /** @name Iterators stuff and other types
63  @{ */
64  typedef KEY key_type;
65  typedef std::pair<KEY, VALUE> value_type;
66  typedef VECTOR_T vec_t;
67  typedef typename vec_t::size_type size_type;
68  typedef typename vec_t::iterator iterator;
70  typedef std::reverse_iterator<iterator> reverse_iterator;
71  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
72 
73  inline iterator begin() { return m_vec.begin(); }
74  inline iterator end() { return m_vec.end(); }
75  inline const_iterator begin() const { return m_vec.begin(); }
76  inline const_iterator end() const { return m_vec.end(); }
77  inline reverse_iterator rbegin() { return reverse_iterator(end()); }
79  {
80  return const_reverse_iterator(end());
81  }
82  inline reverse_iterator rend() { return reverse_iterator(begin()); }
84  {
85  return const_reverse_iterator(begin());
86  }
87  /** @} */
88  private:
89  /** The actual container */
91 
92  public:
93  /** @name Constructors, read/write access and other operations
94  @{ */
95  //!< Default constructor - does nothing */
96  inline map_as_vector() {}
97  /** Copy constructor */
99  inline size_t size() const { return m_vec.size(); }
100  inline bool empty() const { return m_vec.empty(); }
101  /** Count how many entries have a given key value - unlike std::map<K,V>,
102  * recall that this class will say an element i<N-1 exists just due to an
103  * insertion of element at N */
104  inline size_type count(const key_type i) const
105  {
106  return (i < m_vec.size()) ? 1 : 0;
107  }
108 
109  /** Maximum size due to system limits */
110  inline size_type max_size() const { return m_vec.max_size(); }
111  /** Return a read-only reference to the internal vector */
112  inline const vec_t& getVector() const { return m_vec; }
113  /** Clear the contents of this container */
114  inline void clear() { m_vec.clear(); }
115  /** Efficient swap with another object */
116  inline void swap(map_as_vector<KEY, VALUE>& o) { m_vec.swap(o.m_vec); }
117  /** Write/read via [i] operator, that creates all elements up to (and
118  * including) the i'th if they didn't exist already. */
119  inline VALUE& operator[](const size_t i)
120  {
121  if (m_vec.size() <= i) m_vec.resize(i + 1);
122  m_vec[i].first = i;
123  return m_vec[i].second;
124  }
125 
126  /** Insert pair<key,val>, as in std::map (guess_point is actually ignored in
127  * this class) */
128  inline void insert(
129  const iterator& guess_point, const value_type& keyvalpair)
130  {
131  this->operator[](keyvalpair.first) = keyvalpair;
132  }
133  /** Insert pair<key,val>, as in std::map */
134  inline void insert(const value_type& keyvalpair)
135  {
136  this->operator[](keyvalpair.first) = keyvalpair;
137  }
138 
139  /** Constant-time find, returning an iterator to the <key,val> pair or to
140  * end() if not found (that is, if it's above the maximum index in the
141  * vector) */
142  inline iterator find(const size_t i)
143  {
144  if (i < m_vec.size())
145  return m_vec.begin() + i;
146  else
147  return m_vec.end();
148  }
149  /** Constant-time find, returning an iterator to the <key,val> pair or to
150  * end() if not found (that is, if it's above the maximum index in the
151  * vector) */
152  inline const_iterator find(const size_t i) const
153  {
154  if (i < m_vec.size())
155  return m_vec.begin() + i;
156  else
157  return m_vec.end();
158  }
159 
160  /** @} */
161 
162 }; // end class map_as_vector
163 
164 } // End of namespace
165 } // End of namespace
166 #endif
A STL-like container which looks and behaves (almost exactly) like a std::map<> but is implemented as...
Definition: map_as_vector.h:60
vec_t m_vec
The actual container.
Definition: map_as_vector.h:90
vec_t::size_type size_type
Definition: map_as_vector.h:67
const vec_t & getVector() const
Return a read-only reference to the internal vector.
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: map_as_vector.h:71
iterator find(const size_t i)
Constant-time find, returning an iterator to the <key,val> pair or to end() if not found (that is,...
const_reverse_iterator rend() const
Definition: map_as_vector.h:83
VALUE & operator[](const size_t i)
Write/read via [i] operator, that creates all elements up to (and including) the i'th if they didn't ...
vec_t::const_iterator const_iterator
Definition: map_as_vector.h:69
void swap(map_as_vector< KEY, VALUE > &o)
Efficient swap with another object.
const_iterator begin() const
Definition: map_as_vector.h:75
map_as_vector()
< Default constructor - does nothing *‍/
Definition: map_as_vector.h:96
reverse_iterator rbegin()
Definition: map_as_vector.h:77
void insert(const iterator &guess_point, const value_type &keyvalpair)
Insert pair<key,val>, as in std::map (guess_point is actually ignored in this class)
vec_t::iterator iterator
Definition: map_as_vector.h:68
void clear()
Clear the contents of this container.
size_type count(const key_type i) const
Count how many entries have a given key value - unlike std::map<K,V>, recall that this class will say...
size_type max_size() const
Maximum size due to system limits.
std::pair< KEY, VALUE > value_type
Definition: map_as_vector.h:65
const_reverse_iterator rbegin() const
Definition: map_as_vector.h:78
const_iterator find(const size_t i) const
Constant-time find, returning an iterator to the <key,val> pair or to end() if not found (that is,...
const_iterator end() const
Definition: map_as_vector.h:76
void insert(const value_type &keyvalpair)
Insert pair<key,val>, as in std::map.
map_as_vector(const map_as_vector< KEY, VALUE > &o)
Copy constructor.
Definition: map_as_vector.h:98
reverse_iterator rend()
Definition: map_as_vector.h:82
std::reverse_iterator< iterator > reverse_iterator
Definition: map_as_vector.h:70
Scalar * iterator
Definition: eigen_plugins.h:26
const Scalar * const_iterator
Definition: eigen_plugins.h:27
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Helper types for STL containers with Eigen memory allocators.



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 63ea9d1f1 Thu Nov 23 00:06:53 2017 +0100 at mar 26 may 2026 12:19:29 CEST