-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid copy of key for hash table bucket lookup (#783)
- Loading branch information
1 parent
0962e4f
commit 6ef07d2
Showing
9 changed files
with
128 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
38 changes: 38 additions & 0 deletions
38
test/test-files/std/data/unordered-map-complex-key/source.spice
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import "std/data/unordered-map"; | ||
|
||
f<int> main() { | ||
UnorderedMap<String, int> map = UnorderedMap<String, int>(); | ||
assert map.getSize() == 0; | ||
assert map.isEmpty(); | ||
map.upsert(String("one"), 1); | ||
map.upsert(String("two"), 2); | ||
map.upsert(String("three"), 3); | ||
map.upsert(String("four"), 4); | ||
assert map.getSize() == 4; | ||
assert !map.isEmpty(); | ||
assert map.contains(String("one")); | ||
assert map.contains(String("two")); | ||
assert map.contains(String("three")); | ||
assert map.contains(String("four")); | ||
assert !map.contains(String("five")); | ||
assert map.get(String("one")) == 1; | ||
assert map[String("two")] == 2; | ||
assert map.get(String("three")) == 3; | ||
assert map.get(String("four")) == 4; | ||
const Result<int> item5 = map.getSafe(String("five")); | ||
assert item5.isErr(); | ||
map.remove(String("two")); | ||
assert !map.contains(String("two")); | ||
assert map.getSize() == 3; | ||
map.clear(); | ||
assert map.getSize() == 0; | ||
assert map.isEmpty(); | ||
map.upsert(String("one"), 1); | ||
map.upsert(String("one"), 11); | ||
assert map.getSize() == 1; | ||
assert map[String("one")] == 11; | ||
map.remove(String("one")); | ||
assert map.isEmpty(); | ||
|
||
printf("All assertions passed!\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
All assertions passed! |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
Hash (double): 4638355772470722560 | ||
Hash (int): 123 | ||
Hash (long): 123 | ||
Hash (short): 123 | ||
Hash (char): 0 | ||
Hash (byte): 0 | ||
Hash (string): -1763540338 | ||
Hash (double): 0 | ||
Hash (long): 123 | ||
Hash (char): 123 | ||
Hash (byte): 123 | ||
Hash (string): 5904905660241445518 | ||
Hash (String): 5904905660241445518 | ||
Hash (HashableType): 124 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,24 @@ | ||
import "std/math/hash"; | ||
|
||
type HashableType struct : IHashable { | ||
unsigned long content = 124l | ||
} | ||
|
||
f<Hash> HashableType.hash() { | ||
return this.content; | ||
} | ||
|
||
f<int> main() { | ||
// Trivial hashes | ||
printf("Hash (int): %d\n", hash(123)); | ||
printf("Hash (long): %d\n", hash(123l)); | ||
printf("Hash (short): %d\n", hash(123s)); | ||
printf("Hash (char): %d\n", hash(123.0)); | ||
printf("Hash (byte): %d\n", hash(123.0)); | ||
printf("Hash (string): %d\n", hash("Hello, World!")); | ||
// Complex hashes | ||
printf("Hash (double): %d\n", hash(123.0)); | ||
// Commonly used hash functions | ||
printf("Hash (double): %lu\n", hash(123.0)); | ||
printf("Hash (int): %lu\n", hash(123)); | ||
printf("Hash (short): %lu\n", hash(123s)); | ||
printf("Hash (long): %lu\n", hash(123l)); | ||
printf("Hash (char): %lu\n", hash(cast<char>(123))); | ||
printf("Hash (byte): %lu\n", hash(cast<byte>(123))); | ||
printf("Hash (string): %lu\n", hash("Hello, World!")); | ||
printf("Hash (String): %lu\n", hash(String("Hello, World!"))); | ||
// Custom hash implementation | ||
const HashableType ht; | ||
printf("Hash (HashableType): %lu\n", hash(ht)); | ||
} |