Skip to content

Commit 3cc444f

Browse files
Emmanuel NyarkoEmmanuel Nyarko
authored andcommitted
Hashtable module for unordered_map
1 parent 2754025 commit 3cc444f

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

extras/test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <string>
1212
#include <vector>
1313
#include <set>
14+
#include <unordered_map>
1415

1516
namespace stdcpp {
1617
namespace test {
@@ -37,3 +38,6 @@ template std::size_t stdcpp::test::cppSizeOf<std::vector<int> >();
3738

3839
template class std::set<int>;
3940
template std::size_t stdcpp::test::cppSizeOf<std::set<int> >();
41+
42+
template class std::unordered_map<int, char>;
43+
template std::size_t stdcpp::test::cppSizeOf<std::unordered_map<int, char> >();

source/stdcpp/Hashtable.d

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* D header file for C++ Hashtable for unordered_map.
3+
*
4+
* Copyright: Copyright (c) 2018 D Language Foundation
5+
* License: Distributed under the
6+
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
7+
* (See accompanying file LICENSE)
8+
* Authors: Emmanuel Nyarko
9+
*
10+
*/
11+
module stdcpp.Hashtable;
12+
13+
import stdcpp.allocator;
14+
import stdcpp.utility : pair;
15+
import stdcpp.xutility : StdNamespace;
16+
17+
extern(C++, (StdNamespace)):
18+
19+
version (CppRuntime_Gcc)
20+
{
21+
/**
22+
* structs for hashtable template arguments to get the right mangling
23+
*/
24+
struct hash(T)
25+
{
26+
}
27+
28+
struct equal_to(T = void)
29+
{
30+
}
31+
32+
extern(C++, "__detail") struct _Select1st
33+
{
34+
}
35+
36+
extern(C++, "__detail") struct _Mod_range_hashing
37+
{
38+
}
39+
40+
extern(C++, "__detail") struct _Default_ranged_hash
41+
{
42+
}
43+
44+
extern(C++, "__detail") struct _Prime_rehash_policy
45+
{
46+
}
47+
48+
extern(C++, "__detail") struct _Hashtable_traits(bool _Cache_hash_code, bool _Constant_iterators, bool _Unique_keys)
49+
{
50+
import stdcpp.type_traits : bool_constant;
51+
alias __hash_cached = bool_constant!(_Cache_hash_code);
52+
}
53+
54+
extern(C++, "__detail") struct _Hashtable_alloc(_NodeAlloc)
55+
{
56+
alias __node_base = _Hash_node_base;
57+
alias __node_base_ptr = __node_base*;
58+
alias __buckets_ptr = __node_base_ptr*;
59+
}
60+
61+
extern(C++, "__detail") struct _Hash_node_base
62+
{
63+
_Hash_node_base* _M_next;
64+
}
65+
66+
extern(C++, "__detail") struct _Hash_node(_Value, bool _Cache_hash_code)
67+
{
68+
_Hash_node_base __hb;
69+
}
70+
71+
alias __umap_traits(bool _cache) = _Hashtable_traits!(_cache, false, true);
72+
73+
alias __umap_hashtable(_Key, _Tp, _Hash, _Pred, Alloc, _Tr = __umap_traits!(false)) = _Hashtable!(_Key, pair!(const(_Key), _Tp), Alloc, _Select1st, _Pred, _Hash,
74+
_Mod_range_hashing, _Default_ranged_hash, _Prime_rehash_policy, _Tr );
75+
extern(C++, class) struct _Hashtable(_Key, _Value, _Alloc, _ExtractKey, _Equal,
76+
_Hash, _RangeHash, _Unused, _RehashPolicy, _Traits)
77+
{
78+
alias size_type = size_t;
79+
alias __traits_type = _Traits;
80+
alias __hash_cached = __traits_type.__hash_cached;
81+
alias __node_type = _Hash_node!(_Value, __hash_cached.value);
82+
alias __node_alloc_type = allocator_traits!(_Alloc).rebind_alloc!(__node_type);
83+
alias __hashtable_alloc = _Hashtable_alloc!(__node_alloc_type);
84+
alias __node_base = __hashtable_alloc.__node_base;
85+
alias __buckets_ptr = __hashtable_alloc.__buckets_ptr;
86+
alias __node_base_ptr = __hashtable_alloc.__node_base_ptr;
87+
88+
private:
89+
90+
__buckets_ptr _M_buckets;
91+
size_type _M_bucket_count = 1;
92+
__node_base _M_before_begin;
93+
size_type _M_element_count = 0;
94+
_RehashPolicy _M_rehash_policy;
95+
__node_base_ptr _M_single_bucket;
96+
void* _M_void;
97+
}
98+
}

source/stdcpp/unordered_map.d

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,7 @@ extern(C++, class) struct unordered_map(Key, value, Hash, KeyEqual, Alloc)
101101
private _Hashtable _M_h;
102102
}
103103
}
104+
else version(CppRuntime_Microsoft)
105+
{
106+
static assert(0, "CpppRuntime not yet supported");
107+
}

0 commit comments

Comments
 (0)