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-2018, 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 containers
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/containers/map_as_vector.h>
54  * \ingroup mrpt_containers_grp
55  */
56 template <typename KEY, typename VALUE,
57  typename VECTOR_T =
60 {
61  public:
62  /** @name Iterators stuff and other types
63  @{ */
64  using key_type = KEY;
65  using value_type = std::pair<KEY, VALUE>;
66  using vec_t = VECTOR_T;
67  using size_type = typename vec_t::size_type;
68  using iterator = typename vec_t::iterator;
70  using reverse_iterator = std::reverse_iterator<iterator>;
71  using const_reverse_iterator = std::reverse_iterator<const_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
mrpt::containers::map_as_vector::end
iterator end()
Definition: map_as_vector.h:74
mrpt::containers::map_as_vector::insert
void insert(const value_type &keyvalpair)
Insert pair<key,val>, as in std::map.
Definition: map_as_vector.h:134
mrpt::containers::map_as_vector< KEY, VALUE >::iterator
typename vec_t::iterator iterator
Definition: map_as_vector.h:68
const_iterator
const Scalar * const_iterator
Definition: eigen_plugins.h:27
mrpt::containers::map_as_vector::rbegin
const_reverse_iterator rbegin() const
Definition: map_as_vector.h:78
mrpt::containers::map_as_vector::insert
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)
Definition: map_as_vector.h:128
mrpt::containers::map_as_vector::find
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,...
Definition: map_as_vector.h:152
mrpt::containers::map_as_vector::max_size
size_type max_size() const
Maximum size due to system limits.
Definition: map_as_vector.h:110
mrpt::containers::map_as_vector< KEY, VALUE >::reverse_iterator
std::reverse_iterator< iterator > reverse_iterator
Definition: map_as_vector.h:70
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::containers::map_as_vector::getVector
const vec_t & getVector() const
Return a read-only reference to the internal vector.
Definition: map_as_vector.h:112
mrpt::containers::map_as_vector::begin
const_iterator begin() const
Definition: map_as_vector.h:75
mrpt::containers::map_as_vector::find
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,...
Definition: map_as_vector.h:142
mrpt::containers::map_as_vector::rbegin
reverse_iterator rbegin()
Definition: map_as_vector.h:77
mrpt::aligned_std_vector
std::vector< T, mrpt::aligned_allocator_cpp11< T > > aligned_std_vector
Definition: aligned_std_vector.h:15
mrpt::containers::map_as_vector::map_as_vector
map_as_vector()
< Default constructor - does nothing *‍/
Definition: map_as_vector.h:96
mrpt::containers::map_as_vector::clear
void clear()
Clear the contents of this container.
Definition: map_as_vector.h:114
mrpt::containers::map_as_vector::map_as_vector
map_as_vector(const map_as_vector< KEY, VALUE > &o)
Copy constructor.
Definition: map_as_vector.h:98
mrpt::containers::map_as_vector::rend
const_reverse_iterator rend() const
Definition: map_as_vector.h:83
aligned_std_vector.h
mrpt::containers::map_as_vector::swap
void swap(map_as_vector< KEY, VALUE > &o)
Efficient swap with another object.
Definition: map_as_vector.h:116
mrpt::containers::map_as_vector::size
size_t size() const
Definition: map_as_vector.h:99
mrpt::containers::map_as_vector::empty
bool empty() const
Definition: map_as_vector.h:100
mrpt::containers::map_as_vector::rend
reverse_iterator rend()
Definition: map_as_vector.h:82
mrpt::containers::map_as_vector::end
const_iterator end() const
Definition: map_as_vector.h:76
mrpt::containers::map_as_vector< KEY, VALUE >::key_type
KEY key_type
Definition: map_as_vector.h:64
mrpt::containers::map_as_vector::m_vec
vec_t m_vec
The actual container.
Definition: map_as_vector.h:90
mrpt::containers::map_as_vector< KEY, VALUE >::vec_t
typename mrpt::aligned_std_vector< std::pair< KEY, VALUE >> vec_t
Definition: map_as_vector.h:66
mrpt::containers::map_as_vector::begin
iterator begin()
Definition: map_as_vector.h:73
mrpt::bayes::particle_storage_mode::VALUE
@ VALUE
mrpt::containers::map_as_vector< KEY, VALUE >::const_iterator
typename vec_t::const_iterator const_iterator
Definition: map_as_vector.h:69
mrpt::containers::map_as_vector< KEY, VALUE >::const_reverse_iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: map_as_vector.h:71
mrpt::containers::map_as_vector< KEY, VALUE >::value_type
std::pair< KEY, VALUE > value_type
Definition: map_as_vector.h:65
mrpt::containers::map_as_vector
A STL-like container which looks and behaves (almost exactly) like a std::map<> but is implemented as...
Definition: map_as_vector.h:59
mrpt::containers::map_as_vector< KEY, VALUE >::size_type
typename vec_t::size_type size_type
Definition: map_as_vector.h:67
iterator
Scalar * iterator
Definition: eigen_plugins.h:26
mrpt::containers::map_as_vector::operator[]
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 ...
Definition: map_as_vector.h:119
mrpt::containers::map_as_vector::count
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...
Definition: map_as_vector.h:104



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at mié 12 jul 2023 10:03:34 CEST