parent
1e7a10001a
commit
73ce40aaf7
1 changed files with 135 additions and 135 deletions
|
|
@ -19,15 +19,15 @@
|
||||||
##
|
##
|
||||||
## ## Lists
|
## ## Lists
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
var l = initDoublyLinkedList[int]()
|
var list = initDoublyLinkedList[int]()
|
||||||
let
|
let
|
||||||
a = newDoublyLinkedNode[int](3)
|
a = newDoublyLinkedNode[int](3)
|
||||||
b = newDoublyLinkedNode[int](7)
|
b = newDoublyLinkedNode[int](7)
|
||||||
c = newDoublyLinkedNode[int](9)
|
c = newDoublyLinkedNode[int](9)
|
||||||
|
|
||||||
l.add(a)
|
list.add(a)
|
||||||
l.add(b)
|
list.add(b)
|
||||||
l.prepend(c)
|
list.prepend(c)
|
||||||
|
|
||||||
assert a.next == b
|
assert a.next == b
|
||||||
assert a.prev == c
|
assert a.prev == c
|
||||||
|
|
@ -38,15 +38,15 @@ runnableExamples:
|
||||||
|
|
||||||
## ## Rings
|
## ## Rings
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
var l = initSinglyLinkedRing[int]()
|
var ring = initSinglyLinkedRing[int]()
|
||||||
let
|
let
|
||||||
a = newSinglyLinkedNode[int](3)
|
a = newSinglyLinkedNode[int](3)
|
||||||
b = newSinglyLinkedNode[int](7)
|
b = newSinglyLinkedNode[int](7)
|
||||||
c = newSinglyLinkedNode[int](9)
|
c = newSinglyLinkedNode[int](9)
|
||||||
|
|
||||||
l.add(a)
|
ring.add(a)
|
||||||
l.add(b)
|
ring.add(b)
|
||||||
l.prepend(c)
|
ring.prepend(c)
|
||||||
|
|
||||||
assert c.next == a
|
assert c.next == a
|
||||||
assert a.next == b
|
assert a.next == b
|
||||||
|
|
@ -189,21 +189,21 @@ func toDoublyLinkedList*[T](elems: openArray[T]): DoublyLinkedList[T] {.since: (
|
||||||
result.add(elem)
|
result.add(elem)
|
||||||
|
|
||||||
template itemsListImpl() {.dirty.} =
|
template itemsListImpl() {.dirty.} =
|
||||||
var it = list.head
|
var it = L.head
|
||||||
while it != nil:
|
while it != nil:
|
||||||
yield it.value
|
yield it.value
|
||||||
it = it.next
|
it = it.next
|
||||||
|
|
||||||
template itemsRingImpl() {.dirty.} =
|
template itemsRingImpl() {.dirty.} =
|
||||||
var it = ring.head
|
var it = L.head
|
||||||
if it != nil:
|
if it != nil:
|
||||||
while true:
|
while true:
|
||||||
yield it.value
|
yield it.value
|
||||||
it = it.next
|
it = it.next
|
||||||
if it == ring.head: break
|
if it == L.head: break
|
||||||
|
|
||||||
iterator items*[T](list: SomeLinkedList[T]): T =
|
iterator items*[T](L: SomeLinkedList[T]): T =
|
||||||
## Yields every value of `list`.
|
## Yields every value of `L`.
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `mitems iterator <#mitems.i,SomeLinkedList[T]>`_
|
## * `mitems iterator <#mitems.i,SomeLinkedList[T]>`_
|
||||||
|
|
@ -218,8 +218,8 @@ iterator items*[T](list: SomeLinkedList[T]): T =
|
||||||
|
|
||||||
itemsListImpl()
|
itemsListImpl()
|
||||||
|
|
||||||
iterator items*[T](ring: SomeLinkedRing[T]): T =
|
iterator items*[T](L: SomeLinkedRing[T]): T =
|
||||||
## Yields every value of `ring`.
|
## Yields every value of `L`.
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `mitems iterator <#mitems.i,SomeLinkedRing[T]>`_
|
## * `mitems iterator <#mitems.i,SomeLinkedRing[T]>`_
|
||||||
|
|
@ -234,8 +234,8 @@ iterator items*[T](ring: SomeLinkedRing[T]): T =
|
||||||
|
|
||||||
itemsRingImpl()
|
itemsRingImpl()
|
||||||
|
|
||||||
iterator mitems*[T](list: var SomeLinkedList[T]): var T =
|
iterator mitems*[T](L: var SomeLinkedList[T]): var T =
|
||||||
## Yields every value of `list` so that you can modify it.
|
## Yields every value of `L` so that you can modify it.
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `items iterator <#items.i,SomeLinkedList[T]>`_
|
## * `items iterator <#items.i,SomeLinkedList[T]>`_
|
||||||
|
|
@ -251,8 +251,8 @@ iterator mitems*[T](list: var SomeLinkedList[T]): var T =
|
||||||
|
|
||||||
itemsListImpl()
|
itemsListImpl()
|
||||||
|
|
||||||
iterator mitems*[T](ring: var SomeLinkedRing[T]): var T =
|
iterator mitems*[T](L: var SomeLinkedRing[T]): var T =
|
||||||
## Yields every value of `ring` so that you can modify it.
|
## Yields every value of `L` so that you can modify it.
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `items iterator <#items.i,SomeLinkedRing[T]>`_
|
## * `items iterator <#items.i,SomeLinkedRing[T]>`_
|
||||||
|
|
@ -268,7 +268,7 @@ iterator mitems*[T](ring: var SomeLinkedRing[T]): var T =
|
||||||
|
|
||||||
itemsRingImpl()
|
itemsRingImpl()
|
||||||
|
|
||||||
iterator nodes*[T](list: SomeLinkedList[T]): SomeLinkedNode[T] =
|
iterator nodes*[T](L: SomeLinkedList[T]): SomeLinkedNode[T] =
|
||||||
## Iterates over every node of `x`. Removing the current node from the
|
## Iterates over every node of `x`. Removing the current node from the
|
||||||
## list during traversal is supported.
|
## list during traversal is supported.
|
||||||
##
|
##
|
||||||
|
|
@ -287,13 +287,13 @@ iterator nodes*[T](list: SomeLinkedList[T]): SomeLinkedNode[T] =
|
||||||
x.value = 5 * x.value - 1
|
x.value = 5 * x.value - 1
|
||||||
assert $a == "[49, 99, 199, 249]"
|
assert $a == "[49, 99, 199, 249]"
|
||||||
|
|
||||||
var it = list.head
|
var it = L.head
|
||||||
while it != nil:
|
while it != nil:
|
||||||
let nxt = it.next
|
let nxt = it.next
|
||||||
yield it
|
yield it
|
||||||
it = nxt
|
it = nxt
|
||||||
|
|
||||||
iterator nodes*[T](ring: SomeLinkedRing[T]): SomeLinkedNode[T] =
|
iterator nodes*[T](L: SomeLinkedRing[T]): SomeLinkedNode[T] =
|
||||||
## Iterates over every node of `x`. Removing the current node from the
|
## Iterates over every node of `x`. Removing the current node from the
|
||||||
## list during traversal is supported.
|
## list during traversal is supported.
|
||||||
##
|
##
|
||||||
|
|
@ -312,27 +312,27 @@ iterator nodes*[T](ring: SomeLinkedRing[T]): SomeLinkedNode[T] =
|
||||||
x.value = 5 * x.value - 1
|
x.value = 5 * x.value - 1
|
||||||
assert $a == "[49, 99, 199, 249]"
|
assert $a == "[49, 99, 199, 249]"
|
||||||
|
|
||||||
var it = ring.head
|
var it = L.head
|
||||||
if it != nil:
|
if it != nil:
|
||||||
while true:
|
while true:
|
||||||
let nxt = it.next
|
let nxt = it.next
|
||||||
yield it
|
yield it
|
||||||
it = nxt
|
it = nxt
|
||||||
if it == ring.head: break
|
if it == L.head: break
|
||||||
|
|
||||||
proc `$`*[T](l: SomeLinkedCollection[T]): string =
|
proc `$`*[T](L: SomeLinkedCollection[T]): string =
|
||||||
## Turns a list into its string representation for logging and printing.
|
## Turns a list into its string representation for logging and printing.
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
let a = [1, 2, 3, 4].toSinglyLinkedList
|
let a = [1, 2, 3, 4].toSinglyLinkedList
|
||||||
assert $a == "[1, 2, 3, 4]"
|
assert $a == "[1, 2, 3, 4]"
|
||||||
|
|
||||||
result = "["
|
result = "["
|
||||||
for x in nodes(l):
|
for x in nodes(L):
|
||||||
if result.len > 1: result.add(", ")
|
if result.len > 1: result.add(", ")
|
||||||
result.addQuoted(x.value)
|
result.addQuoted(x.value)
|
||||||
result.add("]")
|
result.add("]")
|
||||||
|
|
||||||
proc find*[T](l: SomeLinkedCollection[T], value: T): SomeLinkedNode[T] =
|
proc find*[T](L: SomeLinkedCollection[T], value: T): SomeLinkedNode[T] =
|
||||||
## Searches in the list for a value. Returns `nil` if the value does not
|
## Searches in the list for a value. Returns `nil` if the value does not
|
||||||
## exist.
|
## exist.
|
||||||
##
|
##
|
||||||
|
|
@ -343,10 +343,10 @@ proc find*[T](l: SomeLinkedCollection[T], value: T): SomeLinkedNode[T] =
|
||||||
assert a.find(9).value == 9
|
assert a.find(9).value == 9
|
||||||
assert a.find(1) == nil
|
assert a.find(1) == nil
|
||||||
|
|
||||||
for x in nodes(l):
|
for x in nodes(L):
|
||||||
if x.value == value: return x
|
if x.value == value: return x
|
||||||
|
|
||||||
proc contains*[T](l: SomeLinkedCollection[T], value: T): bool {.inline.} =
|
proc contains*[T](L: SomeLinkedCollection[T], value: T): bool {.inline.} =
|
||||||
## Searches in the list for a value. Returns `false` if the value does not
|
## Searches in the list for a value. Returns `false` if the value does not
|
||||||
## exist, `true` otherwise. This allows the usage of the `in` and `notin`
|
## exist, `true` otherwise. This allows the usage of the `in` and `notin`
|
||||||
## operators.
|
## operators.
|
||||||
|
|
@ -360,7 +360,7 @@ proc contains*[T](l: SomeLinkedCollection[T], value: T): bool {.inline.} =
|
||||||
assert(not a.contains(1))
|
assert(not a.contains(1))
|
||||||
assert 2 notin a
|
assert 2 notin a
|
||||||
|
|
||||||
result = find(l, value) != nil
|
result = find(L, value) != nil
|
||||||
|
|
||||||
proc prepend*[T: SomeLinkedList](a: var T, b: T) {.since: (1, 5, 1).} =
|
proc prepend*[T: SomeLinkedList](a: var T, b: T) {.since: (1, 5, 1).} =
|
||||||
## Prepends a shallow copy of `b` to the beginning of `a`.
|
## Prepends a shallow copy of `b` to the beginning of `a`.
|
||||||
|
|
@ -411,8 +411,8 @@ proc prependMoved*[T: SomeLinkedList](a, b: var T) {.since: (1, 5, 1).} =
|
||||||
(b, a) = (a, b)
|
(b, a) = (a, b)
|
||||||
else: swap a, b
|
else: swap a, b
|
||||||
|
|
||||||
proc add*[T](list: var SinglyLinkedList[T], n: SinglyLinkedNode[T]) {.inline.} =
|
proc add*[T](L: var SinglyLinkedList[T], n: SinglyLinkedNode[T]) {.inline.} =
|
||||||
## Appends (adds to the end) a node `n` to `list`. Efficiency: O(1).
|
## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `add proc <#add,SinglyLinkedList[T],T>`_ for appending a value
|
## * `add proc <#add,SinglyLinkedList[T],T>`_ for appending a value
|
||||||
|
|
@ -426,14 +426,14 @@ proc add*[T](list: var SinglyLinkedList[T], n: SinglyLinkedNode[T]) {.inline.} =
|
||||||
assert a.contains(9)
|
assert a.contains(9)
|
||||||
|
|
||||||
n.next = nil
|
n.next = nil
|
||||||
if list.tail != nil:
|
if L.tail != nil:
|
||||||
assert(list.tail.next == nil)
|
assert(L.tail.next == nil)
|
||||||
list.tail.next = n
|
L.tail.next = n
|
||||||
list.tail = n
|
L.tail = n
|
||||||
if list.head == nil: list.head = n
|
if L.head == nil: L.head = n
|
||||||
|
|
||||||
proc add*[T](list: var SinglyLinkedList[T], value: T) {.inline.} =
|
proc add*[T](L: var SinglyLinkedList[T], value: T) {.inline.} =
|
||||||
## Appends (adds to the end) a value to `list`. Efficiency: O(1).
|
## Appends (adds to the end) a value to `L`. Efficiency: O(1).
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `add proc <#add,SinglyLinkedList[T],T>`_ for appending a value
|
## * `add proc <#add,SinglyLinkedList[T],T>`_ for appending a value
|
||||||
|
|
@ -446,11 +446,11 @@ proc add*[T](list: var SinglyLinkedList[T], value: T) {.inline.} =
|
||||||
a.add(8)
|
a.add(8)
|
||||||
assert a.contains(9)
|
assert a.contains(9)
|
||||||
|
|
||||||
add(list, newSinglyLinkedNode(value))
|
add(L, newSinglyLinkedNode(value))
|
||||||
|
|
||||||
proc prepend*[T](list: var SinglyLinkedList[T],
|
proc prepend*[T](L: var SinglyLinkedList[T],
|
||||||
n: SinglyLinkedNode[T]) {.inline.} =
|
n: SinglyLinkedNode[T]) {.inline.} =
|
||||||
## Prepends (adds to the beginning) a node to `list`. Efficiency: O(1).
|
## Prepends (adds to the beginning) a node to `L`. Efficiency: O(1).
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `add proc <#add,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
|
## * `add proc <#add,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
|
||||||
|
|
@ -463,12 +463,12 @@ proc prepend*[T](list: var SinglyLinkedList[T],
|
||||||
a.prepend(n)
|
a.prepend(n)
|
||||||
assert a.contains(9)
|
assert a.contains(9)
|
||||||
|
|
||||||
n.next = list.head
|
n.next = L.head
|
||||||
list.head = n
|
L.head = n
|
||||||
if list.tail == nil: list.tail = n
|
if L.tail == nil: L.tail = n
|
||||||
|
|
||||||
proc prepend*[T](list: var SinglyLinkedList[T], value: T) {.inline.} =
|
proc prepend*[T](L: var SinglyLinkedList[T], value: T) {.inline.} =
|
||||||
## Prepends (adds to the beginning) a node to `list`. Efficiency: O(1).
|
## Prepends (adds to the beginning) a node to `L`. Efficiency: O(1).
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `add proc <#add,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
|
## * `add proc <#add,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
|
||||||
|
|
@ -482,7 +482,7 @@ proc prepend*[T](list: var SinglyLinkedList[T], value: T) {.inline.} =
|
||||||
a.prepend(8)
|
a.prepend(8)
|
||||||
assert a.contains(9)
|
assert a.contains(9)
|
||||||
|
|
||||||
prepend(list, newSinglyLinkedNode(value))
|
prepend(L, newSinglyLinkedNode(value))
|
||||||
|
|
||||||
func copy*[T](a: SinglyLinkedList[T]): SinglyLinkedList[T] {.since: (1, 5, 1).} =
|
func copy*[T](a: SinglyLinkedList[T]): SinglyLinkedList[T] {.since: (1, 5, 1).} =
|
||||||
## Creates a shallow copy of `a`.
|
## Creates a shallow copy of `a`.
|
||||||
|
|
@ -540,8 +540,8 @@ proc addMoved*[T](a, b: var SinglyLinkedList[T]) {.since: (1, 5, 1).} =
|
||||||
b.head = nil
|
b.head = nil
|
||||||
b.tail = nil
|
b.tail = nil
|
||||||
|
|
||||||
proc add*[T](list: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
|
proc add*[T](L: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
|
||||||
## Appends (adds to the end) a node `n` to `list`. Efficiency: O(1).
|
## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `add proc <#add,DoublyLinkedList[T],T>`_ for appending a value
|
## * `add proc <#add,DoublyLinkedList[T],T>`_ for appending a value
|
||||||
|
|
@ -557,15 +557,15 @@ proc add*[T](list: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
|
||||||
assert a.contains(9)
|
assert a.contains(9)
|
||||||
|
|
||||||
n.next = nil
|
n.next = nil
|
||||||
n.prev = list.tail
|
n.prev = L.tail
|
||||||
if list.tail != nil:
|
if L.tail != nil:
|
||||||
assert(list.tail.next == nil)
|
assert(L.tail.next == nil)
|
||||||
list.tail.next = n
|
L.tail.next = n
|
||||||
list.tail = n
|
L.tail = n
|
||||||
if list.head == nil: list.head = n
|
if L.head == nil: L.head = n
|
||||||
|
|
||||||
proc add*[T](list: var DoublyLinkedList[T], value: T) =
|
proc add*[T](L: var DoublyLinkedList[T], value: T) =
|
||||||
## Appends (adds to the end) a value to `list`. Efficiency: O(1).
|
## Appends (adds to the end) a value to `L`. Efficiency: O(1).
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
|
## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
|
||||||
|
|
@ -581,10 +581,10 @@ proc add*[T](list: var DoublyLinkedList[T], value: T) =
|
||||||
a.add(8)
|
a.add(8)
|
||||||
assert a.contains(9)
|
assert a.contains(9)
|
||||||
|
|
||||||
add(list, newDoublyLinkedNode(value))
|
add(L, newDoublyLinkedNode(value))
|
||||||
|
|
||||||
proc prepend*[T](list: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
|
proc prepend*[T](L: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
|
||||||
## Prepends (adds to the beginning) a node `n` to `list`. Efficiency: O(1).
|
## Prepends (adds to the beginning) a node `n` to `L`. Efficiency: O(1).
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
|
## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
|
||||||
|
|
@ -600,15 +600,15 @@ proc prepend*[T](list: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
|
||||||
assert a.contains(9)
|
assert a.contains(9)
|
||||||
|
|
||||||
n.prev = nil
|
n.prev = nil
|
||||||
n.next = list.head
|
n.next = L.head
|
||||||
if list.head != nil:
|
if L.head != nil:
|
||||||
assert(list.head.prev == nil)
|
assert(L.head.prev == nil)
|
||||||
list.head.prev = n
|
L.head.prev = n
|
||||||
list.head = n
|
L.head = n
|
||||||
if list.tail == nil: list.tail = n
|
if L.tail == nil: L.tail = n
|
||||||
|
|
||||||
proc prepend*[T](list: var DoublyLinkedList[T], value: T) =
|
proc prepend*[T](L: var DoublyLinkedList[T], value: T) =
|
||||||
## Prepends (adds to the beginning) a value to `list`. Efficiency: O(1).
|
## Prepends (adds to the beginning) a value to `L`. Efficiency: O(1).
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
|
## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
|
||||||
|
|
@ -624,7 +624,7 @@ proc prepend*[T](list: var DoublyLinkedList[T], value: T) =
|
||||||
a.prepend(8)
|
a.prepend(8)
|
||||||
assert a.contains(9)
|
assert a.contains(9)
|
||||||
|
|
||||||
prepend(list, newDoublyLinkedNode(value))
|
prepend(L, newDoublyLinkedNode(value))
|
||||||
|
|
||||||
func copy*[T](a: DoublyLinkedList[T]): DoublyLinkedList[T] {.since: (1, 5, 1).} =
|
func copy*[T](a: DoublyLinkedList[T]): DoublyLinkedList[T] {.since: (1, 5, 1).} =
|
||||||
## Creates a shallow copy of `a`.
|
## Creates a shallow copy of `a`.
|
||||||
|
|
@ -705,9 +705,9 @@ proc add*[T: SomeLinkedList](a: var T, b: T) {.since: (1, 5, 1).} =
|
||||||
var tmp = b.copy
|
var tmp = b.copy
|
||||||
a.addMoved(tmp)
|
a.addMoved(tmp)
|
||||||
|
|
||||||
proc remove*[T](list: var SinglyLinkedList[T], n: SinglyLinkedNode[T]): bool {.discardable.} =
|
proc remove*[T](L: var SinglyLinkedList[T], n: SinglyLinkedNode[T]): bool {.discardable.} =
|
||||||
## Removes a node `n` from `list`.
|
## Removes a node `n` from `L`.
|
||||||
## Returns `true` if `n` was found in `list`.
|
## Returns `true` if `n` was found in `L`.
|
||||||
## Efficiency: O(n); the list is traversed until `n` is found.
|
## Efficiency: O(n); the list is traversed until `n` is found.
|
||||||
## Attempting to remove an element not contained in the list is a no-op.
|
## Attempting to remove an element not contained in the list is a no-op.
|
||||||
## When the list is cyclic, the cycle is preserved after removal.
|
## When the list is cyclic, the cycle is preserved after removal.
|
||||||
|
|
@ -728,12 +728,12 @@ proc remove*[T](list: var SinglyLinkedList[T], n: SinglyLinkedNode[T]): bool {.d
|
||||||
ai
|
ai
|
||||||
assert s == [2, 2, 2, 2]
|
assert s == [2, 2, 2, 2]
|
||||||
|
|
||||||
if n == list.head:
|
if n == L.head:
|
||||||
list.head = n.next
|
L.head = n.next
|
||||||
if list.tail.next == n:
|
if L.tail.next == n:
|
||||||
list.tail.next = list.head # restore cycle
|
L.tail.next = L.head # restore cycle
|
||||||
else:
|
else:
|
||||||
var prev = list.head
|
var prev = L.head
|
||||||
while prev.next != n and prev.next != nil:
|
while prev.next != n and prev.next != nil:
|
||||||
prev = prev.next
|
prev = prev.next
|
||||||
if prev.next == nil:
|
if prev.next == nil:
|
||||||
|
|
@ -741,9 +741,9 @@ proc remove*[T](list: var SinglyLinkedList[T], n: SinglyLinkedNode[T]): bool {.d
|
||||||
prev.next = n.next
|
prev.next = n.next
|
||||||
true
|
true
|
||||||
|
|
||||||
proc remove*[T](list: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
|
proc remove*[T](L: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
|
||||||
## Removes a node `n` from `list`. Efficiency: O(1).
|
## Removes a node `n` from `L`. Efficiency: O(1).
|
||||||
## This function assumes, for the sake of efficiency, that `n` is contained in `list`,
|
## This function assumes, for the sake of efficiency, that `n` is contained in `L`,
|
||||||
## otherwise the effects are undefined.
|
## otherwise the effects are undefined.
|
||||||
## When the list is cyclic, the cycle is preserved after removal.
|
## When the list is cyclic, the cycle is preserved after removal.
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
|
|
@ -763,15 +763,15 @@ proc remove*[T](list: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
|
||||||
ai
|
ai
|
||||||
assert s == [2, 2, 2, 2]
|
assert s == [2, 2, 2, 2]
|
||||||
|
|
||||||
if n == list.tail: list.tail = n.prev
|
if n == L.tail: L.tail = n.prev
|
||||||
if n == list.head: list.head = n.next
|
if n == L.head: L.head = n.next
|
||||||
if n.next != nil: n.next.prev = n.prev
|
if n.next != nil: n.next.prev = n.prev
|
||||||
if n.prev != nil: n.prev.next = n.next
|
if n.prev != nil: n.prev.next = n.next
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
proc add*[T](ring: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
|
proc add*[T](L: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
|
||||||
## Appends (adds to the end) a node `n` to `ring`. Efficiency: O(1).
|
## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `add proc <#add,SinglyLinkedRing[T],T>`_ for appending a value
|
## * `add proc <#add,SinglyLinkedRing[T],T>`_ for appending a value
|
||||||
|
|
@ -784,17 +784,17 @@ proc add*[T](ring: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
|
||||||
a.add(n)
|
a.add(n)
|
||||||
assert a.contains(9)
|
assert a.contains(9)
|
||||||
|
|
||||||
if ring.head != nil:
|
if L.head != nil:
|
||||||
n.next = ring.head
|
n.next = L.head
|
||||||
assert(ring.tail != nil)
|
assert(L.tail != nil)
|
||||||
ring.tail.next = n
|
L.tail.next = n
|
||||||
else:
|
else:
|
||||||
n.next = n
|
n.next = n
|
||||||
ring.head = n
|
L.head = n
|
||||||
ring.tail = n
|
L.tail = n
|
||||||
|
|
||||||
proc add*[T](ring: var SinglyLinkedRing[T], value: T) =
|
proc add*[T](L: var SinglyLinkedRing[T], value: T) =
|
||||||
## Appends (adds to the end) a value to `ring`. Efficiency: O(1).
|
## Appends (adds to the end) a value to `L`. Efficiency: O(1).
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `add proc <#add,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
|
## * `add proc <#add,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
|
||||||
|
|
@ -808,10 +808,10 @@ proc add*[T](ring: var SinglyLinkedRing[T], value: T) =
|
||||||
a.add(8)
|
a.add(8)
|
||||||
assert a.contains(9)
|
assert a.contains(9)
|
||||||
|
|
||||||
add(ring, newSinglyLinkedNode(value))
|
add(L, newSinglyLinkedNode(value))
|
||||||
|
|
||||||
proc prepend*[T](ring: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
|
proc prepend*[T](L: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
|
||||||
## Prepends (adds to the beginning) a node `n` to `ring`. Efficiency: O(1).
|
## Prepends (adds to the beginning) a node `n` to `L`. Efficiency: O(1).
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `add proc <#add,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
|
## * `add proc <#add,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
|
||||||
|
|
@ -824,17 +824,17 @@ proc prepend*[T](ring: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
|
||||||
a.prepend(n)
|
a.prepend(n)
|
||||||
assert a.contains(9)
|
assert a.contains(9)
|
||||||
|
|
||||||
if ring.head != nil:
|
if L.head != nil:
|
||||||
n.next = ring.head
|
n.next = L.head
|
||||||
assert(ring.tail != nil)
|
assert(L.tail != nil)
|
||||||
ring.tail.next = n
|
L.tail.next = n
|
||||||
else:
|
else:
|
||||||
n.next = n
|
n.next = n
|
||||||
ring.tail = n
|
L.tail = n
|
||||||
ring.head = n
|
L.head = n
|
||||||
|
|
||||||
proc prepend*[T](ring: var SinglyLinkedRing[T], value: T) =
|
proc prepend*[T](L: var SinglyLinkedRing[T], value: T) =
|
||||||
## Prepends (adds to the beginning) a value to `ring`. Efficiency: O(1).
|
## Prepends (adds to the beginning) a value to `L`. Efficiency: O(1).
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `add proc <#add,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
|
## * `add proc <#add,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
|
||||||
|
|
@ -848,12 +848,12 @@ proc prepend*[T](ring: var SinglyLinkedRing[T], value: T) =
|
||||||
a.prepend(8)
|
a.prepend(8)
|
||||||
assert a.contains(9)
|
assert a.contains(9)
|
||||||
|
|
||||||
prepend(ring, newSinglyLinkedNode(value))
|
prepend(L, newSinglyLinkedNode(value))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
proc add*[T](ring: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
|
proc add*[T](L: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
|
||||||
## Appends (adds to the end) a node `n` to `ring`. Efficiency: O(1).
|
## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `add proc <#add,DoublyLinkedRing[T],T>`_ for appending a value
|
## * `add proc <#add,DoublyLinkedRing[T],T>`_ for appending a value
|
||||||
|
|
@ -868,18 +868,18 @@ proc add*[T](ring: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
|
||||||
a.add(n)
|
a.add(n)
|
||||||
assert a.contains(9)
|
assert a.contains(9)
|
||||||
|
|
||||||
if ring.head != nil:
|
if L.head != nil:
|
||||||
n.next = ring.head
|
n.next = L.head
|
||||||
n.prev = ring.head.prev
|
n.prev = L.head.prev
|
||||||
ring.head.prev.next = n
|
L.head.prev.next = n
|
||||||
ring.head.prev = n
|
L.head.prev = n
|
||||||
else:
|
else:
|
||||||
n.prev = n
|
n.prev = n
|
||||||
n.next = n
|
n.next = n
|
||||||
ring.head = n
|
L.head = n
|
||||||
|
|
||||||
proc add*[T](ring: var DoublyLinkedRing[T], value: T) =
|
proc add*[T](L: var DoublyLinkedRing[T], value: T) =
|
||||||
## Appends (adds to the end) a value to `ring`. Efficiency: O(1).
|
## Appends (adds to the end) a value to `L`. Efficiency: O(1).
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `add proc <#add,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
|
## * `add proc <#add,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
|
||||||
|
|
@ -895,10 +895,10 @@ proc add*[T](ring: var DoublyLinkedRing[T], value: T) =
|
||||||
a.add(8)
|
a.add(8)
|
||||||
assert a.contains(9)
|
assert a.contains(9)
|
||||||
|
|
||||||
add(ring, newDoublyLinkedNode(value))
|
add(L, newDoublyLinkedNode(value))
|
||||||
|
|
||||||
proc prepend*[T](ring: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
|
proc prepend*[T](L: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
|
||||||
## Prepends (adds to the beginning) a node `n` to `ring`. Efficiency: O(1).
|
## Prepends (adds to the beginning) a node `n` to `L`. Efficiency: O(1).
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `add proc <#add,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
|
## * `add proc <#add,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
|
||||||
|
|
@ -913,18 +913,18 @@ proc prepend*[T](ring: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
|
||||||
a.prepend(n)
|
a.prepend(n)
|
||||||
assert a.contains(9)
|
assert a.contains(9)
|
||||||
|
|
||||||
if ring.head != nil:
|
if L.head != nil:
|
||||||
n.next = ring.head
|
n.next = L.head
|
||||||
n.prev = ring.head.prev
|
n.prev = L.head.prev
|
||||||
ring.head.prev.next = n
|
L.head.prev.next = n
|
||||||
ring.head.prev = n
|
L.head.prev = n
|
||||||
else:
|
else:
|
||||||
n.prev = n
|
n.prev = n
|
||||||
n.next = n
|
n.next = n
|
||||||
ring.head = n
|
L.head = n
|
||||||
|
|
||||||
proc prepend*[T](ring: var DoublyLinkedRing[T], value: T) =
|
proc prepend*[T](L: var DoublyLinkedRing[T], value: T) =
|
||||||
## Prepends (adds to the beginning) a value to `ring`. Efficiency: O(1).
|
## Prepends (adds to the beginning) a value to `L`. Efficiency: O(1).
|
||||||
##
|
##
|
||||||
## **See also:**
|
## **See also:**
|
||||||
## * `add proc <#add,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
|
## * `add proc <#add,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
|
||||||
|
|
@ -940,11 +940,11 @@ proc prepend*[T](ring: var DoublyLinkedRing[T], value: T) =
|
||||||
a.prepend(8)
|
a.prepend(8)
|
||||||
assert a.contains(9)
|
assert a.contains(9)
|
||||||
|
|
||||||
prepend(ring, newDoublyLinkedNode(value))
|
prepend(L, newDoublyLinkedNode(value))
|
||||||
|
|
||||||
proc remove*[T](ring: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
|
proc remove*[T](L: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
|
||||||
## Removes `n` from `ring`. Efficiency: O(1).
|
## Removes `n` from `L`. Efficiency: O(1).
|
||||||
## This function assumes, for the sake of efficiency, that `n` is contained in `ring`,
|
## This function assumes, for the sake of efficiency, that `n` is contained in `L`,
|
||||||
## otherwise the effects are undefined.
|
## otherwise the effects are undefined.
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
var a = initDoublyLinkedRing[int]()
|
var a = initDoublyLinkedRing[int]()
|
||||||
|
|
@ -956,13 +956,13 @@ proc remove*[T](ring: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
|
||||||
|
|
||||||
n.next.prev = n.prev
|
n.next.prev = n.prev
|
||||||
n.prev.next = n.next
|
n.prev.next = n.next
|
||||||
if n == ring.head:
|
if n == L.head:
|
||||||
let p = ring.head.prev
|
let p = L.head.prev
|
||||||
if p == ring.head:
|
if p == L.head:
|
||||||
# only one element left:
|
# only one element left:
|
||||||
ring.head = nil
|
L.head = nil
|
||||||
else:
|
else:
|
||||||
ring.head = p
|
L.head = p
|
||||||
|
|
||||||
proc append*[T](a: var (SinglyLinkedList[T] | SinglyLinkedRing[T]),
|
proc append*[T](a: var (SinglyLinkedList[T] | SinglyLinkedRing[T]),
|
||||||
b: SinglyLinkedList[T] | SinglyLinkedNode[T] | T) =
|
b: SinglyLinkedList[T] | SinglyLinkedNode[T] | T) =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue