Documentation ¶
Overview ¶
Package slicemultimap implements a multimap backed by go's native slice.
A slicemultimap is a multimap that can hold duplicate key-value pairs and that maintains the insertion ordering of values for a given key.
This multimap is typically known as ListMultimap in other languages.
Elements are unordered in the map.
Structure is not thread safe.
Index ¶
- type MultiMap
- func (m *MultiMap) Clear()
- func (m *MultiMap) Contains(key interface{}, value interface{}) bool
- func (m *MultiMap) ContainsKey(key interface{}) (found bool)
- func (m *MultiMap) ContainsValue(value interface{}) bool
- func (m *MultiMap) Empty() bool
- func (m *MultiMap) Entries() []multimap.Entry
- func (m *MultiMap) Get(key interface{}) (values []interface{}, found bool)
- func (m *MultiMap) KeySet() []interface{}
- func (m *MultiMap) Keys() []interface{}
- func (m *MultiMap) Put(key interface{}, value interface{})
- func (m *MultiMap) PutAll(key interface{}, values []interface{})
- func (m *MultiMap) Remove(key interface{}, value interface{})
- func (m *MultiMap) RemoveAll(key interface{})
- func (m *MultiMap) Size() int
- func (m *MultiMap) Values() []interface{}
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MultiMap ¶
type MultiMap struct {
// contains filtered or unexported fields
}
MultiMap holds the elements in go's native map.
func (*MultiMap) Clear ¶
func (m *MultiMap) Clear()
Clear removes all elements from the map.
Example ¶
The following examples is to demonstrate basic usage of MultiMap.
package main import ( "fmt" "github.com/jwangsadinata/go-multimap/slicemultimap" ) func main() { // Create a new multimap m := slicemultimap.New() // empty // Put some contents to the multimap. m.Put(1, "x") // 1->x m.Put(2, "b") // 1->x, 2->b m.Put(1, "a") // 1->a, 1->x, 2->b // Clear the current map. m.Clear() // empty // Verify that it is empty. fmt.Printf("%v\n", m.Empty()) fmt.Printf("%v\n", m.Size()) }
Output: true 0
func (*MultiMap) Contains ¶
Contains returns true if this multimap contains at least one key-value pair with the key key and the value value.
Example ¶
package main import ( "fmt" "github.com/jwangsadinata/go-multimap/slicemultimap" ) func main() { // Create a new multimap m := slicemultimap.New() // empty // Put some contents to the multimap. m.Put(1, "x") // 1->x m.Put(2, "b") // 1->x, 2->b m.Put(1, "a") // 1->a, 1->x, 2->b // Check whether the multimap contains a certain key/value pair. found := m.Contains(1, "a") // true fmt.Printf("%v\n", found) found = m.Contains(1, "b") // false fmt.Printf("%v\n", found) found = m.Contains(2, "b") // true fmt.Printf("%v\n", found) }
Output: true false true
func (*MultiMap) ContainsKey ¶
ContainsKey returns true if this multimap contains at least one key-value pair with the key key.
Example ¶
package main import ( "fmt" "github.com/jwangsadinata/go-multimap/slicemultimap" ) func main() { // Create a new multimap m := slicemultimap.New() // empty // Put some contents to the multimap. m.Put(1, "x") // 1->x m.Put(2, "b") // 1->x, 2->b m.Put(1, "a") // 1->a, 1->x, 2->b // Check whether the multimap contains a certain key. found := m.ContainsKey(1) // true fmt.Printf("%v\n", found) found = m.ContainsKey(2) // true fmt.Printf("%v\n", found) found = m.ContainsKey(3) // true fmt.Printf("%v\n", found) }
Output: true true false
func (*MultiMap) ContainsValue ¶
ContainsValue returns true if this multimap contains at least one key-value pair with the value value.
Example ¶
package main import ( "fmt" "github.com/jwangsadinata/go-multimap/slicemultimap" ) func main() { // Create a new multimap m := slicemultimap.New() // empty // Put some contents to the multimap. m.Put(1, "x") // 1->x m.Put(2, "b") // 1->x, 2->b m.Put(1, "a") // 1->a, 1->x, 2->b // Check whether the multimap contains a certain value. found := m.ContainsValue("a") // true fmt.Printf("%v\n", found) found = m.ContainsValue("b") // true fmt.Printf("%v\n", found) found = m.ContainsValue("c") // true fmt.Printf("%v\n", found) }
Output: true true false
func (*MultiMap) Entries ¶
Entries view collection of all key-value pairs contained in this multimap. The return type is a slice of multimap.Entry instances. Retrieving the key and value from the entries result will be as trivial as:
- var entry = m.Entries()[0]
- var key = entry.Key
- var value = entry.Value
Example ¶
package main import ( "fmt" "sort" "strings" "github.com/jwangsadinata/go-multimap/slicemultimap" ) func main() { // Create a new multimap m := slicemultimap.New() // empty // Put some contents to the multimap. m.Put(1, "x") // 1->x m.Put(2, "b") // 1->x, 2->b m.Put(1, "a") // 1->a, 1->x, 2->b // Retrieve all the keys in the map. entries := m.Entries() // {1,a}, {1,x}, {2,b} (random order) // Workaround for test output consistency. tmp := make([]struct { Key int Value string }, len(entries)) count := 0 for _, e := range entries { tmp[count] = struct { Key int Value string }{e.Key.(int), e.Value.(string)} count++ } sort.Sort(byKeyThenValue(tmp)) fmt.Printf("%v\n", tmp) } // Helper for sorting entries. type byKeyThenValue []struct { Key int Value string } func (a byKeyThenValue) Len() int { return len(a) } func (a byKeyThenValue) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a byKeyThenValue) Less(i, j int) bool { if a[i].Key < a[j].Key { return true } else if a[i].Key > a[j].Key { return false } else { return strings.Compare(a[i].Value, a[j].Value) < 0 } }
Output: [{1 a} {1 x} {2 b}]
func (*MultiMap) Get ¶
Get searches the element in the multimap by key. It returns its value or nil if key is not found in multimap. Second return parameter is true if key was found, otherwise false.
Example ¶
package main import ( "fmt" "sort" "github.com/jwangsadinata/go-multimap/slicemultimap" ) func main() { // Create a new multimap m := slicemultimap.New() // empty // Put some contents to the multimap. m.Put(1, "x") // 1->x m.Put(2, "b") // 1->x, 2->b m.Put(1, "a") // 1->a, 1->x, 2->b // Retrieve values from the map. value, found := m.Get(3) // nil, false fmt.Printf("%v, %v\n", value, found) value, found = m.Get(2) // {b}, true fmt.Printf("%v, %v\n", value, found) value, found = m.Get(1) // {a, x}, true (random order) // Workaround for test output consistency: tmp := make([]string, len(value)) count := 0 for _, s := range value { tmp[count] = s.(string) count++ } sort.Strings(tmp) fmt.Printf("%v, %v\n", tmp, found) }
Output: [], false [b], true [a x], true
func (*MultiMap) KeySet ¶
func (m *MultiMap) KeySet() []interface{}
KeySet returns all distinct keys contained in this multimap.
Example ¶
package main import ( "fmt" "sort" "github.com/jwangsadinata/go-multimap/slicemultimap" ) func main() { // Create a new multimap m := slicemultimap.New() // empty // Put some contents to the multimap. m.Put(1, "x") // 1->x m.Put(2, "b") // 1->x, 2->b m.Put(1, "a") // 1->a, 1->x, 2->b // Retrieve all the distinct keys in the multimap. keys := m.KeySet() // 1, 2 (random order) // Workaround for test output consistency. tmp := make([]int, len(keys)) count := 0 for _, key := range keys { tmp[count] = key.(int) count++ } sort.Ints(tmp) fmt.Printf("%v\n", tmp) }
Output: [1 2]
func (*MultiMap) Keys ¶
func (m *MultiMap) Keys() []interface{}
Keys returns a view collection containing the key from each key-value pair in this multimap. This is done without collapsing duplicates.
Example ¶
package main import ( "fmt" "sort" "github.com/jwangsadinata/go-multimap/slicemultimap" ) func main() { // Create a new multimap m := slicemultimap.New() // empty // Put some contents to the multimap. m.Put(1, "x") // 1->x m.Put(2, "b") // 1->x, 2->b m.Put(1, "a") // 1->a, 1->x, 2->b // Retrieve all the keys in the multimap. keys := m.Keys() // 1, 1, 2 (random order) // Workaround for test output consistency. tmp := make([]int, len(keys)) count := 0 for _, key := range keys { tmp[count] = key.(int) count++ } sort.Ints(tmp) fmt.Printf("%v\n", tmp) }
Output: [1 1 2]
func (*MultiMap) Put ¶
func (m *MultiMap) Put(key interface{}, value interface{})
Put stores a key-value pair in this multimap.
Example ¶
package main import ( "fmt" "github.com/jwangsadinata/go-multimap/slicemultimap" ) func main() { // Create a new multimap m := slicemultimap.New() // empty // Put some contents to the multimap. m.Put(1, "x") // 1->x m.Put(2, "b") // 1->x, 2->b m.Put(1, "a") // 1->a, 1->x, 2->b // Verify that the map has the right size. fmt.Println(m.Size()) }
Output: 3
func (*MultiMap) PutAll ¶
func (m *MultiMap) PutAll(key interface{}, values []interface{})
PutAll stores a key-value pair in this multimap for each of the values, all using the same key key.
func (*MultiMap) Remove ¶
func (m *MultiMap) Remove(key interface{}, value interface{})
Remove removes a single key-value pair from this multimap, if such exists.
Example ¶
package main import ( "fmt" "github.com/jwangsadinata/go-multimap/slicemultimap" ) func main() { // Create a new multimap m := slicemultimap.New() // empty // Put some contents to the multimap. m.Put(1, "x") // 1->x m.Put(2, "b") // 1->x, 2->b m.Put(1, "a") // 1->a, 1->x, 2->b // Remove a key/value pair from the multimap. m.Remove(2, "b") // Verify that the map has less number of key/value pair. fmt.Println(m.Size()) // 2 // Also, verify that there are no more (2, "b") key/value pair. value, found := m.Get(2) fmt.Printf("%v, %v", value, found) // nil, false }
Output: 2 [], false
func (*MultiMap) RemoveAll ¶
func (m *MultiMap) RemoveAll(key interface{})
RemoveAll removes all values associated with the key from the multimap.
Example ¶
package main import ( "fmt" "github.com/jwangsadinata/go-multimap/slicemultimap" ) func main() { // Create a new multimap m := slicemultimap.New() // empty // Put some contents to the multimap. m.Put(1, "x") // 1->x m.Put(2, "b") // 1->x, 2->b m.Put(1, "a") // 1->a, 1->x, 2->b // Remove a key/value pair from the multimap. m.RemoveAll(1) // Verify that the map has less number of key/value pair. fmt.Println(m.Size()) // 1 // Also, verify that there are no more values with key 1. value, found := m.Get(1) fmt.Printf("%v, %v", value, found) // nil, false }
Output: 1 [], false
func (*MultiMap) Values ¶
func (m *MultiMap) Values() []interface{}
Values returns all values from each key-value pair contained in this multimap. This is done without collapsing duplicates. (size of Values() = MultiMap.Size()).
Example ¶
package main import ( "fmt" "sort" "github.com/jwangsadinata/go-multimap/slicemultimap" ) func main() { // Create a new multimap m := slicemultimap.New() // empty // Put some contents to the multimap. m.Put(1, "x") // 1->x m.Put(2, "b") // 1->x, 2->b m.Put(1, "a") // 1->a, 1->x, 2->b // Retrieve all the keys in the map. values := m.Values() // a, b, x (random order) // Workaround for test output consistency. tmp := make([]string, len(values)) count := 0 for _, value := range values { tmp[count] = value.(string) count++ } sort.Strings(tmp) fmt.Printf("%v\n", tmp) }
Output: [a b x]