init code
This commit is contained in:
42
internal/utils/collectionx/count_map.go
Normal file
42
internal/utils/collectionx/count_map.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package collectionx
|
||||
|
||||
import "github.com/samber/lo"
|
||||
|
||||
type CountMapN[T comparable] struct {
|
||||
N int
|
||||
Map map[T][]int
|
||||
}
|
||||
|
||||
func NewCountMapN[T comparable](n int) *CountMapN[T] {
|
||||
return &CountMapN[T]{
|
||||
N: n,
|
||||
Map: make(map[T][]int),
|
||||
}
|
||||
}
|
||||
|
||||
func (cm *CountMapN[T]) Inc(key T, ith int, value int) {
|
||||
m, ok := cm.Map[key]
|
||||
if !ok {
|
||||
m = make([]int, cm.N)
|
||||
cm.Map[key] = m
|
||||
}
|
||||
|
||||
m[ith] += value
|
||||
}
|
||||
|
||||
func (cm *CountMapN[T]) Get(key T, ith int) int {
|
||||
m, ok := cm.Map[key]
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
|
||||
return m[ith]
|
||||
}
|
||||
|
||||
func (cm *CountMapN[T]) Sum(key T) int {
|
||||
m, ok := cm.Map[key]
|
||||
if !ok {
|
||||
return 0
|
||||
}
|
||||
return lo.Sum(m)
|
||||
}
|
||||
Reference in New Issue
Block a user