lists.nim 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. swap a, b
  333. proc add*[T](L: var SinglyLinkedList[T], n: SinglyLinkedNode[T]) {.inline.} =
  334. ## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
  335. ##
  336. ## **See also:**
  337. ## * `add proc <#add,SinglyLinkedList[T],T>`_ for appending a value
  338. ## * `prepend proc <#prepend,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
  339. ## for prepending a node
  340. ## * `prepend proc <#prepend,SinglyLinkedList[T],T>`_ for prepending a value
  341. runnableExamples:
  342. var a = initSinglyLinkedList[int]()
  343. let n = newSinglyLinkedNode[int](9)
  344. a.add(n)
  345. assert a.contains(9)
  346. n.next = nil
  347. if L.tail != nil:
  348. assert(L.tail.next == nil)
  349. L.tail.next = n
  350. L.tail = n
  351. if L.head == nil: L.head = n
  352. proc add*[T](L: var SinglyLinkedList[T], value: T) {.inline.} =
  353. ## Appends (adds to the end) a value to `L`. Efficiency: O(1).
  354. ##
  355. ## **See also:**
  356. ## * `add proc <#add,SinglyLinkedList[T],T>`_ for appending a value
  357. ## * `prepend proc <#prepend,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
  358. ## for prepending a node
  359. ## * `prepend proc <#prepend,SinglyLinkedList[T],T>`_ for prepending a value
  360. runnableExamples:
  361. var a = initSinglyLinkedList[int]()
  362. a.add(9)
  363. a.add(8)
  364. assert a.contains(9)
  365. add(L, newSinglyLinkedNode(value))
  366. proc prepend*[T](L: var SinglyLinkedList[T],
  367. n: SinglyLinkedNode[T]) {.inline.} =
  368. ## Prepends (adds to the beginning) a node to `L`. Efficiency: O(1).
  369. ##
  370. ## **See also:**
  371. ## * `add proc <#add,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
  372. ## for appending a node
  373. ## * `add proc <#add,SinglyLinkedList[T],T>`_ for appending a value
  374. ## * `prepend proc <#prepend,SinglyLinkedList[T],T>`_ for prepending a value
  375. runnableExamples:
  376. var a = initSinglyLinkedList[int]()
  377. let n = newSinglyLinkedNode[int](9)
  378. a.prepend(n)
  379. assert a.contains(9)
  380. n.next = L.head
  381. L.head = n
  382. if L.tail == nil: L.tail = n
  383. proc prepend*[T](L: var SinglyLinkedList[T], value: T) {.inline.} =
  384. ## Prepends (adds to the beginning) a node to `L`. Efficiency: O(1).
  385. ##
  386. ## **See also:**
  387. ## * `add proc <#add,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
  388. ## for appending a node
  389. ## * `add proc <#add,SinglyLinkedList[T],T>`_ for appending a value
  390. ## * `prepend proc <#prepend,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
  391. ## for prepending a node
  392. runnableExamples:
  393. var a = initSinglyLinkedList[int]()
  394. a.prepend(9)
  395. a.prepend(8)
  396. assert a.contains(9)
  397. prepend(L, newSinglyLinkedNode(value))
  398. func copy*[T](a: SinglyLinkedList[T]): SinglyLinkedList[T] {.since: (1, 5, 1).} =
  399. ## Creates a shallow copy of `a`.
  400. runnableExamples:
  401. from std/sequtils import toSeq
  402. type Foo = ref object
  403. x: int
  404. var
  405. f = Foo(x: 1)
  406. a = [f].toSinglyLinkedList
  407. let b = a.copy
  408. a.add([f].toSinglyLinkedList)
  409. assert a.toSeq == [f, f]
  410. assert b.toSeq == [f] # b isn't modified...
  411. f.x = 42
  412. assert a.head.value.x == 42
  413. assert b.head.value.x == 42 # ... but the elements are not deep copied
  414. let c = [1, 2, 3].toSinglyLinkedList
  415. assert $c == $c.copy
  416. result = initSinglyLinkedList[T]()
  417. for x in a.items:
  418. result.add(x)
  419. proc addMoved*[T](a, b: var SinglyLinkedList[T]) {.since: (1, 5, 1).} =
  420. ## Moves `b` to the end of `a`. Efficiency: O(1).
  421. ## Note that `b` becomes empty after the operation unless it has the same address as `a`.
  422. ## Self-adding results in a cycle.
  423. ##
  424. ## **See also:**
  425. ## * `add proc <#add,T,T>`_ for adding a copy of a list
  426. runnableExamples:
  427. import std/[sequtils, enumerate, sugar]
  428. var
  429. a = [1, 2, 3].toSinglyLinkedList
  430. b = [4, 5].toSinglyLinkedList
  431. c = [0, 1].toSinglyLinkedList
  432. a.addMoved(b)
  433. assert a.toSeq == [1, 2, 3, 4, 5]
  434. assert b.toSeq == []
  435. c.addMoved(c)
  436. let s = collect:
  437. for i, ci in enumerate(c):
  438. if i == 6: break
  439. ci
  440. assert s == [0, 1, 0, 1, 0, 1]
  441. if b.head != nil:
  442. if a.head == nil:
  443. a.head = b.head
  444. else:
  445. a.tail.next = b.head
  446. a.tail = b.tail
  447. if a.addr != b.addr:
  448. b.head = nil
  449. b.tail = nil
  450. proc add*[T](L: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
  451. ## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
  452. ##
  453. ## **See also:**
  454. ## * `add proc <#add,DoublyLinkedList[T],T>`_ for appending a value
  455. ## * `prepend proc <#prepend,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  456. ## for prepending a node
  457. ## * `prepend proc <#prepend,DoublyLinkedList[T],T>`_ for prepending a value
  458. ## * `remove proc <#remove,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  459. ## for removing a node
  460. runnableExamples:
  461. var a = initDoublyLinkedList[int]()
  462. let n = newDoublyLinkedNode[int](9)
  463. a.add(n)
  464. assert a.contains(9)
  465. n.next = nil
  466. n.prev = L.tail
  467. if L.tail != nil:
  468. assert(L.tail.next == nil)
  469. L.tail.next = n
  470. L.tail = n
  471. if L.head == nil: L.head = n
  472. proc add*[T](L: var DoublyLinkedList[T], value: T) =
  473. ## Appends (adds to the end) a value to `L`. Efficiency: O(1).
  474. ##
  475. ## **See also:**
  476. ## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  477. ## for appending a node
  478. ## * `prepend proc <#prepend,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  479. ## for prepending a node
  480. ## * `prepend proc <#prepend,DoublyLinkedList[T],T>`_ for prepending a value
  481. ## * `remove proc <#remove,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  482. ## for removing a node
  483. runnableExamples:
  484. var a = initDoublyLinkedList[int]()
  485. a.add(9)
  486. a.add(8)
  487. assert a.contains(9)
  488. add(L, newDoublyLinkedNode(value))
  489. proc prepend*[T](L: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
  490. ## Prepends (adds to the beginning) a node `n` to `L`. Efficiency: O(1).
  491. ##
  492. ## **See also:**
  493. ## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  494. ## for appending a node
  495. ## * `add proc <#add,DoublyLinkedList[T],T>`_ for appending a value
  496. ## * `prepend proc <#prepend,DoublyLinkedList[T],T>`_ for prepending a value
  497. ## * `remove proc <#remove,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  498. ## for removing a node
  499. runnableExamples:
  500. var a = initDoublyLinkedList[int]()
  501. let n = newDoublyLinkedNode[int](9)
  502. a.prepend(n)
  503. assert a.contains(9)
  504. n.prev = nil
  505. n.next = L.head
  506. if L.head != nil:
  507. assert(L.head.prev == nil)
  508. L.head.prev = n
  509. L.head = n
  510. if L.tail == nil: L.tail = n
  511. proc prepend*[T](L: var DoublyLinkedList[T], value: T) =
  512. ## Prepends (adds to the beginning) a value 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],DoublyLinkedNode[T]>`_
  519. ## for prepending a node
  520. ## * `remove proc <#remove,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  521. ## for removing a node
  522. runnableExamples:
  523. var a = initDoublyLinkedList[int]()
  524. a.prepend(9)
  525. a.prepend(8)
  526. assert a.contains(9)
  527. prepend(L, newDoublyLinkedNode(value))
  528. func copy*[T](a: DoublyLinkedList[T]): DoublyLinkedList[T] {.since: (1, 5, 1).} =
  529. ## Creates a shallow copy of `a`.
  530. runnableExamples:
  531. from std/sequtils import toSeq
  532. type Foo = ref object
  533. x: int
  534. var
  535. f = Foo(x: 1)
  536. a = [f].toDoublyLinkedList
  537. let b = a.copy
  538. a.add([f].toDoublyLinkedList)
  539. assert a.toSeq == [f, f]
  540. assert b.toSeq == [f] # b isn't modified...
  541. f.x = 42
  542. assert a.head.value.x == 42
  543. assert b.head.value.x == 42 # ... but the elements are not deep copied
  544. let c = [1, 2, 3].toDoublyLinkedList
  545. assert $c == $c.copy
  546. result = initDoublyLinkedList[T]()
  547. for x in a.items:
  548. result.add(x)
  549. proc addMoved*[T](a, b: var DoublyLinkedList[T]) {.since: (1, 5, 1).} =
  550. ## Moves `b` to the end of `a`. Efficiency: O(1).
  551. ## Note that `b` becomes empty after the operation unless it has the same address as `a`.
  552. ## Self-adding results in a cycle.
  553. ##
  554. ## **See also:**
  555. ## * `add proc <#add,T,T>`_
  556. ## for adding a copy of a list
  557. runnableExamples:
  558. import std/[sequtils, enumerate, sugar]
  559. var
  560. a = [1, 2, 3].toDoublyLinkedList
  561. b = [4, 5].toDoublyLinkedList
  562. c = [0, 1].toDoublyLinkedList
  563. a.addMoved(b)
  564. assert a.toSeq == [1, 2, 3, 4, 5]
  565. assert b.toSeq == []
  566. c.addMoved(c)
  567. let s = collect:
  568. for i, ci in enumerate(c):
  569. if i == 6: break
  570. ci
  571. assert s == [0, 1, 0, 1, 0, 1]
  572. if b.head != nil:
  573. if a.head == nil:
  574. a.head = b.head
  575. else:
  576. b.head.prev = a.tail
  577. a.tail.next = b.head
  578. a.tail = b.tail
  579. if a.addr != b.addr:
  580. b.head = nil
  581. b.tail = nil
  582. proc add*[T: SomeLinkedList](a: var T, b: T) {.since: (1, 5, 1).} =
  583. ## Appends a shallow copy of `b` to the end of `a`.
  584. ##
  585. ## **See also:**
  586. ## * `addMoved proc <#addMoved,SinglyLinkedList[T],SinglyLinkedList[T]>`_
  587. ## * `addMoved proc <#addMoved,DoublyLinkedList[T],DoublyLinkedList[T]>`_
  588. ## for moving the second list instead of copying
  589. runnableExamples:
  590. from std/sequtils import toSeq
  591. var a = [1, 2, 3].toSinglyLinkedList
  592. let b = [4, 5].toSinglyLinkedList
  593. a.add(b)
  594. assert a.toSeq == [1, 2, 3, 4, 5]
  595. assert b.toSeq == [4, 5]
  596. a.add(a)
  597. assert a.toSeq == [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
  598. var tmp = b.copy
  599. a.addMoved(tmp)
  600. proc remove*[T](L: var SinglyLinkedList[T], n: SinglyLinkedNode[T]): bool {.discardable.} =
  601. ## Removes a node `n` from `L`.
  602. ## Returns `true` if `n` was found in `L`.
  603. ## Efficiency: O(n); the list is traversed until `n` is found.
  604. ## Attempting to remove an element not contained in the list is a no-op.
  605. ## When the list is cyclic, the cycle is preserved after removal.
  606. runnableExamples:
  607. import std/[sequtils, enumerate, sugar]
  608. var a = [0, 1, 2].toSinglyLinkedList
  609. let n = a.head.next
  610. assert n.value == 1
  611. assert a.remove(n) == true
  612. assert a.toSeq == [0, 2]
  613. assert a.remove(n) == false
  614. assert a.toSeq == [0, 2]
  615. a.addMoved(a) # cycle: [0, 2, 0, 2, ...]
  616. a.remove(a.head)
  617. let s = collect:
  618. for i, ai in enumerate(a):
  619. if i == 4: break
  620. ai
  621. assert s == [2, 2, 2, 2]
  622. if n == L.head:
  623. L.head = n.next
  624. if L.tail.next == n:
  625. L.tail.next = L.head # restore cycle
  626. else:
  627. var prev {.cursor.} = L.head
  628. while prev.next != n and prev.next != nil:
  629. prev = prev.next
  630. if prev.next == nil:
  631. return false
  632. prev.next = n.next
  633. if L.tail == n:
  634. L.tail = prev # update tail if we removed the last node
  635. true
  636. proc remove*[T](L: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
  637. ## Removes a node `n` from `L`. Efficiency: O(1).
  638. ## This function assumes, for the sake of efficiency, that `n` is contained in `L`,
  639. ## otherwise the effects are undefined.
  640. ## When the list is cyclic, the cycle is preserved after removal.
  641. runnableExamples:
  642. import std/[sequtils, enumerate, sugar]
  643. var a = [0, 1, 2].toSinglyLinkedList
  644. let n = a.head.next
  645. assert n.value == 1
  646. a.remove(n)
  647. assert a.toSeq == [0, 2]
  648. a.remove(n)
  649. assert a.toSeq == [0, 2]
  650. a.addMoved(a) # cycle: [0, 2, 0, 2, ...]
  651. a.remove(a.head)
  652. let s = collect:
  653. for i, ai in enumerate(a):
  654. if i == 4: break
  655. ai
  656. assert s == [2, 2, 2, 2]
  657. if n == L.tail: L.tail = n.prev
  658. if n == L.head: L.head = n.next
  659. if n.next != nil: n.next.prev = n.prev
  660. if n.prev != nil: n.prev.next = n.next
  661. proc add*[T](L: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
  662. ## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
  663. ##
  664. ## **See also:**
  665. ## * `add proc <#add,SinglyLinkedRing[T],T>`_ for appending a value
  666. ## * `prepend proc <#prepend,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
  667. ## for prepending a node
  668. ## * `prepend proc <#prepend,SinglyLinkedRing[T],T>`_ for prepending a value
  669. runnableExamples:
  670. var a = initSinglyLinkedRing[int]()
  671. let n = newSinglyLinkedNode[int](9)
  672. a.add(n)
  673. assert a.contains(9)
  674. if L.head != nil:
  675. n.next = L.head
  676. assert(L.tail != nil)
  677. L.tail.next = n
  678. else:
  679. n.next = n
  680. L.head = n
  681. L.tail = n
  682. proc add*[T](L: var SinglyLinkedRing[T], value: T) =
  683. ## Appends (adds to the end) a value to `L`. Efficiency: O(1).
  684. ##
  685. ## **See also:**
  686. ## * `add proc <#add,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
  687. ## for appending a node
  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. a.add(9)
  694. a.add(8)
  695. assert a.contains(9)
  696. add(L, newSinglyLinkedNode(value))
  697. proc prepend*[T](L: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
  698. ## Prepends (adds to the beginning) a node `n` to `L`. Efficiency: O(1).
  699. ##
  700. ## **See also:**
  701. ## * `add proc <#add,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
  702. ## for appending a node
  703. ## * `add proc <#add,SinglyLinkedRing[T],T>`_ for appending a value
  704. ## * `prepend proc <#prepend,SinglyLinkedRing[T],T>`_ for prepending a value
  705. runnableExamples:
  706. var a = initSinglyLinkedRing[int]()
  707. let n = newSinglyLinkedNode[int](9)
  708. a.prepend(n)
  709. assert a.contains(9)
  710. if L.head != nil:
  711. n.next = L.head
  712. assert(L.tail != nil)
  713. L.tail.next = n
  714. else:
  715. n.next = n
  716. L.tail = n
  717. L.head = n
  718. proc prepend*[T](L: var SinglyLinkedRing[T], value: T) =
  719. ## Prepends (adds to the beginning) a value to `L`. Efficiency: O(1).
  720. ##
  721. ## **See also:**
  722. ## * `add proc <#add,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
  723. ## for appending a node
  724. ## * `add proc <#add,SinglyLinkedRing[T],T>`_ for appending a value
  725. ## * `prepend proc <#prepend,SinglyLinkedRing[T],SinglyLinkedNode[T]>`_
  726. ## for prepending a node
  727. runnableExamples:
  728. var a = initSinglyLinkedRing[int]()
  729. a.prepend(9)
  730. a.prepend(8)
  731. assert a.contains(9)
  732. prepend(L, newSinglyLinkedNode(value))
  733. proc add*[T](L: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
  734. ## Appends (adds to the end) a node `n` to `L`. Efficiency: O(1).
  735. ##
  736. ## **See also:**
  737. ## * `add proc <#add,DoublyLinkedRing[T],T>`_ for appending a value
  738. ## * `prepend proc <#prepend,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  739. ## for prepending a node
  740. ## * `prepend proc <#prepend,DoublyLinkedRing[T],T>`_ for prepending a value
  741. ## * `remove proc <#remove,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  742. ## for removing a node
  743. runnableExamples:
  744. var a = initDoublyLinkedRing[int]()
  745. let n = newDoublyLinkedNode[int](9)
  746. a.add(n)
  747. assert a.contains(9)
  748. if L.head != nil:
  749. n.next = L.head
  750. n.prev = L.head.prev
  751. L.head.prev.next = n
  752. L.head.prev = n
  753. else:
  754. n.prev = n
  755. n.next = n
  756. L.head = n
  757. proc add*[T](L: var DoublyLinkedRing[T], value: T) =
  758. ## Appends (adds to the end) a value to `L`. Efficiency: O(1).
  759. ##
  760. ## **See also:**
  761. ## * `add proc <#add,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  762. ## for appending a node
  763. ## * `prepend proc <#prepend,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  764. ## for prepending a node
  765. ## * `prepend proc <#prepend,DoublyLinkedRing[T],T>`_ for prepending a value
  766. ## * `remove proc <#remove,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  767. ## for removing a node
  768. runnableExamples:
  769. var a = initDoublyLinkedRing[int]()
  770. a.add(9)
  771. a.add(8)
  772. assert a.contains(9)
  773. add(L, newDoublyLinkedNode(value))
  774. proc prepend*[T](L: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
  775. ## Prepends (adds to the beginning) a node `n` to `L`. Efficiency: O(1).
  776. ##
  777. ## **See also:**
  778. ## * `add proc <#add,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  779. ## for appending a node
  780. ## * `add proc <#add,DoublyLinkedRing[T],T>`_ for appending a value
  781. ## * `prepend proc <#prepend,DoublyLinkedRing[T],T>`_ for prepending a value
  782. ## * `remove proc <#remove,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  783. ## for removing a node
  784. runnableExamples:
  785. var a = initDoublyLinkedRing[int]()
  786. let n = newDoublyLinkedNode[int](9)
  787. a.prepend(n)
  788. assert a.contains(9)
  789. if L.head != nil:
  790. n.next = L.head
  791. n.prev = L.head.prev
  792. L.head.prev.next = n
  793. L.head.prev = n
  794. else:
  795. n.prev = n
  796. n.next = n
  797. L.head = n
  798. proc prepend*[T](L: var DoublyLinkedRing[T], value: T) =
  799. ## Prepends (adds to the beginning) a value to `L`. Efficiency: O(1).
  800. ##
  801. ## **See also:**
  802. ## * `add proc <#add,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  803. ## for appending a node
  804. ## * `add proc <#add,DoublyLinkedRing[T],T>`_ for appending a value
  805. ## * `prepend proc <#prepend,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  806. ## for prepending a node
  807. ## * `remove proc <#remove,DoublyLinkedRing[T],DoublyLinkedNode[T]>`_
  808. ## for removing a node
  809. runnableExamples:
  810. var a = initDoublyLinkedRing[int]()
  811. a.prepend(9)
  812. a.prepend(8)
  813. assert a.contains(9)
  814. prepend(L, newDoublyLinkedNode(value))
  815. proc remove*[T](L: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
  816. ## Removes `n` from `L`. Efficiency: O(1).
  817. ## This function assumes, for the sake of efficiency, that `n` is contained in `L`,
  818. ## otherwise the effects are undefined.
  819. runnableExamples:
  820. var a = initDoublyLinkedRing[int]()
  821. let n = newDoublyLinkedNode[int](5)
  822. a.add(n)
  823. assert 5 in a
  824. a.remove(n)
  825. assert 5 notin a
  826. n.next.prev = n.prev
  827. n.prev.next = n.next
  828. if n == L.head:
  829. let p = L.head.prev
  830. if p == L.head:
  831. # only one element left:
  832. L.head = nil
  833. else:
  834. L.head = p
  835. proc append*[T](a: var (SinglyLinkedList[T] | SinglyLinkedRing[T]),
  836. b: SinglyLinkedList[T] | SinglyLinkedNode[T] | T) =
  837. ## Alias for `a.add(b)`.
  838. ##
  839. ## **See also:**
  840. ## * `add proc <#add,SinglyLinkedList[T],SinglyLinkedNode[T]>`_
  841. ## * `add proc <#add,SinglyLinkedList[T],T>`_
  842. ## * `add proc <#add,T,T>`_
  843. a.add(b)
  844. proc append*[T](a: var (DoublyLinkedList[T] | DoublyLinkedRing[T]),
  845. b: DoublyLinkedList[T] | DoublyLinkedNode[T] | T) =
  846. ## Alias for `a.add(b)`.
  847. ##
  848. ## **See also:**
  849. ## * `add proc <#add,DoublyLinkedList[T],DoublyLinkedNode[T]>`_
  850. ## * `add proc <#add,DoublyLinkedList[T],T>`_
  851. ## * `add proc <#add,T,T>`_
  852. a.add(b)
  853. proc appendMoved*[T: SomeLinkedList](a, b: var T) {.since: (1, 5, 1).} =
  854. ## Alias for `a.addMoved(b)`.
  855. ##
  856. ## **See also:**
  857. ## * `addMoved proc <#addMoved,SinglyLinkedList[T],SinglyLinkedList[T]>`_
  858. ## * `addMoved proc <#addMoved,DoublyLinkedList[T],DoublyLinkedList[T]>`_
  859. a.addMoved(b)
  860. func toSinglyLinkedList*[T](elems: openArray[T]): SinglyLinkedList[T] {.since: (1, 5, 1).} =
  861. ## Creates a new `SinglyLinkedList` from the members of `elems`.
  862. runnableExamples:
  863. from std/sequtils import toSeq
  864. let a = [1, 2, 3, 4, 5].toSinglyLinkedList
  865. assert a.toSeq == [1, 2, 3, 4, 5]
  866. result = initSinglyLinkedList[T]()
  867. for elem in elems.items:
  868. result.add(elem)
  869. func toSinglyLinkedRing*[T](elems: openArray[T]): SinglyLinkedRing[T] =
  870. ## Creates a new `SinglyLinkedRing` from the members of `elems`.
  871. runnableExamples:
  872. from std/sequtils import toSeq
  873. let a = [1, 2, 3, 4, 5].toSinglyLinkedRing
  874. assert a.toSeq == [1, 2, 3, 4, 5]
  875. result = initSinglyLinkedRing[T]()
  876. for elem in elems.items:
  877. result.add(elem)
  878. func toDoublyLinkedList*[T](elems: openArray[T]): DoublyLinkedList[T] {.since: (1, 5, 1).} =
  879. ## Creates a new `DoublyLinkedList` from the members of `elems`.
  880. runnableExamples:
  881. from std/sequtils import toSeq
  882. let a = [1, 2, 3, 4, 5].toDoublyLinkedList
  883. assert a.toSeq == [1, 2, 3, 4, 5]
  884. result = initDoublyLinkedList[T]()
  885. for elem in elems.items:
  886. result.add(elem)
  887. func toDoublyLinkedRing*[T](elems: openArray[T]): DoublyLinkedRing[T] =
  888. ## Creates a new `DoublyLinkedRing` from the members of `elems`.
  889. runnableExamples:
  890. from std/sequtils import toSeq
  891. let a = [1, 2, 3, 4, 5].toDoublyLinkedRing
  892. assert a.toSeq == [1, 2, 3, 4, 5]
  893. result = initDoublyLinkedRing[T]()
  894. for elem in elems.items:
  895. result.add(elem)