Skip to content

Commit 21d6e3e

Browse files
author
Enrico Granata
committed
Add support for hashing to Range type
1 parent 4fb9344 commit 21d6e3e

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

lib/aria/range/range.aria

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@ struct RangeImpl {
8989
func prettyprint() {
9090
return "Range from={0} to={1}".format(this.from, this.to);
9191
}
92+
93+
func hash() {
94+
val h = this.from ^ (this.to + 0xe3779b97f4a7c15);
95+
h = h ^ (h >> 30);
96+
h = h * 0xf58476d1ce4e5b9;
97+
h = h ^ (h >> 27);
98+
h = h * 0x4d049bb133111eb;
99+
h = h ^ (h >> 31);
100+
return h;
101+
}
102+
103+
operator == (other: RangeImpl) {
104+
return this.from == other.from && this.to == other.to;
105+
}
92106
}
93107

94108
struct RangeFrom {

tests/range_hash.aria

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
import Range from aria.range.range;
3+
4+
func main() {
5+
val r1 = Range.from(5).to(10);
6+
val r2 = Range.from(6).to(10);
7+
val r3 = Range.from(5).to(11);
8+
9+
assert r1.hash() != r2.hash();
10+
assert r1.hash() != r3.hash();
11+
assert r2.hash() != r3.hash();
12+
13+
assert r1.hash() == r1.hash();
14+
assert r2.hash() == r2.hash();
15+
assert r3.hash() == r3.hash();
16+
17+
val r4 = Range.from(6).to(10);
18+
19+
assert r4.hash() == r2.hash();
20+
assert r2 == r4;
21+
}

0 commit comments

Comments
 (0)