trecursive.nim 594 B

1234567891011121314151617181920212223242526272829303132333435
  1. discard """
  2. output: '''
  3. test1 OK
  4. '''
  5. """
  6. import smart_ptr
  7. type
  8. Node[T] = object
  9. value: T
  10. next: SharedPtr[Node[T]]
  11. ForwardList[T] = object
  12. first: SharedPtr[Node[T]]
  13. len: Natural
  14. proc pushFront*[T] (list: var ForwardList[T], val: sink T) =
  15. var newNode = newSharedPtr(Node[T](value: val))
  16. var result = false
  17. while not result:
  18. var head = list.first
  19. newNode.get.next = head
  20. result = list.first.cas(head, newNode)
  21. list.len.atomicInc()
  22. proc test1() =
  23. var list: ForwardList[int]
  24. list.pushFront(1)
  25. doAssert list.len == 1
  26. echo "test1 OK"
  27. test1()