StableVector.hpp
Go to the documentation of this file.
1 
28 /*
29  * StableVector.hpp
30  *
31  * @date 08.06.2017
32  * @author Johan M. von Behren <johan@vonbehren.eu>
33  */
34 
35 #ifndef LVR2_ATTRMAPS_STABLEVECTOR_H_
36 #define LVR2_ATTRMAPS_STABLEVECTOR_H_
37 
38 #include <vector>
39 #include <utility>
40 #include <boost/optional.hpp>
41 #include <boost/shared_array.hpp>
42 
43 using std::move;
44 using std::vector;
45 
46 
47 #include "lvr2/util/BaseHandle.hpp"
49 
50 
51 namespace lvr2
52 {
53 
60 template<typename HandleT, typename ElemT>
62 {
63 private:
65  const vector<boost::optional<ElemT>>* m_elements;
66 
68  size_t m_pos;
69 public:
70  StableVectorIterator(const vector<boost::optional<ElemT>>* deleted, bool startAtEnd = false);
71 
73  bool operator==(const StableVectorIterator& other) const;
74  bool operator!=(const StableVectorIterator& other) const;
75 
77 
78  bool isAtEnd() const;
79 
80  HandleT operator*() const;
81 };
82 
103 template<typename HandleT, typename ElemT>
105 {
106  static_assert(
107  std::is_base_of<BaseHandle<Index>, HandleT>::value,
108  "HandleT must inherit from BaseHandle!"
109  );
110 
111 public:
112 
113  using ElementType = ElemT;
114  using HandleType = HandleT;
115 
120 
128  StableVector(size_t countElements, const ElementType& defaultValue);
129 
130  StableVector(size_t countElements, const boost::shared_array<ElementType>& sharedArray);
131 
137  HandleType push(const ElementType& elem);
138 
144  HandleType push(ElementType&& elem);
145 
156  void increaseSize(HandleType upTo);
157 
167  void increaseSize(HandleType upTo, const ElementType& elem);
168 
172  HandleType nextHandle() const;
173 
182  void erase(HandleType handle);
183 
187  void clear();
188 
195  boost::optional<ElementType&> get(HandleType handle);
196 
203  boost::optional<const ElementType&> get(HandleType handle) const;
204 
212  void set(HandleType handle, const ElementType& elem);
213 
221  void set(HandleType handle, ElementType&& elem);
222 
231 
239  const ElementType& operator[](HandleType handle) const;
240 
244  size_t size() const;
245 
249  size_t numUsed() const;
250 
258 
264 
274  void reserve(size_t newCap);
275 
276 private:
278  size_t m_usedCount;
279 
281  vector<boost::optional<ElementType>> m_elements;
282 
287  void checkAccess(HandleType handle) const;
288 };
289 
290 } // namespace lvr2
291 
292 #include "lvr2/attrmaps/StableVector.tcc"
293 
294 #endif /* LVR2_ATTRMAPS_STABLEVECTOR_H_ */
lvr2::StableVector::set
void set(HandleType handle, const ElementType &elem)
Set a value for the existing handle.
Handles.hpp
lvr2::StableVector::increaseSize
void increaseSize(HandleType upTo)
Increases the size of the vector to the length of upTo.
lvr2::StableVectorIterator::StableVectorIterator
StableVectorIterator(const vector< boost::optional< ElemT >> *deleted, bool startAtEnd=false)
lvr2::StableVectorIterator
Iterator over handles in this vector, which skips deleted elements.
Definition: StableVector.hpp:61
lvr2::StableVector::HandleType
HandleT HandleType
Definition: StableVector.hpp:114
lvr2::StableVectorIterator::operator!=
bool operator!=(const StableVectorIterator &other) const
lvr2::BaseHandle< Index >
lvr2::Cluster
Represents a group of handles, which are somehow connected.
Definition: Cluster.hpp:50
lvr2::StableVector::StableVector
StableVector()
Creates an empty StableVector.
Definition: StableVector.hpp:119
lvr2::StableVector::begin
StableVectorIterator< HandleType, ElementType > begin() const
Returns an iterator to the first element of this vector.
lvr2::StableVector::nextHandle
HandleType nextHandle() const
The handle which would be returned by calling push now.
lvr2::StableVectorIterator::operator*
HandleT operator*() const
lvr2::StableVectorIterator::isAtEnd
bool isAtEnd() const
lvr2::StableVectorIterator::m_elements
const vector< boost::optional< ElemT > > * m_elements
Reference to the deleted marker array this iterator belongs to.
Definition: StableVector.hpp:65
lvr2::StableVectorIterator::operator++
StableVectorIterator & operator++()
lvr2::StableVector::size
size_t size() const
Absolute size of the vector (including deleted elements).
lvr2::StableVector
A vector which guarantees stable indices and features O(1) deletion.
Definition: StableVector.hpp:104
lvr2::StableVector::m_usedCount
size_t m_usedCount
Count of used elements in elements vector.
Definition: StableVector.hpp:278
lvr2::StableVectorIterator::m_pos
size_t m_pos
Current position in the vector.
Definition: StableVector.hpp:68
lvr2::StableVector::ElementType
ElemT ElementType
Definition: StableVector.hpp:113
lvr2::StableVector::checkAccess
void checkAccess(HandleType handle) const
Assert that the requested handle is not deleted or throw an exception otherwise.
lvr2::StableVectorIterator::operator==
bool operator==(const StableVectorIterator &other) const
lvr2::StableVector::get
boost::optional< ElementType & > get(HandleType handle)
Returns the element referred to by handle.
lvr2::StableVector::push
HandleType push(const ElementType &elem)
Adds the given element to the vector.
lvr2::StableVector::erase
void erase(HandleType handle)
Mark the element behind the given handle as deleted.
lvr2::StableVector::numUsed
size_t numUsed() const
Number of non-deleted elements.
lvr2::StableVector::clear
void clear()
Removes all elements from the vector.
lvr2::StableVector::end
StableVectorIterator< HandleType, ElementType > end() const
Returns an iterator to the element after the last element of this vector.
lvr2::StableVectorIterator::operator=
StableVectorIterator & operator=(const StableVectorIterator &other)
lvr2::ClusterHandle
Handle to access Cluster of the ClusterBiMap.
Definition: Handles.hpp:152
BaseHandle.hpp
lvr2
Definition: BaseBufferManipulators.hpp:39
lvr2::StableVector::operator[]
ElementType & operator[](HandleType handle)
Returns the element referred to by handle.
lvr2::StableVector::m_elements
vector< boost::optional< ElementType > > m_elements
Vector for stored elements.
Definition: StableVector.hpp:281
lvr2::StableVector::reserve
void reserve(size_t newCap)
Increase the capacity of the vector to a value that's greater or equal to newCap.


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Wed Mar 2 2022 00:37:25