gtsam  4.0.0
gtsam
FastVector.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 #pragma once
20 
22 #include <boost/serialization/nvp.hpp>
23 #include <boost/serialization/vector.hpp>
24 #include <vector>
25 
26 namespace gtsam {
27 
35 template<typename VALUE>
36 class FastVector: public std::vector<VALUE,
37  typename internal::FastDefaultVectorAllocator<VALUE>::type> {
38 
39 public:
40 
41  typedef std::vector<VALUE,
42  typename internal::FastDefaultVectorAllocator<VALUE>::type> Base;
43 
46  }
47 
49  explicit FastVector(size_t size) :
50  Base(size) {
51  }
52 
54  explicit FastVector(size_t size, const VALUE& initial) :
55  Base(size, initial) {
56  }
57 
59  template<typename INPUTITERATOR>
60  explicit FastVector(INPUTITERATOR first, INPUTITERATOR last) {
61  // This if statement works around a bug in boost pool allocator and/or
62  // STL vector where if the size is zero, the pool allocator will allocate
63  // huge amounts of memory.
64  if (first != last)
65  Base::assign(first, last);
66  }
67 
69  FastVector(const Base& x) :
70  Base(x) {
71  }
72 
74  template<typename ALLOCATOR>
75  FastVector(const std::vector<VALUE, ALLOCATOR>& x) {
76  // This if statement works around a bug in boost pool allocator and/or
77  // STL vector where if the size is zero, the pool allocator will allocate
78  // huge amounts of memory.
79  if (x.size() > 0)
80  Base::assign(x.begin(), x.end());
81  }
82 
84  operator std::vector<VALUE>() const {
85  return std::vector<VALUE>(this->begin(), this->end());
86  }
87 
88 private:
90  friend class boost::serialization::access;
91  template<class ARCHIVE>
92  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
93  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
94  }
95 
96 };
97 
98 }
FastVector(size_t size, const VALUE &initial)
Constructor from size and initial values.
Definition: FastVector.h:54
FastVector()
Default constructor.
Definition: FastVector.h:45
FastVector(size_t size)
Constructor from size.
Definition: FastVector.h:49
Definition: FastVector.h:36
An easy way to control which allocator is used for Fast* collections.
FastVector(const Base &x)
Copy constructor from the base class.
Definition: FastVector.h:69
FastVector(const std::vector< VALUE, ALLOCATOR > &x)
Copy constructor from a standard STL container.
Definition: FastVector.h:75
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
FastVector(INPUTITERATOR first, INPUTITERATOR last)
Constructor from a range, passes through to base class.
Definition: FastVector.h:60