titer2.nim 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. discard """
  2. output: '''true
  3. 3
  4. 4
  5. 5
  6. 0
  7. 1
  8. 2
  9. 3
  10. 4'''
  11. cmd: "nim $target --gc:none --hints:on --warnings:off $options $file"
  12. """
  13. import hashes
  14. type
  15. TSlotEnum = enum seEmpty, seFilled, seDeleted
  16. TKeyValuePair[A, B] = tuple[slot: TSlotEnum, key: A, val: B]
  17. TKeyValuePairSeq[A, B] = seq[TKeyValuePair[A, B]]
  18. TTable*[A, B] {.final.} = object
  19. data: TKeyValuePairSeq[A, B]
  20. counter: int
  21. iterator mycountup(a, b: int): int =
  22. var res = a
  23. while res <= b:
  24. yield res
  25. inc(res)
  26. when true:
  27. iterator pairs*[A, B](t: TTable[A, B]): tuple[key: A, val: B] =
  28. ## iterates over any (key, value) pair in the table `t`.
  29. for h in mycountup(0, high(t.data)):
  30. var k = t.data[h].key
  31. if t.data[h].slot == seFilled: yield (k, t.data[h].val)
  32. else:
  33. iterator pairs*(t: TTable[int, string]): tuple[key: int, val: string] =
  34. ## iterates over any (key, value) pair in the table `t`.
  35. for h in mycountup(0, high(t.data)):
  36. var k = t.data[h].key
  37. if t.data[h].slot == seFilled: yield (k, t.data[h].val)
  38. proc initTable*[A, B](initialSize=64): TTable[A, B] =
  39. ## creates a new hash table that is empty. `initialSize` needs to be
  40. ## a power of two.
  41. result.counter = 0
  42. newSeq(result.data, initialSize)
  43. block Test1:
  44. # generic cache does not instantiate the same iterator[types] twice. This
  45. # means we have only one instantiation of 'h'. However, this is the same for
  46. # a non-generic iterator!
  47. var t = initTable[int, string]()
  48. for k, v in t.pairs: discard
  49. for k, v in t.pairs: discard
  50. echo "true"
  51. # bug #1560
  52. for i in @[3, 4, 5]:
  53. echo($i)
  54. # bug #6992
  55. for i in 0 ..< 5u32:
  56. echo i