gtsam
Loading...
Searching...
No Matches
ConcurrentMap.h
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
22#include <gtsam/base/FastMap.h>
23
24// Change class depending on whether we are using TBB
25#ifdef GTSAM_USE_TBB
26
27// Include TBB header
28# include <tbb/concurrent_unordered_map.h>
29# undef min // TBB seems to include Windows.h which defines these macros that cause problems
30# undef max
31# undef ERROR
32
33#include <functional> // std::hash()
34
35// Use TBB concurrent_unordered_map for ConcurrentMap
36template <typename KEY, typename VALUE>
37using ConcurrentMapBase = tbb::concurrent_unordered_map<
38 KEY,
39 VALUE,
40 std::hash<KEY>
41 >;
42
43#else
44
45// If we're not using TBB, use a std::map
46template <typename KEY, typename VALUE>
47using ConcurrentMapBase = gtsam::FastMap<KEY, VALUE>;
48
49#endif
50
51#if GTSAM_ENABLE_BOOST_SERIALIZATION
52#include <boost/serialization/nvp.hpp>
53#include <boost/serialization/split_member.hpp>
54#endif
55
57
58namespace gtsam {
59
68template<typename KEY, typename VALUE>
69class ConcurrentMap : public ConcurrentMapBase<KEY,VALUE> {
70
71public:
72
73 typedef ConcurrentMapBase<KEY,VALUE> Base;
74
77
79 template<typename INPUTITERATOR>
80 ConcurrentMap(INPUTITERATOR first, INPUTITERATOR last) : Base(first, last) {}
81
84
86 ConcurrentMap(const Base& x) : Base(x) {}
87
88 ConcurrentMap& operator=(const ConcurrentMap& other) = default;
89
91 bool exists(const KEY& e) const { return this->count(e); }
92
93#ifndef GTSAM_USE_TBB
94 // If we're not using TBB and this is actually a FastMap, we need to add these functions and hide
95 // the original erase functions.
96 void unsafe_erase(typename Base::iterator position) { ((Base*)this)->erase(position); }
97 typename Base::size_type unsafe_erase(const KEY& k) { return ((Base*)this)->erase(k); }
98 void unsafe_erase(typename Base::iterator first, typename Base::iterator last) {
99 return ((Base*)this)->erase(first, last); }
100private:
101 void erase() {}
102public:
103#endif
104
105private:
106#if GTSAM_ENABLE_BOOST_SERIALIZATION
108 friend class boost::serialization::access;
109 template<class Archive>
110 void save(Archive& ar, const unsigned int /*version*/) const
111 {
112 // Copy to an STL container and serialize that
113 FastVector<std::pair<KEY, VALUE> > map(this->size());
114 std::copy(this->begin(), this->end(), map.begin());
115 ar & BOOST_SERIALIZATION_NVP(map);
116 }
117 template<class Archive>
118 void load(Archive& ar, const unsigned int /*version*/)
119 {
120 this->clear();
121 // Load into STL container and then fill our map
123 ar & BOOST_SERIALIZATION_NVP(map);
124 this->insert(map.begin(), map.end());
125 }
126 BOOST_SERIALIZATION_SPLIT_MEMBER()
127#endif
128};
129
130}
A thin wrapper around std::vector that uses a custom allocator.
A thin wrapper around std::map that uses boost's fast_pool_allocator.
Included from all GTSAM files.
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
Global functions in a separate testing namespace.
Definition chartTesting.h:28
void save(const Matrix &A, const string &s, const string &filename)
save a matrix to file, which can be loaded by matlab
Definition Matrix.cpp:154
FastMap is a thin wrapper around std::map that uses the boost fast_pool_allocator instead of the defa...
Definition ConcurrentMap.h:69
ConcurrentMap(INPUTITERATOR first, INPUTITERATOR last)
Constructor from a range, passes through to base class.
Definition ConcurrentMap.h:80
ConcurrentMap(const ConcurrentMap< KEY, VALUE > &x)
Copy constructor from another ConcurrentMap.
Definition ConcurrentMap.h:83
bool exists(const KEY &e) const
Handy 'exists' function.
Definition ConcurrentMap.h:91
ConcurrentMap(const Base &x)
Copy constructor from the base map class.
Definition ConcurrentMap.h:86
ConcurrentMap()
Default constructor.
Definition ConcurrentMap.h:76
FastMap is a thin wrapper around std::map that uses the boost fast_pool_allocator instead of the defa...
Definition FastMap.h:40