list.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 interface{}
  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.
  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.
  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. n := at.next
  80. at.next = e
  81. e.prev = at
  82. e.next = n
  83. n.prev = e
  84. e.list = l
  85. l.len++
  86. return e
  87. }
  88. // insertValue is a convenience wrapper for insert(&Element{Value: v}, at).
  89. func (l *List) insertValue(v interface{}, at *Element) *Element {
  90. return l.insert(&Element{Value: v}, at)
  91. }
  92. // remove removes e from its list, decrements l.len, and returns e.
  93. func (l *List) remove(e *Element) *Element {
  94. e.prev.next = e.next
  95. e.next.prev = e.prev
  96. e.next = nil // avoid memory leaks
  97. e.prev = nil // avoid memory leaks
  98. e.list = nil
  99. l.len--
  100. return e
  101. }
  102. // Remove removes e from l if e is an element of list l.
  103. // It returns the element value e.Value.
  104. func (l *List) Remove(e *Element) interface{} {
  105. if e.list == l {
  106. // if e.list == l, l must have been initialized when e was inserted
  107. // in l or l == nil (e is a zero Element) and l.remove will crash
  108. l.remove(e)
  109. }
  110. return e.Value
  111. }
  112. // PushFront inserts a new element e with value v at the front of list l and returns e.
  113. func (l *List) PushFront(v interface{}) *Element {
  114. l.lazyInit()
  115. return l.insertValue(v, &l.root)
  116. }
  117. // PushBack inserts a new element e with value v at the back of list l and returns e.
  118. func (l *List) PushBack(v interface{}) *Element {
  119. l.lazyInit()
  120. return l.insertValue(v, l.root.prev)
  121. }
  122. // InsertBefore inserts a new element e with value v immediately before mark and returns e.
  123. // If mark is not an element of l, the list is not modified.
  124. func (l *List) InsertBefore(v interface{}, mark *Element) *Element {
  125. if mark.list != l {
  126. return nil
  127. }
  128. // see comment in List.Remove about initialization of l
  129. return l.insertValue(v, mark.prev)
  130. }
  131. // InsertAfter inserts a new element e with value v immediately after mark and returns e.
  132. // If mark is not an element of l, the list is not modified.
  133. func (l *List) InsertAfter(v interface{}, mark *Element) *Element {
  134. if mark.list != l {
  135. return nil
  136. }
  137. // see comment in List.Remove about initialization of l
  138. return l.insertValue(v, mark)
  139. }
  140. // MoveToFront moves element e to the front of list l.
  141. // If e is not an element of l, the list is not modified.
  142. func (l *List) MoveToFront(e *Element) {
  143. if e.list != l || l.root.next == e {
  144. return
  145. }
  146. // see comment in List.Remove about initialization of l
  147. l.insert(l.remove(e), &l.root)
  148. }
  149. // MoveToBack moves element e to the back of list l.
  150. // If e is not an element of l, the list is not modified.
  151. func (l *List) MoveToBack(e *Element) {
  152. if e.list != l || l.root.prev == e {
  153. return
  154. }
  155. // see comment in List.Remove about initialization of l
  156. l.insert(l.remove(e), l.root.prev)
  157. }
  158. // MoveBefore moves element e to its new position before mark.
  159. // If e or mark is not an element of l, or e == mark, the list is not modified.
  160. func (l *List) MoveBefore(e, mark *Element) {
  161. if e.list != l || e == mark || mark.list != l {
  162. return
  163. }
  164. l.insert(l.remove(e), mark.prev)
  165. }
  166. // MoveAfter moves element e to its new position after mark.
  167. // If e or mark is not an element of l, or e == mark, the list is not modified.
  168. func (l *List) MoveAfter(e, mark *Element) {
  169. if e.list != l || e == mark || mark.list != l {
  170. return
  171. }
  172. l.insert(l.remove(e), mark)
  173. }
  174. // PushBackList inserts a copy of an other list at the back of list l.
  175. // The lists l and other may be the same.
  176. func (l *List) PushBackList(other *List) {
  177. l.lazyInit()
  178. for i, e := other.Len(), other.Front(); i > 0; i, e = i-1, e.Next() {
  179. l.insertValue(e.Value, l.root.prev)
  180. }
  181. }
  182. // PushFrontList inserts a copy of an other list at the front of list l.
  183. // The lists l and other may be the same.
  184. func (l *List) PushFrontList(other *List) {
  185. l.lazyInit()
  186. for i, e := other.Len(), other.Back(); i > 0; i, e = i-1, e.Prev() {
  187. l.insertValue(e.Value, &l.root)
  188. }
  189. }