diff --git a/src/cmd/test/main.go b/src/cmd/test/main.go index aa46234..dee5d0e 100644 --- a/src/cmd/test/main.go +++ b/src/cmd/test/main.go @@ -5,6 +5,7 @@ import ( //"github.com/mojosa-software/godat/src/slicex" //"github.com/mojosa-software/godat/src/poolx" "github.com/mojosa-software/godat/src/rangex" + "github.com/mojosa-software/godat/src/mapx" "fmt" ) @@ -13,13 +14,38 @@ type Struct struct { Value int } +type MyMap struct { + mapx.Map[string, int] +} + +func NewMyMap() *MyMap { + return &MyMap{ + Map: mapx.New[string, int](), + } +} + func main() { - rangex.New[float32](0, .001, 1).Chan().ForEach( + rangex.New[float32](0, .001, 0.050).Chan().ForEach( func(i int, v float32) bool { fmt.Println(i, v) return true }, ) + + m := mapx.New[string, int]() + m.Set("suck", 1) + m.Set("cock", 10) + + for k, v := range m { + fmt.Println(k, v) + } + + fmt.Println(m.Has("dick")) + + mm := NewMyMap() + mm.Set("dicker", 15) + + fmt.Println(mm.Get("dicker")) /*m := map[string] string { "Key1" : "Value1", "Key2" : "Value2", diff --git a/src/mapx/main.go b/src/mapx/main.go index 1a9f607..2d0f285 100644 --- a/src/mapx/main.go +++ b/src/mapx/main.go @@ -1,9 +1,22 @@ package mapx +import ( + "fmt" +) + // The package implements some more specific // map structures for special uses. -func Keys[K comparable, V any](m map[K] V) []K { +// General map type, wrap for the built-in one. +type Map[K comparable, V any] map[K] V + +// Returns new basic map. +func New[K comparable, V any]() Map[K, V] { + return make(Map[K, V]) +} + +// Returns slice of keys of the map. +func (m Map[K, V]) Keys() []K { r := make([]K, 0, len(m)) for k := range m { r = append(r, k) @@ -12,7 +25,8 @@ func Keys[K comparable, V any](m map[K] V) []K { return r } -func Values[K comparable, V any](m map[K] V) []V { +// Returns slice of values of the map. +func (m Map[K, V]) Values(sm map[K] V) []V { r := make([]V, 0, len(m)) for _, v := range m { r = append(r, v) @@ -21,12 +35,23 @@ func Values[K comparable, V any](m map[K] V) []V { return r } -func Reverse[K, V comparable](m map[K] V) map[V] K { - r := make(map[V] K) - for k, v := range m { - r[v] = k - } - - return r +// Checks if the map contains the key. +func (m Map[K, V]) Has(k K) bool { + _, ok := m[k] + return ok +} + +// Sets the new value by key or resets if it exists. +func (m Map[K, V]) Set(k K, v V) { + m[k] = v +} + +// Returns the value by key. Panics if there is no such key. +func (m Map[K, V]) Get(k K) V { + v, ok := m[k] + if !ok { + panic(fmt.Sprintf("there is no such key '%v'", k)) + } + return v } diff --git a/src/mapx/ordered.go b/src/mapx/ordered.go index 86e9e8e..7d0a3f0 100644 --- a/src/mapx/ordered.go +++ b/src/mapx/ordered.go @@ -1,5 +1,9 @@ package mapx +import ( + "github.com/mojosa-software/godat/src/iterx" +) + // The type makes the underlying map ordered, // so every time you pass through all the values // they will be in the same order. @@ -19,18 +23,41 @@ func NewOrdered[K comparable, V any]() *OrderedMap[K, V] { func (m *OrderedMap[K, V]) Set(k K, v V) { _, ok := m.store[k] if !ok { - keys = append(keys, k) + m.keys = append(m.keys, k) } m.store[k] = v } +func (m *OrderedMap[K, V]) Has(k K) bool { + _, ok := m.store[k] + return ok +} + // Get the value from the map. -func (m *OrderedMap[K, V]) Get(k K) (V, bool) { - v, ok := m.store[k] - return v, ok +func (m *OrderedMap[K, V]) Get(k K) (V) { + v := m.store[k] + return v +} + +// +func (m *OrderedMap[K, V]) Keys() []K { + return m.keys } // Return channel of pairs. -func (m *OrderedMap[K, V]) Range() chan Pair +func (m *OrderedMap[K, V]) Chan() iterx.PairChan[K, V] { + chn := make(iterx.PairChan[K, V]) + go func(){ + for _, k := range m.keys { + chn <- iterx.Pair[K, V]{ + K: k, + V: m.Get(k), + } + } + close(chn) + }() + + return chn +}