thashsets.nim 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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