diff --git a/go.mod b/go.mod index 1762160..90a9c15 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/surdeus/godat +module github.com/mojosa-software/godat go 1.19 diff --git a/src/cmd/sparse/main.go b/src/cmd/sparse/main.go index 34a61d4..e6ca2c2 100644 --- a/src/cmd/sparse/main.go +++ b/src/cmd/sparse/main.go @@ -1,7 +1,7 @@ package main import ( - "github.com/surdeus/godat/src/sparsex" + "github.com/mojosa-software/godat/src/sparsex" "fmt" ) diff --git a/src/cmd/test/main.go b/src/cmd/test/main.go index 4a6a2af..aa46234 100644 --- a/src/cmd/test/main.go +++ b/src/cmd/test/main.go @@ -1,9 +1,10 @@ package main import ( - "github.com/surdeus/godat/src/mapx" - "github.com/surdeus/godat/src/slicex" - "github.com/surdeus/godat/src/poolx" + //"github.com/mojosa-software/godat/src/mapx" + //"github.com/mojosa-software/godat/src/slicex" + //"github.com/mojosa-software/godat/src/poolx" + "github.com/mojosa-software/godat/src/rangex" "fmt" ) @@ -13,7 +14,13 @@ type Struct struct { } func main() { - m := map[string] string { + rangex.New[float32](0, .001, 1).Chan().ForEach( + func(i int, v float32) bool { + fmt.Println(i, v) + return true + }, + ) + /*m := map[string] string { "Key1" : "Value1", "Key2" : "Value2", "Key3" : "Value3", @@ -49,5 +56,5 @@ func main() { ll.Del(1) for p := range ll.Range() { fmt.Println(p) - } + }*/ } diff --git a/src/iterx/iter.go b/src/iterx/iter.go new file mode 100644 index 0000000..22acec7 --- /dev/null +++ b/src/iterx/iter.go @@ -0,0 +1,20 @@ +package iterx + +// Implementing the interface lets us iterate through the +// the data by lightweight channels. +type Channeler[K any, V any] interface { + Chan() PairChan[K, V] +} + +// Implementing the interface provides the way to +// convert the type to slice. +type Slicer[V any] interface { + Slice() []V +} + +// Implementing the interface provides us the way to +// convert the type to map. +type Mapper[K comparable, V any] interface { + Map() map[K] V +} + diff --git a/src/iterx/pair.go b/src/iterx/pair.go new file mode 100644 index 0000000..679d31f --- /dev/null +++ b/src/iterx/pair.go @@ -0,0 +1,27 @@ +package iterx + +// The type describes pair of key and value. +type Pair[K any, V any] struct { + V V + K K +} + +// The type describes channel of pairs. +type PairChan[K any, V any] chan Pair[K, V] + +// Slice of pairs. +type Pairs[K any, V any] []Pair[K, V] + +// ForEach for channels, like in JS. +// Be careful since the function does not close the +// channel so if fn breaks the loop then there can +// be values left. +func (pc PairChan[K, V]) ForEach(fn func(k K, v V) bool) { + for p := range pc { + if !fn(p.K, p.V) { + break + } + } +} + + diff --git a/src/mapx/main.go b/src/mapx/main.go index 52baf48..1a9f607 100644 --- a/src/mapx/main.go +++ b/src/mapx/main.go @@ -1,39 +1,7 @@ package mapx -// The type implements map type where -// you can get, set and delete by value -// since it store everything as ONLY entity -// both for keys and values way. -// Use only when you do not care about the order. -type UniqMap[K, V comparable] struct { - store map[K] V - rstore map[V] K -} - -// Returns new empty UniqMap. -func NewUniq[K, V comparable]() *UniqMap[K, V] { - return &UniqMap[K, V]{ - make(map[K] V), - make(map[V] K), - } -} - -// Sets new value v for the k key. -func (m *UniqMap[K, V]) Set(k K, v V) { - m.store[k] = v - m.rstore[v] = k -} - -// Get value by the k key. -func (m *UniqMap[K, V]) Get(k K) (V, bool) { - v, ok := m.store[k] - return v, ok -} - -func (m *UniqMap[K, V]) GetByValue(v V) (K, bool) { - k, ok := m.rstore[v] - return k, ok -} +// The package implements some more specific +// map structures for special uses. func Keys[K comparable, V any](m map[K] V) []K { r := make([]K, 0, len(m)) diff --git a/src/mapx/ordered.go b/src/mapx/ordered.go new file mode 100644 index 0000000..86e9e8e --- /dev/null +++ b/src/mapx/ordered.go @@ -0,0 +1,36 @@ +package mapx + +// The type makes the underlying map ordered, +// so every time you pass through all the values +// they will be in the same order. +type OrderedMap[K comparable, V any] struct { + store map[K] V + keys []K +} + +// Returns the new empty ordered map. +func NewOrdered[K comparable, V any]() *OrderedMap[K, V] { + return &OrderedMap[K, V]{ + store: make(map[K] V), + } +} + +// Set or reset the value in the map. +func (m *OrderedMap[K, V]) Set(k K, v V) { + _, ok := m.store[k] + if !ok { + keys = append(keys, k) + } + + m.store[k] = v +} + +// Get the value from the map. +func (m *OrderedMap[K, V]) Get(k K) (V, bool) { + v, ok := m.store[k] + return v, ok +} + +// Return channel of pairs. +func (m *OrderedMap[K, V]) Range() chan Pair + diff --git a/src/mapx/uniq.go b/src/mapx/uniq.go new file mode 100644 index 0000000..a643e26 --- /dev/null +++ b/src/mapx/uniq.go @@ -0,0 +1,37 @@ +package mapx + +// The type implements map type where +// you can get, set and delete by value +// since it store everything as ONLY entity +// both for keys and values way. +// Use only when you do not care about the order. +type UniqMap[K, V comparable] struct { + store map[K] V + rstore map[V] K +} + +// Returns new empty UniqMap. +func NewUniq[K, V comparable]() *UniqMap[K, V] { + return &UniqMap[K, V]{ + make(map[K] V), + make(map[V] K), + } +} + +// Sets new value v for the k key. +func (m *UniqMap[K, V]) Set(k K, v V) { + m.store[k] = v + m.rstore[v] = k +} + +// Get value by the k key. +func (m *UniqMap[K, V]) Get(k K) (V, bool) { + v, ok := m.store[k] + return v, ok +} + +func (m *UniqMap[K, V]) GetByValue(v V) (K, bool) { + k, ok := m.rstore[v] + return k, ok +} + diff --git a/src/poolx/main.go b/src/poolx/main.go index c2dc1c7..220ecf9 100644 --- a/src/poolx/main.go +++ b/src/poolx/main.go @@ -1,7 +1,7 @@ package poolx import ( - "github.com/surdeus/godat/src/llx" + "github.com/mojosa-software/godat/src/llx" ) // Ordered value-only based structure. diff --git a/src/rangex/range.go b/src/rangex/range.go new file mode 100644 index 0000000..7b71049 --- /dev/null +++ b/src/rangex/range.go @@ -0,0 +1,55 @@ +package rangex + +import ( + "github.com/mojosa-software/godat/src/iterx" + cnts "golang.org/x/exp/constraints" +) + + +type Range[V cnts.Ordered] struct { + start, step, end V +} + +// Returns the new Range strucucture with corresponding start, step and end. +// If the values can never reach the end then the function will return nil. +func New[V cnts.Ordered](start, step, end V) *Range[V] { + return &Range[V]{ + start, step, end, + } +} + +func (r *Range[V]) Chan() iterx.PairChan[int, V] { + start, step, end := r.start, r.step, r.end + c := make(iterx.PairChan[int, V]) + go func(){ + var compare func(a, b V) bool + + less := func(a, b V) bool { + return a < b + } + + more := func(a, b V) bool { + return a > b + } + + if start < end { + compare = less + } else { + compare = more + } + + j := 0 + for i := start ; compare(i, end) ; i += step { + c <- iterx.Pair[int, V]{ + K:j, + V:i, + } + j++ + } + + close(c) + }() + + return c +} +