Skip to content

Latest commit

 

History

History
77 lines (55 loc) · 1.12 KB

collections.multimaps.md

File metadata and controls

77 lines (55 loc) · 1.12 KB

Overview

  • How to use a Multimap Data Structure (A.K.A. Bag)

Creation

type bagEntry[T any] struct {
    count int
    value T
}

// key:   string
// value: int
bag := make(map[string]*bagEntry[int])

Size

TODO: what do Guava MultiMaps do?

Insertion/Update

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}
}

Removal

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)
    }
}

Retrieval

TODO

Check for key

TODO...

Iteration

TODO...

Shallow copy

TODO: ...

Other Resources

  1. Guava MultiMap
  2. Apache commons :: Bag