mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-19 20:32:35 +02:00
refactor: remove unused old code
This commit is contained in:
parent
f7688a942a
commit
2b91d99ec6
2 changed files with 0 additions and 113 deletions
|
@ -29,64 +29,6 @@ func NewMap[MapType Map[KT, VT], KT comparable, VT any]() Map[KT, VT] {
|
||||||
return NewMapOf[KT, VT]()
|
return NewMapOf[KT, VT]()
|
||||||
}
|
}
|
||||||
|
|
||||||
// MapFind iterates over the map and returns the first value
|
|
||||||
// that satisfies the given criteria. The iteration is stopped
|
|
||||||
// once a value is found. If no value satisfies the criteria,
|
|
||||||
// the function returns the zero value of CT.
|
|
||||||
//
|
|
||||||
// The criteria function takes a value of type VT and returns a
|
|
||||||
// value of type CT and a boolean indicating whether the value
|
|
||||||
// satisfies the criteria. The boolean value is used to determine
|
|
||||||
// whether the iteration should be stopped.
|
|
||||||
//
|
|
||||||
// The function is safe for concurrent use.
|
|
||||||
func MapFind[KT comparable, VT, CT any](m Map[KT, VT], criteria func(VT) (CT, bool)) (_ CT) {
|
|
||||||
result := make(chan CT, 1)
|
|
||||||
|
|
||||||
m.Range(func(key KT, value VT) bool {
|
|
||||||
select {
|
|
||||||
case <-result: // already have a result
|
|
||||||
return false // stop iteration
|
|
||||||
default:
|
|
||||||
if got, ok := criteria(value); ok {
|
|
||||||
result <- got
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
select {
|
|
||||||
case v := <-result:
|
|
||||||
return v
|
|
||||||
default:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// MergeFrom merges the contents of another Map into this one, ignoring duplicated keys.
|
|
||||||
//
|
|
||||||
// Parameters:
|
|
||||||
//
|
|
||||||
// other: Map of values to add from
|
|
||||||
//
|
|
||||||
// Returns:
|
|
||||||
//
|
|
||||||
// Map of duplicated keys-value pairs
|
|
||||||
func (m Map[KT, VT]) MergeFrom(other Map[KT, VT]) Map[KT, VT] {
|
|
||||||
dups := NewMapOf[KT, VT]()
|
|
||||||
|
|
||||||
other.Range(func(k KT, v VT) bool {
|
|
||||||
if _, ok := m.Load(k); ok {
|
|
||||||
dups.Store(k, v)
|
|
||||||
} else {
|
|
||||||
m.Store(k, v)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
return dups
|
|
||||||
}
|
|
||||||
|
|
||||||
// RangeAll calls the given function for each key-value pair in the map.
|
// RangeAll calls the given function for each key-value pair in the map.
|
||||||
//
|
//
|
||||||
// Parameters:
|
// Parameters:
|
||||||
|
|
|
@ -18,58 +18,3 @@ func TestNewMapFrom(t *testing.T) {
|
||||||
ExpectTrue(t, m.Has("b"))
|
ExpectTrue(t, m.Has("b"))
|
||||||
ExpectTrue(t, m.Has("c"))
|
ExpectTrue(t, m.Has("c"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMapFind(t *testing.T) {
|
|
||||||
m := NewMapFrom(map[string]map[string]int{
|
|
||||||
"a": {
|
|
||||||
"a": 1,
|
|
||||||
},
|
|
||||||
"b": {
|
|
||||||
"a": 1,
|
|
||||||
"b": 2,
|
|
||||||
},
|
|
||||||
"c": {
|
|
||||||
"b": 2,
|
|
||||||
"c": 3,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
res := MapFind(m, func(inner map[string]int) (int, bool) {
|
|
||||||
if _, ok := inner["c"]; ok && inner["c"] == 3 {
|
|
||||||
return inner["c"], true
|
|
||||||
}
|
|
||||||
return 0, false
|
|
||||||
})
|
|
||||||
ExpectEqual(t, res, 3)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMergeFrom(t *testing.T) {
|
|
||||||
m1 := NewMapFrom(map[string]int{
|
|
||||||
"a": 1,
|
|
||||||
"b": 2,
|
|
||||||
"c": 3,
|
|
||||||
"d": 4,
|
|
||||||
})
|
|
||||||
m2 := NewMapFrom(map[string]int{
|
|
||||||
"a": 1,
|
|
||||||
"c": 123,
|
|
||||||
"e": 456,
|
|
||||||
"f": 6,
|
|
||||||
})
|
|
||||||
dup := m1.MergeFrom(m2)
|
|
||||||
|
|
||||||
ExpectEqual(t, m1.Size(), 6)
|
|
||||||
ExpectTrue(t, m1.Has("e"))
|
|
||||||
ExpectTrue(t, m1.Has("f"))
|
|
||||||
c, _ := m1.Load("c")
|
|
||||||
d, _ := m1.Load("d")
|
|
||||||
e, _ := m1.Load("e")
|
|
||||||
f, _ := m1.Load("f")
|
|
||||||
ExpectEqual(t, c, 3)
|
|
||||||
ExpectEqual(t, d, 4)
|
|
||||||
ExpectEqual(t, e, 456)
|
|
||||||
ExpectEqual(t, f, 6)
|
|
||||||
|
|
||||||
ExpectEqual(t, dup.Size(), 2)
|
|
||||||
ExpectTrue(t, dup.Has("a"))
|
|
||||||
ExpectTrue(t, dup.Has("c"))
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue