diff --git a/src/cmd/test/main.go b/src/cmd/test/main.go index b5fe3d2..8238c30 100644 --- a/src/cmd/test/main.go +++ b/src/cmd/test/main.go @@ -41,13 +41,12 @@ func main() { fmt.Printf("%q\n", mapx.Reverse(m)) fmt.Printf("%v\n", mapx.Reverse(m1)) - ll := llx.New[int]() + ll := llx.NewComparable[int]() ll.Append(0) ll.Append(1) ll.Append(2) - ll.Push(11) ll.Set(1, 256) - ll.Del(3) + ll.DelVal(256) for p := range ll.Range() { fmt.Println(p) } diff --git a/src/llx/main.go b/src/llx/main.go index 167a66e..621b89a 100644 --- a/src/llx/main.go +++ b/src/llx/main.go @@ -25,6 +25,19 @@ 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] { return &LinkedList[V]{ @@ -98,10 +111,21 @@ func (ll *LinkedList[V]) Del(i int) (bool) { return true } -/*func (ll *LinkedList[V comparable]) DelByVal(v V) int { - p := ll.before.next +// 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) { diff --git a/src/poolx/main.go b/src/poolx/main.go index 8de05fb..425de03 100644 --- a/src/poolx/main.go +++ b/src/poolx/main.go @@ -6,7 +6,7 @@ import ( // Ordered value-only based structure. // Fast deleting by value. -// Cannot store multiple the same values. +// Cannot store multiple equal values. type Pool[V comparable] struct { store *llx.LinkedList[V] @@ -23,6 +23,8 @@ func New[V comparable]() *Pool { func (p *Pool[V]) Append(v V) { p.store.Append(v) - } +func (p *Pool[V]) Del(v V) bool { + return true +}