Keep implementing the structures.
This commit is contained in:
parent
302da3ec07
commit
4494e83428
3 changed files with 44 additions and 13 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue