Rename insert() to set() to avoid confusion with std::map::insert()
[junction.git] / junction / extra / impl / MapAdapter_CDS_Cuckoo.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_CDS_CUCKOO_H
14 #define JUNCTION_EXTRA_IMPL_MAPADAPTER_CDS_CUCKOO_H
15
16 #include <junction/Core.h>
17
18 #if !JUNCTION_WITH_CDS
19 #error "You must configure with JUNCTION_WITH_CDS=1!"
20 #endif
21
22 #include <junction/MapTraits.h>
23 #include <cds/init.h>
24 #include <cds/gc/hp.h>
25 #include <memory.h> // memcpy required by cuckoo_map.h
26 #include <cds/container/cuckoo_map.h>
27
28 namespace junction {
29 namespace extra {
30
31 class MapAdapter {
32 public:
33     static TURF_CONSTEXPR const char* MapName = "CDS CuckooMap";
34
35     cds::gc::HP* m_hpGC;
36
37     MapAdapter(ureg) {
38         cds::Initialize();
39         m_hpGC = new cds::gc::HP;
40     }
41
42     ~MapAdapter() {
43         delete m_hpGC;
44         cds::Terminate();
45     }
46
47     class ThreadContext {
48     public:
49         ThreadContext(MapAdapter&, ureg) {
50         }
51
52         void registerThread() {
53             cds::threading::Manager::attachThread();
54         }
55
56         void unregisterThread() {
57             cds::threading::Manager::detachThread();
58         }
59
60         void update() {
61         }
62     };
63
64     class Map {
65     private:
66         struct Hash1 {
67             size_t operator()(u32 s) const {
68                 return junction::hash(s);
69             }
70         };
71
72         struct Hash2 {
73             size_t operator()(u32 s) const {
74                 return junction::hash(s + 0x9e3779b9);
75             }
76         };
77
78         struct Traits : cds::container::cuckoo::traits {
79             typedef std::equal_to<u32> equal_to;
80             typedef cds::opt::hash_tuple<Hash1, Hash2> hash;
81         };
82
83         cds::container::CuckooMap<u32, void*, Traits> m_map;
84
85     public:
86         Map(ureg capacity) : m_map() {
87         }
88
89         void set(u32 key, void* value) {
90             m_map.insert(key, value);
91         }
92
93         void* get(u32 key) {
94             void* result = NULL;
95             m_map.find(key, [&result](std::pair<const u32, void*>& item) { result = item.second; });
96             return result;
97         }
98
99         void erase(u32 key) {
100             m_map.erase(key);
101         }
102     };
103
104     static ureg getInitialCapacity(ureg maxPopulation) {
105         return maxPopulation / 4;
106     }
107 };
108
109 } // namespace extra
110 } // namespace junction
111
112 #endif // JUNCTION_EXTRA_IMPL_MAPADAPTER_CDS_CUCKOO_H