2024-04-02 10:03:40 +00:00
|
|
|
package structr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type indexed_item struct {
|
|
|
|
// linked list elem this item
|
|
|
|
// is stored in a main list.
|
|
|
|
elem list_elem
|
|
|
|
|
2024-08-14 12:08:24 +00:00
|
|
|
// cached data with type.
|
|
|
|
data interface{}
|
|
|
|
|
2024-04-02 10:03:40 +00:00
|
|
|
// indexed stores the indices
|
|
|
|
// this item is stored under.
|
|
|
|
indexed []*index_entry
|
|
|
|
}
|
|
|
|
|
|
|
|
var indexed_item_pool sync.Pool
|
|
|
|
|
|
|
|
// new_indexed_item returns a new prepared indexed_item.
|
|
|
|
func new_indexed_item() *indexed_item {
|
|
|
|
v := indexed_item_pool.Get()
|
|
|
|
if v == nil {
|
2024-10-02 10:58:20 +00:00
|
|
|
i := new(indexed_item)
|
|
|
|
i.elem.data = unsafe.Pointer(i)
|
|
|
|
v = i
|
2024-04-02 10:03:40 +00:00
|
|
|
}
|
|
|
|
item := v.(*indexed_item)
|
|
|
|
return item
|
|
|
|
}
|
|
|
|
|
|
|
|
// free_indexed_item releases the indexed_item.
|
|
|
|
func free_indexed_item(item *indexed_item) {
|
2024-10-02 10:58:20 +00:00
|
|
|
if len(item.indexed) > 0 ||
|
|
|
|
item.elem.next != nil ||
|
|
|
|
item.elem.prev != nil {
|
|
|
|
should_not_reach()
|
|
|
|
return
|
|
|
|
}
|
2024-04-02 10:03:40 +00:00
|
|
|
item.data = nil
|
|
|
|
indexed_item_pool.Put(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
// drop_index will drop the given index entry from item's indexed.
|
|
|
|
func (i *indexed_item) drop_index(entry *index_entry) {
|
|
|
|
for x := 0; x < len(i.indexed); x++ {
|
|
|
|
if i.indexed[x] != entry {
|
|
|
|
// Prof. Obiwan:
|
|
|
|
// this is not the index
|
|
|
|
// we are looking for.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-10-02 10:58:20 +00:00
|
|
|
// Reslice index entries minus 'x'.
|
2024-05-06 19:44:22 +00:00
|
|
|
_ = copy(i.indexed[x:], i.indexed[x+1:])
|
2024-09-28 20:47:46 +00:00
|
|
|
i.indexed[len(i.indexed)-1] = nil
|
2024-04-02 10:03:40 +00:00
|
|
|
i.indexed = i.indexed[:len(i.indexed)-1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|