setmultimap

package
v0.0.0-...-63ace11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2022 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package setmultimap implements a multimap backed by a set.

A setmultimap cannot hold duplicate key-value pairs. Adding a key-value pair that's already in the multimap has no effect.

Elements are unordered in the map.

Structure is not thread safe.

Index

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 New

func New() *MultiMap

New instantiates a new multimap.

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/maoge/paas-metasvr-go/pkg/utils/multimap/setmultimap"
)

func main() {
	// Create a new multimap
	m := setmultimap.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

func (m *MultiMap) Contains(key interface{}, value interface{}) bool

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/maoge/paas-metasvr-go/pkg/utils/multimap/setmultimap"
)

func main() {
	// Create a new multimap
	m := setmultimap.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

func (m *MultiMap) ContainsKey(key interface{}) (found bool)

ContainsKey returns true if this multimap contains at least one key-value pair with the key key.

Example
package main

import (
	"fmt"

	"github.com/maoge/paas-metasvr-go/pkg/utils/multimap/setmultimap"
)

func main() {
	// Create a new multimap
	m := setmultimap.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

func (m *MultiMap) ContainsValue(value interface{}) bool

ContainsValue returns true if this multimap contains at least one key-value pair with the value value.

Example
package main

import (
	"fmt"

	"github.com/maoge/paas-metasvr-go/pkg/utils/multimap/setmultimap"
)

func main() {
	// Create a new multimap
	m := setmultimap.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) Empty

func (m *MultiMap) Empty() bool

Empty returns true if multimap does not contain any key-value pairs.

func (*MultiMap) Entries

func (m *MultiMap) Entries() []multimap.Entry

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/maoge/paas-metasvr-go/pkg/utils/multimap/setmultimap"
)

func main() {
	// Create a new multimap
	m := setmultimap.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

func (m *MultiMap) Get(key interface{}) (values []interface{}, found bool)

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/maoge/paas-metasvr-go/pkg/utils/multimap/setmultimap"
)

func main() {
	// Create a new multimap
	m := setmultimap.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/maoge/paas-metasvr-go/pkg/utils/multimap/setmultimap"
)

func main() {
	// Create a new multimap
	m := setmultimap.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/maoge/paas-metasvr-go/pkg/utils/multimap/setmultimap"
)

func main() {
	// Create a new multimap
	m := setmultimap.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/maoge/paas-metasvr-go/pkg/utils/multimap/setmultimap"
)

func main() {
	// Create a new multimap
	m := setmultimap.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/maoge/paas-metasvr-go/pkg/utils/multimap/setmultimap"
)

func main() {
	// Create a new multimap
	m := setmultimap.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/maoge/paas-metasvr-go/pkg/utils/multimap/setmultimap"
)

func main() {
	// Create a new multimap
	m := setmultimap.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) Size

func (m *MultiMap) Size() int

Size returns number of key-value pairs in the multimap.

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/maoge/paas-metasvr-go/pkg/utils/multimap/setmultimap"
)

func main() {
	// Create a new multimap
	m := setmultimap.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]

type Set

type Set map[interface{}]struct{}

Set represents a set object

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL