Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit 6f5b747

Browse files
committed
stdlib: add HashMap#includes and HashSet#includes, add tests
works on #88
1 parent f064cff commit 6f5b747

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

dora/stdlib/collections.dora

+64
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,38 @@ impl[K: Hash + Identity + Equals, V] HashMap[K, V] {
624624
false
625625
}
626626

627+
@pub fun includes(key: K): Bool {
628+
assert(self.entries <= self.cap);
629+
630+
if self.entries == 0i64 {
631+
return false;
632+
}
633+
634+
var hash = key.hash();
635+
var idx = hash.toInt64() & (self.cap - 1i64);
636+
637+
while true {
638+
if self
639+
... .isLive(idx) {
640+
let currentKey = self.keys.get(idx);
641+
642+
if currentKey.hash() == hash && currentKey.identicalTo(key) {
643+
return true;
644+
}
645+
idx = (idx + 1i64) & (self.cap - 1i64);
646+
}
647+
... .isDeleted(idx) {
648+
// There might be live entries after a deleted one.
649+
idx = (idx + 1i64) & (self.cap - 1i64);
650+
}
651+
else {
652+
return false;
653+
}
654+
}
655+
656+
false
657+
}
658+
627659
@pub fun get(key: K): Option[V] {
628660
assert(self.entries <= self.cap);
629661

@@ -878,6 +910,38 @@ impl[K: Hash + Identity + Equals] HashSet[K] {
878910
unreachable[Option[K]]()
879911
}
880912

913+
@pub fun includes(key: K): Bool {
914+
assert(self.entries <= self.cap);
915+
916+
if self.entries == 0i64 {
917+
return false;
918+
}
919+
920+
var hash = key.hash();
921+
var idx = hash.toInt64() & (self.cap - 1i64);
922+
923+
while true {
924+
if self
925+
... .isLive(idx) {
926+
let currentKey = self.keys.get(idx);
927+
928+
if currentKey.hash() == hash && currentKey.identicalTo(key) {
929+
return true;
930+
}
931+
idx = (idx + 1i64) & (self.cap - 1i64);
932+
}
933+
... .isDeleted(idx) {
934+
// There might be live entries after a deleted one.
935+
idx = (idx + 1i64) & (self.cap - 1i64);
936+
}
937+
else {
938+
return false;
939+
}
940+
}
941+
942+
false
943+
}
944+
881945
@pub fun contains(key: K): Bool {
882946
assert(self.entries <= self.cap);
883947

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
fun main(): Unit {
2+
float64();
3+
float32();
4+
}
5+
6+
fun float64(): Unit {
7+
let set = std::HashSet[Float64]::new(Float64::infinityPositive(), Float64::infinityNegative());
8+
9+
assert(set.size() == 2);
10+
assert(set.includes(Float64::infinityPositive()));
11+
assert(set.includes(Float64::infinityNegative()));
12+
}
13+
14+
fun float32(): Unit {
15+
let set = std::HashSet[Float32]::new(Float32::infinityPositive(), Float32::infinityNegative());
16+
17+
assert(set.size() == 2);
18+
assert(set.includes(Float32::infinityPositive()));
19+
assert(set.includes(Float32::infinityNegative()));
20+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
fun main(): Unit {
2+
float64();
3+
float32();
4+
}
5+
6+
fun float64(): Unit {
7+
let set = std::HashSet[Float64]::new(Float64::notANumber());
8+
9+
assert(set.size() == 1i64);
10+
assert(set.includes(Float64::notANumber()));
11+
}
12+
13+
fun float32(): Unit {
14+
let set = std::HashSet[Float32]::new(Float32::notANumber());
15+
16+
assert(set.size() == 1i64);
17+
assert(set.includes(Float32::notANumber()));
18+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
fun main(): Unit {
2+
float64();
3+
float32();
4+
}
5+
6+
fun float64(): Unit {
7+
let set = std::HashSet[Float64]::new(0.0);
8+
9+
assert(set.size() == 1i64);
10+
assert(set.includes(0.0));
11+
assert(set.includes(-0.0).not());
12+
}
13+
14+
fun float32(): Unit {
15+
let set = std::HashSet[Float32]::new(0.0f32);
16+
17+
assert(set.size() == 1i64);
18+
assert(set.includes(0.0f32));
19+
assert(set.includes(-0.0f32).not());
20+
}

tests/stdlib/hashset-includes.dora

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fun main(): Unit {
2+
let set = std::HashSet[Int32]::new(1i32, 10'000i32, 7i32);
3+
4+
assert(set.size() == 3);
5+
assert(set.includes(1i32));
6+
assert(set.includes(10'000i32));
7+
assert(set.includes(7i32));
8+
assert(set.includes(0i32).not());
9+
}

0 commit comments

Comments
 (0)