gods/poolx/main.go

47 lines
784 B
Go
Raw Normal View History

package poolx
2023-04-28 17:01:57 +03:00
import (
2023-07-09 11:39:35 +03:00
"github.com/mojosa-software/godat/src/llx"
2023-07-21 20:38:32 +03:00
"github.com/mojosa-software/godat/src/iterx"
2023-04-28 17:01:57 +03:00
)
// Ordered value-only based structure.
// Fast deleting by value.
// Cannot store multiple equal values.
type Pool[V comparable] struct {
2023-04-28 17:01:57 +03:00
store *llx.LinkedList[V]
}
// Return new empty pool.
func New[V comparable]() *Pool[V] {
return &Pool[V]{
store: llx.New[V](),
}
}
2023-04-28 17:01:57 +03:00
func (p *Pool[V]) Append(v V) {
p.store.Append(v)
}
// Deletes the first appearance of the value in the list.
2023-07-21 20:38:32 +03:00
func (p *Pool[V]) DeleteValue(v V) bool {
i := 0
ll := p.store
for e := ll.First() ; e != nil ; e = e.Next() {
if e.Value() == v {
2023-07-21 20:38:32 +03:00
ll.Delete(i)
return true
}
i++
}
return false
}
2023-07-21 20:38:32 +03:00
func (p *Pool[V]) Chan() iterx.PairChan[int, V] {
return p.store.Chan()
}