mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
fix bug
This commit is contained in:
@ -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())
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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] {
|
||||
|
@ -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 {
|
||||
|
24
maps/sorted_map_test.go
Normal file
24
maps/sorted_map_test.go
Normal 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)
|
||||
}))
|
||||
}
|
Reference in New Issue
Block a user