1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 00:22:41 +08:00
This commit is contained in:
2022-05-12 10:33:35 +08:00
parent 3dc3fbb0f2
commit 7930a64e59
5 changed files with 43 additions and 18 deletions

View File

@ -8,12 +8,13 @@ type hashMap[K constraints.Ordered, V any] struct {
m map[K]V m map[K]V
} }
func NewHashMap[K constraints.Ordered, V any]() *hashMap[K, V] { func NewHashMap[K constraints.Ordered, V any](maps ...map[K]V) *hashMap[K, V] {
return &hashMap[K, V]{m: make(map[K]V)} return &hashMap[K, V]{m: make(map[K]V)}
} }
func newHashMap[K constraints.Ordered, V any](maps ...map[K]V) *hashMap[K, V] { // synchronized
return &hashMap[K, V]{m: Merge(maps...)} func (m *hashMap[K, V]) Synchronized() *hashMap[K, V] {
return m
} }
func (m *hashMap[K, V]) Set(key K, value V) { func (m *hashMap[K, V]) Set(key K, value V) {
@ -92,10 +93,5 @@ func (m *hashMap[K, V]) Count() int {
} }
func (m *hashMap[K, V]) Clone() Map[K, V] { func (m *hashMap[K, V]) Clone() Map[K, V] {
r := make(map[K]V, len(m.m)) return NewHashMap(m.ToMap())
for k, v := range m.m {
r[k] = v
}
return newHashMap(r)
} }

View File

@ -40,10 +40,10 @@ func Merge[K comparable, V any](mm ...map[K]V) map[K]V {
// 按照键值生成字符串 // 按照键值生成字符串
func Join[K constraints.Ordered, V any](m Map[K, V], sep string, f func(k K, v V) string) string { func Join[K constraints.Ordered, V any](m Map[K, V], sep string, f func(k K, v V) string) string {
slice := make([]string, 0, m.Count()) slice := make([]string, 0, m.Count())
for _, k := range m.Keys() {
v, _ := m.Get(k) m.ForEach(func(k K, v V) {
slice = append(slice, f(k, v)) slice = append(slice, f(k, v))
} })
return strings.Join(slice, sep) return strings.Join(slice, sep)
} }

View File

@ -15,7 +15,7 @@ type rw_map[K constraints.Ordered, V any] struct {
func NewRWMap[K constraints.Ordered, V any](maps ...map[K]V) *rw_map[K, V] { func NewRWMap[K constraints.Ordered, V any](maps ...map[K]V) *rw_map[K, V] {
merged := Merge(maps...) merged := Merge(maps...)
return &rw_map[K, V]{m: newHashMap(merged)} return &rw_map[K, V]{m: NewHashMap(merged)}
} }
func newRWMap[K constraints.Ordered, V any](m Map[K, V]) *rw_map[K, V] { func newRWMap[K constraints.Ordered, V any](m Map[K, V]) *rw_map[K, V] {

View File

@ -13,6 +13,7 @@ var (
) )
type SortedMap[K constraints.Ordered, V any] interface { type SortedMap[K constraints.Ordered, V any] interface {
Map[K, V]
Asc() SortedMap[K, V] Asc() SortedMap[K, V]
Desc() SortedMap[K, V] Desc() SortedMap[K, V]
} }
@ -26,7 +27,7 @@ func NewSortedMap[K constraints.Ordered, V any](maps ...map[K]V) *sorted_map[K,
merged := Merge(maps...) merged := Merge(maps...)
return &sorted_map[K, V]{ return &sorted_map[K, V]{
keys: keys(merged), keys: keys(merged),
maps: newHashMap(merged), maps: NewHashMap(merged),
} }
} }
@ -35,11 +36,11 @@ func NewSortedByMap[K constraints.Ordered, V any](m Map[K, V]) *sorted_map[K, V]
} }
func (m *sorted_map[K, V]) Get(key K) (V, bool) { func (m *sorted_map[K, V]) Get(key K) (V, bool) {
return m.Get(key) return m.maps.Get(key)
} }
func (m *sorted_map[K, V]) Set(key K, value V) { func (m *sorted_map[K, V]) Set(key K, value V) {
m.Set(key, value) m.maps.Set(key, value)
m.keys = append(m.keys, key) m.keys = append(m.keys, key)
} }
@ -85,11 +86,15 @@ func (m *sorted_map[K, V]) Iter() <-chan *Entry[K, V] {
} }
func (m *sorted_map[K, V]) ForEach(f func(K, V)) { func (m *sorted_map[K, V]) ForEach(f func(K, V)) {
m.maps.ForEach(f) for _, k := range m.keys {
if v, ok := m.Get(k); ok {
f(k, v)
}
}
} }
func (m *sorted_map[K, V]) Exist(key K) bool { func (m *sorted_map[K, V]) Exist(key K) bool {
return m.Exist(key) return m.maps.Exist(key)
} }
func (m *sorted_map[K, V]) Keys() []K { func (m *sorted_map[K, V]) Keys() []K {

24
maps/sorted_map_test.go Normal file
View File

@ -0,0 +1,24 @@
package maps
import (
"fmt"
"testing"
)
func TestSortMapConvert(t *testing.T) {
var m = NewSortedMap(map[string]any{"aaa": "bbb"}).Asc()
var c Map[string, any] = m
t.Log(Join(c, " ", func(k string, v any) string {
return fmt.Sprintf("%s=%v", k, v)
}))
}
func TestSortedJoin(t *testing.T) {
var m = NewSortedMap(map[string]any{"b": "b", "a": "a", "d": "d", "c": "c"}).Asc()
t.Log(Join[string, any](m, "&", func(k string, v any) string {
return fmt.Sprintf("%s=%v", k, v)
}))
}