2023-02-17 16:08:25 +03:00
|
|
|
package sparsex
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
cons "golang.org/x/exp/constraints"
|
|
|
|
)
|
|
|
|
|
2023-08-28 12:06:51 +03:00
|
|
|
// The package implements sparse array structure.
|
|
|
|
// You can make sparse matrices by assigning
|
|
|
|
// the value type to another sparse array.
|
2023-02-17 16:08:25 +03:00
|
|
|
|
2023-07-21 19:00:09 +03:00
|
|
|
// The sparse array type.
|
2023-08-28 12:06:51 +03:00
|
|
|
// Does not sort by default on setting so by
|
|
|
|
// default is not ordered for the Chan, Keys, KeyChan, Slice methods.
|
|
|
|
// Its made for the optimization sakes because
|
|
|
|
// initially the package was made for the gox (2D game engine) package use
|
|
|
|
// to provide dynamic layering.
|
|
|
|
// See *Sparse[K, V].ShouldSort method to change it.
|
2023-02-17 16:08:25 +03:00
|
|
|
type Sparse[K cons.Ordered, V any] struct {
|
|
|
|
store map[K] V
|
2023-08-28 12:06:51 +03:00
|
|
|
def V
|
2023-02-17 16:08:25 +03:00
|
|
|
keys []K
|
|
|
|
shouldSort bool
|
|
|
|
}
|
|
|
|
|
2023-08-28 12:06:51 +03:00
|
|
|
// Returns new sparse array taking all the values from valuesStores
|
|
|
|
// into the structure.
|
|
|
|
func New[K cons.Ordered, V any](valueStores ...map[K] V) *Sparse[K, V] {
|
|
|
|
|
|
|
|
finalLen := 0
|
|
|
|
for _, valueStore := range valueStores {
|
|
|
|
finalLen = len(valueStore)
|
|
|
|
}
|
|
|
|
keys := make([]K, finalLen)
|
|
|
|
store := make(map[K] V)
|
|
|
|
for _, valueStore := range valueStores {
|
|
|
|
i := 0
|
|
|
|
for k, v := range valueStore {
|
|
|
|
keys[i] = k
|
|
|
|
store[k] = v
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
2023-02-17 16:08:25 +03:00
|
|
|
return &Sparse[K, V]{
|
2023-08-28 12:06:51 +03:00
|
|
|
store: store,
|
|
|
|
keys: keys,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define if should sort each time we set or delete.
|
|
|
|
func (s *Sparse[K, V]) ShouldSort(v bool) *Sparse[K, V] {
|
|
|
|
s.shouldSort = v
|
|
|
|
if s.shouldSort {
|
|
|
|
s.Sort()
|
2023-02-17 16:08:25 +03:00
|
|
|
}
|
2023-08-28 12:06:51 +03:00
|
|
|
return s
|
2023-02-17 16:08:25 +03:00
|
|
|
}
|
|
|
|
|
2023-08-28 12:06:51 +03:00
|
|
|
// Sets the default sparse array value.
|
|
|
|
// Returned when there is no key for the value.
|
|
|
|
func (s *Sparse[K, V]) Default(v V) *Sparse[K, V] {
|
|
|
|
s.def = v
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the value by the key. The secound value
|
|
|
|
// represents whether the array contains the value.
|
|
|
|
// If the array does not contain the value then default one
|
|
|
|
// will be returned.
|
2023-02-17 16:08:25 +03:00
|
|
|
func (s *Sparse[K, V]) Get(key K) (V, bool) {
|
|
|
|
val, ok := s.store[key]
|
2023-08-28 12:06:51 +03:00
|
|
|
if !ok {
|
|
|
|
val = s.def
|
|
|
|
}
|
2023-02-17 16:08:25 +03:00
|
|
|
return val, ok
|
|
|
|
}
|
|
|
|
|
2023-07-21 19:00:09 +03:00
|
|
|
// Set the value to the key.
|
2023-02-17 16:08:25 +03:00
|
|
|
func (s *Sparse[K, V]) Set(k K, v V) {
|
|
|
|
_, ok := s.store[k]
|
|
|
|
if !ok {
|
|
|
|
s.keys = append(s.keys, k)
|
|
|
|
if s.shouldSort {
|
|
|
|
s.Sort()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.store[k] = v
|
|
|
|
}
|
|
|
|
|
2023-07-21 19:00:09 +03:00
|
|
|
// Delete the value by the key.
|
2023-08-28 12:06:51 +03:00
|
|
|
func (s *Sparse[K, V]) Delete(k K) {
|
2023-02-17 16:08:25 +03:00
|
|
|
delete(s.store, k)
|
|
|
|
|
|
|
|
// To know if the loop was run.
|
|
|
|
idx := -1
|
|
|
|
|
|
|
|
for i, v := range s.keys {
|
|
|
|
if v == k {
|
|
|
|
idx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if idx != -1 {
|
|
|
|
s.keys = append(s.keys[:idx], s.keys[idx+1:]...)
|
|
|
|
if s.shouldSort {
|
|
|
|
s.Sort()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 12:06:51 +03:00
|
|
|
// Alias for the Delete method.
|
|
|
|
func (s *Sparse[K, V]) Del(k K) {
|
|
|
|
s.Delete(k)
|
|
|
|
}
|
|
|
|
|
2023-07-21 19:00:09 +03:00
|
|
|
// Returns channel of pairs.
|
|
|
|
func (s *Sparse[K, V]) Chan(
|
2023-08-28 12:06:51 +03:00
|
|
|
) chan V {
|
2023-02-17 16:08:25 +03:00
|
|
|
keys := s.keys
|
|
|
|
store := s.store
|
2023-08-28 12:06:51 +03:00
|
|
|
ret := make(chan V)
|
2023-02-17 16:08:25 +03:00
|
|
|
|
|
|
|
go func() {
|
2023-07-21 19:00:09 +03:00
|
|
|
for _, k := range keys {
|
2023-08-28 12:06:51 +03:00
|
|
|
ret <- store[k]
|
2023-02-17 16:08:25 +03:00
|
|
|
}
|
|
|
|
close(ret)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-08-28 12:06:51 +03:00
|
|
|
// Returns a slice of the keys of the array.
|
|
|
|
func (s *Sparse[K, V]) Keys() []K {
|
|
|
|
return s.keys
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns channel of keys in its order in the structure.
|
|
|
|
func (s *Sparse[K, V]) KeyChan() chan K {
|
|
|
|
ret := make(chan K)
|
|
|
|
go func() {
|
|
|
|
for _, k := range s.keys {
|
|
|
|
ret <- k
|
|
|
|
}
|
|
|
|
close(ret)
|
|
|
|
}()
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns slice of the already set values.
|
|
|
|
func (s *Sparse[K, V]) Slice() []V {
|
|
|
|
ret := []V{}
|
|
|
|
for v := range s.Chan() {
|
|
|
|
ret = append(ret, v)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the keys making current array finely ordered.
|
2023-02-17 16:08:25 +03:00
|
|
|
func (s *Sparse[K, V]) Sort() {
|
|
|
|
sort.Slice(s.keys, func(i, j int) bool {
|
|
|
|
return s.keys[i] < s.keys[j]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|