Rename insert() to set() to avoid confusion with std::map::insert()
[junction.git] / junction / extra / impl / MapAdapter_TBB.h
1 /*------------------------------------------------------------------------
2   Junction: Concurrent data structures in C++
3   Copyright (c) 2016 Jeff Preshing
4
5   Distributed under the Simplified BSD License.
6   Original location: https://github.com/preshing/junction
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the LICENSE file for more information.
11 ------------------------------------------------------------------------*/
12
13 #ifndef JUNCTION_EXTRA_IMPL_MAPADAPTER_TBB_H
14 #define JUNCTION_EXTRA_IMPL_MAPADAPTER_TBB_H
15
16 #include <junction/Core.h>
17
18 #if !JUNCTION_WITH_TBB
19 #error "You must configure with JUNCTION_WITH_TBB=1!"
20 #endif
21
22 #include <tbb/concurrent_hash_map.h>
23
24 namespace junction {
25 namespace extra {
26
27 class MapAdapter {
28 public:
29     static TURF_CONSTEXPR const char* MapName = "Intel TBB concurrent_hash_map";
30
31     MapAdapter(ureg) {
32     }
33
34     class ThreadContext {
35     public:
36         ThreadContext(MapAdapter&, ureg) {
37         }
38
39         void registerThread() {
40         }
41
42         void unregisterThread() {
43         }
44
45         void update() {
46         }
47     };
48
49     class Map {
50     private:
51         tbb::concurrent_hash_map<u32, void*> m_map;
52
53     public:
54         Map(ureg capacity) : m_map(capacity) {
55         }
56
57         void set(u32 key, void* value) {
58             m_map.insert(std::make_pair(key, value));
59         }
60
61         void* get(u32 key) {
62             tbb::concurrent_hash_map<u32, void*>::const_accessor result;
63             if (m_map.find(result, key))
64                 return result->second;
65             else
66                 return NULL;
67         }
68
69         void erase(u32 key) {
70             m_map.erase(key);
71         }
72     };
73
74     static ureg getInitialCapacity(ureg maxPopulation) {
75         return maxPopulation / 4;
76     }
77 };
78
79 } // namespace extra
80 } // namespace junction
81
82 #endif // JUNCTION_EXTRA_IMPL_MAPADAPTER_TBB_H