From 4494e83428fd75cae69fb954eccbbfd5376bef0c Mon Sep 17 00:00:00 2001 From: surdeus Date: Fri, 28 Apr 2023 17:01:57 +0300 Subject: [PATCH] Keep implementing the structures. --- src/cmd/test/main.go | 2 ++ src/llx/main.go | 32 +++++++++++++++++++++++++++++--- src/poolx/main.go | 23 +++++++++++++---------- 3 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/cmd/test/main.go b/src/cmd/test/main.go index 45c2ecf..b5fe3d2 100644 --- a/src/cmd/test/main.go +++ b/src/cmd/test/main.go @@ -45,7 +45,9 @@ func main() { ll.Append(0) ll.Append(1) ll.Append(2) + ll.Push(11) ll.Set(1, 256) + ll.Del(3) for p := range ll.Range() { fmt.Println(p) } diff --git a/src/llx/main.go b/src/llx/main.go index f41a33c..167a66e 100644 --- a/src/llx/main.go +++ b/src/llx/main.go @@ -16,7 +16,6 @@ type LinkedList[V any] struct { } type Element[V any] struct { - prev *Element[V] next *Element[V] value V } @@ -75,12 +74,40 @@ func (ll *LinkedList[V]) Set(i int, v V) (bool) { return true } +func (ll *LinkedList[V]) Del(i int) (bool) { + if ll.ln <= i { + return false + } + + if i == 0 { + ll.before.next = + ll.before.next.next + ll.ln-- + return true + } + + el1, _ := ll.GetEl(i-1) + if i == ll.ln - 1 { + el1.next = nil + } else { + el2, _ := ll.GetEl(i+1) + el1.next = el2 + } + + ll.ln-- + return true +} + +/*func (ll *LinkedList[V comparable]) DelByVal(v V) int { + p := ll.before.next + i := 0 +}*/ + // Push in the beginning of the list. func (ll *LinkedList[V]) Push(v V) { prevNext := ll.before.next nextNext := &Element[V]{ next: prevNext, - prev: nil, value: v, } ll.before.next = nextNext @@ -100,7 +127,6 @@ func (ll *LinkedList[V]) Append(v V) { last := &Element[V]{ next: nil, - prev: ll.last, value: v, } diff --git a/src/poolx/main.go b/src/poolx/main.go index c43440b..8de05fb 100644 --- a/src/poolx/main.go +++ b/src/poolx/main.go @@ -1,25 +1,28 @@ package poolx -// The package implements ordered -// pool structure without any indexex. -// Should be used with only-one-value based -// structures. +import ( + "github.com/surdeus/godat/src/llx" +) + +// Ordered value-only based structure. +// Fast deleting by value. +// Cannot store multiple the same values. type Pool[V comparable] struct { - store map[V] uint64 - last uint64 + store *llx.LinkedList[V] + keys map[V] int } // Returns new empty pool. func New[V comparable]() *Pool { return &Pool{ - make(map[V] uint64), + llx.New[V]() 0, } } -func (p *Pool[V]) Push(v V) { - p.last++ - map[V] = p.last +func (p *Pool[V]) Append(v V) { + p.store.Append(v) + }