...
This commit is contained in:
parent
5c01581006
commit
863da1e309
3 changed files with 23 additions and 19 deletions
|
@ -12,12 +12,12 @@ func main() {
|
||||||
unord.Set(-4, "die")
|
unord.Set(-4, "die")
|
||||||
unord.Set(-1000, "withme")
|
unord.Set(-1000, "withme")
|
||||||
|
|
||||||
for v := range unord.Vals() {
|
for p := range unord.Chan() {
|
||||||
fmt.Println(v.K, v.V)
|
fmt.Println(p.K, p.V)
|
||||||
}
|
}
|
||||||
|
|
||||||
unord.Sort()
|
unord.Sort()
|
||||||
for v := range unord.Vals() {
|
for p := range unord.Chan() {
|
||||||
fmt.Println(v.K, v.V)
|
fmt.Println(p.K, p.V)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
|
|
||||||
// The package implements some more specific
|
// The package implements some more specific
|
||||||
// map structures for special uses.
|
// map structures for special uses.
|
||||||
|
// Implemented mostly to be embedded to other structures.
|
||||||
|
|
||||||
// General map type, wrap for the built-in one.
|
// General map type, wrap for the built-in one.
|
||||||
type Map[K comparable, V any] map[K] V
|
type Map[K comparable, V any] map[K] V
|
||||||
|
|
|
@ -3,38 +3,38 @@ package sparsex
|
||||||
import (
|
import (
|
||||||
"sort"
|
"sort"
|
||||||
cons "golang.org/x/exp/constraints"
|
cons "golang.org/x/exp/constraints"
|
||||||
|
"github.com/mojosa-software/godat/src/iterx"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The package implements a simple ordered map.
|
// The package implements a simple ordered map.
|
||||||
// In fact can be used as a sparse array so it is
|
// In fact can be used as a sparse array so it is
|
||||||
// where the name comes from.
|
// where the name comes from.
|
||||||
|
|
||||||
type Pair[K cons.Ordered, V any] struct {
|
// The sparse array type.
|
||||||
K K
|
|
||||||
V V
|
|
||||||
}
|
|
||||||
|
|
||||||
type Sparse[K cons.Ordered, V any] struct {
|
type Sparse[K cons.Ordered, V any] struct {
|
||||||
store map[K] V
|
store map[K] V
|
||||||
keys []K
|
keys []K
|
||||||
shouldSort bool
|
shouldSort bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns new sparse array
|
// Returns new sparse array.
|
||||||
func New[K cons.Ordered, V any](s bool) *Sparse[K, V] {
|
// If shouldSort == true then it will sort the array on
|
||||||
|
// each change.
|
||||||
|
func New[K cons.Ordered, V any](shouldSort bool) *Sparse[K, V] {
|
||||||
return &Sparse[K, V]{
|
return &Sparse[K, V]{
|
||||||
store: make(map[K] V),
|
store: make(map[K] V),
|
||||||
keys: []K{},
|
keys: []K{},
|
||||||
shouldSort: s,
|
shouldSort: shouldSort,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the value by the key.
|
||||||
func (s *Sparse[K, V]) Get(key K) (V, bool) {
|
func (s *Sparse[K, V]) Get(key K) (V, bool) {
|
||||||
val, ok := s.store[key]
|
val, ok := s.store[key]
|
||||||
return val, ok
|
return val, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set the value to the key.
|
||||||
func (s *Sparse[K, V]) Set(k K, v V) {
|
func (s *Sparse[K, V]) Set(k K, v V) {
|
||||||
_, ok := s.store[k]
|
_, ok := s.store[k]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -47,6 +47,7 @@ func (s *Sparse[K, V]) Set(k K, v V) {
|
||||||
s.store[k] = v
|
s.store[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete the value by the key.
|
||||||
func (s Sparse[K, V]) Del(k K) {
|
func (s Sparse[K, V]) Del(k K) {
|
||||||
delete(s.store, k)
|
delete(s.store, k)
|
||||||
|
|
||||||
|
@ -68,16 +69,18 @@ func (s Sparse[K, V]) Del(k K) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Sparse[K, V]) Vals(
|
// Returns channel of pairs.
|
||||||
) chan Pair[K, V] {
|
func (s *Sparse[K, V]) Chan(
|
||||||
|
) iterx.PairChan[K, V] {
|
||||||
keys := s.keys
|
keys := s.keys
|
||||||
store := s.store
|
store := s.store
|
||||||
ret := make(chan Pair[K, V])
|
ret := make(iterx.PairChan[K, V])
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for _, v := range keys {
|
for _, k := range keys {
|
||||||
ret <- Pair[K, V]{
|
ret <- iterx.Pair[K, V]{
|
||||||
v, store[v],
|
K: k,
|
||||||
|
V: store[k],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
close(ret)
|
close(ret)
|
||||||
|
|
Loading…
Reference in a new issue