Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make std/lists usable with ptr instead of ref #593

Draft
wants to merge 2 commits into
base: devel
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions lib/pure/collections/lists.nim
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ type
value*: T
SinglyLinkedNode*[T] = ref SinglyLinkedNodeObj[T]

SinglyLinkedNodePtr*[T] = object
next*: ptr SinglyLinkedNodePtr[T]
value*: T

SinglyLinkedList*[T] = object ## \
## A singly linked list.
##
Expand All @@ -89,6 +93,10 @@ type
head*: <//>(SinglyLinkedNode[T])
tail* {.cursor.}: SinglyLinkedNode[T]

SinglyLinkedListPtr*[T] = object
head*: ptr SinglyLinkedNodePtr[T]
tail*: ptr SinglyLinkedNodePtr[T]

DoublyLinkedList*[T] = object ## \
## A doubly linked list.
##
Expand All @@ -112,7 +120,7 @@ type
## a new empty ring.
head*: DoublyLinkedNode[T]

SomeLinkedList*[T] = SinglyLinkedList[T] | DoublyLinkedList[T]
SomeLinkedList*[T] = SinglyLinkedList[T] | DoublyLinkedList[T] | SinglyLinkedListPtr[T]

SomeLinkedRing*[T] = SinglyLinkedRing[T] | DoublyLinkedRing[T]

Expand All @@ -126,6 +134,9 @@ proc initSinglyLinkedList*[T](): SinglyLinkedList[T] =
var a = initSinglyLinkedList[int]()
discard

proc initSinglyLinkedListPtr*[T](): SinglyLinkedListPtr[T] =
discard

proc initDoublyLinkedList*[T](): DoublyLinkedList[T] =
## Creates a new doubly linked list that is empty.
runnableExamples:
Expand Down Expand Up @@ -399,8 +410,9 @@ proc prependMoved*[T: SomeLinkedList](a, b: var T) {.since: (1, 5, 1).} =
(b, a) = (a, b)
else: swap a, b

proc add*[T](L: var SinglyLinkedList[T],
n: SinglyLinkedNode[T]) {.inline.} =
# proc add*[T](L: var (SinglyLinkedList[T] | SinglyLinkedListPtr[T]), n: type(L.tail)) {.inline.} = # xxx make this work
proc add*[T](L: var (SinglyLinkedList[T] | SinglyLinkedListPtr[T]),
n: SinglyLinkedNode[T] | ptr SinglyLinkedNodePtr[T]) {.inline.} =
## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
##
## See also:
Expand Down
8 changes: 8 additions & 0 deletions tests/stdlib/tlists.nim
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,11 @@ block remove: # return value check
doAssert l.toSeq == [1]
doAssert l.remove(l.head) == true
doAssert l.toSeq == []

block: # SinglyLinkedNodePtr, etc
var list = initSinglyLinkedListPtr[int]()
var nodes: array[4, SinglyLinkedNodePtr[int]]
for i,n in mpairs(nodes):
n.value = i
list.add n.addr
doAssert toSeq(list) == [0,1,2,3]