hashes.nim 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements efficient computations of hash values for diverse
  10. ## Nim types. All the procs are based on these two building blocks:
  11. ## - `!& proc <#!&,Hash,int>`_ used to start or mix a hash value, and
  12. ## - `!$ proc <#!$,Hash>`_ used to finish the hash value.
  13. ##
  14. ## If you want to implement hash procs for your custom types,
  15. ## you will end up writing the following kind of skeleton of code:
  16. runnableExamples:
  17. type
  18. Something = object
  19. foo: int
  20. bar: string
  21. iterator items(x: Something): Hash =
  22. yield hash(x.foo)
  23. yield hash(x.bar)
  24. proc hash(x: Something): Hash =
  25. ## Computes a Hash from `x`.
  26. var h: Hash = 0
  27. # Iterate over parts of `x`.
  28. for xAtom in x:
  29. # Mix the atom with the partial hash.
  30. h = h !& xAtom
  31. # Finish the hash.
  32. result = !$h
  33. ## If your custom types contain fields for which there already is a `hash` proc,
  34. ## you can simply hash together the hash values of the individual fields:
  35. runnableExamples:
  36. type
  37. Something = object
  38. foo: int
  39. bar: string
  40. proc hash(x: Something): Hash =
  41. ## Computes a Hash from `x`.
  42. var h: Hash = 0
  43. h = h !& hash(x.foo)
  44. h = h !& hash(x.bar)
  45. result = !$h
  46. ## **Note:** If the type has a `==` operator, the following must hold:
  47. ## If two values compare equal, their hashes must also be equal.
  48. ##
  49. ## See also
  50. ## ========
  51. ## * `md5 module <md5.html>`_ for the MD5 checksum algorithm
  52. ## * `base64 module <base64.html>`_ for a Base64 encoder and decoder
  53. ## * `std/sha1 module <sha1.html>`_ for a SHA-1 encoder and decoder
  54. ## * `tables module <tables.html>`_ for hash tables
  55. import std/private/since
  56. type
  57. Hash* = int ## A hash value. Hash tables using these values should
  58. ## always have a size of a power of two so they can use the `and`
  59. ## operator instead of `mod` for truncation of the hash value.
  60. proc `!&`*(h: Hash, val: int): Hash {.inline.} =
  61. ## Mixes a hash value `h` with `val` to produce a new hash value.
  62. ##
  63. ## This is only needed if you need to implement a `hash` proc for a new datatype.
  64. let h = cast[uint](h)
  65. let val = cast[uint](val)
  66. var res = h + val
  67. res = res + res shl 10
  68. res = res xor (res shr 6)
  69. result = cast[Hash](res)
  70. proc `!$`*(h: Hash): Hash {.inline.} =
  71. ## Finishes the computation of the hash value.
  72. ##
  73. ## This is only needed if you need to implement a `hash` proc for a new datatype.
  74. let h = cast[uint](h) # Hash is practically unsigned.
  75. var res = h + h shl 3
  76. res = res xor (res shr 11)
  77. res = res + res shl 15
  78. result = cast[Hash](res)
  79. proc hiXorLoFallback64(a, b: uint64): uint64 {.inline.} =
  80. let # Fall back in 64-bit arithmetic
  81. aH = a shr 32
  82. aL = a and 0xFFFFFFFF'u64
  83. bH = b shr 32
  84. bL = b and 0xFFFFFFFF'u64
  85. rHH = aH * bH
  86. rHL = aH * bL
  87. rLH = aL * bH
  88. rLL = aL * bL
  89. t = rLL + (rHL shl 32)
  90. var c = if t < rLL: 1'u64 else: 0'u64
  91. let lo = t + (rLH shl 32)
  92. c += (if lo < t: 1'u64 else: 0'u64)
  93. let hi = rHH + (rHL shr 32) + (rLH shr 32) + c
  94. return hi xor lo
  95. proc hiXorLo(a, b: uint64): uint64 {.inline.} =
  96. # XOR of the high & low 8 bytes of the full 16 byte product.
  97. when nimvm:
  98. result = hiXorLoFallback64(a, b) # `result =` is necessary here.
  99. else:
  100. when Hash.sizeof < 8:
  101. result = hiXorLoFallback64(a, b)
  102. elif defined(gcc) or defined(llvm_gcc) or defined(clang):
  103. {.emit: """__uint128_t r = `a`; r *= `b`; `result` = (r >> 64) ^ r;""".}
  104. elif defined(windows) and not defined(tcc):
  105. proc umul128(a, b: uint64, c: ptr uint64): uint64 {.importc: "_umul128", header: "intrin.h".}
  106. var b = b
  107. let c = umul128(a, b, addr b)
  108. result = c xor b
  109. else:
  110. result = hiXorLoFallback64(a, b)
  111. when defined(js):
  112. import std/jsbigints
  113. import std/private/jsutils
  114. proc hiXorLoJs(a, b: JsBigInt): JsBigInt =
  115. let
  116. prod = a * b
  117. mask = big"0xffffffffffffffff" # (big"1" shl big"64") - big"1"
  118. result = (prod shr big"64") xor (prod and mask)
  119. template hashWangYiJS(x: JsBigInt): Hash =
  120. let
  121. P0 = big"0xa0761d6478bd642f"
  122. P1 = big"0xe7037ed1a0b428db"
  123. P58 = big"0xeb44accab455d16d" # big"0xeb44accab455d165" xor big"8"
  124. res = hiXorLoJs(hiXorLoJs(P0, x xor P1), P58)
  125. cast[Hash](toNumber(wrapToInt(res, 32)))
  126. template toBits(num: float): JsBigInt =
  127. let
  128. x = newArrayBuffer(8)
  129. y = newFloat64Array(x)
  130. if hasBigUint64Array():
  131. let z = newBigUint64Array(x)
  132. y[0] = num
  133. z[0]
  134. else:
  135. let z = newUint32Array(x)
  136. y[0] = num
  137. big(z[0]) + big(z[1]) shl big(32)
  138. proc hashWangYi1*(x: int64|uint64|Hash): Hash {.inline.} =
  139. ## Wang Yi's hash_v1 for 64-bit ints (see https://github.com/rurban/smhasher for
  140. ## more details). This passed all scrambling tests in Spring 2019 and is simple.
  141. ##
  142. ## **Note:** It's ok to define `proc(x: int16): Hash = hashWangYi1(Hash(x))`.
  143. const P0 = 0xa0761d6478bd642f'u64
  144. const P1 = 0xe7037ed1a0b428db'u64
  145. const P58 = 0xeb44accab455d165'u64 xor 8'u64
  146. template h(x): untyped = hiXorLo(hiXorLo(P0, uint64(x) xor P1), P58)
  147. when nimvm:
  148. when defined(js): # Nim int64<->JS Number & VM match => JS gets 32-bit hash
  149. result = cast[Hash](h(x)) and cast[Hash](0xFFFFFFFF)
  150. else:
  151. result = cast[Hash](h(x))
  152. else:
  153. when defined(js):
  154. if hasJsBigInt():
  155. result = hashWangYiJS(big(x))
  156. else:
  157. result = cast[Hash](x) and cast[Hash](0xFFFFFFFF)
  158. else:
  159. result = cast[Hash](h(x))
  160. proc hashData*(data: pointer, size: int): Hash =
  161. ## Hashes an array of bytes of size `size`.
  162. var h: Hash = 0
  163. when defined(js):
  164. var p: cstring
  165. asm """`p` = `Data`"""
  166. else:
  167. var p = cast[cstring](data)
  168. var i = 0
  169. var s = size
  170. while s > 0:
  171. h = h !& ord(p[i])
  172. inc(i)
  173. dec(s)
  174. result = !$h
  175. proc hashIdentity*[T: Ordinal|enum](x: T): Hash {.inline, since: (1, 3).} =
  176. ## The identity hash, i.e. `hashIdentity(x) = x`.
  177. cast[Hash](ord(x))
  178. when defined(nimIntHash1):
  179. proc hash*[T: Ordinal|enum](x: T): Hash {.inline.} =
  180. ## Efficient hashing of integers.
  181. cast[Hash](ord(x))
  182. else:
  183. proc hash*[T: Ordinal|enum](x: T): Hash {.inline.} =
  184. ## Efficient hashing of integers.
  185. hashWangYi1(uint64(ord(x)))
  186. when defined(js):
  187. var objectID = 0
  188. proc getObjectId(x: pointer): int =
  189. asm """
  190. if (typeof `x` == "object") {
  191. if ("_NimID" in `x`)
  192. `result` = `x`["_NimID"];
  193. else {
  194. `result` = ++`objectID`;
  195. `x`["_NimID"] = `result`;
  196. }
  197. }
  198. """
  199. proc hash*(x: pointer): Hash {.inline.} =
  200. ## Efficient `hash` overload.
  201. when defined(js):
  202. let y = getObjectId(x)
  203. else:
  204. let y = cast[int](x)
  205. hash(y) # consistent with code expecting scrambled hashes depending on `nimIntHash1`.
  206. proc hash*[T](x: ptr[T]): Hash {.inline.} =
  207. ## Efficient `hash` overload.
  208. runnableExamples:
  209. var a: array[10, uint8]
  210. assert a[0].addr.hash != a[1].addr.hash
  211. assert cast[pointer](a[0].addr).hash == a[0].addr.hash
  212. hash(cast[pointer](x))
  213. when defined(nimEnableHashRef):
  214. proc hash*[T](x: ref[T]): Hash {.inline.} =
  215. ## Efficient `hash` overload.
  216. runnableExamples:
  217. type A = ref object
  218. x: int
  219. let a = A(x: 3)
  220. let ha = a.hash
  221. assert ha != A(x: 3).hash # A(x: 3) is a different ref object from `a`.
  222. a.x = 4
  223. assert ha == a.hash # the hash only depends on the address
  224. runnableExamples:
  225. # you can overload `hash` if you want to customize semantics
  226. type A[T] = ref object
  227. x, y: T
  228. proc hash(a: A): Hash = hash(a.x)
  229. assert A[int](x: 3, y: 4).hash == A[int](x: 3, y: 5).hash
  230. # xxx pending bug #17733, merge as `proc hash*(pointer | ref | ptr): Hash`
  231. # or `proc hash*[T: ref | ptr](x: T): Hash`
  232. hash(cast[pointer](x))
  233. proc hash*(x: float): Hash {.inline.} =
  234. ## Efficient hashing of floats.
  235. let y = x + 0.0 # for denormalization
  236. when nimvm:
  237. # workaround a JS VM bug: bug #16547
  238. result = hashWangYi1(cast[int64](float64(y)))
  239. else:
  240. when not defined(js):
  241. result = hashWangYi1(cast[Hash](y))
  242. else:
  243. result = hashWangYiJS(toBits(y))
  244. # Forward declarations before methods that hash containers. This allows
  245. # containers to contain other containers
  246. proc hash*[A](x: openArray[A]): Hash
  247. proc hash*[A](x: set[A]): Hash
  248. when defined(js):
  249. proc imul(a, b: uint32): uint32 =
  250. # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/imul
  251. let mask = 0xffff'u32
  252. var
  253. aHi = (a shr 16) and mask
  254. aLo = a and mask
  255. bHi = (b shr 16) and mask
  256. bLo = b and mask
  257. result = (aLo * bLo) + (aHi * bLo + aLo * bHi) shl 16
  258. else:
  259. template imul(a, b: uint32): untyped = a * b
  260. proc rotl32(x: uint32, r: int): uint32 {.inline.} =
  261. (x shl r) or (x shr (32 - r))
  262. proc murmurHash(x: openArray[byte]): Hash =
  263. # https://github.com/PeterScott/murmur3/blob/master/murmur3.c
  264. const
  265. c1 = 0xcc9e2d51'u32
  266. c2 = 0x1b873593'u32
  267. n1 = 0xe6546b64'u32
  268. m1 = 0x85ebca6b'u32
  269. m2 = 0xc2b2ae35'u32
  270. let
  271. size = len(x)
  272. stepSize = 4 # 32-bit
  273. n = size div stepSize
  274. var
  275. h1: uint32
  276. i = 0
  277. # body
  278. while i < n * stepSize:
  279. var k1: uint32
  280. when defined(js) or defined(sparc) or defined(sparc64):
  281. var j = stepSize
  282. while j > 0:
  283. dec j
  284. k1 = (k1 shl 8) or (ord(x[i+j])).uint32
  285. else:
  286. k1 = cast[ptr uint32](unsafeAddr x[i])[]
  287. inc i, stepSize
  288. k1 = imul(k1, c1)
  289. k1 = rotl32(k1, 15)
  290. k1 = imul(k1, c2)
  291. h1 = h1 xor k1
  292. h1 = rotl32(h1, 13)
  293. h1 = h1*5 + n1
  294. # tail
  295. var k1: uint32
  296. var rem = size mod stepSize
  297. while rem > 0:
  298. dec rem
  299. k1 = (k1 shl 8) or (ord(x[i+rem])).uint32
  300. k1 = imul(k1, c1)
  301. k1 = rotl32(k1, 15)
  302. k1 = imul(k1, c2)
  303. h1 = h1 xor k1
  304. # finalization
  305. h1 = h1 xor size.uint32
  306. h1 = h1 xor (h1 shr 16)
  307. h1 = imul(h1, m1)
  308. h1 = h1 xor (h1 shr 13)
  309. h1 = imul(h1, m2)
  310. h1 = h1 xor (h1 shr 16)
  311. return cast[Hash](h1)
  312. proc hashVmImpl(x: cstring, sPos, ePos: int): Hash =
  313. doAssert false, "implementation override in compiler/vmops.nim"
  314. proc hashVmImpl(x: string, sPos, ePos: int): Hash =
  315. doAssert false, "implementation override in compiler/vmops.nim"
  316. proc hashVmImplChar(x: openArray[char], sPos, ePos: int): Hash =
  317. doAssert false, "implementation override in compiler/vmops.nim"
  318. proc hashVmImplByte(x: openArray[byte], sPos, ePos: int): Hash =
  319. doAssert false, "implementation override in compiler/vmops.nim"
  320. proc hash*(x: string): Hash =
  321. ## Efficient hashing of strings.
  322. ##
  323. ## **See also:**
  324. ## * `hashIgnoreStyle <#hashIgnoreStyle,string>`_
  325. ## * `hashIgnoreCase <#hashIgnoreCase,string>`_
  326. runnableExamples:
  327. doAssert hash("abracadabra") != hash("AbracadabrA")
  328. when not defined(nimToOpenArrayCString):
  329. result = 0
  330. for c in x:
  331. result = result !& ord(c)
  332. result = !$result
  333. else:
  334. when nimvm:
  335. result = hashVmImpl(x, 0, high(x))
  336. else:
  337. result = murmurHash(toOpenArrayByte(x, 0, high(x)))
  338. proc hash*(x: cstring): Hash =
  339. ## Efficient hashing of null-terminated strings.
  340. runnableExamples:
  341. doAssert hash(cstring"abracadabra") == hash("abracadabra")
  342. doAssert hash(cstring"AbracadabrA") == hash("AbracadabrA")
  343. doAssert hash(cstring"abracadabra") != hash(cstring"AbracadabrA")
  344. when not defined(nimToOpenArrayCString):
  345. result = 0
  346. var i = 0
  347. while x[i] != '\0':
  348. result = result !& ord(x[i])
  349. inc i
  350. result = !$result
  351. else:
  352. when nimvm:
  353. hashVmImpl(x, 0, high(x))
  354. else:
  355. when not defined(js) and defined(nimToOpenArrayCString):
  356. murmurHash(toOpenArrayByte(x, 0, x.high))
  357. else:
  358. let xx = $x
  359. murmurHash(toOpenArrayByte(xx, 0, high(xx)))
  360. proc hash*(sBuf: string, sPos, ePos: int): Hash =
  361. ## Efficient hashing of a string buffer, from starting
  362. ## position `sPos` to ending position `ePos` (included).
  363. ##
  364. ## `hash(myStr, 0, myStr.high)` is equivalent to `hash(myStr)`.
  365. runnableExamples:
  366. var a = "abracadabra"
  367. doAssert hash(a, 0, 3) == hash(a, 7, 10)
  368. when not defined(nimToOpenArrayCString):
  369. result = 0
  370. for i in sPos..ePos:
  371. result = result !& ord(sBuf[i])
  372. result = !$result
  373. else:
  374. murmurHash(toOpenArrayByte(sBuf, sPos, ePos))
  375. proc hashIgnoreStyle*(x: string): Hash =
  376. ## Efficient hashing of strings; style is ignored.
  377. ##
  378. ## **Note:** This uses a different hashing algorithm than `hash(string)`.
  379. ##
  380. ## **See also:**
  381. ## * `hashIgnoreCase <#hashIgnoreCase,string>`_
  382. runnableExamples:
  383. doAssert hashIgnoreStyle("aBr_aCa_dAB_ra") == hashIgnoreStyle("abracadabra")
  384. doAssert hashIgnoreStyle("abcdefghi") != hash("abcdefghi")
  385. var h: Hash = 0
  386. var i = 0
  387. let xLen = x.len
  388. while i < xLen:
  389. var c = x[i]
  390. if c == '_':
  391. inc(i)
  392. else:
  393. if c in {'A'..'Z'}:
  394. c = chr(ord(c) + (ord('a') - ord('A'))) # toLower()
  395. h = h !& ord(c)
  396. inc(i)
  397. result = !$h
  398. proc hashIgnoreStyle*(sBuf: string, sPos, ePos: int): Hash =
  399. ## Efficient hashing of a string buffer, from starting
  400. ## position `sPos` to ending position `ePos` (included); style is ignored.
  401. ##
  402. ## **Note:** This uses a different hashing algorithm than `hash(string)`.
  403. ##
  404. ## `hashIgnoreStyle(myBuf, 0, myBuf.high)` is equivalent
  405. ## to `hashIgnoreStyle(myBuf)`.
  406. runnableExamples:
  407. var a = "ABracada_b_r_a"
  408. doAssert hashIgnoreStyle(a, 0, 3) == hashIgnoreStyle(a, 7, a.high)
  409. var h: Hash = 0
  410. var i = sPos
  411. while i <= ePos:
  412. var c = sBuf[i]
  413. if c == '_':
  414. inc(i)
  415. else:
  416. if c in {'A'..'Z'}:
  417. c = chr(ord(c) + (ord('a') - ord('A'))) # toLower()
  418. h = h !& ord(c)
  419. inc(i)
  420. result = !$h
  421. proc hashIgnoreCase*(x: string): Hash =
  422. ## Efficient hashing of strings; case is ignored.
  423. ##
  424. ## **Note:** This uses a different hashing algorithm than `hash(string)`.
  425. ##
  426. ## **See also:**
  427. ## * `hashIgnoreStyle <#hashIgnoreStyle,string>`_
  428. runnableExamples:
  429. doAssert hashIgnoreCase("ABRAcaDABRA") == hashIgnoreCase("abRACAdabra")
  430. doAssert hashIgnoreCase("abcdefghi") != hash("abcdefghi")
  431. var h: Hash = 0
  432. for i in 0..x.len-1:
  433. var c = x[i]
  434. if c in {'A'..'Z'}:
  435. c = chr(ord(c) + (ord('a') - ord('A'))) # toLower()
  436. h = h !& ord(c)
  437. result = !$h
  438. proc hashIgnoreCase*(sBuf: string, sPos, ePos: int): Hash =
  439. ## Efficient hashing of a string buffer, from starting
  440. ## position `sPos` to ending position `ePos` (included); case is ignored.
  441. ##
  442. ## **Note:** This uses a different hashing algorithm than `hash(string)`.
  443. ##
  444. ## `hashIgnoreCase(myBuf, 0, myBuf.high)` is equivalent
  445. ## to `hashIgnoreCase(myBuf)`.
  446. runnableExamples:
  447. var a = "ABracadabRA"
  448. doAssert hashIgnoreCase(a, 0, 3) == hashIgnoreCase(a, 7, 10)
  449. var h: Hash = 0
  450. for i in sPos..ePos:
  451. var c = sBuf[i]
  452. if c in {'A'..'Z'}:
  453. c = chr(ord(c) + (ord('a') - ord('A'))) # toLower()
  454. h = h !& ord(c)
  455. result = !$h
  456. proc hash*[T: tuple | object | proc](x: T): Hash =
  457. ## Efficient `hash` overload.
  458. runnableExamples:
  459. # for `tuple|object`, `hash` must be defined for each component of `x`.
  460. type Obj = object
  461. x: int
  462. y: string
  463. type Obj2[T] = object
  464. x: int
  465. y: string
  466. assert hash(Obj(x: 520, y: "Nim")) != hash(Obj(x: 520, y: "Nim2"))
  467. # you can define custom hashes for objects (even if they're generic):
  468. proc hash(a: Obj2): Hash = hash((a.x))
  469. assert hash(Obj2[float](x: 520, y: "Nim")) == hash(Obj2[float](x: 520, y: "Nim2"))
  470. runnableExamples:
  471. # proc
  472. proc fn1() = discard
  473. const fn1b = fn1
  474. assert hash(fn1b) == hash(fn1)
  475. # closure
  476. proc outer =
  477. var a = 0
  478. proc fn2() = a.inc
  479. assert fn2 is "closure"
  480. let fn2b = fn2
  481. assert hash(fn2b) == hash(fn2)
  482. assert hash(fn2) != hash(fn1)
  483. outer()
  484. when T is "closure":
  485. result = hash((rawProc(x), rawEnv(x)))
  486. elif T is (proc):
  487. result = hash(pointer(x))
  488. else:
  489. for f in fields(x):
  490. result = result !& hash(f)
  491. result = !$result
  492. proc hash*[A](x: openArray[A]): Hash =
  493. ## Efficient hashing of arrays and sequences.
  494. ## There must be a `hash` proc defined for the element type `A`.
  495. when A is byte:
  496. result = murmurHash(x)
  497. elif A is char:
  498. when nimvm:
  499. result = hashVmImplChar(x, 0, x.high)
  500. else:
  501. result = murmurHash(toOpenArrayByte(x, 0, x.high))
  502. else:
  503. for a in x:
  504. result = result !& hash(a)
  505. result = !$result
  506. proc hash*[A](aBuf: openArray[A], sPos, ePos: int): Hash =
  507. ## Efficient hashing of portions of arrays and sequences, from starting
  508. ## position `sPos` to ending position `ePos` (included).
  509. ## There must be a `hash` proc defined for the element type `A`.
  510. ##
  511. ## `hash(myBuf, 0, myBuf.high)` is equivalent to `hash(myBuf)`.
  512. runnableExamples:
  513. let a = [1, 2, 5, 1, 2, 6]
  514. doAssert hash(a, 0, 1) == hash(a, 3, 4)
  515. when A is byte:
  516. when nimvm:
  517. result = hashVmImplByte(aBuf, sPos, ePos)
  518. else:
  519. result = murmurHash(toOpenArray(aBuf, sPos, ePos))
  520. elif A is char:
  521. when nimvm:
  522. result = hashVmImplChar(aBuf, sPos, ePos)
  523. else:
  524. result = murmurHash(toOpenArrayByte(aBuf, sPos, ePos))
  525. else:
  526. for i in sPos .. ePos:
  527. result = result !& hash(aBuf[i])
  528. result = !$result
  529. proc hash*[A](x: set[A]): Hash =
  530. ## Efficient hashing of sets.
  531. ## There must be a `hash` proc defined for the element type `A`.
  532. for it in items(x):
  533. result = result !& hash(it)
  534. result = !$result