equality and hashing for closures

This commit is contained in:
Araq
2012-07-17 17:26:02 +02:00
parent 19e57bf70d
commit 832da8a303
7 changed files with 31 additions and 11 deletions

View File

@@ -67,6 +67,13 @@ proc hash*(x: Pointer): THash {.inline.} =
else:
result = (cast[THash](x)) shr 3 # skip the alignment
proc hash*[T: proc](x: T): THash {.inline.} =
## efficient hashing of proc vars; closures are supported too.
when T is "closure":
result = hash(rawProc(x)) !& hash(rawEnv(x))
else:
result = hash(pointer(x))
proc hash*(x: int): THash {.inline.} =
## efficient hashing of integers
result = x

View File

@@ -1348,6 +1348,7 @@ proc isNil*(x: string): bool {.noSideEffect, magic: "IsNil".}
proc isNil*[T](x: ptr T): bool {.noSideEffect, magic: "IsNil".}
proc isNil*(x: pointer): bool {.noSideEffect, magic: "IsNil".}
proc isNil*(x: cstring): bool {.noSideEffect, magic: "IsNil".}
proc isNil*[T: proc](x: T): bool {.noSideEffect, magic: "IsNil".}
## Fast check whether `x` is nil. This is sometimes more efficient than
## ``== nil``.