1 /*------------------------------------------------------------------------
2 Junction: Concurrent data structures in C++
3 Copyright (c) 2016 Jeff Preshing
5 Distributed under the Simplified BSD License.
6 Original location: https://github.com/preshing/junction
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 ------------------------------------------------------------------------*/
13 #ifndef JUNCTION_EXTRA_IMPL_MAPADAPTER_CDS_CUCKOO_H
14 #define JUNCTION_EXTRA_IMPL_MAPADAPTER_CDS_CUCKOO_H
16 #include <junction/Core.h>
18 #if !JUNCTION_WITH_CDS
19 #error "You must configure with JUNCTION_WITH_CDS=1!"
22 #include <junction/MapTraits.h>
24 #include <cds/gc/hp.h>
25 #include <memory.h> // memcpy required by cuckoo_map.h
26 #include <cds/container/cuckoo_map.h>
33 static TURF_CONSTEXPR const char* MapName = "CDS CuckooMap";
39 m_hpGC = new cds::gc::HP;
49 ThreadContext(MapAdapter&, ureg) {
52 void registerThread() {
53 cds::threading::Manager::attachThread();
56 void unregisterThread() {
57 cds::threading::Manager::detachThread();
67 size_t operator()(u32 s) const {
68 return junction::hash(s);
73 size_t operator()(u32 s) const {
74 return junction::hash(s + 0x9e3779b9);
78 struct Traits : cds::container::cuckoo::traits {
79 typedef std::equal_to<u32> equal_to;
80 typedef cds::opt::hash_tuple<Hash1, Hash2> hash;
83 cds::container::CuckooMap<u32, void*, Traits> m_map;
86 Map(ureg capacity) : m_map() {
89 void insert(u32 key, void* value) {
90 m_map.insert(key, value);
95 m_map.find(key, [&result](std::pair<const u32, void*>& item){ result = item.second; });
104 static ureg getInitialCapacity(ureg maxPopulation) {
105 return maxPopulation / 4;
110 } // namespace junction
112 #endif // JUNCTION_EXTRA_IMPL_MAPADAPTER_CDS_CUCKOO_H