github.com/matrixorigin/matrixone@v1.2.0/pkg/util/list/dequeue.go (about) 1 // Copyright 2023 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package list 16 17 // Deque deque 18 type Deque[E any] interface { 19 // Len returns the number of elements of Deque. 20 // The complexity is O(1). 21 Len() int 22 // Clear clears Deque 23 Clear() 24 // Iter call fn on all elements which after the offset, stopped if false returned 25 Iter(offset int, fn func(E) bool) 26 // Front returns the first element of Deque, false if the list is empty. 27 Front() (*Element[E], bool) 28 // PopFront removes and returns the first element of Deque 29 PopFront() *Element[E] 30 // Front returns the first element of Deque, panic if the list is empty. 31 MustFront() *Element[E] 32 // Back returns the last element of Deque, false if the list is empty. 33 Back() (*Element[E], bool) 34 // PopBack removes and returns the last element of Deque 35 PopBack() *Element[E] 36 // MustBack returns the last element of Deque, panic if the list is empty. 37 MustBack() *Element[E] 38 // PushFront inserts a new element e with value v at the front of deque. 39 PushFront(v E) *Element[E] 40 // PushBack inserts a new element e with value v at the back of deque. 41 PushBack(v E) *Element[E] 42 // InsertBefore inserts a new element e with value v immediately before mark and returns e. 43 // If mark is not an element of l, the list is not modified. 44 // The mark must not be nil. 45 InsertBefore(v E, mark *Element[E]) *Element[E] 46 // InsertAfter inserts a new element e with value v immediately after mark and returns e. 47 // If mark is not an element of l, the list is not modified. 48 // The mark must not be nil. 49 InsertAfter(v E, mark *Element[E]) *Element[E] 50 // MoveToFront moves element e to the front of list l. 51 // If e is not an element of l, the list is not modified. 52 // The element must not be nil. 53 MoveToFront(e *Element[E]) 54 // MoveToBack moves element e to the back of list l. 55 // If e is not an element of l, the list is not modified. 56 // The element must not be nil. 57 MoveToBack(e *Element[E]) 58 // MoveBefore moves element e to its new position before mark. 59 // If e or mark is not an element of l, or e == mark, the list is not modified. 60 // The element and mark must not be nil. 61 MoveBefore(e, mark *Element[E]) 62 // MoveAfter moves element e to its new position after mark. 63 // If e or mark is not an element of l, or e == mark, the list is not modified. 64 // The element and mark must not be nil. 65 MoveAfter(e, mark *Element[E]) 66 // Remove removes e from l if e is an element of list l. 67 // It returns the element value e.Value. 68 // The element must not be nil. 69 Remove(e *Element[E]) E 70 // Truncate trancate deque, keeping the first size elements 71 Truncate(keeping int) 72 // Drain removes the specified range in the deque, returns drained 73 Drain(from, to int) Deque[E] 74 } 75 76 // New returns an initialized Deque. 77 func New[E any]() Deque[E] { 78 q := newDefaultDequeue[E]() 79 q.Clear() 80 return q 81 } 82 83 // Element is an Element of a linked Deque. 84 type Element[E any] struct { 85 // Next and previous pointers in the doubly-linked Deque of elements. 86 // To simplify the implementation, internally a Deque l is implemented 87 // as a ring, such that &l.root is both the next element of the last 88 // Deque element (l.Back()) and the previous element of the first Deque 89 // element (l.Front()). 90 next, prev *Element[E] 91 92 // The list to which this element belongs. 93 list *defaultDeque[E] 94 95 // The value stored with this element. 96 Value E 97 } 98 99 // Next returns the next Deque element or nil. 100 func (e *Element[E]) Next() *Element[E] { 101 if p := e.next; e.list != nil && p != &e.list.root { 102 return p 103 } 104 return nil 105 } 106 107 // Prev returns the previous Deque element or nil. 108 func (e *Element[E]) Prev() *Element[E] { 109 if p := e.prev; e.list != nil && p != &e.list.root { 110 return p 111 } 112 return nil 113 } 114 115 type defaultDeque[E any] struct { 116 root Element[E] // sentinel list element, only &root, root.prev, and root.next are used 117 len int // current list length excluding (this) sentinel element 118 } 119 120 func newDefaultDequeue[E any]() *defaultDeque[E] { 121 return &defaultDeque[E]{} 122 } 123 124 func (q *defaultDeque[E]) Clear() { 125 q.root.next = &q.root 126 q.root.prev = &q.root 127 q.len = 0 128 } 129 130 func (q *defaultDeque[E]) Truncate(keeping int) { 131 if keeping >= q.len { 132 return 133 } 134 135 q.doRangeRemove(keeping, q.len, false) 136 } 137 138 func (q *defaultDeque[E]) Len() int { return q.len } 139 140 func (q *defaultDeque[E]) Iter(offset int, fn func(E) bool) { 141 if q.len == 0 { 142 return 143 } 144 145 skipped := 0 146 v, _ := q.Front() 147 for e := v; e != nil; e = e.Next() { 148 if skipped < offset { 149 skipped++ 150 continue 151 } 152 153 if !fn(e.Value) { 154 return 155 } 156 } 157 } 158 159 func (q *defaultDeque[E]) Front() (*Element[E], bool) { 160 if q.len == 0 { 161 return nil, false 162 } 163 return q.root.next, true 164 } 165 166 func (q *defaultDeque[E]) PopFront() *Element[E] { 167 if q.len == 0 { 168 return nil 169 } 170 171 return q.remove(q.root.next) 172 } 173 174 func (q *defaultDeque[E]) MustFront() *Element[E] { 175 if q.len == 0 { 176 panic("MustFront on a empty deque") 177 } 178 179 return q.root.next 180 } 181 182 func (q *defaultDeque[E]) Back() (*Element[E], bool) { 183 if q.len == 0 { 184 return nil, false 185 } 186 return q.root.prev, true 187 } 188 189 func (q *defaultDeque[E]) PopBack() *Element[E] { 190 if q.len == 0 { 191 return nil 192 } 193 194 return q.remove(q.root.prev) 195 } 196 197 func (q *defaultDeque[E]) MustBack() *Element[E] { 198 if q.len == 0 { 199 panic("MustBack on a empty deque") 200 } 201 202 return q.root.prev 203 } 204 205 func (q *defaultDeque[E]) PushFront(v E) *Element[E] { 206 q.lazyInit() 207 return q.insertValue(v, &q.root) 208 } 209 210 func (q *defaultDeque[E]) PushBack(v E) *Element[E] { 211 q.lazyInit() 212 return q.insertValue(v, q.root.prev) 213 } 214 215 func (q *defaultDeque[E]) Drain(from, to int) Deque[E] { 216 return q.doRangeRemove(from, to, true) 217 } 218 219 // lazyInit lazily initializes a zero List value. 220 func (q *defaultDeque[E]) lazyInit() { 221 if q.root.next == nil { 222 q.Clear() 223 } 224 } 225 226 // insert inserts e after at, increments l.len, and returns e. 227 func (q *defaultDeque[E]) insert(e, at *Element[E]) *Element[E] { 228 e.prev = at 229 e.next = at.next 230 e.prev.next = e 231 e.next.prev = e 232 e.list = q 233 q.len++ 234 return e 235 } 236 237 // insertValue is a convenience wrapper for insert(&Element{Value: v}, at). 238 func (q *defaultDeque[E]) insertValue(v E, at *Element[E]) *Element[E] { 239 return q.insert(&Element[E]{Value: v}, at) 240 } 241 242 // remove removes e from its list, decrements l.len, and returns e. 243 func (q *defaultDeque[E]) remove(e *Element[E]) *Element[E] { 244 e.prev.next = e.next 245 e.next.prev = e.prev 246 e.next = nil // avoid memory leaks 247 e.prev = nil // avoid memory leaks 248 e.list = nil 249 q.len-- 250 return e 251 } 252 253 // move moves e to next to at and returns e. 254 func (q *defaultDeque[E]) move(e, at *Element[E]) *Element[E] { 255 if e == at { 256 return e 257 } 258 e.prev.next = e.next 259 e.next.prev = e.prev 260 261 e.prev = at 262 e.next = at.next 263 e.prev.next = e 264 e.next.prev = e 265 266 return e 267 } 268 269 func (q *defaultDeque[E]) Remove(e *Element[E]) E { 270 if e.list == q { 271 // if e.list == l, l must have been initialized when e was inserted 272 // in l or l == nil (e is a zero Element) and l.remove will crash 273 q.remove(e) 274 } 275 return e.Value 276 } 277 278 func (q *defaultDeque[E]) InsertBefore(v E, mark *Element[E]) *Element[E] { 279 if mark.list != q { 280 return nil 281 } 282 // see comment in List.Remove about initialization of l 283 return q.insertValue(v, mark.prev) 284 } 285 286 func (q *defaultDeque[E]) InsertAfter(v E, mark *Element[E]) *Element[E] { 287 if mark.list != q { 288 return nil 289 } 290 // see comment in List.Remove about initialization of l 291 return q.insertValue(v, mark) 292 } 293 294 func (q *defaultDeque[E]) MoveToFront(e *Element[E]) { 295 if e.list != q || q.root.next == e { 296 return 297 } 298 // see comment in List.Remove about initialization of l 299 q.move(e, &q.root) 300 } 301 302 func (q *defaultDeque[E]) MoveToBack(e *Element[E]) { 303 if e.list != q || q.root.prev == e { 304 return 305 } 306 // see comment in List.Remove about initialization of l 307 q.move(e, q.root.prev) 308 } 309 310 func (q *defaultDeque[E]) MoveBefore(e, mark *Element[E]) { 311 if e.list != q || e == mark || mark.list != q { 312 return 313 } 314 q.move(e, mark.prev) 315 } 316 317 func (q *defaultDeque[E]) MoveAfter(e, mark *Element[E]) { 318 if e.list != q || e == mark || mark.list != q { 319 return 320 } 321 q.move(e, mark) 322 } 323 324 func (q *defaultDeque[E]) doRangeRemove(from, to int, withRemoved bool) Deque[E] { 325 if from >= to { 326 return nil 327 } 328 329 q.lazyInit() 330 if q.len == 0 { 331 return nil 332 } 333 334 if to > q.len { 335 to = q.len 336 } 337 338 i := 0 339 var left *Element[E] 340 var drainedRight *Element[E] 341 right := &q.root 342 for e := q.root.next; e != &q.root && e.list != nil; e = e.next { 343 if i >= from && i < to { 344 if left == nil { 345 left = e 346 } 347 drainedRight = e 348 } else if i >= to { 349 right = e 350 break 351 } 352 353 i++ 354 } 355 356 q.len -= i - from 357 left.prev.next = right 358 right.prev = left.prev 359 if right == &q.root { 360 q.root.prev = left.prev 361 } 362 363 if !withRemoved { 364 return nil 365 } 366 367 drained := newDefaultDequeue[E]() 368 drained.Clear() 369 left.prev = &drained.root 370 drained.root.next = left 371 drained.root.prev = drainedRight 372 drainedRight.next = &drained.root 373 drained.len = i - from 374 for e := left; e != &q.root && e.list != nil; e = e.next { 375 e.list = drained 376 if e == drainedRight { 377 break 378 } 379 } 380 return drained 381 }