toverl4.nim 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. discard """
  2. output: '''true
  3. 5.0'''
  4. """
  5. #bug #592
  6. type
  7. ElementKind = enum inner, leaf
  8. TElement[TKey, TData] = object
  9. case kind: ElementKind
  10. of inner:
  11. key: TKey
  12. left, right: ref TElement[Tkey, TData]
  13. of leaf:
  14. data: TData
  15. PElement[TKey, TData] = ref TElement[TKey, TData]
  16. proc newElement[Tkey, TData](other: PElement[TKey, TData]): PElement[Tkey, TData] =
  17. case other.kind:
  18. of inner:
  19. PElement[TKey, TData](kind: ElementKind.inner, key: other.key, left: other.left, right: other.right)
  20. of leaf:
  21. PElement[TKey, TData](kind: ElementKind.leaf, data: other.data)
  22. proc newElement[TKey, TData](key: TKey, left: PElement[TKey, TData] = nil, right: PElement[TKey, TData] = nil) : PElement[TKey, TData] =
  23. PElement[TKey, TData](kind: ElementKind.inner, key: key, left: left, right: right)
  24. proc newElement[Tkey, TData](key: Tkey, data: TData) : PElement[Tkey, TData] =
  25. PElement[TKey, TData](kind: ElementKind.leaf, data: data)
  26. proc find*[TKey, TData](root: PElement[TKey, TData], key: TKey): TData {.raises: [KeyError].} =
  27. if root.left == nil:
  28. raise newException(KeyError, "key does not exist: " & key)
  29. var tmp_element = addr(root)
  30. while tmp_element.kind == inner and tmp_element.right != nil:
  31. tmp_element = if tmp_element.key > key:
  32. addr(tmp_element.left)
  33. else:
  34. addr(tmp_element.right)
  35. if tmp_element.key == key:
  36. return tmp_element.left.data
  37. else:
  38. raise newException(KeyError, "key does not exist: " & key)
  39. proc add*[TKey, TData](root: var PElement[TKey, TData], key: TKey, data: TData) : bool =
  40. if root.left == nil:
  41. root.key = key
  42. root.left = newElement[TKey, TData](key, data)
  43. return true
  44. var tmp_element = addr(root)
  45. while tmp_element.kind == ElementKind.inner and tmp_element.right != nil:
  46. tmp_element = if tmp_element.key > key:
  47. addr(tmp_element.left)
  48. else:
  49. addr(tmp_element.right)
  50. if tmp_element.key == key:
  51. return false
  52. var old_element = newElement[TKey, TData](tmp_element[])
  53. var new_element = newElement[TKey, TData](key, data)
  54. tmp_element[] = if tmp_element.key < key:
  55. newElement(key, old_element, new_element)
  56. else:
  57. newElement(tmp_element.key, new_element, old_element)
  58. return true
  59. var tree = PElement[int, int](kind: ElementKind.inner, key: 0, left: nil, right: nil)
  60. let result = add(tree, 1, 1)
  61. echo(result)
  62. # bug #3748
  63. type
  64. Foo = object
  65. bar: int
  66. proc bar(cur: Foo, val: int, s:seq[string]) =
  67. discard cur.bar
  68. proc does_fail(): Foo =
  69. let a = @["a"]
  70. result.bar(5, a)
  71. doAssert does_fail().bar == 0
  72. # bug #20645
  73. type Zzz[Gen] = object
  74. proc testZ(z: Zzz) =
  75. echo z.Gen(5)
  76. testZ(Zzz[float]())