lists.nim 7.8 KB

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