gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
22 
23 #include <set>
24 #include <iostream>
25 #include <string>
26 #include <cmath>
27 #include <boost/mpl/has_xxx.hpp>
28 #include <boost/utility/enable_if.hpp>
29 #include <boost/serialization/nvp.hpp>
30 #include <boost/serialization/set.hpp>
31 
32 BOOST_MPL_HAS_XXX_TRAIT_DEF(print)
33 
34 namespace gtsam {
35 
36 // This is used internally to allow this container to be Testable even when it
37 // contains non-testable elements.
38 template<typename VALUE, class ENABLE = void>
40 
48 template<typename VALUE, class ENABLE = void>
49 class FastSet: public std::set<VALUE, std::less<VALUE>, typename internal::FastDefaultAllocator<VALUE>::type> {
50 
51 public:
52 
53  typedef std::set<VALUE, std::less<VALUE>, typename internal::FastDefaultAllocator<VALUE>::type> Base;
54 
56  FastSet() {
57  }
58 
60  template<typename INPUTITERATOR>
61  explicit FastSet(INPUTITERATOR first, INPUTITERATOR last) :
62  Base(first, last) {
63  }
64 
66  template<typename INPUTCONTAINER>
67  explicit FastSet(const INPUTCONTAINER& container) :
68  Base(container.begin(), container.end()) {
69  }
70 
72  FastSet(const FastSet<VALUE>& x) :
73  Base(x) {
74  }
75 
77  FastSet(const Base& x) :
78  Base(x) {
79  }
80 
81 #ifdef GTSAM_ALLOCATOR_BOOSTPOOL
82 
83  FastSet(const std::set<VALUE>& x) {
84  // This if statement works around a bug in boost pool allocator and/or
85  // STL vector where if the size is zero, the pool allocator will allocate
86  // huge amounts of memory.
87  if(x.size() > 0)
88  Base::insert(x.begin(), x.end());
89  }
90 #endif
91 
93  operator std::set<VALUE>() const {
94  return std::set<VALUE>(this->begin(), this->end());
95  }
96 
98  bool exists(const VALUE& e) const { return this->find(e) != this->end(); }
99 
101  void print(const std::string& str = "") const { FastSetTestableHelper<VALUE>::print(*this, str); }
102 
104  bool equals(const FastSet<VALUE>& other, double tol = 1e-9) const { return FastSetTestableHelper<VALUE>::equals(*this, other, tol); }
105 
107  void merge(const FastSet& other) {
108  Base::insert(other.begin(),other.end());
109  }
110 
111 private:
113  friend class boost::serialization::access;
114  template<class ARCHIVE>
115  void serialize(ARCHIVE & ar, const unsigned int version) {
116  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
117  }
118 };
119 
120 // This is the default Testable interface for *non*Testable elements, which
121 // uses stream operators.
122 template<typename VALUE, class ENABLE>
123 struct FastSetTestableHelper {
124 
125  typedef FastSet<VALUE> Set;
126 
127  static void print(const Set& set, const std::string& str) {
128  std::cout << str << "\n";
129  for (typename Set::const_iterator it = set.begin(); it != set.end(); ++it)
130  std::cout << " " << *it << "\n";
131  std::cout.flush();
132  }
133 
134  static bool equals(const Set& set1, const Set& set2, double tol) {
135  typename Set::const_iterator it1 = set1.begin();
136  typename Set::const_iterator it2 = set2.begin();
137  while (it1 != set1.end()) {
138  if (it2 == set2.end() ||
139  fabs((double)(*it1) - (double)(*it2)) > tol)
140  return false;
141  ++it1;
142  ++it2;
143  }
144  return true;
145  }
146 };
147 
148 // This is the Testable interface for Testable elements
149 template<typename VALUE>
150 struct FastSetTestableHelper<VALUE, typename boost::enable_if<has_print<VALUE> >::type> {
151 
152  typedef FastSet<VALUE> Set;
153 
154  static void print(const Set& set, const std::string& str) {
155  std::cout << str << "\n";
156  for (typename Set::const_iterator it = set.begin(); it != set.end(); ++it)
157  it->print(" ");
158  std::cout.flush();
159  }
160 
161  static bool equals(const Set& set1, const Set& set2, double tol) {
162  typename Set::const_iterator it1 = set1.begin();
163  typename Set::const_iterator it2 = set2.begin();
164  while (it1 != set1.end()) {
165  if (it2 == set2.end() ||
166  !it1->equals(*it2, tol))
167  return false;
168  ++it1;
169  ++it2;
170  }
171  return true;
172  }
173 };
174 
175 }
FastSet(INPUTITERATOR first, INPUTITERATOR last)
Constructor from a range, passes through to base class.
Definition: FastSet.h:61
bool equals(const FastSet< VALUE > &other, double tol=1e-9) const
Check for equality within tolerance to implement Testable.
Definition: FastSet.h:104
void print(const std::string &str="") const
Print to implement Testable.
Definition: FastSet.h:101
FastSet()
Default constructor.
Definition: FastSet.h:56
bool exists(const VALUE &e) const
Handy 'exists' function.
Definition: FastSet.h:98
void print(const Matrix &A, const string &s, ostream &stream)
print a matrix
Definition: Matrix.cpp:183
Definition: FastSet.h:39
An easy way to control which allocator is used for Fast* collections.
Template to create a binary predicate.
Definition: Testable.h:102
FastSet(const Base &x)
Copy constructor from the base set class.
Definition: FastSet.h:77
FastSet(const INPUTCONTAINER &container)
Constructor from a iterable container, passes through to base class.
Definition: FastSet.h:67
Definition: FastSet.h:49
FastSet(const FastSet< VALUE > &x)
Copy constructor from another FastSet.
Definition: FastSet.h:72
void merge(const FastSet &other)
insert another set: handy for MATLAB access
Definition: FastSet.h:107