lists.nim 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Implementation of:
  10. ## * `singly linked lists <#SinglyLinkedList>`_
  11. ## * `doubly linked lists <#DoublyLinkedList>`_
  12. ## * `singly linked rings <#SinglyLinkedRing>`_ (circular lists)
  13. ## * `doubly linked rings <#DoublyLinkedRing>`_ (circular lists)
  14. ##
  15. ## # Basic Usage
  16. ## Because it makes no sense to do otherwise, the `next` and `prev` pointers
  17. ## are not hidden from you and can be manipulated directly for efficiency.
  18. ##
  19. ## ## Lists
  20. runnableExamples:
  21. var list = initDoublyLinkedList[int]()
  22. let
  23. a = newDoublyLinkedNode[int](3)
  24. b = newDoublyLinkedNode[int](7)
  25. c = newDoublyLinkedNode[int](9)
  26. list.add(a)
  27. list.add(b)
  28. list.prepend(c)
  29. assert a.next == b
  30. assert a.prev == c
  31. assert c.next == a
  32. assert c.next.next == b
  33. assert c.prev == nil
  34. assert b.next == nil
  35. ## ## Rings
  36. runnableExamples:
  37. var ring = initSinglyLinkedRing[int]()
  38. let
  39. a = newSinglyLinkedNode[int](3)
  40. b = newSinglyLinkedNode[int](7)
  41. c = newSinglyLinkedNode[int](9)
  42. ring.add(a)
  43. ring.add(b)
  44. ring.prepend(c)
  45. assert c.next == a
  46. assert a.next == b
  47. assert c.next.next == b
  48. assert b.next == c
  49. assert c.next.next.next == c
  50. ## # See also
  51. ## * `deques module <deques.html>`_ for double-ended queues
  52. import std/private/since
  53. when defined(nimPreviewSlimSystem):
  54. import std/assertions
  55. type
  56. DoublyLinkedNodeObj*[T] = object
  57. ## A node of a doubly linked list.
  58. ##
  59. ## It consists of a `value` field, and pointers to `next` and `prev`.
  60. next*: DoublyLinkedNode[T]
  61. prev* {.cursor.}: DoublyLinkedNode[T]
  62. value*: T
  63. DoublyLinkedNode*[T] = ref DoublyLinkedNodeObj[T]
  64. SinglyLinkedNodeObj*[T] = object
  65. ## A node of a singly linked list.
  66. ##
  67. ## It consists of a `value` field, and a pointer to `next`.
  68. next*: SinglyLinkedNode[T]
  69. value*: T
  70. SinglyLinkedNode*[T] = ref SinglyLinkedNodeObj[T]
  71. SinglyLinkedList*[T] = object
  72. ## A singly linked list.
  73. head*: SinglyLinkedNode[T]
  74. tail* {.cursor.}: SinglyLinkedNode[T]
  75. DoublyLinkedList*[T] = object
  76. ## A doubly linked list.
  77. head*: DoublyLinkedNode[T]
  78. tail* {.cursor.}: DoublyLinkedNode[T]
  79. SinglyLinkedRing*[T] = object
  80. ## A singly linked ring.
  81. head*: SinglyLinkedNode[T]
  82. tail* {.cursor.}: SinglyLinkedNode[T]
  83. DoublyLinkedRing*[T] = object
  84. ## A doubly linked ring.
  85. head*: DoublyLinkedNode[T]
  86. SomeLinkedList*[T] = SinglyLinkedList[T] | DoublyLinkedList[T]
  87. SomeLinkedRing*[T] = SinglyLinkedRing[T] | DoublyLinkedRing[T]
  88. SomeLinkedCollection*[T] = SomeLinkedList[T] | SomeLinkedRing[T]
  89. SomeLinkedNode*[T] = SinglyLinkedNode[T] | DoublyLinkedNode[T]
  90. proc initSinglyLinkedList*[T](): SinglyLinkedList[T] =
  91. ## Creates a new singly linked list that is empty.
  92. ##
  93. ## Singly linked lists are initialized by default, so it is not necessary to
  94. ## call this function explicitly.
  95. runnableExamples:
  96. let a = initSinglyLinkedList[int]()
  97. discard
  98. proc initDoublyLinkedList*[T](): DoublyLinkedList[T] =
  99. ## Creates a new doubly linked list that is empty.
  100. ##
  101. ## Doubly linked lists are initialized by default, so it is not necessary to
  102. ## call this function explicitly.
  103. runnableExamples:
  104. let a = initDoublyLinkedList[int]()
  105. discard
  106. proc initSinglyLinkedRing*[T](): SinglyLinkedRing[T] =
  107. ## Creates a new singly linked ring that is empty.
  108. ##
  109. ## Singly linked rings are initialized by default, so it is not necessary to
  110. ## call this function explicitly.
  111. runnableExamples:
  112. let a = initSinglyLinkedRing[int]()
  113. discard
  114. proc initDoublyLinkedRing*[T](): DoublyLinkedRing[T] =
  115. ## Creates a new doubly linked ring that is empty.
  116. ##
  117. ## Doubly linked rings are initialized by default, so it is not necessary to
  118. ## call this function explicitly.
  119. runnableExamples:
  120. let a = initDoublyLinkedRing[int]()
  121. discard
  122. proc newDoublyLinkedNode*[T](value: T): DoublyLinkedNode[T] =
  123. ## Creates a new doubly linked node with the given `value`.
  124. runnableExamples:
  125. let n = newDoublyLinkedNode[int](5)
  126. assert n.value == 5
  127. new(result)
  128. result.value = value
  129. proc newSinglyLinkedNode*[T](value: T): SinglyLinkedNode[T] =
  130. ## Creates a new singly linked node with the given `value`.
  131. runnableExamples:
  132. let n = newSinglyLinkedNode[int](5)
  133. assert n.value == 5
  134. new(result)
  135. result.value = value
  136. func toSinglyLinkedList*[T](elems: openArray[T]): SinglyLinkedList[T] {.since: (1, 5, 1).} =
  137. ## Creates a new `SinglyLinkedList` from the members of `elems`.
  138. runnableExamples:
  139. from std/sequtils import toSeq
  140. let a = [1, 2, 3, 4, 5].toSinglyLinkedList
  141. assert a.toSeq == [1, 2, 3, 4, 5]
  142. result = initSinglyLinkedList[T]()
  143. for elem in elems.items:
  144. result.add(elem)
  145. func toDoublyLinkedList*[T](elems: openArray[T]): DoublyLinkedList[T] {.since: (1, 5, 1).} =
  146. ## Creates a new `DoublyLinkedList` from the members of `elems`.
  147. runnableExamples:
  148. from std/sequtils import toSeq
  149. let a = [1, 2, 3, 4, 5].toDoublyLinkedList
  150. assert a.toSeq == [1, 2, 3, 4, 5]
  151. result = initDoublyLinkedList[T]()
  152. for elem in elems.items:
  153. result.add(elem)
  154. template itemsListImpl() {.dirty.} =
  155. var it = L.head
  156. while it != nil:
  157. yield it.value
  158. it = it.next
  159. template itemsRingImpl() {.dirty.} =
  160. var it = L.head
  161. if it != nil:
  162. while true:
  163. yield it.value
  164. it = it.next
  165. if it == L.head: break
  166. iterator items*[T](L: SomeLinkedList[T]): T =
  167. ## Yields every value of `L`.
  168. ##
  169. ## **See also:**
  170. ## * `mitems iterator <#mitems.i,SomeLinkedList[T]>`_
  171. ## * `nodes iterator <#nodes.i,SomeLinkedList[T]>`_
  172. runnableExamples:
  173. from std/sugar import collect
  174. from std/sequtils import toSeq
  175. let a = collect(initSinglyLinkedList):
  176. for i in 1..3: 10 * i
  177. assert toSeq(items(a)) == toSeq(a)
  178. assert toSeq(a) == @[10, 20, 30]
  179. itemsListImpl()
  180. iterator items*[T](L: SomeLinkedRing[T]): T =
  181. ## Yields every value of `L`.
  182. ##
  183. ## **See also:**
  184. ## * `mitems iterator <#mitems.i,SomeLinkedRing[T]>`_
  185. ## * `nodes iterator <#nodes.i,SomeLinkedRing[T]>`_
  186. runnableExamples:
  187. from std/sugar import collect
  188. from std/sequtils import toSeq
  189. let a = collect(initSinglyLinkedRing):
  190. for i in 1..3: 10 * i
  191. assert toSeq(items(a)) == toSeq(a)
  192. assert toSeq(a) == @[10, 20, 30]
  193. itemsRingImpl()
  194. iterator mitems*[T](L: var SomeLinkedList[T]): var T =
  195. ## Yields every value of `L` so that you can modify it.
  196. ##
  197. ## **See also:**
  198. ## * `items iterator <#items.i,SomeLinkedList[T]>`_
  199. ## * `nodes iterator <#nodes.i,SomeLinkedList[T]>`_
  200. runnableExamples:
  201. var a = initSinglyLinkedList[int]()
  202. for i in 1..5:
  203. a.add(10 * i)
  204. assert $a == "[10, 20, 30, 40, 50]"
  205. for x in mitems(a):
  206. x = 5 * x - 1
  207. assert $a == "[49, 99, 149, 199, 249]"
  208. itemsListImpl()
  209. iterator mitems*[T](L: var SomeLinkedRing[T]): var T =
  210. ## Yields every value of `L` so that you can modify it.
  211. ##
  212. ## **See also:**
  213. ## * `items iterator <#items.i,SomeLinkedRing[T]>`_
  214. ## * `nodes iterator <#nodes.i,SomeLinkedRing[T]>`_
  215. runnableExamples:
  216. var a = initSinglyLinkedRing[int]()
  217. for i in 1..5:
  218. a.add(10 * i)
  219. assert $a == "[10, 20, 30, 40, 50]"
  220. for x in mitems(a):
  221. x = 5 * x - 1
  222. assert $a == "[49, 99, 149, 199, 249]"
  223. itemsRingImpl()
  224. iterator nodes*[T](L: SomeLinkedList[T]): SomeLinkedNode[T] =
  225. ## Iterates over every node of `x`. Removing the current node from the
  226. ## list during traversal is supported.
  227. ##
  228. ## **See also:**
  229. ## * `items iterator <#items.i,SomeLinkedList[T]>`_
  230. ## * `mitems iterator <#mitems.i,SomeLinkedList[T]>`_
  231. runnableExamples:
  232. var a = initDoublyLinkedList[int]()
  233. for i in 1..5:
  234. a.add(10 * i)
  235. assert $a == "[10, 20, 30, 40, 50]"
  236. for x in nodes(a):
  237. if x.value == 30:
  238. a.remove(x)
  239. else:
  240. x.value = 5 * x.value - 1
  241. assert $a == "[49, 99, 199, 249]"
  242. var it = L.head
  243. while it != nil:
  244. let nxt = it.next
  245. yield it
  246. it = nxt
  247. iterator nodes*[T](L: SomeLinkedRing[T]): SomeLinkedNode[T] =
  248. ## Iterates over every node of `x`. Removing the current node from the
  249. ## list during traversal is supported.
  250. ##
  251. ## **See also:**
  252. ## * `items iterator <#items.i,SomeLinkedRing[T]>`_
  253. ## * `mitems iterator <#mitems.i,SomeLinkedRing[T]>`_
  254. runnableExamples:
  255. var a = initDoublyLinkedRing[int]()
  256. for i in 1..5:
  257. a.add(10 * i)
  258. assert $a == "[10, 20, 30, 40, 50]"
  259. for x in nodes(a):
  260. if x.value == 30:
  261. a.remove(x)
  262. else:
  263. x.value = 5 * x.value - 1
  264. assert $a == "[49, 99, 199, 249]"
  265. var it = L.head
  266. if it != nil:
  267. while true:
  268. let nxt = it.next
  269. yield it
  270. it = nxt
  271. if it == L.head: break
  272. proc `$`*[T](L: SomeLinkedCollection[T]): string =
  273. ## Turns a list into its string representation for logging and printing.
  274. runnableExamples:
  275. let a = [1, 2, 3, 4].toSinglyLinkedList
  276. assert $a == "[1, 2, 3, 4]"
  277. result = "["
  278. for x in nodes(L):
  279. if result.len > 1: result.add(", ")
  280. result.addQuoted(x.value)
  281. result.add("]")
  282. proc find*[T](L: SomeLinkedCollection[T], value: T): SomeLinkedNode[T] =
  283. ## Searches in the list for a value. Returns `nil` if the value does not
  284. ## exist.
  285. ##
  286. ## **See also:**
  287. ## * `contains proc <#contains,SomeLinkedCollection[T],T>`_
  288. runnableExamples:
  289. let a = [9, 8].toSinglyLinkedList
  290. assert a.find(9).value == 9
  291. assert a.find(1) == nil
  292. for x in nodes(L):
  293. if x.value == value: return x
  294. proc contains*[T](L: SomeLinkedCollection[T], value: T): bool {.inline.} =
  295. ## Searches in the list for a value. Returns `false` if the value does not
  296. ## exist, `true` otherwise. This allows the usage of the `in` and `notin`
  297. ## operators.
  298. ##
  299. ## **See also:**
  300. ## * `find proc <#find,SomeLinkedCollection[T],T>`_
  301. runnableExamples:
  302. let a = [9, 8].toSinglyLinkedList
  303. assert a.contains(9)
  304. assert 8 in a
  305. assert(not a.contains(1))
  306. assert 2 notin a
  307. result = find(L, value) != nil
  308. proc prepend*[T: SomeLinkedList](a: var T, b: T) {.since: (1, 5, 1).} =
  309. ## Prepends a shallow copy of `b` to the beginning of `a`.
  310. ##
  311. ## **See also:**
  312. ## * `prependMoved proc <#prependMoved,T,T>`_
  313. ## for moving the second list instead of copying
  314. runnableExamples:
  315. from std/sequtils import toSeq
  316. var a = [4, 5].toSinglyLinkedList
  317. let b = [1, 2, 3].toSinglyLinkedList
  318. a.prepend(b)
  319. assert a.toSeq == [1, 2, 3, 4, 5]
  320. assert b.toSeq == [1, 2, 3]
  321. a.prepend(a)
  322. assert a.toSeq == [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
  323. var tmp = b.copy
  324. tmp.addMoved(a)
  325. a = tmp
  326. proc prependMoved*[T: SomeLinkedList](a, b: var T) {.since: (1, 5, 1).} =
  327. ## Moves `b` before the head of `a`. Efficiency: O(1).
  328. ## Note that `b` becomes empty after the operation unless it has the same address as `a`.
  329. ## Self-prepending results in a cycle.
  330. ##
  331. ## **See also:**
  332. ## * `prepend proc <#prepend,T,T>`_
  333. ## for prepending a copy of a list
  334. runnableExamples:
  335. import std/[sequtils, enumerate, sugar]
  336. var
  337. a = [4, 5].toSinglyLinkedList
  338. b = [1, 2, 3].toSinglyLinkedList
  339. c = [0, 1].toSinglyLinkedList
  340. a.prependMoved(b)
  341. assert a.toSeq == [1, 2, 3, 4, 5]
  342. assert b.toSeq == []
  343. c.prependMoved(c)
  344. let s = collect:
  345. for i, ci in enumerate(c):
  346. if i == 6: break
  347. ci
  348. assert s == [0, 1, 0, 1, 0, 1]
  349. b.addMoved(a)
  350. when defined(js): # XXX: swap broken in js; bug #16771
  351. (b, a) = (a, b)
  352. else: swap a, b
  353. proc add*[T](L: var SinglyLinkedList[T], n: SinglyLinkedNode[T]) {.inline.} =
  354. ## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
  355. ##
  356. ## **See also:**
  357. ## * `add proc <#add,SinglyLinkedList[T],T>`_ for appending a value
  358. ## * `prepend proc <#prepend,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
  359. ## for prepending a node
  360. ## * `prepend proc <#prepend,SinglyLinkedList[T],T>`_ for prepending a value
  361. runnableExamples:
  362. var a = initSinglyLinkedList[int]()
  363. let n = newSinglyLinkedNode[int](9)
  364. a.add(n)
  365. assert a.contains(9)
  366. n.next = nil
  367. if L.tail != nil:
  368. assert(L.tail.next == nil)
  369. L.tail.next = n
  370. L.tail = n
  371. if L.head == nil: L.head = n
  372. proc add*[T](L: var SinglyLinkedList[T], value: T) {.inline.} =
  373. ## Appends (adds to the end) a value to `L`. Efficiency: O(1).
  374. ##
  375. ## **See also:**
  376. ## * `add proc <#add,SinglyLinkedList[T],T>`_ for appending a value
  377. ## * `prepend proc <#prepend,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
  378. ## for prepending a node
  379. ## * `prepend proc <#prepend,SinglyLinkedList[T],T>`_ for prepending a value
  380. runnableExamples:
  381. var a = initSinglyLinkedList[int]()
  382. a.add(9)
  383. a.add(8)
  384. assert a.contains(9)
  385. add(L, newSinglyLinkedNode(value))
  386. proc prepend*[T](L: var SinglyLinkedList[T],
  387. n: SinglyLinkedNode[T]) {.inline.} =
  388. ## Prepends (adds to the beginning) a node to `L`. Efficiency: O(1).
  389. ##
  390. ## **See also:**
  391. ## * `add proc <#add,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
  392. ## for appending a node
  393. ## * `add proc <#add,SinglyLinkedList[T],T>`_ for appending a value
  394. ## * `prepend proc <#prepend,SinglyLinkedList[T],T>`_ for prepending a value
  395. runnableExamples:
  396. var a = initSinglyLinkedList[int]()
  397. let n = newSinglyLinkedNode[int](9)
  398. a.prepend(n)
  399. assert a.contains(9)
  400. n.next = L.head
  401. L.head = n
  402. if L.tail == nil: L.tail = n
  403. proc prepend*[T](L: var SinglyLinkedList[T], value: T) {.inline.} =
  404. ## Prepends (adds to the beginning) a node to `L`. Efficiency: O(1).
  405. ##
  406. ## **See also:**
  407. ## * `add proc <#add,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
  408. ## for appending a node
  409. ## * `add proc <#add,SinglyLinkedList[T],T>`_ for appending a value
  410. ## * `prepend proc <#prepend,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
  411. ## for prepending a node
  412. runnableExamples:
  413. var a = initSinglyLinkedList[int]()
  414. a.prepend(9)
  415. a.prepend(8)
  416. assert a.contains(9)
  417. prepend(L, newSinglyLinkedNode(value))
  418. func copy*[T](a: SinglyLinkedList[T]): SinglyLinkedList[T] {.since: (1, 5, 1).} =
  419. ## Creates a shallow copy of `a`.
  420. runnableExamples:
  421. from std/sequtils import toSeq
  422. type Foo = ref object
  423. x: int
  424. var
  425. f = Foo(x: 1)
  426. a = [f].toSinglyLinkedList
  427. let b = a.copy
  428. a.add([f].toSinglyLinkedList)
  429. assert a.toSeq == [f, f]
  430. assert b.toSeq == [f] # b isn't modified...
  431. f.x = 42
  432. assert a.head.value.x == 42
  433. assert b.head.value.x == 42 # ... but the elements are not deep copied
  434. let c = [1, 2, 3].toSinglyLinkedList
  435. assert $c == $c.copy
  436. result = initSinglyLinkedList[T]()
  437. for x in a.items:
  438. result.add(x)
  439. proc addMoved*[T](a, b: var SinglyLinkedList[T]) {.since: (1, 5, 1).} =
  440. ## Moves `b` to the end of `a`. Efficiency: O(1).
  441. ## Note that `b` becomes empty after the operation unless it has the same address as `a`.
  442. ## Self-adding results in a cycle.
  443. ##
  444. ## **See also:**
  445. ## * `add proc <#add,T,T>`_ for adding a copy of a list
  446. runnableExamples:
  447. import std/[sequtils, enumerate, sugar]
  448. var
  449. a = [1, 2, 3].toSinglyLinkedList
  450. b = [4, 5].toSinglyLinkedList
  451. c = [0, 1].toSinglyLinkedList
  452. a.addMoved(b)
  453. assert a.toSeq == [1, 2, 3, 4, 5]
  454. assert b.toSeq == []
  455. c.addMoved(c)
  456. let s = collect:
  457. for i, ci in enumerate(c):
  458. if i == 6: break
  459. ci
  460. assert s == [0, 1, 0, 1, 0, 1]
  461. if b.head != nil:
  462. if a.head == nil:
  463. a.head = b.head
  464. else:
  465. a.tail.next = b.head
  466. a.tail = b.tail
  467. if a.addr != b.addr:
  468. b.head = nil
  469. b.tail = nil
  470. proc add*[T](L: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
  471. ## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
  472. ##
  473. ## **See also:**
  474. ## * `add proc <#add,DoublyLinkedList[T],T>`_ for appending a value
  475. ## * `prepend proc <#prepend,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  476. ## for prepending a node
  477. ## * `prepend proc <#prepend,DoublyLinkedList[T],T>`_ for prepending a value
  478. ## * `remove proc <#remove,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  479. ## for removing a node
  480. runnableExamples:
  481. var a = initDoublyLinkedList[int]()
  482. let n = newDoublyLinkedNode[int](9)
  483. a.add(n)
  484. assert a.contains(9)
  485. n.next = nil
  486. n.prev = L.tail
  487. if L.tail != nil:
  488. assert(L.tail.next == nil)
  489. L.tail.next = n
  490. L.tail = n
  491. if L.head == nil: L.head = n
  492. proc add*[T](L: var DoublyLinkedList[T], value: T) =
  493. ## Appends (adds to the end) a value to `L`. Efficiency: O(1).
  494. ##
  495. ## **See also:**
  496. ## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  497. ## for appending a node
  498. ## * `prepend proc <#prepend,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  499. ## for prepending a node
  500. ## * `prepend proc <#prepend,DoublyLinkedList[T],T>`_ for prepending a value
  501. ## * `remove proc <#remove,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  502. ## for removing a node
  503. runnableExamples:
  504. var a = initDoublyLinkedList[int]()
  505. a.add(9)
  506. a.add(8)
  507. assert a.contains(9)
  508. add(L, newDoublyLinkedNode(value))
  509. proc prepend*[T](L: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
  510. ## Prepends (adds to the beginning) a node `n` to `L`. Efficiency: O(1).
  511. ##
  512. ## **See also:**
  513. ## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  514. ## for appending a node
  515. ## * `add proc <#add,DoublyLinkedList[T],T>`_ for appending a value
  516. ## * `prepend proc <#prepend,DoublyLinkedList[T],T>`_ for prepending a value
  517. ## * `remove proc <#remove,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  518. ## for removing a node
  519. runnableExamples:
  520. var a = initDoublyLinkedList[int]()
  521. let n = newDoublyLinkedNode[int](9)
  522. a.prepend(n)
  523. assert a.contains(9)
  524. n.prev = nil
  525. n.next = L.head
  526. if L.head != nil:
  527. assert(L.head.prev == nil)
  528. L.head.prev = n
  529. L.head = n
  530. if L.tail == nil: L.tail = n
  531. proc prepend*[T](L: var DoublyLinkedList[T], value: T) =
  532. ## Prepends (adds to the beginning) a value to `L`. Efficiency: O(1).
  533. ##
  534. ## **See also:**
  535. ## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  536. ## for appending a node
  537. ## * `add proc <#add,DoublyLinkedList[T],T>`_ for appending a value
  538. ## * `prepend proc <#prepend,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  539. ## for prepending a node
  540. ## * `remove proc <#remove,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  541. ## for removing a node
  542. runnableExamples:
  543. var a = initDoublyLinkedList[int]()
  544. a.prepend(9)
  545. a.prepend(8)
  546. assert a.contains(9)
  547. prepend(L, newDoublyLinkedNode(value))
  548. func copy*[T](a: DoublyLinkedList[T]): DoublyLinkedList[T] {.since: (1, 5, 1).} =
  549. ## Creates a shallow copy of `a`.
  550. runnableExamples:
  551. from std/sequtils import toSeq
  552. type Foo = ref object
  553. x: int
  554. var
  555. f = Foo(x: 1)
  556. a = [f].toDoublyLinkedList
  557. let b = a.copy
  558. a.add([f].toDoublyLinkedList)
  559. assert a.toSeq == [f, f]
  560. assert b.toSeq == [f] # b isn't modified...
  561. f.x = 42
  562. assert a.head.value.x == 42
  563. assert b.head.value.x == 42 # ... but the elements are not deep copied
  564. let c = [1, 2, 3].toDoublyLinkedList
  565. assert $c == $c.copy
  566. result = initDoublyLinkedList[T]()
  567. for x in a.items:
  568. result.add(x)
  569. proc addMoved*[T](a, b: var DoublyLinkedList[T]) {.since: (1, 5, 1).} =
  570. ## Moves `b` to the end of `a`. Efficiency: O(1).
  571. ## Note that `b` becomes empty after the operation unless it has the same address as `a`.
  572. ## Self-adding results in a cycle.
  573. ##
  574. ## **See also:**
  575. ## * `add proc <#add,T,T>`_
  576. ## for adding a copy of a list
  577. runnableExamples:
  578. import std/[sequtils, enumerate, sugar]
  579. var
  580. a = [1, 2, 3].toDoublyLinkedList
  581. b = [4, 5].toDoublyLinkedList
  582. c = [0, 1].toDoublyLinkedList
  583. a.addMoved(b)
  584. assert a.toSeq == [1, 2, 3, 4, 5]
  585. assert b.toSeq == []
  586. c.addMoved(c)
  587. let s = collect:
  588. for i, ci in enumerate(c):
  589. if i == 6: break
  590. ci
  591. assert s == [0, 1, 0, 1, 0, 1]
  592. if b.head != nil:
  593. if a.head == nil:
  594. a.head = b.head
  595. else:
  596. b.head.prev = a.tail
  597. a.tail.next = b.head
  598. a.tail = b.tail
  599. if a.addr != b.addr:
  600. b.head = nil
  601. b.tail = nil
  602. proc add*[T: SomeLinkedList](a: var T, b: T) {.since: (1, 5, 1).} =
  603. ## Appends a shallow copy of `b` to the end of `a`.
  604. ##
  605. ## **See also:**
  606. ## * `addMoved proc <#addMoved,SinglyLinkedList[T],SinglyLinkedList[T]>`_
  607. ## * `addMoved proc <#addMoved,DoublyLinkedList[T],DoublyLinkedList[T]>`_
  608. ## for moving the second list instead of copying
  609. runnableExamples:
  610. from std/sequtils import toSeq
  611. var a = [1, 2, 3].toSinglyLinkedList
  612. let b = [4, 5].toSinglyLinkedList
  613. a.add(b)
  614. assert a.toSeq == [1, 2, 3, 4, 5]
  615. assert b.toSeq == [4, 5]
  616. a.add(a)
  617. assert a.toSeq == [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
  618. var tmp = b.copy
  619. a.addMoved(tmp)
  620. proc remove*[T](L: var SinglyLinkedList[T], n: SinglyLinkedNode[T]): bool {.discardable.} =
  621. ## Removes a node `n` from `L`.
  622. ## Returns `true` if `n` was found in `L`.
  623. ## Efficiency: O(n); the list is traversed until `n` is found.
  624. ## Attempting to remove an element not contained in the list is a no-op.
  625. ## When the list is cyclic, the cycle is preserved after removal.
  626. runnableExamples:
  627. import std/[sequtils, enumerate, sugar]
  628. var a = [0, 1, 2].toSinglyLinkedList
  629. let n = a.head.next
  630. assert n.value == 1
  631. assert a.remove(n) == true
  632. assert a.toSeq == [0, 2]
  633. assert a.remove(n) == false
  634. assert a.toSeq == [0, 2]
  635. a.addMoved(a) # cycle: [0, 2, 0, 2, ...]
  636. a.remove(a.head)
  637. let s = collect:
  638. for i, ai in enumerate(a):
  639. if i == 4: break
  640. ai
  641. assert s == [2, 2, 2, 2]
  642. if n == L.head:
  643. L.head = n.next
  644. if L.tail.next == n:
  645. L.tail.next = L.head # restore cycle
  646. else:
  647. var prev = L.head
  648. while prev.next != n and prev.next != nil:
  649. prev = prev.next
  650. if prev.next == nil:
  651. return false
  652. prev.next = n.next
  653. if L.tail == n:
  654. L.tail = prev # update tail if we removed the last node
  655. true
  656. proc remove*[T](L: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
  657. ## Removes a node `n` from `L`. Efficiency: O(1).
  658. ## This function assumes, for the sake of efficiency, that `n` is contained in `L`,
  659. ## otherwise the effects are undefined.
  660. ## When the list is cyclic, the cycle is preserved after removal.
  661. runnableExamples:
  662. import std/[sequtils, enumerate, sugar]
  663. var a = [0, 1, 2].toSinglyLinkedList
  664. let n = a.head.next
  665. assert n.value == 1
  666. a.remove(n)
  667. assert a.toSeq == [0, 2]
  668. a.remove(n)
  669. assert a.toSeq == [0, 2]
  670. a.addMoved(a) # cycle: [0, 2, 0, 2, ...]
  671. a.remove(a.head)
  672. let s = collect:
  673. for i, ai in enumerate(a):
  674. if i == 4: break
  675. ai
  676. assert s == [2, 2, 2, 2]
  677. if n == L.tail: L.tail = n.prev
  678. if n == L.head: L.head = n.next
  679. if n.next != nil: n.next.prev = n.prev
  680. if n.prev != nil: n.prev.next = n.next
  681. proc add*[T](L: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
  682. ## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
  683. ##
  684. ## **See also:**
  685. ## * `add proc <#add,SinglyLinkedRing[T],T>`_ for appending a value
  686. ## * `prepend proc <#prepend,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
  687. ## for prepending a node
  688. ## * `prepend proc <#prepend,SinglyLinkedRing[T],T>`_ for prepending a value
  689. runnableExamples:
  690. var a = initSinglyLinkedRing[int]()
  691. let n = newSinglyLinkedNode[int](9)
  692. a.add(n)
  693. assert a.contains(9)
  694. if L.head != nil:
  695. n.next = L.head
  696. assert(L.tail != nil)
  697. L.tail.next = n
  698. else:
  699. n.next = n
  700. L.head = n
  701. L.tail = n
  702. proc add*[T](L: var SinglyLinkedRing[T], value: T) =
  703. ## Appends (adds to the end) a value to `L`. Efficiency: O(1).
  704. ##
  705. ## **See also:**
  706. ## * `add proc <#add,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
  707. ## for appending a node
  708. ## * `prepend proc <#prepend,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
  709. ## for prepending a node
  710. ## * `prepend proc <#prepend,SinglyLinkedRing[T],T>`_ for prepending a value
  711. runnableExamples:
  712. var a = initSinglyLinkedRing[int]()
  713. a.add(9)
  714. a.add(8)
  715. assert a.contains(9)
  716. add(L, newSinglyLinkedNode(value))
  717. proc prepend*[T](L: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
  718. ## Prepends (adds to the beginning) a node `n` to `L`. Efficiency: O(1).
  719. ##
  720. ## **See also:**
  721. ## * `add proc <#add,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
  722. ## for appending a node
  723. ## * `add proc <#add,SinglyLinkedRing[T],T>`_ for appending a value
  724. ## * `prepend proc <#prepend,SinglyLinkedRing[T],T>`_ for prepending a value
  725. runnableExamples:
  726. var a = initSinglyLinkedRing[int]()
  727. let n = newSinglyLinkedNode[int](9)
  728. a.prepend(n)
  729. assert a.contains(9)
  730. if L.head != nil:
  731. n.next = L.head
  732. assert(L.tail != nil)
  733. L.tail.next = n
  734. else:
  735. n.next = n
  736. L.tail = n
  737. L.head = n
  738. proc prepend*[T](L: var SinglyLinkedRing[T], value: T) =
  739. ## Prepends (adds to the beginning) a value to `L`. Efficiency: O(1).
  740. ##
  741. ## **See also:**
  742. ## * `add proc <#add,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
  743. ## for appending a node
  744. ## * `add proc <#add,SinglyLinkedRing[T],T>`_ for appending a value
  745. ## * `prepend proc <#prepend,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
  746. ## for prepending a node
  747. runnableExamples:
  748. var a = initSinglyLinkedRing[int]()
  749. a.prepend(9)
  750. a.prepend(8)
  751. assert a.contains(9)
  752. prepend(L, newSinglyLinkedNode(value))
  753. proc add*[T](L: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
  754. ## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
  755. ##
  756. ## **See also:**
  757. ## * `add proc <#add,DoublyLinkedRing[T],T>`_ for appending a value
  758. ## * `prepend proc <#prepend,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  759. ## for prepending a node
  760. ## * `prepend proc <#prepend,DoublyLinkedRing[T],T>`_ for prepending a value
  761. ## * `remove proc <#remove,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  762. ## for removing a node
  763. runnableExamples:
  764. var a = initDoublyLinkedRing[int]()
  765. let n = newDoublyLinkedNode[int](9)
  766. a.add(n)
  767. assert a.contains(9)
  768. if L.head != nil:
  769. n.next = L.head
  770. n.prev = L.head.prev
  771. L.head.prev.next = n
  772. L.head.prev = n
  773. else:
  774. n.prev = n
  775. n.next = n
  776. L.head = n
  777. proc add*[T](L: var DoublyLinkedRing[T], value: T) =
  778. ## Appends (adds to the end) a value to `L`. Efficiency: O(1).
  779. ##
  780. ## **See also:**
  781. ## * `add proc <#add,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  782. ## for appending a node
  783. ## * `prepend proc <#prepend,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  784. ## for prepending a node
  785. ## * `prepend proc <#prepend,DoublyLinkedRing[T],T>`_ for prepending a value
  786. ## * `remove proc <#remove,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  787. ## for removing a node
  788. runnableExamples:
  789. var a = initDoublyLinkedRing[int]()
  790. a.add(9)
  791. a.add(8)
  792. assert a.contains(9)
  793. add(L, newDoublyLinkedNode(value))
  794. proc prepend*[T](L: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
  795. ## Prepends (adds to the beginning) a node `n` to `L`. Efficiency: O(1).
  796. ##
  797. ## **See also:**
  798. ## * `add proc <#add,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  799. ## for appending a node
  800. ## * `add proc <#add,DoublyLinkedRing[T],T>`_ for appending a value
  801. ## * `prepend proc <#prepend,DoublyLinkedRing[T],T>`_ for prepending a value
  802. ## * `remove proc <#remove,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  803. ## for removing a node
  804. runnableExamples:
  805. var a = initDoublyLinkedRing[int]()
  806. let n = newDoublyLinkedNode[int](9)
  807. a.prepend(n)
  808. assert a.contains(9)
  809. if L.head != nil:
  810. n.next = L.head
  811. n.prev = L.head.prev
  812. L.head.prev.next = n
  813. L.head.prev = n
  814. else:
  815. n.prev = n
  816. n.next = n
  817. L.head = n
  818. proc prepend*[T](L: var DoublyLinkedRing[T], value: T) =
  819. ## Prepends (adds to the beginning) a value to `L`. Efficiency: O(1).
  820. ##
  821. ## **See also:**
  822. ## * `add proc <#add,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  823. ## for appending a node
  824. ## * `add proc <#add,DoublyLinkedRing[T],T>`_ for appending a value
  825. ## * `prepend proc <#prepend,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  826. ## for prepending a node
  827. ## * `remove proc <#remove,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  828. ## for removing a node
  829. runnableExamples:
  830. var a = initDoublyLinkedRing[int]()
  831. a.prepend(9)
  832. a.prepend(8)
  833. assert a.contains(9)
  834. prepend(L, newDoublyLinkedNode(value))
  835. proc remove*[T](L: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
  836. ## Removes `n` from `L`. Efficiency: O(1).
  837. ## This function assumes, for the sake of efficiency, that `n` is contained in `L`,
  838. ## otherwise the effects are undefined.
  839. runnableExamples:
  840. var a = initDoublyLinkedRing[int]()
  841. let n = newDoublyLinkedNode[int](5)
  842. a.add(n)
  843. assert 5 in a
  844. a.remove(n)
  845. assert 5 notin a
  846. n.next.prev = n.prev
  847. n.prev.next = n.next
  848. if n == L.head:
  849. let p = L.head.prev
  850. if p == L.head:
  851. # only one element left:
  852. L.head = nil
  853. else:
  854. L.head = p
  855. proc append*[T](a: var (SinglyLinkedList[T] | SinglyLinkedRing[T]),
  856. b: SinglyLinkedList[T] | SinglyLinkedNode[T] | T) =
  857. ## Alias for `a.add(b)`.
  858. ##
  859. ## **See also:**
  860. ## * `add proc <#add,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
  861. ## * `add proc <#add,SinglyLinkedList[T],T>`_
  862. ## * `add proc <#add,T,T>`_
  863. a.add(b)
  864. proc append*[T](a: var (DoublyLinkedList[T] | DoublyLinkedRing[T]),
  865. b: DoublyLinkedList[T] | DoublyLinkedNode[T] | T) =
  866. ## Alias for `a.add(b)`.
  867. ##
  868. ## **See also:**
  869. ## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  870. ## * `add proc <#add,DoublyLinkedList[T],T>`_
  871. ## * `add proc <#add,T,T>`_
  872. a.add(b)
  873. proc appendMoved*[T: SomeLinkedList](a, b: var T) {.since: (1, 5, 1).} =
  874. ## Alias for `a.addMoved(b)`.
  875. ##
  876. ## **See also:**
  877. ## * `addMoved proc <#addMoved,SinglyLinkedList[T],SinglyLinkedList[T]>`_
  878. ## * `addMoved proc <#addMoved,DoublyLinkedList[T],DoublyLinkedList[T]>`_
  879. a.addMoved(b)