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