gtsam 4.2.0
gtsam
FastSet.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
21#include <boost/version.hpp>
22#if BOOST_VERSION >= 107400
23#include <boost/serialization/library_version_type.hpp>
24#endif
25#include <boost/serialization/nvp.hpp>
26#include <boost/serialization/set.hpp>
28#include <gtsam/base/Testable.h>
29
30#include <functional>
31#include <set>
32
33namespace boost {
34namespace serialization {
35class access;
36} /* namespace serialization */
37} /* namespace boost */
38
39namespace gtsam {
40
48template<typename VALUE>
49class FastSet: public std::set<VALUE, std::less<VALUE>,
50 typename internal::FastDefaultAllocator<VALUE>::type> {
51
52 BOOST_CONCEPT_ASSERT ((IsTestable<VALUE> ));
53
54public:
55
56 typedef std::set<VALUE, std::less<VALUE>,
57 typename internal::FastDefaultAllocator<VALUE>::type> Base;
58
59 using Base::Base; // Inherit the set constructors
60
61 FastSet() = default;
62
64 template<typename INPUTCONTAINER>
65 explicit FastSet(const INPUTCONTAINER& container) :
66 Base(container.begin(), container.end()) {
67 }
68
71 Base(x) {
72 }
73
75 FastSet(const Base& x) :
76 Base(x) {
77 }
78
79#ifdef GTSAM_ALLOCATOR_BOOSTPOOL
81 FastSet(const std::set<VALUE>& x) {
82 // This if statement works around a bug in boost pool allocator and/or
83 // STL vector where if the size is zero, the pool allocator will allocate
84 // huge amounts of memory.
85 if(x.size() > 0)
86 Base::insert(x.begin(), x.end());
87 }
88#endif
89
91 operator std::set<VALUE>() const {
92 return std::set<VALUE>(this->begin(), this->end());
93 }
94
96 bool exists(const VALUE& e) const {
97 return this->find(e) != this->end();
98 }
99
101 void print(const std::string& str = "") const {
102 for (typename Base::const_iterator it = this->begin(); it != this->end(); ++it)
103 traits<VALUE>::Print(*it, str);
104 }
105
107 bool equals(const FastSet<VALUE>& other, double tol = 1e-9) const {
108 typename Base::const_iterator it1 = this->begin(), it2 = other.begin();
109 while (it1 != this->end()) {
110 if (it2 == other.end() || !traits<VALUE>::Equals(*it2, *it2, tol))
111 return false;
112 ++it1;
113 ++it2;
114 }
115 return true;
116 }
117
119 void merge(const FastSet& other) {
120 Base::insert(other.begin(), other.end());
121 }
122
123private:
126 template<class ARCHIVE>
127 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
128 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
129 }
130};
131
132}
Concept check for values that can be used in unit tests.
An easy way to control which allocator is used for Fast* collections.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
FastSet is a thin wrapper around std::set that uses the boost fast_pool_allocator instead of the defa...
Definition: FastSet.h:50
void merge(const FastSet &other)
insert another set: handy for MATLAB access
Definition: FastSet.h:119
void print(const std::string &str="") const
Print to implement Testable: pretty basic.
Definition: FastSet.h:101
FastSet(const INPUTCONTAINER &container)
Constructor from a iterable container, passes through to base class.
Definition: FastSet.h:65
FastSet(const FastSet< VALUE > &x)
Copy constructor from another FastSet.
Definition: FastSet.h:70
bool exists(const VALUE &e) const
Handy 'exists' function.
Definition: FastSet.h:96
bool equals(const FastSet< VALUE > &other, double tol=1e-9) const
Check for equality within tolerance to implement Testable.
Definition: FastSet.h:107
friend class boost::serialization::access
Serialization function.
Definition: FastSet.h:125
FastSet(const Base &x)
Copy constructor from the base set class.
Definition: FastSet.h:75
FastSet()=default
Default constructor.
A testable concept check that should be placed in applicable unit tests and in generic algorithms.
Definition: Testable.h:58