diff --git a/maps/hash_map.go b/maps/hash_map.go index 2e893db..ace0b0c 100644 --- a/maps/hash_map.go +++ b/maps/hash_map.go @@ -8,12 +8,13 @@ type hashMap[K constraints.Ordered, V any] struct { 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)} } -func newHashMap[K constraints.Ordered, V any](maps ...map[K]V) *hashMap[K, V] { - return &hashMap[K, V]{m: Merge(maps...)} +// synchronized +func (m *hashMap[K, V]) Synchronized() *hashMap[K, V] { + return m } 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] { - r := make(map[K]V, len(m.m)) - for k, v := range m.m { - r[k] = v - } - - return newHashMap(r) + return NewHashMap(m.ToMap()) } diff --git a/maps/map.go b/maps/map.go index aa688f5..77d51f4 100644 --- a/maps/map.go +++ b/maps/map.go @@ -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 { 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)) - } + }) return strings.Join(slice, sep) } diff --git a/maps/rwlock_map.go b/maps/rwlock_map.go index 96ee914..e2d03a8 100644 --- a/maps/rwlock_map.go +++ b/maps/rwlock_map.go @@ -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] { 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] { diff --git a/maps/sort_map.go b/maps/sort_map.go index 6b2c7f2..42c7a5d 100644 --- a/maps/sort_map.go +++ b/maps/sort_map.go @@ -13,6 +13,7 @@ var ( ) type SortedMap[K constraints.Ordered, V any] interface { + Map[K, V] Asc() 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...) return &sorted_map[K, V]{ 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) { - return m.Get(key) + return m.maps.Get(key) } 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) } @@ -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)) { - 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 { - return m.Exist(key) + return m.maps.Exist(key) } func (m *sorted_map[K, V]) Keys() []K { diff --git a/maps/sorted_map_test.go b/maps/sorted_map_test.go new file mode 100644 index 0000000..fb30728 --- /dev/null +++ b/maps/sorted_map_test.go @@ -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) + })) +}