diff options
author | 2024-04-02 11:03:40 +0100 | |
---|---|---|
committer | 2024-04-02 12:03:40 +0200 | |
commit | adf345f1ec0cb76a0df94a4505143d891659cba9 (patch) | |
tree | e0cca289c0a50f30191d4b65a2c336704570e470 /vendor/codeberg.org/gruf/go-structr/list.go | |
parent | [feature] Option to hide followers/following (#2788) (diff) | |
download | gotosocial-adf345f1ec0cb76a0df94a4505143d891659cba9.tar.xz |
[chore] bump go structr cache version -> v0.6.0 (#2773)
* update go-structr library -> v0.6.0, add necessary wrapping types + code changes to support these changes
* update readme with go-structr package changes
* improved wrapping of the SliceCache type
* add code comments for the cache wrapper types
* remove test.out :innocent:
---------
Co-authored-by: tobi <31960611+tsmethurst@users.noreply.github.com>
Diffstat (limited to 'vendor/codeberg.org/gruf/go-structr/list.go')
-rw-r--r-- | vendor/codeberg.org/gruf/go-structr/list.go | 78 |
1 files changed, 57 insertions, 21 deletions
diff --git a/vendor/codeberg.org/gruf/go-structr/list.go b/vendor/codeberg.org/gruf/go-structr/list.go index 398f84ceb..17e1899ad 100644 --- a/vendor/codeberg.org/gruf/go-structr/list.go +++ b/vendor/codeberg.org/gruf/go-structr/list.go @@ -5,8 +5,6 @@ import ( "unsafe" ) -var list_pool sync.Pool - // elem represents an elem // in a doubly-linked list. type list_elem struct { @@ -28,28 +26,28 @@ type list struct { len int } -func list_acquire() *list { - // Acquire from pool. +var list_pool sync.Pool + +// new_list returns a new prepared list. +func new_list() *list { v := list_pool.Get() if v == nil { v = new(list) } - - // Cast list value. - return v.(*list) + list := v.(*list) + return list } -func list_release(l *list) { - // Reset list. - l.head = nil - l.tail = nil - l.len = 0 - - // Release to pool. - list_pool.Put(l) +// free_list releases the list. +func free_list(list *list) { + list.head = nil + list.tail = nil + list.len = 0 + list_pool.Put(list) } -func list_push_front(l *list, elem *list_elem) { +// push_front will push the given elem to front (head) of list. +func (l *list) push_front(elem *list_elem) { if l.len == 0 { // Set new tail + head l.head = elem @@ -77,12 +75,49 @@ func list_push_front(l *list, elem *list_elem) { l.len++ } -func list_move_front(l *list, elem *list_elem) { - list_remove(l, elem) - list_push_front(l, elem) +// push_back will push the given elem to back (tail) of list. +func (l *list) push_back(elem *list_elem) { + if l.len == 0 { + // Set new tail + head + l.head = elem + l.tail = elem + + // Link elem to itself + elem.next = elem + elem.prev = elem + } else { + oldTail := l.tail + + // Link to old tail + elem.prev = oldTail + oldTail.next = elem + + // Link up to head + elem.next = l.head + l.head.prev = elem + + // Set new tail + l.tail = elem + } + + // Incr count + l.len++ +} + +// move_front will move given elem to front (head) of list. +func (l *list) move_front(elem *list_elem) { + l.remove(elem) + l.push_front(elem) +} + +// move_back will move given elem to back (tail) of list. +func (l *list) move_back(elem *list_elem) { + l.remove(elem) + l.push_back(elem) } -func list_remove(l *list, elem *list_elem) { +// remove will remove given elem from list. +func (l *list) remove(elem *list_elem) { if l.len <= 1 { // Drop elem's links elem.next = nil @@ -121,7 +156,8 @@ func list_remove(l *list, elem *list_elem) { l.len-- } -func list_rangefn(l *list, fn func(*list_elem)) { +// rangefn will range all elems in list, passing each to fn. +func (l *list) rangefn(fn func(*list_elem)) { if fn == nil { panic("nil fn") } |