Skip to content

Commit

Permalink
Support structs for key types in the hashmap/treemap. #71
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackerpilot committed Jun 18, 2018
1 parent c0c4ce6 commit 64ef163
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/containers/hashmap.d
Expand Up @@ -252,7 +252,7 @@ struct HashMap(K, V, Allocator = Mallocator, alias hashFunction = generateHash!K
foreach (ref const bucket; buckets)
{
foreach (item; bucket)
app.put(item.key);
app.put(cast(K) item.key);
}
return app.data;
}
Expand Down Expand Up @@ -437,7 +437,7 @@ private:
}
}
Node* n;
n = buckets[index].insertAnywhere(Node(hash, key, value));
n = buckets[index].insertAnywhere(Node(hash, cast(ContainerStorageType!K) key, value));
if (modifyLength)
_length++;
if (shouldRehash())
Expand Down Expand Up @@ -481,7 +481,7 @@ private:
foreach (ref bucket; oldBuckets)
{
foreach (node; bucket)
insert(node.key, node.value, node.hash, false);
insert(cast(K) node.key, node.value, node.hash, false);
typeid(typeof(bucket)).destroy(&bucket);
}
static if (useGC)
Expand Down
12 changes: 6 additions & 6 deletions src/containers/treemap.d
Expand Up @@ -54,9 +54,9 @@ struct TreeMap(K, V, Allocator = Mallocator, alias less = "a < b",
/**
* Inserts or overwrites the given key-value pair.
*/
void insert(const K key, V value) @safe
void insert(const K key, V value) @trusted
{
auto tme = TreeMapElement(key, value);
auto tme = TreeMapElement(cast(ContainerStorageType!K) key, value);
auto r = tree.equalRange(tme);
if (r.empty)
tree.insert(tme, true);
Expand All @@ -78,7 +78,7 @@ struct TreeMap(K, V, Allocator = Mallocator, alias less = "a < b",
auto opIndex(this This)(const K key) inout
{
alias CET = ContainerElementType!(This, V);
auto tme = TreeMapElement(key);
auto tme = TreeMapElement(cast(ContainerStorageType!K) key);
return cast(CET) tree.equalRange(tme).front.value;
}

Expand Down Expand Up @@ -126,16 +126,16 @@ struct TreeMap(K, V, Allocator = Mallocator, alias less = "a < b",
*/
bool remove(const K key)
{
auto tme = TreeMapElement(key);
auto tme = TreeMapElement(cast(ContainerStorageType!K) key);
return tree.remove(tme);
}

/**
* Returns: true if the mapping contains the given key
*/
bool containsKey(const K key) inout pure nothrow @nogc @safe
bool containsKey(const K key) inout pure nothrow @nogc @trusted
{
auto tme = TreeMapElement(key);
auto tme = TreeMapElement(cast(ContainerStorageType!K) key);
return tree.contains(tme);
}

Expand Down
39 changes: 39 additions & 0 deletions test/compile_test.d
Expand Up @@ -69,6 +69,45 @@ private void testContainerDouble(alias Container)()
{
testContainerDoubleVal!(Container)();
testContainerDoubleRef!(Container)();
testContainerDoubleAggregateKey!(Container)();
}

private void testContainerDoubleAggregateKey(alias Container)()
{
static struct KeyType
{
int a;
string[] c;

int opCmp(ref const KeyType other) const
{
if (other.a < a)
return -1;
return other.a > a;
}

size_t toHash() const
{
return 10;
}

bool opEquals(ref const KeyType other) const
{
return a == other.a;
}
}

Container!(const KeyType, int) cm;

Container!(immutable KeyType, int) im;

checkIndexFunctionality!(int, const KeyType)(cm);

checkIndexFunctionality!(int, const KeyType)(im);

checkSliceFunctionality!(int)(cm);

checkSliceFunctionality!(int)(im);
}

private void testContainerDoubleVal(alias Container)()
Expand Down

0 comments on commit 64ef163

Please sign in to comment.