ConcurrentMap.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 
21 #include <gtsam/global_includes.h>
22 
23 // Change class depending on whether we are using TBB
24 #ifdef GTSAM_USE_TBB
25 
26 // Include TBB header
27 # include <tbb/concurrent_unordered_map.h>
28 # undef min // TBB seems to include Windows.h which defines these macros that cause problems
29 # undef max
30 # undef ERROR
31 
32 #include <functional> // std::hash()
33 
34 // Use TBB concurrent_unordered_map for ConcurrentMap
35 template <typename KEY, typename VALUE>
36 using ConcurrentMapBase = tbb::concurrent_unordered_map<
37  KEY,
38  VALUE,
39  std::hash<KEY>
40  >;
41 
42 #else
43 
44 // If we're not using TBB, use a FastMap for ConcurrentMap
45 #include <gtsam/base/FastMap.h>
46 template <typename KEY, typename 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 
56 #include <gtsam/base/FastVector.h>
57 
58 namespace gtsam {
59 
68 template<typename KEY, typename VALUE>
69 class ConcurrentMap : public ConcurrentMapBase<KEY,VALUE> {
70 
71 public:
72 
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); }
100 private:
101  void erase() {}
102 public:
103 #endif
104 
105 private:
106 #if GTSAM_ENABLE_BOOST_SERIALIZATION
107 
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
122  FastVector<std::pair<KEY, VALUE> > 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 }
gtsam::ConcurrentMap::Base
ConcurrentMapBase< KEY, VALUE > Base
Definition: ConcurrentMap.h:73
gtsam::ConcurrentMap
Definition: ConcurrentMap.h:69
FastVector.h
A thin wrapper around std::vector that uses a custom allocator.
global_includes.h
Included from all GTSAM files.
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
gtsam::position
Point3_ position(const NavState_ &X)
Definition: navigation/expressions.h:37
gtsam::FastVector
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
gtsam::FastMap
Definition: FastMap.h:39
Eigen::last
static const symbolic::SymbolExpr< internal::symbolic_last_tag > last
Definition: IndexedViewHelper.h:38
gtsam::ConcurrentMap::exists
bool exists(const KEY &e) const
Definition: ConcurrentMap.h:91
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
Eigen::internal::VALUE
@ VALUE
Definition: SpecialFunctionsImpl.h:729
gtsam::ConcurrentMap::ConcurrentMap
ConcurrentMap(INPUTITERATOR first, INPUTITERATOR last)
Definition: ConcurrentMap.h:80
gtsam::ConcurrentMap::unsafe_erase
void unsafe_erase(typename Base::iterator first, typename Base::iterator last)
Definition: ConcurrentMap.h:98
gtsam::ConcurrentMap::erase
void erase()
Definition: ConcurrentMap.h:101
gtsam::ConcurrentMap::ConcurrentMap
ConcurrentMap(const ConcurrentMap< KEY, VALUE > &x)
Definition: ConcurrentMap.h:83
gtsam::ConcurrentMap::unsafe_erase
void unsafe_erase(typename Base::iterator position)
Definition: ConcurrentMap.h:96
test_KarcherMeanFactor.KEY
int KEY
Definition: test_KarcherMeanFactor.py:22
gtsam::ConcurrentMap::ConcurrentMap
ConcurrentMap()
Definition: ConcurrentMap.h:76
gtsam::ConcurrentMap::ConcurrentMap
ConcurrentMap(const Base &x)
Definition: ConcurrentMap.h:86
gtsam::save
void save(const Matrix &A, const string &s, const string &filename)
Definition: Matrix.cpp:167
gtsam
traits
Definition: SFMdata.h:40
gtsam::ConcurrentMap::operator=
ConcurrentMap & operator=(const ConcurrentMap &other)=default
gtsam::ConcurrentMap::unsafe_erase
Base::size_type unsafe_erase(const KEY &k)
Definition: ConcurrentMap.h:97
Eigen::placeholders::end
static const EIGEN_DEPRECATED end_t end
Definition: IndexedViewHelper.h:181
Base
Definition: test_virtual_functions.cpp:156
insert
A insert(1, 2)=0
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
FastMap.h
A thin wrapper around std::map that uses boost's fast_pool_allocator.


gtsam
Author(s):
autogenerated on Tue Jan 7 2025 04:01:59