packet_linkedlist.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // This file was automatically generated by genny.
  2. // Any changes will be lost if this file is regenerated.
  3. // see https://github.com/cheekybits/genny
  4. package ackhandler
  5. // Linked list implementation from the Go standard library.
  6. // PacketElement is an element of a linked list.
  7. type PacketElement struct {
  8. // Next and previous pointers in the doubly-linked list of elements.
  9. // To simplify the implementation, internally a list l is implemented
  10. // as a ring, such that &l.root is both the next element of the last
  11. // list element (l.Back()) and the previous element of the first list
  12. // element (l.Front()).
  13. next, prev *PacketElement
  14. // The list to which this element belongs.
  15. list *PacketList
  16. // The value stored with this element.
  17. Value Packet
  18. }
  19. // Next returns the next list element or nil.
  20. func (e *PacketElement) Next() *PacketElement {
  21. if p := e.next; e.list != nil && p != &e.list.root {
  22. return p
  23. }
  24. return nil
  25. }
  26. // Prev returns the previous list element or nil.
  27. func (e *PacketElement) Prev() *PacketElement {
  28. if p := e.prev; e.list != nil && p != &e.list.root {
  29. return p
  30. }
  31. return nil
  32. }
  33. // PacketList is a linked list of Packets.
  34. type PacketList struct {
  35. root PacketElement // sentinel list element, only &root, root.prev, and root.next are used
  36. len int // current list length excluding (this) sentinel element
  37. }
  38. // Init initializes or clears list l.
  39. func (l *PacketList) Init() *PacketList {
  40. l.root.next = &l.root
  41. l.root.prev = &l.root
  42. l.len = 0
  43. return l
  44. }
  45. // NewPacketList returns an initialized list.
  46. func NewPacketList() *PacketList { return new(PacketList).Init() }
  47. // Len returns the number of elements of list l.
  48. // The complexity is O(1).
  49. func (l *PacketList) Len() int { return l.len }
  50. // Front returns the first element of list l or nil if the list is empty.
  51. func (l *PacketList) Front() *PacketElement {
  52. if l.len == 0 {
  53. return nil
  54. }
  55. return l.root.next
  56. }
  57. // Back returns the last element of list l or nil if the list is empty.
  58. func (l *PacketList) Back() *PacketElement {
  59. if l.len == 0 {
  60. return nil
  61. }
  62. return l.root.prev
  63. }
  64. // lazyInit lazily initializes a zero List value.
  65. func (l *PacketList) lazyInit() {
  66. if l.root.next == nil {
  67. l.Init()
  68. }
  69. }
  70. // insert inserts e after at, increments l.len, and returns e.
  71. func (l *PacketList) insert(e, at *PacketElement) *PacketElement {
  72. n := at.next
  73. at.next = e
  74. e.prev = at
  75. e.next = n
  76. n.prev = e
  77. e.list = l
  78. l.len++
  79. return e
  80. }
  81. // insertValue is a convenience wrapper for insert(&Element{Value: v}, at).
  82. func (l *PacketList) insertValue(v Packet, at *PacketElement) *PacketElement {
  83. return l.insert(&PacketElement{Value: v}, at)
  84. }
  85. // remove removes e from its list, decrements l.len, and returns e.
  86. func (l *PacketList) remove(e *PacketElement) *PacketElement {
  87. e.prev.next = e.next
  88. e.next.prev = e.prev
  89. e.next = nil // avoid memory leaks
  90. e.prev = nil // avoid memory leaks
  91. e.list = nil
  92. l.len--
  93. return e
  94. }
  95. // Remove removes e from l if e is an element of list l.
  96. // It returns the element value e.Value.
  97. // The element must not be nil.
  98. func (l *PacketList) Remove(e *PacketElement) Packet {
  99. if e.list == l {
  100. // if e.list == l, l must have been initialized when e was inserted
  101. // in l or l == nil (e is a zero Element) and l.remove will crash
  102. l.remove(e)
  103. }
  104. return e.Value
  105. }
  106. // PushFront inserts a new element e with value v at the front of list l and returns e.
  107. func (l *PacketList) PushFront(v Packet) *PacketElement {
  108. l.lazyInit()
  109. return l.insertValue(v, &l.root)
  110. }
  111. // PushBack inserts a new element e with value v at the back of list l and returns e.
  112. func (l *PacketList) PushBack(v Packet) *PacketElement {
  113. l.lazyInit()
  114. return l.insertValue(v, l.root.prev)
  115. }
  116. // InsertBefore inserts a new element e with value v immediately before mark and returns e.
  117. // If mark is not an element of l, the list is not modified.
  118. // The mark must not be nil.
  119. func (l *PacketList) InsertBefore(v Packet, mark *PacketElement) *PacketElement {
  120. if mark.list != l {
  121. return nil
  122. }
  123. // see comment in List.Remove about initialization of l
  124. return l.insertValue(v, mark.prev)
  125. }
  126. // InsertAfter inserts a new element e with value v immediately after mark and returns e.
  127. // If mark is not an element of l, the list is not modified.
  128. // The mark must not be nil.
  129. func (l *PacketList) InsertAfter(v Packet, mark *PacketElement) *PacketElement {
  130. if mark.list != l {
  131. return nil
  132. }
  133. // see comment in List.Remove about initialization of l
  134. return l.insertValue(v, mark)
  135. }
  136. // MoveToFront moves element e to the front of list l.
  137. // If e is not an element of l, the list is not modified.
  138. // The element must not be nil.
  139. func (l *PacketList) MoveToFront(e *PacketElement) {
  140. if e.list != l || l.root.next == e {
  141. return
  142. }
  143. // see comment in List.Remove about initialization of l
  144. l.insert(l.remove(e), &l.root)
  145. }
  146. // MoveToBack moves element e to the back of list l.
  147. // If e is not an element of l, the list is not modified.
  148. // The element must not be nil.
  149. func (l *PacketList) MoveToBack(e *PacketElement) {
  150. if e.list != l || l.root.prev == e {
  151. return
  152. }
  153. // see comment in List.Remove about initialization of l
  154. l.insert(l.remove(e), l.root.prev)
  155. }
  156. // MoveBefore moves element e to its new position before mark.
  157. // If e or mark is not an element of l, or e == mark, the list is not modified.
  158. // The element and mark must not be nil.
  159. func (l *PacketList) MoveBefore(e, mark *PacketElement) {
  160. if e.list != l || e == mark || mark.list != l {
  161. return
  162. }
  163. l.insert(l.remove(e), mark.prev)
  164. }
  165. // MoveAfter moves element e to its new position after mark.
  166. // If e or mark is not an element of l, or e == mark, the list is not modified.
  167. // The element and mark must not be nil.
  168. func (l *PacketList) MoveAfter(e, mark *PacketElement) {
  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. They must not be nil.
  176. func (l *PacketList) PushBackList(other *PacketList) {
  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. They must not be nil.
  184. func (l *PacketList) PushFrontList(other *PacketList) {
  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. }