Added more test cmds.
This commit is contained in:
parent
863da1e309
commit
63bff23ca4
6 changed files with 84 additions and 15 deletions
22
src/cmd/ll/main.go
Normal file
22
src/cmd/ll/main.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/mojosa-software/godat/src/llx"
|
||||
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ll := llx.New[string]()
|
||||
ll.Append("zero")
|
||||
ll.Append("one")
|
||||
ll.Append("two")
|
||||
ll.Append("three")
|
||||
ll.Append("four")
|
||||
ll.Push("minus one")
|
||||
|
||||
for p := range ll.Chan() {
|
||||
fmt.Println(p.K, p.V)
|
||||
}
|
||||
}
|
||||
|
26
src/cmd/pool/main.go
Normal file
26
src/cmd/pool/main.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/mojosa-software/godat/src/poolx"
|
||||
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
values := []string{
|
||||
"zero", "one",
|
||||
"should be deleted",
|
||||
"two", "three",
|
||||
}
|
||||
pool := poolx.New[string]()
|
||||
for _, v := range values {
|
||||
pool.Append(v)
|
||||
}
|
||||
|
||||
pool.DeleteValue("should be deleted")
|
||||
|
||||
for p := range pool.Chan() {
|
||||
fmt.Println(p.K, p.V)
|
||||
}
|
||||
}
|
||||
|
11
src/iterx/misc.go
Normal file
11
src/iterx/misc.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package iterx
|
||||
|
||||
func ChanToSlice[V any](c chan V) []V {
|
||||
ret := []V{}
|
||||
for v := range c {
|
||||
ret = append(ret, v)
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
|
@ -5,6 +5,11 @@ package llx
|
|||
// linked list than in standard library since it uses
|
||||
// the new conception of generics.
|
||||
|
||||
import (
|
||||
"github.com/mojosa-software/godat/src/iterx"
|
||||
)
|
||||
|
||||
// The type represents linked list data structure.
|
||||
type LinkedList[V any] struct {
|
||||
// First empty element (not used to store values).
|
||||
// For fast pushing.
|
||||
|
@ -15,17 +20,12 @@ type LinkedList[V any] struct {
|
|||
ln int
|
||||
}
|
||||
|
||||
// The type represents element of the linked list.
|
||||
type Element[V any] struct {
|
||||
next *Element[V]
|
||||
value V
|
||||
}
|
||||
|
||||
type Pair[V any] struct {
|
||||
I int
|
||||
V V
|
||||
}
|
||||
|
||||
|
||||
// Returns new empty linked list storing the V type.
|
||||
func New[V any]() *LinkedList[V] {
|
||||
return &LinkedList[V]{
|
||||
|
@ -36,7 +36,7 @@ func New[V any]() *LinkedList[V] {
|
|||
}
|
||||
|
||||
// Get length of the linked list.
|
||||
func (ll *LinkedList[V]) Len() int {
|
||||
func (ll *LinkedList[V]) Length() int {
|
||||
return ll.ln
|
||||
}
|
||||
|
||||
|
@ -75,7 +75,8 @@ func (ll *LinkedList[V]) Set(i int, v V) (bool) {
|
|||
return true
|
||||
}
|
||||
|
||||
func (ll *LinkedList[V]) Del(i int) (bool) {
|
||||
// Deletes the element by its index.
|
||||
func (ll *LinkedList[V]) Delete(i int) (bool) {
|
||||
if ll.ln <= i {
|
||||
return false
|
||||
}
|
||||
|
@ -133,32 +134,40 @@ func (ll *LinkedList[V]) Append(v V) {
|
|||
ll.ln++
|
||||
}
|
||||
|
||||
// Returns the first element of the linked list.
|
||||
func (ll *LinkedList[V]) First() *Element[V] {
|
||||
return ll.before.next
|
||||
}
|
||||
|
||||
// Get elements value.
|
||||
func (ll *Element[V]) Value() V {
|
||||
return ll.value
|
||||
}
|
||||
|
||||
// Returns the next element. If the returned value == nil,
|
||||
// then it is the last element.
|
||||
func (ll *Element[V]) Next() *Element[V] {
|
||||
return ll.next
|
||||
}
|
||||
|
||||
// Returns the last element.
|
||||
func (ll *LinkedList[V]) Last() *Element[V] {
|
||||
return ll.last
|
||||
}
|
||||
|
||||
// Returns a channel of Pair that contains index and the value.
|
||||
func (ll *LinkedList[V]) Range() chan Pair[V] {
|
||||
chn := make(chan Pair[V])
|
||||
func (ll *LinkedList[V]) Chan() iterx.PairChan[int, V] {
|
||||
chn := make(iterx.PairChan[int, V])
|
||||
go func(){
|
||||
i := -1
|
||||
el := ll.before
|
||||
for el.next != nil {
|
||||
i++
|
||||
el = el.next
|
||||
chn <- Pair[V]{i, el.value}
|
||||
chn <- iterx.Pair[int, V]{
|
||||
K: i,
|
||||
V: el.value,
|
||||
}
|
||||
}
|
||||
close(chn)
|
||||
}()
|
||||
|
|
|
@ -2,6 +2,7 @@ package poolx
|
|||
|
||||
import (
|
||||
"github.com/mojosa-software/godat/src/llx"
|
||||
"github.com/mojosa-software/godat/src/iterx"
|
||||
)
|
||||
|
||||
// Ordered value-only based structure.
|
||||
|
@ -24,12 +25,12 @@ func (p *Pool[V]) Append(v V) {
|
|||
}
|
||||
|
||||
// Deletes the first appearance of the value in the list.
|
||||
func (p *Pool[V]) Del(v V) bool {
|
||||
func (p *Pool[V]) DeleteValue(v V) bool {
|
||||
i := 0
|
||||
ll := p.store
|
||||
for e := ll.First() ; e != nil ; e = e.Next() {
|
||||
if e.Value() == v {
|
||||
ll.Del(i)
|
||||
ll.Delete(i)
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -39,7 +40,7 @@ func (p *Pool[V]) Del(v V) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func (p *Pool[V]) Range() chan llx.Pair[V] {
|
||||
return p.store.Range()
|
||||
func (p *Pool[V]) Chan() iterx.PairChan[int, V] {
|
||||
return p.store.Chan()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue