thashsets.nim 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. import sets, hashes, algorithm
  2. block setEquality:
  3. var
  4. a = initHashSet[int]()
  5. b = initHashSet[int]()
  6. c = initHashSet[string]()
  7. for i in 0..5: a.incl(i)
  8. for i in 1..6: b.incl(i)
  9. for i in 0..5: c.incl($i)
  10. doAssert map(a, proc(x: int): int = x + 1) == b
  11. doAssert map(a, proc(x: int): string = $x) == c
  12. block setsContainingTuples:
  13. var set = initHashSet[tuple[i: int, i64: int64, f: float]]()
  14. set.incl( (i: 123, i64: 123'i64, f: 3.14) )
  15. doAssert set.contains( (i: 123, i64: 123'i64, f: 3.14) )
  16. doAssert( not set.contains( (i: 456, i64: 789'i64, f: 2.78) ) )
  17. block setWithTuplesWithSeqs:
  18. var s = initHashSet[tuple[s: seq[int]]]()
  19. s.incl( (s: @[1, 2, 3]) )
  20. doAssert s.contains( (s: @[1, 2, 3]) )
  21. doAssert( not s.contains((s: @[4, 5, 6])) )
  22. block setWithSequences:
  23. var s = initHashSet[seq[int]]()
  24. s.incl( @[1, 2, 3] )
  25. doAssert s.contains(@[1, 2, 3])
  26. doAssert( not s.contains(@[4, 5, 6]) )
  27. block setClearWorked:
  28. var s = initHashSet[char]()
  29. for c in "this is a test":
  30. s.incl(c)
  31. doAssert len(s) == 7
  32. clear(s)
  33. doAssert len(s) == 0
  34. s.incl('z')
  35. for c in "this is a test":
  36. s.incl(c)
  37. doAssert len(s) == 8
  38. block orderedSetClearWorked:
  39. var s = initOrderedSet[char]()
  40. for c in "eat at joes":
  41. s.incl(c)
  42. var r = ""
  43. for c in items(s):
  44. add(r, c)
  45. doAssert r == "eat jos"
  46. clear(s)
  47. s.incl('z')
  48. for c in "eat at joes":
  49. s.incl(c)
  50. r = ""
  51. for c in items(s):
  52. add(r, c)
  53. doAssert r == "zeat jos"
  54. block hashForHashedSet:
  55. let
  56. seq1 = "This is the test."
  57. seq2 = "the test is This."
  58. s1 = seq1.toHashSet()
  59. s2 = seq2.toHashSet()
  60. doAssert s1 == s2
  61. doAssert hash(s1) == hash(s2)
  62. block hashForOrderdSet:
  63. let
  64. str = "This is the test."
  65. rstr = str.reversed
  66. var
  67. s1 = initOrderedSet[char]()
  68. s2 = initOrderedSet[char]()
  69. r = initOrderedSet[char]()
  70. expected: Hash
  71. added: seq[char] = @[]
  72. reversed: Hash
  73. radded: seq[char] = @[]
  74. expected = 0
  75. for c in str:
  76. if (not (c in added)):
  77. expected = expected !& hash(c)
  78. added.add(c)
  79. s1.incl(c)
  80. s2.incl(c)
  81. expected = !$expected
  82. doAssert hash(s1) == expected
  83. doAssert hash(s1) == hash(s2)
  84. doAssert hash(s1) != hash(r)
  85. reversed = 0
  86. for c in rstr:
  87. if (not (c in radded)):
  88. reversed = reversed !& hash(c)
  89. radded.add(c)
  90. r.incl(c)
  91. reversed = !$reversed
  92. doAssert hash(r) == reversed
  93. doAssert hash(s1) != reversed
  94. proc testModule() =
  95. ## Internal micro test to validate docstrings and such.
  96. block lenTest:
  97. var values: HashSet[int]
  98. doAssert values.len == 0
  99. doAssert values.card == 0
  100. block setIterator:
  101. type pair = tuple[a, b: int]
  102. var a, b = initHashSet[pair]()
  103. a.incl((2, 3))
  104. a.incl((3, 2))
  105. a.incl((2, 3))
  106. for x, y in a.items:
  107. b.incl((x - 2, y + 1))
  108. doAssert a.len == b.card
  109. doAssert a.len == 2
  110. #echo b
  111. block setContains:
  112. var values = initHashSet[int]()
  113. doAssert(not values.contains(2))
  114. values.incl(2)
  115. doAssert values.contains(2)
  116. values.excl(2)
  117. doAssert(not values.contains(2))
  118. values.incl(4)
  119. var others = toHashSet([6, 7])
  120. values.incl(others)
  121. doAssert values.len == 3
  122. values.init
  123. doAssert values.containsOrIncl(2) == false
  124. doAssert values.containsOrIncl(2) == true
  125. var
  126. a = toHashSet([1, 2])
  127. b = toHashSet([1])
  128. b.incl(2)
  129. doAssert a == b
  130. block exclusions:
  131. var s = toHashSet([2, 3, 6, 7])
  132. s.excl(2)
  133. s.excl(2)
  134. doAssert s.len == 3
  135. var
  136. numbers = toHashSet([1, 2, 3, 4, 5])
  137. even = toHashSet([2, 4, 6, 8])
  138. numbers.excl(even)
  139. #echo numbers
  140. # --> {1, 3, 5}
  141. block toSeqAndString:
  142. var a = toHashSet([2, 7, 5])
  143. var b = initHashSet[int](a.len)
  144. for x in [2, 7, 5]: b.incl(x)
  145. doAssert($a == $b)
  146. #echo a
  147. #echo toHashSet(["no", "esc'aping", "is \" provided"])
  148. #block orderedToSeqAndString:
  149. # echo toOrderedSet([2, 4, 5])
  150. # echo toOrderedSet(["no", "esc'aping", "is \" provided"])
  151. block setOperations:
  152. var
  153. a = toHashSet(["a", "b"])
  154. b = toHashSet(["b", "c"])
  155. c = union(a, b)
  156. doAssert c == toHashSet(["a", "b", "c"])
  157. var d = intersection(a, b)
  158. doAssert d == toHashSet(["b"])
  159. var e = difference(a, b)
  160. doAssert e == toHashSet(["a"])
  161. var f = symmetricDifference(a, b)
  162. doAssert f == toHashSet(["a", "c"])
  163. doAssert d < a and d < b
  164. doAssert((a < a) == false)
  165. doAssert d <= a and d <= b
  166. doAssert((a <= a))
  167. # Alias test.
  168. doAssert a + b == toHashSet(["a", "b", "c"])
  169. doAssert a * b == toHashSet(["b"])
  170. doAssert a - b == toHashSet(["a"])
  171. doAssert a -+- b == toHashSet(["a", "c"])
  172. doAssert disjoint(a, b) == false
  173. doAssert disjoint(a, b - a) == true
  174. block mapSet:
  175. var a = toHashSet([1, 2, 3])
  176. var b = a.map(proc (x: int): string = $x)
  177. doAssert b == toHashSet(["1", "2", "3"])
  178. block lenTest:
  179. var values: OrderedSet[int]
  180. doAssert values.len == 0
  181. doAssert values.card == 0
  182. block setIterator:
  183. type pair = tuple[a, b: int]
  184. var a, b = initOrderedSet[pair]()
  185. a.incl((2, 3))
  186. a.incl((3, 2))
  187. a.incl((2, 3))
  188. for x, y in a.items:
  189. b.incl((x - 2, y + 1))
  190. doAssert a.len == b.card
  191. doAssert a.len == 2
  192. block setPairsIterator:
  193. var s = toOrderedSet([1, 3, 5, 7])
  194. var items = newSeq[tuple[a: int, b: int]]()
  195. for idx, item in s: items.add((idx, item))
  196. doAssert items == @[(0, 1), (1, 3), (2, 5), (3, 7)]
  197. block exclusions:
  198. var s = toOrderedSet([1, 2, 3, 6, 7, 4])
  199. s.excl(3)
  200. s.excl(3)
  201. s.excl(1)
  202. s.excl(4)
  203. var items = newSeq[int]()
  204. for item in s: items.add item
  205. doAssert items == @[2, 6, 7]
  206. block: #9005
  207. var s = initOrderedSet[(int, int)]()
  208. for i in 0 .. 30: incl(s, (i, 0))
  209. for i in 0 .. 30: excl(s, (i, 0))
  210. doAssert s.len == 0
  211. #block orderedSetIterator:
  212. # var a = initOrderedSet[int]()
  213. # for value in [9, 2, 1, 5, 1, 8, 4, 2]:
  214. # a.incl(value)
  215. # for value in a.items:
  216. # echo "Got ", value
  217. block setContains:
  218. var values = initOrderedSet[int]()
  219. doAssert(not values.contains(2))
  220. values.incl(2)
  221. doAssert values.contains(2)
  222. block toSeqAndString:
  223. var a = toOrderedSet([2, 4, 5])
  224. var b = initOrderedSet[int]()
  225. for x in [2, 4, 5]: b.incl(x)
  226. doAssert($a == $b)
  227. doAssert(a == b) # https://github.com/Araq/Nim/issues/1413
  228. block initBlocks:
  229. var a: OrderedSet[int]
  230. a.init(4)
  231. a.incl(2)
  232. a.init
  233. doAssert a.len == 0
  234. a = initOrderedSet[int](4)
  235. a.incl(2)
  236. doAssert a.len == 1
  237. var b: HashSet[int]
  238. b.init(4)
  239. b.incl(2)
  240. b.init
  241. doAssert b.len == 0
  242. b = initHashSet[int](4)
  243. b.incl(2)
  244. doAssert b.len == 1
  245. block missingOrExcl:
  246. var s = toOrderedSet([2, 3, 6, 7])
  247. doAssert s.missingOrExcl(4) == true
  248. doAssert s.missingOrExcl(6) == false
  249. block orderedSetEquality:
  250. type pair = tuple[a, b: int]
  251. var aa = initOrderedSet[pair]()
  252. var bb = initOrderedSet[pair]()
  253. var x = (a: 1, b: 2)
  254. var y = (a: 3, b: 4)
  255. aa.incl(x)
  256. aa.incl(y)
  257. bb.incl(x)
  258. bb.incl(y)
  259. doAssert aa == bb
  260. block setsWithoutInit:
  261. var
  262. a: HashSet[int]
  263. b: HashSet[int]
  264. c: HashSet[int]
  265. d: HashSet[int]
  266. e: HashSet[int]
  267. doAssert a.containsOrIncl(3) == false
  268. doAssert a.contains(3)
  269. doAssert a.len == 1
  270. doAssert a.containsOrIncl(3)
  271. a.incl(3)
  272. doAssert a.len == 1
  273. a.incl(6)
  274. doAssert a.len == 2
  275. b.incl(5)
  276. doAssert b.len == 1
  277. b.excl(5)
  278. b.excl(c)
  279. doAssert b.missingOrExcl(5)
  280. doAssert b.disjoint(c)
  281. d = b + c
  282. doAssert d.len == 0
  283. d = b * c
  284. doAssert d.len == 0
  285. d = b - c
  286. doAssert d.len == 0
  287. d = b -+- c
  288. doAssert d.len == 0
  289. doAssert (d < e) == false
  290. doAssert d <= e
  291. doAssert d == e
  292. block setsWithoutInit:
  293. var
  294. a: OrderedSet[int]
  295. b: OrderedSet[int]
  296. c: OrderedSet[int]
  297. d: HashSet[int]
  298. doAssert a.containsOrIncl(3) == false
  299. doAssert a.contains(3)
  300. doAssert a.len == 1
  301. doAssert a.containsOrIncl(3)
  302. a.incl(3)
  303. doAssert a.len == 1
  304. a.incl(6)
  305. doAssert a.len == 2
  306. b.incl(5)
  307. doAssert b.len == 1
  308. doAssert b.missingOrExcl(5) == false
  309. doAssert b.missingOrExcl(5)
  310. doAssert c.missingOrExcl(9)
  311. d.incl(c)
  312. doAssert d.len == 0
  313. testModule()