Removed mpl-based Testable scheme with GTSAM 4 traits. This means you now have to be Testable to be a "FastSet". Future plan is to completely get rid of ths data structure, which is probably all but fast.
parent
926ae2c7a8
commit
e0afc0e05c
|
|
@ -18,29 +18,22 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <gtsam/base/FastDefaultAllocator.h>
|
|
||||||
|
|
||||||
#include <boost/mpl/has_xxx.hpp>
|
|
||||||
#include <boost/utility/enable_if.hpp>
|
|
||||||
#include <boost/serialization/serialization.hpp>
|
|
||||||
#include <boost/serialization/nvp.hpp>
|
#include <boost/serialization/nvp.hpp>
|
||||||
#include <boost/serialization/set.hpp>
|
#include <boost/serialization/set.hpp>
|
||||||
|
#include <gtsam/base/FastDefaultAllocator.h>
|
||||||
|
#include <gtsam/base/Testable.h>
|
||||||
|
|
||||||
#include <cmath>
|
#include <functional>
|
||||||
#include <iosfwd>
|
|
||||||
#include <iostream>
|
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <string>
|
|
||||||
|
|
||||||
BOOST_MPL_HAS_XXX_TRAIT_DEF(print)
|
namespace boost {
|
||||||
|
namespace serialization {
|
||||||
|
class access;
|
||||||
|
} /* namespace serialization */
|
||||||
|
} /* namespace boost */
|
||||||
|
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
// This is used internally to allow this container to be Testable even when it
|
|
||||||
// contains non-testable elements.
|
|
||||||
template<typename VALUE, class ENABLE = void>
|
|
||||||
struct FastSetTestableHelper;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FastSet is a thin wrapper around std::set that uses the boost
|
* FastSet is a thin wrapper around std::set that uses the boost
|
||||||
* fast_pool_allocator instead of the default STL allocator. This is just a
|
* fast_pool_allocator instead of the default STL allocator. This is just a
|
||||||
|
|
@ -48,12 +41,16 @@ struct FastSetTestableHelper;
|
||||||
* we've seen that the fast_pool_allocator can lead to speedups of several %.
|
* we've seen that the fast_pool_allocator can lead to speedups of several %.
|
||||||
* @addtogroup base
|
* @addtogroup base
|
||||||
*/
|
*/
|
||||||
template<typename VALUE, class ENABLE = void>
|
template<typename VALUE>
|
||||||
class FastSet: public std::set<VALUE, std::less<VALUE>, typename internal::FastDefaultAllocator<VALUE>::type> {
|
class FastSet: public std::set<VALUE, std::less<VALUE>,
|
||||||
|
typename internal::FastDefaultAllocator<VALUE>::type> {
|
||||||
|
|
||||||
|
BOOST_CONCEPT_ASSERT ((IsTestable<VALUE> ));
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
typedef std::set<VALUE, std::less<VALUE>, typename internal::FastDefaultAllocator<VALUE>::type> Base;
|
typedef std::set<VALUE, std::less<VALUE>,
|
||||||
|
typename internal::FastDefaultAllocator<VALUE>::type> Base;
|
||||||
|
|
||||||
/** Default constructor */
|
/** Default constructor */
|
||||||
FastSet() {
|
FastSet() {
|
||||||
|
|
@ -62,23 +59,23 @@ public:
|
||||||
/** Constructor from a range, passes through to base class */
|
/** Constructor from a range, passes through to base class */
|
||||||
template<typename INPUTITERATOR>
|
template<typename INPUTITERATOR>
|
||||||
explicit FastSet(INPUTITERATOR first, INPUTITERATOR last) :
|
explicit FastSet(INPUTITERATOR first, INPUTITERATOR last) :
|
||||||
Base(first, last) {
|
Base(first, last) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Constructor from a iterable container, passes through to base class */
|
/** Constructor from a iterable container, passes through to base class */
|
||||||
template<typename INPUTCONTAINER>
|
template<typename INPUTCONTAINER>
|
||||||
explicit FastSet(const INPUTCONTAINER& container) :
|
explicit FastSet(const INPUTCONTAINER& container) :
|
||||||
Base(container.begin(), container.end()) {
|
Base(container.begin(), container.end()) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Copy constructor from another FastSet */
|
/** Copy constructor from another FastSet */
|
||||||
FastSet(const FastSet<VALUE>& x) :
|
FastSet(const FastSet<VALUE>& x) :
|
||||||
Base(x) {
|
Base(x) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Copy constructor from the base set class */
|
/** Copy constructor from the base set class */
|
||||||
FastSet(const Base& x) :
|
FastSet(const Base& x) :
|
||||||
Base(x) {
|
Base(x) {
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef GTSAM_ALLOCATOR_BOOSTPOOL
|
#ifdef GTSAM_ALLOCATOR_BOOSTPOOL
|
||||||
|
|
@ -88,7 +85,7 @@ public:
|
||||||
// STL vector where if the size is zero, the pool allocator will allocate
|
// STL vector where if the size is zero, the pool allocator will allocate
|
||||||
// huge amounts of memory.
|
// huge amounts of memory.
|
||||||
if(x.size() > 0)
|
if(x.size() > 0)
|
||||||
Base::insert(x.begin(), x.end());
|
Base::insert(x.begin(), x.end());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
@ -98,17 +95,31 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Handy 'exists' function */
|
/** Handy 'exists' function */
|
||||||
bool exists(const VALUE& e) const { return this->find(e) != this->end(); }
|
bool exists(const VALUE& e) const {
|
||||||
|
return this->find(e) != this->end();
|
||||||
|
}
|
||||||
|
|
||||||
/** Print to implement Testable */
|
/** Print to implement Testable: pretty basic */
|
||||||
void print(const std::string& str = "") const { FastSetTestableHelper<VALUE>::print(*this, str); }
|
void print(const std::string& str = "") const {
|
||||||
|
for (typename Base::const_iterator it = this->begin(); it != this->end(); ++it)
|
||||||
|
traits<VALUE>::Print(*it, str);
|
||||||
|
}
|
||||||
|
|
||||||
/** Check for equality within tolerance to implement Testable */
|
/** Check for equality within tolerance to implement Testable */
|
||||||
bool equals(const FastSet<VALUE>& other, double tol = 1e-9) const { return FastSetTestableHelper<VALUE>::equals(*this, other, tol); }
|
bool equals(const FastSet<VALUE>& other, double tol = 1e-9) const {
|
||||||
|
typename Base::const_iterator it1 = this->begin(), it2 = other.begin();
|
||||||
|
while (it1 != this->end()) {
|
||||||
|
if (it2 == other.end() || !traits<VALUE>::Equals(*it2, *it2, tol))
|
||||||
|
return false;
|
||||||
|
++it1;
|
||||||
|
++it2;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/** insert another set: handy for MATLAB access */
|
/** insert another set: handy for MATLAB access */
|
||||||
void merge(const FastSet& other) {
|
void merge(const FastSet& other) {
|
||||||
Base::insert(other.begin(),other.end());
|
Base::insert(other.begin(), other.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
@ -120,59 +131,4 @@ private:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// This is the default Testable interface for *non*Testable elements, which
|
|
||||||
// uses stream operators.
|
|
||||||
template<typename VALUE, class ENABLE>
|
|
||||||
struct FastSetTestableHelper {
|
|
||||||
|
|
||||||
typedef FastSet<VALUE> Set;
|
|
||||||
|
|
||||||
static void print(const Set& set, const std::string& str) {
|
|
||||||
std::cout << str << "\n";
|
|
||||||
for (typename Set::const_iterator it = set.begin(); it != set.end(); ++it)
|
|
||||||
std::cout << " " << *it << "\n";
|
|
||||||
std::cout.flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool equals(const Set& set1, const Set& set2, double tol) {
|
|
||||||
typename Set::const_iterator it1 = set1.begin();
|
|
||||||
typename Set::const_iterator it2 = set2.begin();
|
|
||||||
while (it1 != set1.end()) {
|
|
||||||
if (it2 == set2.end() ||
|
|
||||||
fabs((double)(*it1) - (double)(*it2)) > tol)
|
|
||||||
return false;
|
|
||||||
++it1;
|
|
||||||
++it2;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// This is the Testable interface for Testable elements
|
|
||||||
template<typename VALUE>
|
|
||||||
struct FastSetTestableHelper<VALUE, typename boost::enable_if<has_print<VALUE> >::type> {
|
|
||||||
|
|
||||||
typedef FastSet<VALUE> Set;
|
|
||||||
|
|
||||||
static void print(const Set& set, const std::string& str) {
|
|
||||||
std::cout << str << "\n";
|
|
||||||
for (typename Set::const_iterator it = set.begin(); it != set.end(); ++it)
|
|
||||||
it->print(" ");
|
|
||||||
std::cout.flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool equals(const Set& set1, const Set& set2, double tol) {
|
|
||||||
typename Set::const_iterator it1 = set1.begin();
|
|
||||||
typename Set::const_iterator it2 = set2.begin();
|
|
||||||
while (it1 != set1.end()) {
|
|
||||||
if (it2 == set2.end() ||
|
|
||||||
!it1->equals(*it2, tol))
|
|
||||||
return false;
|
|
||||||
++it1;
|
|
||||||
++it2;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue