range: implements `hash` method on ranges
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 25 Nov 2014 21:09:40 +0000 (16:09 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Tue, 25 Nov 2014 21:12:18 +0000 (16:12 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/standard/collection/range.nit

index 665b263..61b5194 100644 (file)
@@ -109,6 +109,15 @@ class Range[E: Discrete]
        redef fun ==(o) do
                return o isa Range[E] and self.first == o.first and self.last == o.last
        end
+
+       #     var a = new Range[Int](10, 15)
+       #     assert a.hash == 455
+       #     var b = new Range[Int].without_last(10, 15)
+       #     assert b.hash == 432
+       redef fun hash do
+               # 11 and 23 are magic numbers empirically determined to be not so bad.
+               return first.hash * 11 + last.hash * 23
+       end
 end
 
 private class IteratorRange[E: Discrete]