Skip to content

Commit 64ef163

Browse files
author
Hackerpilot
committed
Support structs for key types in the hashmap/treemap. #71
1 parent c0c4ce6 commit 64ef163

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

src/containers/hashmap.d

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ struct HashMap(K, V, Allocator = Mallocator, alias hashFunction = generateHash!K
252252
foreach (ref const bucket; buckets)
253253
{
254254
foreach (item; bucket)
255-
app.put(item.key);
255+
app.put(cast(K) item.key);
256256
}
257257
return app.data;
258258
}
@@ -437,7 +437,7 @@ private:
437437
}
438438
}
439439
Node* n;
440-
n = buckets[index].insertAnywhere(Node(hash, key, value));
440+
n = buckets[index].insertAnywhere(Node(hash, cast(ContainerStorageType!K) key, value));
441441
if (modifyLength)
442442
_length++;
443443
if (shouldRehash())
@@ -481,7 +481,7 @@ private:
481481
foreach (ref bucket; oldBuckets)
482482
{
483483
foreach (node; bucket)
484-
insert(node.key, node.value, node.hash, false);
484+
insert(cast(K) node.key, node.value, node.hash, false);
485485
typeid(typeof(bucket)).destroy(&bucket);
486486
}
487487
static if (useGC)

src/containers/treemap.d

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ struct TreeMap(K, V, Allocator = Mallocator, alias less = "a < b",
5454
/**
5555
* Inserts or overwrites the given key-value pair.
5656
*/
57-
void insert(const K key, V value) @safe
57+
void insert(const K key, V value) @trusted
5858
{
59-
auto tme = TreeMapElement(key, value);
59+
auto tme = TreeMapElement(cast(ContainerStorageType!K) key, value);
6060
auto r = tree.equalRange(tme);
6161
if (r.empty)
6262
tree.insert(tme, true);
@@ -78,7 +78,7 @@ struct TreeMap(K, V, Allocator = Mallocator, alias less = "a < b",
7878
auto opIndex(this This)(const K key) inout
7979
{
8080
alias CET = ContainerElementType!(This, V);
81-
auto tme = TreeMapElement(key);
81+
auto tme = TreeMapElement(cast(ContainerStorageType!K) key);
8282
return cast(CET) tree.equalRange(tme).front.value;
8383
}
8484

@@ -126,16 +126,16 @@ struct TreeMap(K, V, Allocator = Mallocator, alias less = "a < b",
126126
*/
127127
bool remove(const K key)
128128
{
129-
auto tme = TreeMapElement(key);
129+
auto tme = TreeMapElement(cast(ContainerStorageType!K) key);
130130
return tree.remove(tme);
131131
}
132132

133133
/**
134134
* Returns: true if the mapping contains the given key
135135
*/
136-
bool containsKey(const K key) inout pure nothrow @nogc @safe
136+
bool containsKey(const K key) inout pure nothrow @nogc @trusted
137137
{
138-
auto tme = TreeMapElement(key);
138+
auto tme = TreeMapElement(cast(ContainerStorageType!K) key);
139139
return tree.contains(tme);
140140
}
141141

test/compile_test.d

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,45 @@ private void testContainerDouble(alias Container)()
6969
{
7070
testContainerDoubleVal!(Container)();
7171
testContainerDoubleRef!(Container)();
72+
testContainerDoubleAggregateKey!(Container)();
73+
}
74+
75+
private void testContainerDoubleAggregateKey(alias Container)()
76+
{
77+
static struct KeyType
78+
{
79+
int a;
80+
string[] c;
81+
82+
int opCmp(ref const KeyType other) const
83+
{
84+
if (other.a < a)
85+
return -1;
86+
return other.a > a;
87+
}
88+
89+
size_t toHash() const
90+
{
91+
return 10;
92+
}
93+
94+
bool opEquals(ref const KeyType other) const
95+
{
96+
return a == other.a;
97+
}
98+
}
99+
100+
Container!(const KeyType, int) cm;
101+
102+
Container!(immutable KeyType, int) im;
103+
104+
checkIndexFunctionality!(int, const KeyType)(cm);
105+
106+
checkIndexFunctionality!(int, const KeyType)(im);
107+
108+
checkSliceFunctionality!(int)(cm);
109+
110+
checkSliceFunctionality!(int)(im);
72111
}
73112

74113
private void testContainerDoubleVal(alias Container)()

0 commit comments

Comments
 (0)