Changes libcds library path
[junction.git] / junction / extra / impl / MapAdapter_Tervel.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_TERVEL_H
14 #define JUNCTION_EXTRA_IMPL_MAPADAPTER_TERVEL_H
15
16 #include <junction/Core.h>
17
18 #if !JUNCTION_WITH_TERVEL
19 #error "You must configure with JUNCTION_WITH_TERVEL=1!"
20 #endif
21
22 #include <tervel/util/tervel.h>
23 #include <tervel/containers/wf/hash-map/wf_hash_map.h>
24
25 namespace junction {
26 namespace extra {
27
28 class MapAdapter {
29 public:
30     static TURF_CONSTEXPR const char* getMapName() { return "Tervel HashMap"; }
31
32     tervel::Tervel m_tervel;
33
34     MapAdapter(ureg numThreads) : m_tervel(numThreads) {
35     }
36
37     class ThreadContext {
38     public:
39         MapAdapter& m_adapter;
40         tervel::ThreadContext* m_context;
41
42         ThreadContext(MapAdapter& adapter, ureg) : m_adapter(adapter), m_context(NULL) {
43         }
44
45         void registerThread() {
46             m_context = new tervel::ThreadContext(&m_adapter.m_tervel);
47         }
48
49         void unregisterThread() {
50             delete m_context;
51         }
52
53         void update() {
54         }
55     };
56
57     class Map {
58     private:
59         tervel::containers::wf::HashMap<u64, u64> m_map;
60
61     public:
62         Map(ureg capacity) : m_map(capacity, 3) {
63         }
64
65         void assign(u32 key, void* value) {
66             m_map.insert(key, (u64) value);
67         }
68
69         void* get(u32 key) {
70             typename tervel::containers::wf::HashMap<u64, u64>::ValueAccessor va;
71             if (m_map.at(key, va))
72                 return (void*) *va.value();
73             else
74                 return NULL;
75         }
76
77         void erase(u32 key) {
78             m_map.remove(key);
79         }
80     };
81
82     static ureg getInitialCapacity(ureg maxPopulation) {
83         return maxPopulation / 4;
84     }
85 };
86
87 } // namespace extra
88 } // namespace junction
89
90 #endif // JUNCTION_EXTRA_IMPL_MAPADAPTER_TERVEL_H