- How to use a Multimap Data Structure (A.K.A. Bag)
type bagEntry[T any] struct {
count int
value T
}
// key: string
// value: int
bag := make(map[string]*bagEntry[int])
TODO: what do Guava MultiMaps do?
func AddToBag[K comparable, V any](
bag map[K]*bagEntry[V],
key K,
value V) {
if entry, found := bag[key]; found {
entry.count++
return
}
bag[key] = &bagEntry[V]{
count: 1,
value: value}
}
func RemoveFromBag[K comparable, V any](
bag map[K]*bagEntry[V],
key K) {
entry, found := bag[key]
if !found {
return
}
entry.count--
if entry.count <= 0 {
delete(bag, key)
}
}
TODO
TODO...
TODO...
TODO: ...