gtsam  4.0.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 // Use TBB concurrent_unordered_map for ConcurrentMap
33 # define CONCURRENT_MAP_BASE tbb::concurrent_unordered_map<KEY, VALUE>
34 
35 #else
36 
37 // If we're not using TBB, use a FastMap for ConcurrentMap
38 # include <gtsam/base/FastMap.h>
39 # define CONCURRENT_MAP_BASE gtsam::FastMap<KEY, VALUE>
40 
41 #endif
42 
43 #include <boost/serialization/nvp.hpp>
44 #include <boost/serialization/split_member.hpp>
45 #include <boost/static_assert.hpp>
46 
47 #include <gtsam/base/FastVector.h>
48 
49 namespace gtsam {
50 
59 template<typename KEY, typename VALUE>
60 class ConcurrentMap : public CONCURRENT_MAP_BASE {
61 
62 public:
63 
64  typedef CONCURRENT_MAP_BASE Base;
65 
68 
70  template<typename INPUTITERATOR>
71  ConcurrentMap(INPUTITERATOR first, INPUTITERATOR last) : Base(first, last) {}
72 
74  ConcurrentMap(const ConcurrentMap<KEY,VALUE>& x) : Base(x) {}
75 
77  ConcurrentMap(const Base& x) : Base(x) {}
78 
80  bool exists(const KEY& e) const { return this->count(e); }
81 
82 #ifndef GTSAM_USE_TBB
83  // If we're not using TBB and this is actually a FastMap, we need to add these functions and hide
84  // the original erase functions.
85  void unsafe_erase(typename Base::iterator position) { ((Base*)this)->erase(position); }
86  typename Base::size_type unsafe_erase(const KEY& k) { return ((Base*)this)->erase(k); }
87  void unsafe_erase(typename Base::iterator first, typename Base::iterator last) {
88  return ((Base*)this)->erase(first, last); }
89 private:
90  void erase() {}
91 public:
92 #endif
93 
94 private:
96  friend class boost::serialization::access;
97  template<class Archive>
98  void save(Archive& ar, const unsigned int /*version*/) const
99  {
100  // Copy to an STL container and serialize that
101  FastVector<std::pair<KEY, VALUE> > map(this->size());
102  std::copy(this->begin(), this->end(), map.begin());
103  ar & BOOST_SERIALIZATION_NVP(map);
104  }
105  template<class Archive>
106  void load(Archive& ar, const unsigned int /*version*/)
107  {
108  // Load into STL container and then fill our map
109  FastVector<std::pair<KEY, VALUE> > map;
110  ar & BOOST_SERIALIZATION_NVP(map);
111  this->insert(map.begin(), map.end());
112  }
113  BOOST_SERIALIZATION_SPLIT_MEMBER()
114 };
115 
116 }
A thin wrapper around std::vector that uses a custom allocator.
Definition: ConcurrentMap.h:60
Included from all GTSAM files.
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:162
A thin wrapper around std::map that uses boost's fast_pool_allocator.
ConcurrentMap(const ConcurrentMap< KEY, VALUE > &x)
Copy constructor from another ConcurrentMap.
Definition: ConcurrentMap.h:74
ConcurrentMap(INPUTITERATOR first, INPUTITERATOR last)
Constructor from a range, passes through to base class.
Definition: ConcurrentMap.h:71
bool exists(const KEY &e) const
Handy 'exists' function.
Definition: ConcurrentMap.h:80
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
ConcurrentMap()
Default constructor.
Definition: ConcurrentMap.h:67
ConcurrentMap(const Base &x)
Copy constructor from the base map class.
Definition: ConcurrentMap.h:77