Separated pool and linked list implementations.

This commit is contained in:
Andrey Parhomenko 2023-04-28 17:51:39 +03:00
parent 575c83ef52
commit f51a8ab74b
3 changed files with 42 additions and 39 deletions

View file

@ -3,7 +3,7 @@ package main
import ( import (
"github.com/surdeus/godat/src/mapx" "github.com/surdeus/godat/src/mapx"
"github.com/surdeus/godat/src/slicex" "github.com/surdeus/godat/src/slicex"
"github.com/surdeus/godat/src/llx" "github.com/surdeus/godat/src/poolx"
"fmt" "fmt"
) )
@ -41,12 +41,12 @@ func main() {
fmt.Printf("%q\n", mapx.Reverse(m)) fmt.Printf("%q\n", mapx.Reverse(m))
fmt.Printf("%v\n", mapx.Reverse(m1)) fmt.Printf("%v\n", mapx.Reverse(m1))
ll := llx.NewComparable[int]() ll := poolx.New[int]()
ll.Append(0) ll.Append(0)
ll.Append(1) ll.Append(1)
ll.Append(2) ll.Append(2)
ll.Set(1, 256) ll.Del(256)
ll.DelVal(256) ll.Del(1)
for p := range ll.Range() { for p := range ll.Range() {
fmt.Println(p) fmt.Println(p)
} }

View file

@ -25,18 +25,6 @@ type Pair[V any] struct {
V V 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. // Returns new empty linked list storing the V type.
func New[V any]() *LinkedList[V] { func New[V any]() *LinkedList[V] {
@ -111,22 +99,6 @@ func (ll *LinkedList[V]) Del(i int) (bool) {
return true 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. // 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
@ -161,6 +133,22 @@ func (ll *LinkedList[V]) Append(v V) {
ll.ln++ 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. // Returns a channel of Pair that contains index and the value.
func (ll *LinkedList[V]) Range() chan Pair[V] { func (ll *LinkedList[V]) Range() chan Pair[V] {
chn := make(chan Pair[V]) chn := make(chan Pair[V])

View file

@ -10,14 +10,12 @@ import (
type Pool[V comparable] struct { type Pool[V comparable] struct {
store *llx.LinkedList[V] store *llx.LinkedList[V]
keys map[V] int
} }
// Returns new empty pool. // Return new empty pool.
func New[V comparable]() *Pool { func New[V comparable]() *Pool[V] {
return &Pool{ return &Pool[V]{
llx.New[V]() store: llx.New[V](),
0,
} }
} }
@ -25,6 +23,23 @@ func (p *Pool[V]) Append(v V) {
p.store.Append(v) p.store.Append(v)
} }
// Deletes the first appearance of the value in the list.
func (p *Pool[V]) Del(v V) bool { 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()
}