lists.nim 30 KB

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