gtsam  4.1.0
gtsam
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 
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 #include <boost/serialization/nvp.hpp>
52 #include <boost/serialization/split_member.hpp>
53 #include <boost/static_assert.hpp>
54 
55 #include <gtsam/base/FastVector.h>
56 
57 namespace gtsam {
58 
67 template<typename KEY, typename VALUE>
68 class ConcurrentMap : public ConcurrentMapBase<KEY,VALUE> {
69 
70 public:
71 
73 
76 
78  template<typename INPUTITERATOR>
79  ConcurrentMap(INPUTITERATOR first, INPUTITERATOR last) : Base(first, last) {}
80 
82  ConcurrentMap(const ConcurrentMap<KEY,VALUE>& x) : Base(x) {}
83 
85  ConcurrentMap(const Base& x) : Base(x) {}
86 
88  bool exists(const KEY& e) const { return this->count(e); }
89 
90 #ifndef GTSAM_USE_TBB
91  // If we're not using TBB and this is actually a FastMap, we need to add these functions and hide
92  // the original erase functions.
93  void unsafe_erase(typename Base::iterator position) { ((Base*)this)->erase(position); }
94  typename Base::size_type unsafe_erase(const KEY& k) { return ((Base*)this)->erase(k); }
95  void unsafe_erase(typename Base::iterator first, typename Base::iterator last) {
96  return ((Base*)this)->erase(first, last); }
97 private:
98  void erase() {}
99 public:
100 #endif
101 
102 private:
104  friend class boost::serialization::access;
105  template<class Archive>
106  void save(Archive& ar, const unsigned int /*version*/) const
107  {
108  // Copy to an STL container and serialize that
109  FastVector<std::pair<KEY, VALUE> > map(this->size());
110  std::copy(this->begin(), this->end(), map.begin());
111  ar & BOOST_SERIALIZATION_NVP(map);
112  }
113  template<class Archive>
114  void load(Archive& ar, const unsigned int /*version*/)
115  {
116  // Load into STL container and then fill our map
117  FastVector<std::pair<KEY, VALUE> > map;
118  ar & BOOST_SERIALIZATION_NVP(map);
119  this->insert(map.begin(), map.end());
120  }
121  BOOST_SERIALIZATION_SPLIT_MEMBER()
122 };
123 
124 }
FastVector.h
A thin wrapper around std::vector that uses a custom allocator.
gtsam::ConcurrentMap
Definition: ConcurrentMap.h:68
gtsam::ConcurrentMap::exists
bool exists(const KEY &e) const
Handy 'exists' function.
Definition: ConcurrentMap.h:88
gtsam::ConcurrentMap::ConcurrentMap
ConcurrentMap(INPUTITERATOR first, INPUTITERATOR last)
Constructor from a range, passes through to base class.
Definition: ConcurrentMap.h:79
FastMap.h
A thin wrapper around std::map that uses boost's fast_pool_allocator.
gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
global_includes.h
Included from all GTSAM files.
gtsam::ConcurrentMap::ConcurrentMap
ConcurrentMap()
Default constructor.
Definition: ConcurrentMap.h:75
gtsam::ConcurrentMap::ConcurrentMap
ConcurrentMap(const ConcurrentMap< KEY, VALUE > &x)
Copy constructor from another ConcurrentMap.
Definition: ConcurrentMap.h:82
gtsam::FastMap
Definition: FastMap.h:38
gtsam::save
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:166
gtsam::ConcurrentMap::ConcurrentMap
ConcurrentMap(const Base &x)
Copy constructor from the base map class.
Definition: ConcurrentMap.h:85