| 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 | |
| 12 | /** |
| 13 | * @file FastSet.h |
| 14 | * @brief A thin wrapper around std::set that uses boost's fast_pool_allocator. |
| 15 | * @author Richard Roberts |
| 16 | * @date Oct 17, 2010 |
| 17 | */ |
| 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 |
| 31 | #include <gtsam/base/FastDefaultAllocator.h> |
| 32 | #include <gtsam/base/Testable.h> |
| 33 | |
| 34 | #include <functional> |
| 35 | #include <set> |
| 36 | |
| 37 | namespace boost { |
| 38 | namespace serialization { |
| 39 | class access; |
| 40 | } /* namespace serialization */ |
| 41 | } /* namespace boost */ |
| 42 | |
| 43 | namespace gtsam { |
| 44 | |
| 45 | /** |
| 46 | * FastSet is a thin wrapper around std::set that uses the boost |
| 47 | * fast_pool_allocator instead of the default STL allocator. This is just a |
| 48 | * convenience to avoid having lengthy types in the code. Through timing, |
| 49 | * we've seen that the fast_pool_allocator can lead to speedups of several %. |
| 50 | * @ingroup base |
| 51 | */ |
| 52 | template<typename VALUE> |
| 53 | class FastSet: public std::set<VALUE, std::less<VALUE>, |
| 54 | typename internal::FastDefaultAllocator<VALUE>::type> { |
| 55 | |
| 56 | GTSAM_CONCEPT_ASSERT(IsTestable<VALUE>); |
| 57 | |
| 58 | public: |
| 59 | |
| 60 | typedef std::set<VALUE, std::less<VALUE>, |
| 61 | typename internal::FastDefaultAllocator<VALUE>::type> Base; |
| 62 | |
| 63 | using Base::Base; // Inherit the set constructors |
| 64 | |
| 65 | FastSet() = default; ///< Default constructor |
| 66 | |
| 67 | /** Constructor from a iterable container, passes through to base class */ |
| 68 | template<typename INPUTCONTAINER> |
| 69 | explicit FastSet(const INPUTCONTAINER& container) : |
| 70 | Base(container.begin(), container.end()) { |
| 71 | } |
| 72 | |
| 73 | /** Copy constructor from another FastSet */ |
| 74 | FastSet(const FastSet<VALUE>& x) : |
| 75 | Base(x) { |
| 76 | } |
| 77 | |
| 78 | /** Copy constructor from the base set class */ |
| 79 | FastSet(const Base& x) : |
| 80 | Base(x) { |
| 81 | } |
| 82 | |
| 83 | FastSet& operator=(const FastSet& other) = default; |
| 84 | |
| 85 | #ifdef GTSAM_ALLOCATOR_BOOSTPOOL |
| 86 | /** Copy constructor from a standard STL container */ |
| 87 | FastSet(const std::set<VALUE>& x) { |
| 88 | // This if statement works around a bug in boost pool allocator and/or |
| 89 | // STL vector where if the size is zero, the pool allocator will allocate |
| 90 | // huge amounts of memory. |
| 91 | if(x.size() > 0) |
| 92 | Base::insert(x.begin(), x.end()); |
| 93 | } |
| 94 | #endif |
| 95 | |
| 96 | /** Conversion to a standard STL container */ |
| 97 | operator std::set<VALUE>() const { |
| 98 | return std::set<VALUE>(this->begin(), this->end()); |
| 99 | } |
| 100 | |
| 101 | /** Handy 'exists' function */ |
| 102 | bool exists(const VALUE& e) const { |
| 103 | return this->find(e) != this->end(); |
| 104 | } |
| 105 | |
| 106 | /** Print to implement Testable: pretty basic */ |
| 107 | void print(const std::string& str = "" ) const { |
| 108 | for (typename Base::const_iterator it = this->begin(); it != this->end(); ++it) |
| 109 | traits<VALUE>::Print(*it, str); |
| 110 | } |
| 111 | |
| 112 | /** Check for equality within tolerance to implement Testable */ |
| 113 | bool equals(const FastSet<VALUE>& other, double tol = 1e-9) const { |
| 114 | typename Base::const_iterator it1 = this->begin(), it2 = other.begin(); |
| 115 | while (it1 != this->end()) { |
| 116 | if (it2 == other.end() || !traits<VALUE>::Equals(*it2, *it2, tol)) |
| 117 | return false; |
| 118 | ++it1; |
| 119 | ++it2; |
| 120 | } |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | /** insert another set: handy for MATLAB access */ |
| 125 | void merge(const FastSet& other) { |
| 126 | Base::insert(other.begin(), other.end()); |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | #if GTSAM_ENABLE_BOOST_SERIALIZATION |
| 131 | /** Serialization function */ |
| 132 | friend class boost::serialization::access; |
| 133 | template<class ARCHIVE> |
| 134 | void serialize(ARCHIVE & ar, const unsigned int /*version*/) { |
| 135 | ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base); |
| 136 | } |
| 137 | #endif |
| 138 | }; |
| 139 | |
| 140 | } |
| 141 | |