2023-04-28 16:14:03 +03:00
|
|
|
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.
|
2023-04-28 17:25:27 +03:00
|
|
|
// Cannot store multiple equal values.
|
2023-04-28 16:14:03 +03:00
|
|
|
|
|
|
|
type Pool[V comparable] struct {
|
2023-04-28 17:01:57 +03:00
|
|
|
store *llx.LinkedList[V]
|
2023-04-28 16:14:03 +03:00
|
|
|
}
|
|
|
|
|
2023-04-28 17:51:39 +03:00
|
|
|
// Return new empty pool.
|
|
|
|
func New[V comparable]() *Pool[V] {
|
|
|
|
return &Pool[V]{
|
|
|
|
store: llx.New[V](),
|
2023-04-28 16:14:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-28 17:01:57 +03:00
|
|
|
func (p *Pool[V]) Append(v V) {
|
|
|
|
p.store.Append(v)
|
2023-04-28 16:14:03 +03:00
|
|
|
}
|
|
|
|
|
2023-04-28 17:51:39 +03:00
|
|
|
// 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 {
|
2023-04-28 17:51:39 +03:00
|
|
|
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)
|
2023-04-28 17:51:39 +03:00
|
|
|
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()
|
2023-04-28 17:25:27 +03:00
|
|
|
}
|
2023-04-28 17:51:39 +03:00
|
|
|
|