Keep implementing the structures.

This commit is contained in:
Andrey Parhomenko 2023-04-28 17:01:57 +03:00
parent 302da3ec07
commit 4494e83428
3 changed files with 44 additions and 13 deletions

View file

@ -45,7 +45,9 @@ func main() {
ll.Append(0) ll.Append(0)
ll.Append(1) ll.Append(1)
ll.Append(2) ll.Append(2)
ll.Push(11)
ll.Set(1, 256) ll.Set(1, 256)
ll.Del(3)
for p := range ll.Range() { for p := range ll.Range() {
fmt.Println(p) fmt.Println(p)
} }

View file

@ -16,7 +16,6 @@ type LinkedList[V any] struct {
} }
type Element[V any] struct { type Element[V any] struct {
prev *Element[V]
next *Element[V] next *Element[V]
value V value V
} }
@ -75,12 +74,40 @@ func (ll *LinkedList[V]) Set(i int, v V) (bool) {
return true 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. // Push in the beginning of the list.
func (ll *LinkedList[V]) Push(v V) { func (ll *LinkedList[V]) Push(v V) {
prevNext := ll.before.next prevNext := ll.before.next
nextNext := &Element[V]{ nextNext := &Element[V]{
next: prevNext, next: prevNext,
prev: nil,
value: v, value: v,
} }
ll.before.next = nextNext ll.before.next = nextNext
@ -100,7 +127,6 @@ func (ll *LinkedList[V]) Append(v V) {
last := &Element[V]{ last := &Element[V]{
next: nil, next: nil,
prev: ll.last,
value: v, value: v,
} }

View file

@ -1,25 +1,28 @@
package poolx package poolx
// The package implements ordered import (
// pool structure without any indexex. "github.com/surdeus/godat/src/llx"
// Should be used with only-one-value based )
// structures.
// Ordered value-only based structure.
// Fast deleting by value.
// Cannot store multiple the same values.
type Pool[V comparable] struct { type Pool[V comparable] struct {
store map[V] uint64 store *llx.LinkedList[V]
last uint64 keys map[V] int
} }
// Returns new empty pool. // Returns new empty pool.
func New[V comparable]() *Pool { func New[V comparable]() *Pool {
return &Pool{ return &Pool{
make(map[V] uint64), llx.New[V]()
0, 0,
} }
} }
func (p *Pool[V]) Push(v V) { func (p *Pool[V]) Append(v V) {
p.last++ p.store.Append(v)
map[V] = p.last
} }