2024-01-19 12:57:29 +00:00
|
|
|
package structr
|
|
|
|
|
2024-01-29 15:13:53 +00:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// elem represents an elem
|
2024-01-19 12:57:29 +00:00
|
|
|
// in a doubly-linked list.
|
2024-01-29 15:13:53 +00:00
|
|
|
type list_elem struct {
|
|
|
|
next *list_elem
|
|
|
|
prev *list_elem
|
|
|
|
|
|
|
|
// data is a ptr to the
|
|
|
|
// value this linked list
|
|
|
|
// element is embedded-in.
|
|
|
|
data unsafe.Pointer
|
2024-01-19 12:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// list implements a doubly-linked list, where:
|
|
|
|
// - head = index 0 (i.e. the front)
|
|
|
|
// - tail = index n-1 (i.e. the back)
|
2024-01-29 15:13:53 +00:00
|
|
|
type list struct {
|
|
|
|
head *list_elem
|
|
|
|
tail *list_elem
|
2024-01-19 12:57:29 +00:00
|
|
|
len int
|
|
|
|
}
|
|
|
|
|
2024-04-02 10:03:40 +00:00
|
|
|
var list_pool sync.Pool
|
|
|
|
|
|
|
|
// new_list returns a new prepared list.
|
|
|
|
func new_list() *list {
|
2024-01-29 15:13:53 +00:00
|
|
|
v := list_pool.Get()
|
|
|
|
if v == nil {
|
|
|
|
v = new(list)
|
2024-01-19 12:57:29 +00:00
|
|
|
}
|
2024-04-02 10:03:40 +00:00
|
|
|
list := v.(*list)
|
|
|
|
return list
|
2024-01-19 12:57:29 +00:00
|
|
|
}
|
|
|
|
|
2024-04-02 10:03:40 +00:00
|
|
|
// free_list releases the list.
|
|
|
|
func free_list(list *list) {
|
2024-10-02 10:58:20 +00:00
|
|
|
if list.head != nil ||
|
|
|
|
list.tail != nil ||
|
|
|
|
list.len != 0 {
|
|
|
|
should_not_reach()
|
|
|
|
return
|
|
|
|
}
|
2024-04-02 10:03:40 +00:00
|
|
|
list_pool.Put(list)
|
2024-01-19 12:57:29 +00:00
|
|
|
}
|
|
|
|
|
2024-04-02 10:03:40 +00:00
|
|
|
// push_front will push the given elem to front (head) of list.
|
|
|
|
func (l *list) push_front(elem *list_elem) {
|
2024-09-28 20:47:46 +00:00
|
|
|
// Set new head.
|
|
|
|
oldHead := l.head
|
|
|
|
l.head = elem
|
2024-01-19 12:57:29 +00:00
|
|
|
|
2024-09-28 20:47:46 +00:00
|
|
|
if oldHead != nil {
|
2024-01-19 12:57:29 +00:00
|
|
|
// Link to old head
|
|
|
|
elem.next = oldHead
|
|
|
|
oldHead.prev = elem
|
2024-09-28 20:47:46 +00:00
|
|
|
} else {
|
|
|
|
// First in list.
|
|
|
|
l.tail = elem
|
2024-01-19 12:57:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Incr count
|
|
|
|
l.len++
|
|
|
|
}
|
|
|
|
|
2024-04-02 10:03:40 +00:00
|
|
|
// push_back will push the given elem to back (tail) of list.
|
|
|
|
func (l *list) push_back(elem *list_elem) {
|
2024-09-28 20:47:46 +00:00
|
|
|
// Set new tail.
|
|
|
|
oldTail := l.tail
|
|
|
|
l.tail = elem
|
2024-04-02 10:03:40 +00:00
|
|
|
|
2024-09-28 20:47:46 +00:00
|
|
|
if oldTail != nil {
|
2024-04-02 10:03:40 +00:00
|
|
|
// Link to old tail
|
|
|
|
elem.prev = oldTail
|
|
|
|
oldTail.next = elem
|
2024-09-28 20:47:46 +00:00
|
|
|
} else {
|
|
|
|
// First in list.
|
|
|
|
l.head = elem
|
2024-04-02 10:03:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Incr count
|
|
|
|
l.len++
|
|
|
|
}
|
|
|
|
|
|
|
|
// move_front will move given elem to front (head) of list.
|
2024-09-28 20:47:46 +00:00
|
|
|
// if it is already at front this call is a no-op.
|
2024-04-02 10:03:40 +00:00
|
|
|
func (l *list) move_front(elem *list_elem) {
|
2024-09-28 20:47:46 +00:00
|
|
|
if elem == l.head {
|
|
|
|
return
|
|
|
|
}
|
2024-04-02 10:03:40 +00:00
|
|
|
l.remove(elem)
|
|
|
|
l.push_front(elem)
|
|
|
|
}
|
|
|
|
|
2024-09-28 20:47:46 +00:00
|
|
|
// move_back will move given elem to back (tail) of list,
|
|
|
|
// if it is already at back this call is a no-op.
|
2024-04-02 10:03:40 +00:00
|
|
|
func (l *list) move_back(elem *list_elem) {
|
2024-09-28 20:47:46 +00:00
|
|
|
if elem == l.tail {
|
|
|
|
return
|
|
|
|
}
|
2024-04-02 10:03:40 +00:00
|
|
|
l.remove(elem)
|
|
|
|
l.push_back(elem)
|
2024-01-19 12:57:29 +00:00
|
|
|
}
|
|
|
|
|
2024-04-02 10:03:40 +00:00
|
|
|
// remove will remove given elem from list.
|
|
|
|
func (l *list) remove(elem *list_elem) {
|
2024-09-28 20:47:46 +00:00
|
|
|
// Get linked elems.
|
2024-01-19 12:57:29 +00:00
|
|
|
next := elem.next
|
|
|
|
prev := elem.prev
|
|
|
|
|
2024-09-28 20:47:46 +00:00
|
|
|
// Unset elem.
|
|
|
|
elem.next = nil
|
|
|
|
elem.prev = nil
|
|
|
|
|
|
|
|
switch {
|
2024-10-02 10:58:20 +00:00
|
|
|
case next == nil:
|
|
|
|
if prev == nil {
|
|
|
|
// next == nil && prev == nil
|
|
|
|
//
|
|
|
|
// elem is ONLY one in list.
|
|
|
|
l.head = nil
|
|
|
|
l.tail = nil
|
|
|
|
} else {
|
|
|
|
// next == nil && prev != nil
|
|
|
|
//
|
|
|
|
// elem is last in list.
|
|
|
|
l.tail = prev
|
|
|
|
prev.next = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
case prev == nil:
|
|
|
|
// next != nil && prev == nil
|
|
|
|
//
|
|
|
|
// elem is front in list.
|
2024-01-19 12:57:29 +00:00
|
|
|
l.head = next
|
2024-09-28 20:47:46 +00:00
|
|
|
next.prev = nil
|
2024-01-19 12:57:29 +00:00
|
|
|
|
2024-09-28 20:47:46 +00:00
|
|
|
// elem in middle of list.
|
|
|
|
default:
|
|
|
|
next.prev = prev
|
|
|
|
prev.next = next
|
|
|
|
}
|
2024-01-19 12:57:29 +00:00
|
|
|
|
|
|
|
// Decr count
|
|
|
|
l.len--
|
|
|
|
}
|