Skip to content

Commit

Permalink
Merge pull request #2486 from endragor/rational-hash
Browse files Browse the repository at this point in the history
Add hash proc for Rational
  • Loading branch information
Araq committed Apr 8, 2015
2 parents a4bbcf5 + 46e6fd4 commit dfc3f9d
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/pure/rationals.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
## a denominator `den`, both of type int. The denominator can not be 0.

import math
import hashes

type Rational*[T] = object
## a rational number, consisting of a numerator and denominator
Expand Down Expand Up @@ -205,6 +206,17 @@ proc abs*[T](x: Rational[T]): Rational[T] =
result.num = abs x.num
result.den = abs x.den

proc hash*[T](x: Rational[T]): THash =
## Computes hash for rational `x`
# reduce first so that hash(x) == hash(y) for x == y
var copy = x
reduce(copy)

var h: THash = 0
h = h !& hash(copy.num)
h = h !& hash(copy.den)
result = !$h

when isMainModule:
var
z = Rational[int](num: 0, den: 1)
Expand Down Expand Up @@ -242,11 +254,13 @@ when isMainModule:
assert( not(o > o) )
assert( cmp(o, o) == 0 )
assert( cmp(z, z) == 0 )
assert( hash(o) == hash(o) )

assert( a == b )
assert( a >= b )
assert( not(b > a) )
assert( cmp(a, b) == 0 )
assert( hash(a) == hash(b) )

var x = 1//3

Expand Down

0 comments on commit dfc3f9d

Please sign in to comment.