mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-25 08:43:58 +00:00
O(1) concatenation of singly- and doubly linked lists. (#16362)
* O(1) concatenation of singly- and doubly linked lists. There is also new `toSinglyLinkedList` and `toDoublyLinkedList` functions for conversion from `openArray`s, similarly to `toHashSet` or `toTable`. * Add `sequtils` import to runnable examples with `toSeq`. * Added missing call to runnable examples. * Add .since annotation, changelog, and tests. * Rename `lists.concat` as an overload to `lists.append`. * Renamed `append` to `add` in lists. * Remove faulty `add` for `DoublyLinkedList`s. * Improved tests for lists. * `lists.add` moves the second list; added `lists.copy` for shallow copy. * More tests for `lists.add` and `lists.copy`. * More compact tests for lists with templates. * List concatenation with copying (`add`) and moving (tentatively `addMove`) * Renamed `addMove` to `addMoved`; renamed arguments according to the style guide. * Added extended example to `lists.copy`. * Corrected .since annotations to 1.6 * Add .since annotation, changelog, and tests. * Rename `lists.concat` as an overload to `lists.append`. * Renamed `append` to `add` in lists. * Remove faulty `add` for `DoublyLinkedList`s. * `lists.add` moves the second list; added `lists.copy` for shallow copy. * More tests for `lists.add` and `lists.copy`. * List concatenation with copying (`add`) and moving (tentatively `addMove`) * Renamed `addMove` to `addMoved`; renamed arguments according to the style guide. * Since declarations changed to (1,5,1). * Add .since annotation, changelog, and tests. * Rename `lists.concat` as an overload to `lists.append`. * Renamed `append` to `add` in lists. * Remove faulty `add` for `DoublyLinkedList`s. * `lists.add` moves the second list; added `lists.copy` for shallow copy. * More tests for `lists.add` and `lists.copy`. * List concatenation with copying (`add`) and moving (tentatively `addMove`) * Renamed `addMove` to `addMoved`; renamed arguments according to the style guide. * Changelog update. * Fix rebasing errors. * Self-adding with `lists.addMove` results in a cycle now. Added some extra tests.
This commit is contained in:
@@ -2,7 +2,7 @@ discard """
|
||||
targets: "c js"
|
||||
"""
|
||||
|
||||
import lists
|
||||
import lists, sequtils, std/enumerate, std/sugar
|
||||
|
||||
const
|
||||
data = [1, 2, 3, 4, 5, 6]
|
||||
@@ -81,3 +81,70 @@ block tlistsToString:
|
||||
l.append('2')
|
||||
l.append('3')
|
||||
doAssert $l == """['1', '2', '3']"""
|
||||
|
||||
template testCommon(initList, toList) =
|
||||
|
||||
block: # toSinglyLinkedList, toDoublyLinkedList
|
||||
let l = seq[int].default
|
||||
doAssert l.toList.toSeq == []
|
||||
doAssert [1].toList.toSeq == [1]
|
||||
doAssert [1, 2, 3].toList.toSeq == [1, 2, 3]
|
||||
|
||||
block copy:
|
||||
doAssert array[0, int].default.toList.copy.toSeq == []
|
||||
doAssert [1].toList.copy.toSeq == [1]
|
||||
doAssert [1, 2].toList.copy.toSeq == [1, 2]
|
||||
doAssert [1, 2, 3].toList.copy.toSeq == [1, 2, 3]
|
||||
type Foo = ref object
|
||||
x: int
|
||||
var f0 = Foo(x: 0)
|
||||
let f1 = Foo(x: 1)
|
||||
var a = [f0].toList
|
||||
var b = a.copy
|
||||
b.append f1
|
||||
doAssert a.toSeq == [f0]
|
||||
doAssert b.toSeq == [f0, f1]
|
||||
f0.x = 42
|
||||
assert a.head.value.x == 42
|
||||
assert b.head.value.x == 42
|
||||
|
||||
block: # add, addMoved
|
||||
block:
|
||||
var
|
||||
l0 = initList[int]()
|
||||
l1 = [1].toList
|
||||
l2 = [2, 3].toList
|
||||
l3 = [4, 5, 6].toList
|
||||
l0.add l3
|
||||
l1.add l3
|
||||
l2.addMoved l3
|
||||
doAssert l0.toSeq == [4, 5, 6]
|
||||
doAssert l1.toSeq == [1, 4, 5, 6]
|
||||
doAssert l2.toSeq == [2, 3, 4, 5, 6]
|
||||
doAssert l3.toSeq == []
|
||||
l2.add l3 # re-adding l3 that was destroyed is now a no-op
|
||||
doAssert l2.toSeq == [2, 3, 4, 5, 6]
|
||||
doAssert l3.toSeq == []
|
||||
block:
|
||||
var
|
||||
l0 = initList[int]()
|
||||
l1 = [1].toList
|
||||
l2 = [2, 3].toList
|
||||
l3 = [4, 5, 6].toList
|
||||
l3.addMoved l0
|
||||
l2.addMoved l1
|
||||
doAssert l3.toSeq == [4, 5, 6]
|
||||
doAssert l2.toSeq == [2, 3, 1]
|
||||
l3.add l0
|
||||
doAssert l3.toSeq == [4, 5, 6]
|
||||
block:
|
||||
var c = [0, 1].toList
|
||||
c.addMoved c
|
||||
let s = collect:
|
||||
for i, ci in enumerate(c):
|
||||
if i == 6: break
|
||||
ci
|
||||
doAssert s == [0, 1, 0, 1, 0, 1]
|
||||
|
||||
testCommon initSinglyLinkedList, toSinglyLinkedList
|
||||
testCommon initDoublyLinkedList, toDoublyLinkedList
|
||||
|
||||
Reference in New Issue
Block a user