lists.nim 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 singly and doubly linked lists. Because it makes no sense
  10. ## to do so, the 'next' and 'prev' pointers are not hidden from you and can
  11. ## be manipulated directly for efficiency.
  12. when not defined(nimhygiene):
  13. {.pragma: dirty.}
  14. type
  15. DoublyLinkedNodeObj*[T] = object ## a node a doubly linked list consists of
  16. next*, prev*: ref DoublyLinkedNodeObj[T]
  17. value*: T
  18. DoublyLinkedNode*[T] = ref DoublyLinkedNodeObj[T]
  19. SinglyLinkedNodeObj*[T] = object ## a node a singly linked list consists of
  20. next*: ref SinglyLinkedNodeObj[T]
  21. value*: T
  22. SinglyLinkedNode*[T] = ref SinglyLinkedNodeObj[T]
  23. SinglyLinkedList*[T] = object ## a singly linked list
  24. head*, tail*: SinglyLinkedNode[T]
  25. DoublyLinkedList*[T] = object ## a doubly linked list
  26. head*, tail*: DoublyLinkedNode[T]
  27. SinglyLinkedRing*[T] = object ## a singly linked ring
  28. head*, tail*: SinglyLinkedNode[T]
  29. DoublyLinkedRing*[T] = object ## a doubly linked ring
  30. head*: DoublyLinkedNode[T]
  31. SomeLinkedList*[T] = SinglyLinkedList[T] | DoublyLinkedList[T]
  32. SomeLinkedRing*[T] = SinglyLinkedRing[T] | DoublyLinkedRing[T]
  33. SomeLinkedCollection*[T] = SomeLinkedList[T] | SomeLinkedRing[T]
  34. SomeLinkedNode*[T] = SinglyLinkedNode[T] | DoublyLinkedNode[T]
  35. proc initSinglyLinkedList*[T](): SinglyLinkedList[T] =
  36. ## creates a new singly linked list that is empty.
  37. discard
  38. proc initDoublyLinkedList*[T](): DoublyLinkedList[T] =
  39. ## creates a new doubly linked list that is empty.
  40. discard
  41. proc initSinglyLinkedRing*[T](): SinglyLinkedRing[T] =
  42. ## creates a new singly linked ring that is empty.
  43. discard
  44. proc initDoublyLinkedRing*[T](): DoublyLinkedRing[T] =
  45. ## creates a new doubly linked ring that is empty.
  46. discard
  47. proc newDoublyLinkedNode*[T](value: T): DoublyLinkedNode[T] =
  48. ## creates a new doubly linked node with the given `value`.
  49. new(result)
  50. result.value = value
  51. proc newSinglyLinkedNode*[T](value: T): SinglyLinkedNode[T] =
  52. ## creates a new singly linked node with the given `value`.
  53. new(result)
  54. result.value = value
  55. template itemsListImpl() {.dirty.} =
  56. var it = L.head
  57. while it != nil:
  58. yield it.value
  59. it = it.next
  60. template itemsRingImpl() {.dirty.} =
  61. var it = L.head
  62. if it != nil:
  63. while true:
  64. yield it.value
  65. it = it.next
  66. if it == L.head: break
  67. iterator items*[T](L: SomeLinkedList[T]): T =
  68. ## yields every value of `L`.
  69. itemsListImpl()
  70. iterator items*[T](L: SomeLinkedRing[T]): T =
  71. ## yields every value of `L`.
  72. itemsRingImpl()
  73. iterator mitems*[T](L: var SomeLinkedList[T]): var T =
  74. ## yields every value of `L` so that you can modify it.
  75. itemsListImpl()
  76. iterator mitems*[T](L: var SomeLinkedRing[T]): var T =
  77. ## yields every value of `L` so that you can modify it.
  78. itemsRingImpl()
  79. iterator nodes*[T](L: SomeLinkedList[T]): SomeLinkedNode[T] =
  80. ## iterates over every node of `x`. Removing the current node from the
  81. ## list during traversal is supported.
  82. var it = L.head
  83. while it != nil:
  84. var nxt = it.next
  85. yield it
  86. it = nxt
  87. iterator nodes*[T](L: SomeLinkedRing[T]): SomeLinkedNode[T] =
  88. ## iterates over every node of `x`. Removing the current node from the
  89. ## list during traversal is supported.
  90. var it = L.head
  91. if it != nil:
  92. while true:
  93. var nxt = it.next
  94. yield it
  95. it = nxt
  96. if it == L.head: break
  97. proc `$`*[T](L: SomeLinkedCollection[T]): string =
  98. ## turns a list into its string representation.
  99. result = "["
  100. for x in nodes(L):
  101. if result.len > 1: result.add(", ")
  102. result.addQuoted(x.value)
  103. result.add("]")
  104. proc find*[T](L: SomeLinkedCollection[T], value: T): SomeLinkedNode[T] =
  105. ## searches in the list for a value. Returns nil if the value does not
  106. ## exist.
  107. for x in nodes(L):
  108. if x.value == value: return x
  109. proc contains*[T](L: SomeLinkedCollection[T], value: T): bool {.inline.} =
  110. ## searches in the list for a value. Returns false if the value does not
  111. ## exist, true otherwise.
  112. result = find(L, value) != nil
  113. proc append*[T](L: var SinglyLinkedList[T],
  114. n: SinglyLinkedNode[T]) {.inline.} =
  115. ## appends a node `n` to `L`. Efficiency: O(1).
  116. n.next = nil
  117. if L.tail != nil:
  118. assert(L.tail.next == nil)
  119. L.tail.next = n
  120. L.tail = n
  121. if L.head == nil: L.head = n
  122. proc append*[T](L: var SinglyLinkedList[T], value: T) {.inline.} =
  123. ## appends a value to `L`. Efficiency: O(1).
  124. append(L, newSinglyLinkedNode(value))
  125. proc prepend*[T](L: var SinglyLinkedList[T],
  126. n: SinglyLinkedNode[T]) {.inline.} =
  127. ## prepends a node to `L`. Efficiency: O(1).
  128. n.next = L.head
  129. L.head = n
  130. if L.tail == nil: L.tail = n
  131. proc prepend*[T](L: var SinglyLinkedList[T], value: T) {.inline.} =
  132. ## prepends a node to `L`. Efficiency: O(1).
  133. prepend(L, newSinglyLinkedNode(value))
  134. proc append*[T](L: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
  135. ## appends a node `n` to `L`. Efficiency: O(1).
  136. n.next = nil
  137. n.prev = L.tail
  138. if L.tail != nil:
  139. assert(L.tail.next == nil)
  140. L.tail.next = n
  141. L.tail = n
  142. if L.head == nil: L.head = n
  143. proc append*[T](L: var DoublyLinkedList[T], value: T) =
  144. ## appends a value to `L`. Efficiency: O(1).
  145. append(L, newDoublyLinkedNode(value))
  146. proc prepend*[T](L: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
  147. ## prepends a node `n` to `L`. Efficiency: O(1).
  148. n.prev = nil
  149. n.next = L.head
  150. if L.head != nil:
  151. assert(L.head.prev == nil)
  152. L.head.prev = n
  153. L.head = n
  154. if L.tail == nil: L.tail = n
  155. proc prepend*[T](L: var DoublyLinkedList[T], value: T) =
  156. ## prepends a value to `L`. Efficiency: O(1).
  157. prepend(L, newDoublyLinkedNode(value))
  158. proc remove*[T](L: var DoublyLinkedList[T], n: DoublyLinkedNode[T]) =
  159. ## removes `n` from `L`. Efficiency: O(1).
  160. if n == L.tail: L.tail = n.prev
  161. if n == L.head: L.head = n.next
  162. if n.next != nil: n.next.prev = n.prev
  163. if n.prev != nil: n.prev.next = n.next
  164. proc append*[T](L: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
  165. ## appends a node `n` to `L`. Efficiency: O(1).
  166. if L.head != nil:
  167. n.next = L.head
  168. assert(L.tail != nil)
  169. L.tail.next = n
  170. L.tail = n
  171. else:
  172. n.next = n
  173. L.head = n
  174. L.tail = n
  175. proc append*[T](L: var SinglyLinkedRing[T], value: T) =
  176. ## appends a value to `L`. Efficiency: O(1).
  177. append(L, newSinglyLinkedNode(value))
  178. proc prepend*[T](L: var SinglyLinkedRing[T], n: SinglyLinkedNode[T]) =
  179. ## prepends a node `n` to `L`. Efficiency: O(1).
  180. if L.head != nil:
  181. n.next = L.head
  182. assert(L.tail != nil)
  183. L.tail.next = n
  184. else:
  185. n.next = n
  186. L.tail = n
  187. L.head = n
  188. proc prepend*[T](L: var SinglyLinkedRing[T], value: T) =
  189. ## prepends a value to `L`. Efficiency: O(1).
  190. prepend(L, newSinglyLinkedNode(value))
  191. proc append*[T](L: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
  192. ## appends a node `n` to `L`. Efficiency: O(1).
  193. if L.head != nil:
  194. n.next = L.head
  195. n.prev = L.head.prev
  196. L.head.prev.next = n
  197. L.head.prev = n
  198. else:
  199. n.prev = n
  200. n.next = n
  201. L.head = n
  202. proc append*[T](L: var DoublyLinkedRing[T], value: T) =
  203. ## appends a value to `L`. Efficiency: O(1).
  204. append(L, newDoublyLinkedNode(value))
  205. proc prepend*[T](L: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
  206. ## prepends a node `n` to `L`. Efficiency: O(1).
  207. if L.head != nil:
  208. n.next = L.head
  209. n.prev = L.head.prev
  210. L.head.prev.next = n
  211. L.head.prev = n
  212. else:
  213. n.prev = n
  214. n.next = n
  215. L.head = n
  216. proc prepend*[T](L: var DoublyLinkedRing[T], value: T) =
  217. ## prepends a value to `L`. Efficiency: O(1).
  218. prepend(L, newDoublyLinkedNode(value))
  219. proc remove*[T](L: var DoublyLinkedRing[T], n: DoublyLinkedNode[T]) =
  220. ## removes `n` from `L`. Efficiency: O(1).
  221. n.next.prev = n.prev
  222. n.prev.next = n.next
  223. if n == L.head:
  224. var p = L.head.prev
  225. if p == L.head:
  226. # only one element left:
  227. L.head = nil
  228. else:
  229. L.head = L.head.prev