hashes.nim 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 <#!&>`_ used to start or mix a hash value, and
  12. ## - `!$ proc <#!$>`_ used to *finish* the hash value.
  13. ## If you want to implement hash procs for
  14. ## your custom types you will end up writing the following kind of skeleton of
  15. ## code:
  16. ##
  17. ## .. code-block:: Nim
  18. ## proc hash(x: Something): Hash =
  19. ## ## Computes a Hash from `x`.
  20. ## var h: Hash = 0
  21. ## # Iterate over parts of `x`.
  22. ## for xAtom in x:
  23. ## # Mix the atom with the partial hash.
  24. ## h = h !& xAtom
  25. ## # Finish the hash.
  26. ## result = !$h
  27. ##
  28. ## If your custom types contain fields for which there already is a hash proc,
  29. ## like for example objects made up of ``strings``, you can simply hash
  30. ## together the hash value of the individual fields:
  31. ##
  32. ## .. code-block:: Nim
  33. ## proc hash(x: Something): Hash =
  34. ## ## Computes a Hash from `x`.
  35. ## var h: Hash = 0
  36. ## h = h !& hash(x.foo)
  37. ## h = h !& hash(x.bar)
  38. ## result = !$h
  39. import
  40. strutils
  41. type
  42. Hash* = int ## a hash value; hash tables using these values should
  43. ## always have a size of a power of two and can use the ``and``
  44. ## operator instead of ``mod`` for truncation of the hash value.
  45. proc `!&`*(h: Hash, val: int): Hash {.inline.} =
  46. ## mixes a hash value `h` with `val` to produce a new hash value. This is
  47. ## only needed if you need to implement a hash proc for a new datatype.
  48. result = h +% val
  49. result = result +% result shl 10
  50. result = result xor (result shr 6)
  51. proc `!$`*(h: Hash): Hash {.inline.} =
  52. ## finishes the computation of the hash value. This is
  53. ## only needed if you need to implement a hash proc for a new datatype.
  54. result = h +% h shl 3
  55. result = result xor (result shr 11)
  56. result = result +% result shl 15
  57. proc hashData*(data: pointer, size: int): Hash =
  58. ## hashes an array of bytes of size `size`
  59. var h: Hash = 0
  60. when defined(js):
  61. var p: cstring
  62. asm """`p` = `Data`;"""
  63. else:
  64. var p = cast[cstring](data)
  65. var i = 0
  66. var s = size
  67. while s > 0:
  68. h = h !& ord(p[i])
  69. inc(i)
  70. dec(s)
  71. result = !$h
  72. when defined(js):
  73. var objectID = 0
  74. proc hash*(x: pointer): Hash {.inline.} =
  75. ## efficient hashing of pointers
  76. when defined(js):
  77. asm """
  78. if (typeof `x` == "object") {
  79. if ("_NimID" in `x`)
  80. `result` = `x`["_NimID"];
  81. else {
  82. `result` = ++`objectID`;
  83. `x`["_NimID"] = `result`;
  84. }
  85. }
  86. """
  87. else:
  88. result = (cast[Hash](x)) shr 3 # skip the alignment
  89. when not defined(booting):
  90. proc hash*[T: proc](x: T): Hash {.inline.} =
  91. ## efficient hashing of proc vars; closures are supported too.
  92. when T is "closure":
  93. result = hash(rawProc(x)) !& hash(rawEnv(x))
  94. else:
  95. result = hash(pointer(x))
  96. proc hash*(x: int): Hash {.inline.} =
  97. ## efficient hashing of integers
  98. result = x
  99. proc hash*(x: int64): Hash {.inline.} =
  100. ## efficient hashing of int64 integers
  101. result = toU32(x)
  102. proc hash*(x: uint): Hash {.inline.} =
  103. ## efficient hashing of unsigned integers
  104. result = cast[int](x)
  105. proc hash*(x: uint64): Hash {.inline.} =
  106. ## efficient hashing of uint64 integers
  107. result = toU32(cast[int](x))
  108. proc hash*(x: char): Hash {.inline.} =
  109. ## efficient hashing of characters
  110. result = ord(x)
  111. proc hash*[T: Ordinal](x: T): Hash {.inline.} =
  112. ## efficient hashing of other ordinal types (e.g., enums)
  113. result = ord(x)
  114. proc hash*(x: string): Hash =
  115. ## efficient hashing of strings
  116. var h: Hash = 0
  117. for i in 0..x.len-1:
  118. h = h !& ord(x[i])
  119. result = !$h
  120. proc hash*(x: cstring): Hash =
  121. ## efficient hashing of null-terminated strings
  122. var h: Hash = 0
  123. var i = 0
  124. when defined(js):
  125. while i < x.len:
  126. h = h !& ord(x[i])
  127. inc i
  128. else:
  129. while x[i] != 0.char:
  130. h = h !& ord(x[i])
  131. inc i
  132. result = !$h
  133. proc hash*(sBuf: string, sPos, ePos: int): Hash =
  134. ## efficient hashing of a string buffer, from starting
  135. ## position `sPos` to ending position `ePos`
  136. ##
  137. ## ``hash(myStr, 0, myStr.high)`` is equivalent to ``hash(myStr)``
  138. var h: Hash = 0
  139. for i in sPos..ePos:
  140. h = h !& ord(sBuf[i])
  141. result = !$h
  142. proc hashIgnoreStyle*(x: string): Hash =
  143. ## efficient hashing of strings; style is ignored
  144. var h: Hash = 0
  145. var i = 0
  146. let xLen = x.len
  147. while i < xLen:
  148. var c = x[i]
  149. if c == '_':
  150. inc(i)
  151. else:
  152. if c in {'A'..'Z'}:
  153. c = chr(ord(c) + (ord('a') - ord('A'))) # toLower()
  154. h = h !& ord(c)
  155. inc(i)
  156. result = !$h
  157. proc hashIgnoreStyle*(sBuf: string, sPos, ePos: int): Hash =
  158. ## efficient hashing of a string buffer, from starting
  159. ## position `sPos` to ending position `ePos`; style is ignored
  160. ##
  161. ## ``hashIgnoreStyle(myBuf, 0, myBuf.high)`` is equivalent
  162. ## to ``hashIgnoreStyle(myBuf)``
  163. var h: Hash = 0
  164. var i = sPos
  165. while i <= ePos:
  166. var c = sBuf[i]
  167. if c == '_':
  168. inc(i)
  169. else:
  170. if c in {'A'..'Z'}:
  171. c = chr(ord(c) + (ord('a') - ord('A'))) # toLower()
  172. h = h !& ord(c)
  173. inc(i)
  174. result = !$h
  175. proc hashIgnoreCase*(x: string): Hash =
  176. ## efficient hashing of strings; case is ignored
  177. var h: Hash = 0
  178. for i in 0..x.len-1:
  179. var c = x[i]
  180. if c in {'A'..'Z'}:
  181. c = chr(ord(c) + (ord('a') - ord('A'))) # toLower()
  182. h = h !& ord(c)
  183. result = !$h
  184. proc hashIgnoreCase*(sBuf: string, sPos, ePos: int): Hash =
  185. ## efficient hashing of a string buffer, from starting
  186. ## position `sPos` to ending position `ePos`; case is ignored
  187. ##
  188. ## ``hashIgnoreCase(myBuf, 0, myBuf.high)`` is equivalent
  189. ## to ``hashIgnoreCase(myBuf)``
  190. var h: Hash = 0
  191. for i in sPos..ePos:
  192. var c = sBuf[i]
  193. if c in {'A'..'Z'}:
  194. c = chr(ord(c) + (ord('a') - ord('A'))) # toLower()
  195. h = h !& ord(c)
  196. result = !$h
  197. proc hash*(x: float): Hash {.inline.} =
  198. ## efficient hashing of floats.
  199. var y = x + 1.0
  200. result = cast[ptr Hash](addr(y))[]
  201. # Forward declarations before methods that hash containers. This allows
  202. # containers to contain other containers
  203. proc hash*[A](x: openArray[A]): Hash
  204. proc hash*[A](x: set[A]): Hash
  205. proc hash*[T: tuple](x: T): Hash =
  206. ## efficient hashing of tuples.
  207. for f in fields(x):
  208. result = result !& hash(f)
  209. result = !$result
  210. proc hash*[A](x: openArray[A]): Hash =
  211. ## efficient hashing of arrays and sequences.
  212. for it in items(x): result = result !& hash(it)
  213. result = !$result
  214. proc hash*[A](aBuf: openArray[A], sPos, ePos: int): Hash =
  215. ## efficient hashing of portions of arrays and sequences.
  216. ##
  217. ## ``hash(myBuf, 0, myBuf.high)`` is equivalent to ``hash(myBuf)``
  218. for i in sPos..ePos:
  219. result = result !& hash(aBuf[i])
  220. result = !$result
  221. proc hash*[A](x: set[A]): Hash =
  222. ## efficient hashing of sets.
  223. for it in items(x): result = result !& hash(it)
  224. result = !$result
  225. when isMainModule:
  226. doAssert( hash("aa bb aaaa1234") == hash("aa bb aaaa1234", 0, 13) )
  227. doAssert( hash("aa bb aaaa1234") == hash(cstring("aa bb aaaa1234")) )
  228. doAssert( hashIgnoreCase("aa bb aaaa1234") == hash("aa bb aaaa1234") )
  229. doAssert( hashIgnoreStyle("aa bb aaaa1234") == hashIgnoreCase("aa bb aaaa1234") )
  230. let xx = @['H','e','l','l','o']
  231. let ss = "Hello"
  232. doAssert( hash(xx) == hash(ss) )
  233. doAssert( hash(xx) == hash(xx, 0, xx.high) )
  234. doAssert( hash(ss) == hash(ss, 0, ss.high) )