diff options
| author | Eugen Wissner <belka@caraus.de> | 2018-05-17 05:31:14 +0200 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2018-05-17 05:31:14 +0200 |
| commit | c511b97b1be132b526bd5bc9e12ebb888957493d (patch) | |
| tree | 9d2aa0ad30b3674d4b577cf1f26a1b347039ed59 /source/tanya/container/hashtable.d | |
| parent | 385ec19e2fa21c1d59a47bc8ffb8043879f79d63 (diff) | |
| download | tanya-c511b97b1be132b526bd5bc9e12ebb888957493d.tar.gz | |
container.Set and HashTable: Fix constructors
Diffstat (limited to 'source/tanya/container/hashtable.d')
| -rw-r--r-- | source/tanya/container/hashtable.d | 51 |
1 files changed, 43 insertions, 8 deletions
diff --git a/source/tanya/container/hashtable.d b/source/tanya/container/hashtable.d index 951d9ef..b7224fa 100644 --- a/source/tanya/container/hashtable.d +++ b/source/tanya/container/hashtable.d @@ -151,7 +151,7 @@ struct Range(T) * Params: * Key = Key type. * Value = Value type. - * hasher = Hash function for $(D_PARAM K). + * hasher = Hash function for $(D_PARAM Key). */ struct HashTable(Key, Value, alias hasher = hash) if (is(typeof(hasher(Key.init)) == size_t)) @@ -170,6 +170,13 @@ if (is(typeof(hasher(Key.init)) == size_t)) /// ditto alias ConstRange = .Range!(const HashArray); + invariant + { + assert(this.data.lengthIndex < primes.length); + assert(this.data.array.length == 0 + || this.data.array.length == primes[this.data.lengthIndex]); + } + /** * Constructor. * @@ -190,6 +197,13 @@ if (is(typeof(hasher(Key.init)) == size_t)) rehash(n); } + /// + @nogc nothrow pure @safe unittest + { + auto hashTable = HashTable!(string, int)(5); + assert(hashTable.capacity == 7); + } + /// ditto this(shared Allocator allocator) in @@ -198,7 +212,7 @@ if (is(typeof(hasher(Key.init)) == size_t)) } do { - this.data = HashArray(Buckets(allocator)); + this.data = HashArray(allocator); } /** @@ -220,7 +234,7 @@ if (is(typeof(hasher(Key.init)) == size_t)) } do { - this.data = HashArray(Buckets(init.data, allocator)); + this.data = HashArray(init.data, allocator); } /// ditto @@ -232,9 +246,7 @@ if (is(typeof(hasher(Key.init)) == size_t)) } do { - this.data = HashArray(Buckets(move(init.data), allocator)); - this.lengthIndex = init.lengthIndex; - init.lengthIndex = 0; + this.data.move(init.data, allocator); } /** @@ -261,8 +273,7 @@ if (is(typeof(hasher(Key.init)) == size_t)) ref typeof(this) opAssign(S)(S that) @trusted if (is(S == HashTable)) { - swap(this.data, that.data); - swap(this.lengthIndex, that.lengthIndex); + this.data.swap(that.data); return this; } @@ -576,3 +587,27 @@ if (is(typeof(hasher(Key.init)) == size_t)) static assert(is(const HashTable!(string, int))); static assert(isForwardRange!(HashTable!(string, int).Range)); } + +// Constructs by reference +@nogc nothrow pure @safe unittest +{ + auto hashTable1 = HashTable!(string, int)(7); + auto hashTable2 = HashTable!(string, int)(hashTable1); + assert(hashTable1.length == hashTable2.length); + assert(hashTable1.capacity == hashTable2.capacity); +} + +// Constructs by value +@nogc nothrow pure @safe unittest +{ + auto hashTable = HashTable!(string, int)(HashTable!(string, int)(7)); + assert(hashTable.capacity == 7); +} + +// Assigns by value +@nogc nothrow pure @safe unittest +{ + HashTable!(string, int) hashTable; + hashTable = HashTable!(string, int)(7); + assert(hashTable.capacity == 7); +} |
