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



Page generated by Doxygen 1.8.9.1 for MRPT 1.3.0 SVN: at Sun Sep 13 03:55:12 UTC 2015