diff --git a/src/cmd/test/main.go b/src/cmd/test/main.go index 8238c30..4a6a2af 100644 --- a/src/cmd/test/main.go +++ b/src/cmd/test/main.go @@ -3,7 +3,7 @@ package main import ( "github.com/surdeus/godat/src/mapx" "github.com/surdeus/godat/src/slicex" - "github.com/surdeus/godat/src/llx" + "github.com/surdeus/godat/src/poolx" "fmt" ) @@ -41,12 +41,12 @@ func main() { fmt.Printf("%q\n", mapx.Reverse(m)) fmt.Printf("%v\n", mapx.Reverse(m1)) - ll := llx.NewComparable[int]() + ll := poolx.New[int]() ll.Append(0) ll.Append(1) ll.Append(2) - ll.Set(1, 256) - ll.DelVal(256) + ll.Del(256) + ll.Del(1) for p := range ll.Range() { fmt.Println(p) } diff --git a/src/llx/main.go b/src/llx/main.go index 621b89a..e62a8fd 100644 --- a/src/llx/main.go +++ b/src/llx/main.go @@ -25,18 +25,6 @@ type Pair[V any] struct { V V } -type ComparableLinkedList[V comparable] struct { - *LinkedList[V] -} - - -// Returns new empty linked list storing any COMPARABLE values -// and adds a few more methods. -func NewComparable[V comparable]() *ComparableLinkedList[V] { - return &ComparableLinkedList[V]{ - New[V](), - } -} // Returns new empty linked list storing the V type. func New[V any]() *LinkedList[V] { @@ -111,22 +99,6 @@ func (ll *LinkedList[V]) Del(i int) (bool) { return true } -// Deletes the first appearance of the value in the list. -func (cll *ComparableLinkedList[V]) DelVal(v V) bool { - i := 0 - ll := cll.LinkedList - for p:= ll.before.next ; p != nil ; p = p.next { - if p.value == v { - ll.Del(i) - return true - } - - i++ - } - - return false -} - // Push in the beginning of the list. func (ll *LinkedList[V]) Push(v V) { prevNext := ll.before.next @@ -161,6 +133,22 @@ func (ll *LinkedList[V]) Append(v V) { ll.ln++ } +func (ll *LinkedList[V]) First() *Element[V] { + return ll.before.next +} + +func (ll *Element[V]) Value() V { + return ll.value +} + +func (ll *Element[V]) Next() *Element[V] { + return ll.next +} + +func (ll *LinkedList[V]) Last() *Element[V] { + return ll.last +} + // Returns a channel of Pair that contains index and the value. func (ll *LinkedList[V]) Range() chan Pair[V] { chn := make(chan Pair[V]) diff --git a/src/poolx/main.go b/src/poolx/main.go index 425de03..c2dc1c7 100644 --- a/src/poolx/main.go +++ b/src/poolx/main.go @@ -10,14 +10,12 @@ import ( type Pool[V comparable] struct { store *llx.LinkedList[V] - keys map[V] int } -// Returns new empty pool. -func New[V comparable]() *Pool { - return &Pool{ - llx.New[V]() - 0, +// Return new empty pool. +func New[V comparable]() *Pool[V] { + return &Pool[V]{ + store: llx.New[V](), } } @@ -25,6 +23,23 @@ func (p *Pool[V]) Append(v V) { p.store.Append(v) } +// Deletes the first appearance of the value in the list. func (p *Pool[V]) Del(v V) bool { - return true + i := 0 + ll := p.store + for e := ll.First() ; e != nil ; e = e.Next() { + if e.Value() == v { + ll.Del(i) + return true + } + + i++ + } + + return false } + +func (p *Pool[V]) Range() chan llx.Pair[V] { + return p.store.Range() +} +