list.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package list implements a doubly linked list.
  5. //
  6. // To iterate over a list (where l is a *List):
  7. // for e := l.Front(); e != nil; e = e.Next() {
  8. // // do something with e.Value
  9. // }
  10. //
  11. package list
  12. // Element is an element of a linked list.
  13. type Element struct {
  14. // Next and previous pointers in the doubly-linked list of elements.
  15. // To simplify the implementation, internally a list l is implemented
  16. // as a ring, such that &l.root is both the next element of the last
  17. // list element (l.Back()) and the previous element of the first list
  18. // element (l.Front()).
  19. next, prev *Element
  20. // The list to which this element belongs.
  21. list *List
  22. // The value stored with this element.
  23. Value any
  24. }
  25. // Next returns the next list element or nil.
  26. func (e *Element) Next() *Element {
  27. if p := e.next; e.list != nil && p != &e.list.root {
  28. return p
  29. }
  30. return nil
  31. }
  32. // Prev returns the previous list element or nil.
  33. func (e *Element) Prev() *Element {
  34. if p := e.prev; e.list != nil && p != &e.list.root {
  35. return p
  36. }
  37. return nil
  38. }
  39. // List represents a doubly linked list.
  40. // The zero value for List is an empty list ready to use.
  41. type List struct {
  42. root Element // sentinel list element, only &root, root.prev, and root.next are used
  43. len int // current list length excluding (this) sentinel element
  44. }
  45. // Init initializes or clears list l.
  46. func (l *List) Init() *List {
  47. l.root.next = &l.root
  48. l.root.prev = &l.root
  49. l.len = 0
  50. return l
  51. }
  52. // New returns an initialized list.
  53. func New() *List { return new(List).Init() }
  54. // Len returns the number of elements of list l.
  55. // The complexity is O(1).
  56. func (l *List) Len() int { return l.len }
  57. // Front returns the first element of list l or nil if the list is empty.
  58. func (l *List) Front() *Element {
  59. if l.len == 0 {
  60. return nil
  61. }
  62. return l.root.next
  63. }
  64. // Back returns the last element of list l or nil if the list is empty.
  65. func (l *List) Back() *Element {
  66. if l.len == 0 {
  67. return nil
  68. }
  69. return l.root.prev
  70. }
  71. // lazyInit lazily initializes a zero List value.
  72. func (l *List) lazyInit() {
  73. if l.root.next == nil {
  74. l.Init()
  75. }
  76. }
  77. // insert inserts e after at, increments l.len, and returns e.
  78. func (l *List) insert(e, at *Element) *Element {
  79. e.prev = at
  80. e.next = at.next
  81. e.prev.next = e
  82. e.next.prev = e
  83. e.list = l
  84. l.len++
  85. return e
  86. }
  87. // insertValue is a convenience wrapper for insert(&Element{Value: v}, at).
  88. func (l *List) insertValue(v any, at *Element) *Element {
  89. return l.insert(&Element{Value: v}, at)
  90. }
  91. // remove removes e from its list, decrements l.len
  92. func (l *List) remove(e *Element) {
  93. e.prev.next = e.next
  94. e.next.prev = e.prev
  95. e.next = nil // avoid memory leaks
  96. e.prev = nil // avoid memory leaks
  97. e.list = nil
  98. l.len--
  99. }
  100. // move moves e to next to at.
  101. func (l *List) move(e, at *Element) {
  102. if e == at {
  103. return
  104. }
  105. e.prev.next = e.next
  106. e.next.prev = e.prev
  107. e.prev = at
  108. e.next = at.next
  109. e.prev.next = e
  110. e.next.prev = e
  111. }
  112. // Remove removes e from l if e is an element of list l.
  113. // It returns the element value e.Value.
  114. // The element must not be nil.
  115. func (l *List) Remove(e *Element) any {
  116. if e.list == l {
  117. // if e.list == l, l must have been initialized when e was inserted
  118. // in l or l == nil (e is a zero Element) and l.remove will crash
  119. l.remove(e)
  120. }
  121. return e.Value
  122. }
  123. // PushFront inserts a new element e with value v at the front of list l and returns e.
  124. func (l *List) PushFront(v any) *Element {
  125. l.lazyInit()
  126. return l.insertValue(v, &l.root)
  127. }
  128. // PushBack inserts a new element e with value v at the back of list l and returns e.
  129. func (l *List) PushBack(v any) *Element {
  130. l.lazyInit()
  131. return l.insertValue(v, l.root.prev)
  132. }
  133. // InsertBefore inserts a new element e with value v immediately before mark and returns e.
  134. // If mark is not an element of l, the list is not modified.
  135. // The mark must not be nil.
  136. func (l *List) InsertBefore(v any, mark *Element) *Element {
  137. if mark.list != l {
  138. return nil
  139. }
  140. // see comment in List.Remove about initialization of l
  141. return l.insertValue(v, mark.prev)
  142. }
  143. // InsertAfter inserts a new element e with value v immediately after mark and returns e.
  144. // If mark is not an element of l, the list is not modified.
  145. // The mark must not be nil.
  146. func (l *List) InsertAfter(v any, mark *Element) *Element {
  147. if mark.list != l {
  148. return nil
  149. }
  150. // see comment in List.Remove about initialization of l
  151. return l.insertValue(v, mark)
  152. }
  153. // MoveToFront moves element e to the front of list l.
  154. // If e is not an element of l, the list is not modified.
  155. // The element must not be nil.
  156. func (l *List) MoveToFront(e *Element) {
  157. if e.list != l || l.root.next == e {
  158. return
  159. }
  160. // see comment in List.Remove about initialization of l
  161. l.move(e, &l.root)
  162. }
  163. // MoveToBack moves element e to the back of list l.
  164. // If e is not an element of l, the list is not modified.
  165. // The element must not be nil.
  166. func (l *List) MoveToBack(e *Element) {
  167. if e.list != l || l.root.prev == e {
  168. return
  169. }
  170. // see comment in List.Remove about initialization of l
  171. l.move(e, l.root.prev)
  172. }
  173. // MoveBefore moves element e to its new position before mark.
  174. // If e or mark is not an element of l, or e == mark, the list is not modified.
  175. // The element and mark must not be nil.
  176. func (l *List) MoveBefore(e, mark *Element) {
  177. if e.list != l || e == mark || mark.list != l {
  178. return
  179. }
  180. l.move(e, mark.prev)
  181. }
  182. // MoveAfter moves element e to its new position after mark.
  183. // If e or mark is not an element of l, or e == mark, the list is not modified.
  184. // The element and mark must not be nil.
  185. func (l *List) MoveAfter(e, mark *Element) {
  186. if e.list != l || e == mark || mark.list != l {
  187. return
  188. }
  189. l.move(e, mark)
  190. }
  191. // PushBackList inserts a copy of another list at the back of list l.
  192. // The lists l and other may be the same. They must not be nil.
  193. func (l *List) PushBackList(other *List) {
  194. l.lazyInit()
  195. for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() {
  196. l.insertValue(e.Value, l.root.prev)
  197. }
  198. }
  199. // PushFrontList inserts a copy of another list at the front of list l.
  200. // The lists l and other may be the same. They must not be nil.
  201. func (l *List) PushFrontList(other *List) {
  202. l.lazyInit()
  203. for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() {
  204. l.insertValue(e.Value, &l.root)
  205. }
  206. }