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