Documentation ¶
Overview ¶
omultimap package provides common interface to implement ordered multimap, along with an small set of implementations.
An omultimap is very similar to a omap, it also keeps the map in the insertion order when iterating/marshaling, but a given key can hold many values.
Example ¶
package main import ( "encoding/json" "fmt" "github.com/matheusoliveira/go-ordered-map/omap" "github.com/matheusoliveira/go-ordered-map/omultimap" ) func main() { mm := omultimap.New[string, int]() mm.Put("foo", 1) mm.Put("bar", 2) mm.Put("baz", 3) mm.Put("foo", 4) mm.Put("bar", 5) mm.Put("baz", 6) mm.Put("foo", 7) mm.Put("bar", 8) mm.Put("baz", 9) fmt.Println("iterate all keys/values:") for it := mm.Iterator(); it.Next(); { fmt.Printf(" %q / %d\n", it.Key(), it.Value()) } // iterate values of a given key fmt.Printf("values of foo: ") fooIt := mm.GetValuesOf("foo") fmt.Println(omap.IteratorValuesToSlice(fooIt)) // marshal JSON fmt.Printf("marshal output:\n ") js, _ := json.Marshal(mm) fmt.Println(string(js)) // unmarshal JSON fmt.Printf("unmarshal output:\n ") mm2 := omultimap.New[string, int]() err := json.Unmarshal(js, mm2) fmt.Println(mm2, err) }
Output: iterate all keys/values: "foo" / 1 "bar" / 2 "baz" / 3 "foo" / 4 "bar" / 5 "baz" / 6 "foo" / 7 "bar" / 8 "baz" / 9 values of foo: [1 4 7] marshal output: {"foo":1,"bar":2,"baz":3,"foo":4,"bar":5,"baz":6,"foo":7,"bar":8,"baz":9} unmarshal output: omultimap.OMultiMapLinked[foo:1 bar:2 baz:3 foo:4 bar:5 baz:6 foo:7 bar:8 baz:9] <nil>
Index ¶
- type OMultiMap
- type OMultiMapLinked
- func (m *OMultiMapLinked[K, V]) DeleteAll(key K)
- func (m *OMultiMapLinked[K, V]) DeleteAt(interfaceIt omap.OMapIterator[K, V]) error
- func (m *OMultiMapLinked[K, V]) GetValuesOf(key K) omap.OMapIterator[K, V]
- func (m *OMultiMapLinked[K, V]) Iterator() omap.OMapIterator[K, V]
- func (m *OMultiMapLinked[K, V]) Len() int
- func (m OMultiMapLinked[K, V]) MarshalJSON() ([]byte, error)
- func (m *OMultiMapLinked[K, V]) MustDeleteAt(interfaceIt omap.OMapIterator[K, V])
- func (m *OMultiMapLinked[K, V]) Put(key K, values ...V)
- func (m *OMultiMapLinked[K, V]) PutAfter(interfaceIt omap.OMapIterator[K, V], key K, value V) error
- func (m *OMultiMapLinked[K, V]) String() string
- func (m *OMultiMapLinked[K, V]) UnmarshalJSON(b []byte) error
- type OMultiMapLinkedIterator
- func (it *OMultiMapLinkedIterator[K, V]) EOF() bool
- func (it *OMultiMapLinkedIterator[K, V]) IsValid() bool
- func (it *OMultiMapLinkedIterator[K, V]) Key() K
- func (it *OMultiMapLinkedIterator[K, V]) MoveBack() omap.OMapIterator[K, V]
- func (it *OMultiMapLinkedIterator[K, V]) MoveFront() omap.OMapIterator[K, V]
- func (it *OMultiMapLinkedIterator[K, V]) Next() bool
- func (it *OMultiMapLinkedIterator[K, V]) Prev() bool
- func (it *OMultiMapLinkedIterator[K, V]) Value() V
- type OMultiMapLinkedValuesIterator
- func (it *OMultiMapLinkedValuesIterator[K, V]) EOF() bool
- func (it OMultiMapLinkedValuesIterator[K, V]) IsValid() bool
- func (it *OMultiMapLinkedValuesIterator[K, V]) Key() K
- func (it *OMultiMapLinkedValuesIterator[K, V]) MoveBack() omap.OMapIterator[K, V]
- func (it *OMultiMapLinkedValuesIterator[K, V]) MoveFront() omap.OMapIterator[K, V]
- func (it *OMultiMapLinkedValuesIterator[K, V]) Next() bool
- func (it *OMultiMapLinkedValuesIterator[K, V]) Prev() bool
- func (it *OMultiMapLinkedValuesIterator[K, V]) Value() V
- type OMultiMapSync
- func (m *OMultiMapSync[K, V]) DeleteAll(key K)
- func (m *OMultiMapSync[K, V]) DeleteAt(interfaceIt omap.OMapIterator[K, V]) error
- func (m *OMultiMapSync[K, V]) GetValuesOf(key K) omap.OMapIterator[K, V]
- func (m *OMultiMapSync[K, V]) Iterator() omap.OMapIterator[K, V]
- func (m *OMultiMapSync[K, V]) Len() int
- func (m *OMultiMapSync[K, V]) MarshalJSON() ([]byte, error)
- func (m *OMultiMapSync[K, V]) MustDeleteAt(interfaceIt omap.OMapIterator[K, V])
- func (m *OMultiMapSync[K, V]) Put(key K, values ...V)
- func (m *OMultiMapSync[K, V]) PutAfter(interfaceIt omap.OMapIterator[K, V], key K, value V) error
- func (m *OMultiMapSync[K, V]) String() string
- func (m *OMultiMapSync[K, V]) UnmarshalJSON(b []byte) error
- type OMultiMapSyncIterator
- func (it *OMultiMapSyncIterator[K, V]) EOF() bool
- func (it *OMultiMapSyncIterator[K, V]) IsValid() bool
- func (it *OMultiMapSyncIterator[K, V]) Key() K
- func (it *OMultiMapSyncIterator[K, V]) MoveBack() omap.OMapIterator[K, V]
- func (it *OMultiMapSyncIterator[K, V]) MoveFront() omap.OMapIterator[K, V]
- func (it *OMultiMapSyncIterator[K, V]) Next() bool
- func (it *OMultiMapSyncIterator[K, V]) Prev() bool
- func (it *OMultiMapSyncIterator[K, V]) Value() V
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type OMultiMap ¶
type OMultiMap[K comparable, V any] interface { // Add a given key/value to the map. Put(key K, values ...V) // Add a given key/value to the map, after the entry pointed by it. PutAfter(it omap.OMapIterator[K, V], key K, value V) error // Get an iterator over all values of a given key. GetValuesOf(key K) omap.OMapIterator[K, V] // Delete all values stored by a giving key. DeleteAll(key K) // Delete the value currently pointed by the iterator, returning a non-nil error if failed. DeleteAt(omap.OMapIterator[K, V]) error // Same as DeleteAt but with panic in case of failure. MustDeleteAt(omap.OMapIterator[K, V]) // Return an iterator at the beginning of the map. Iterator() omap.OMapIterator[K, V] // Returns the len of the map, similar to builtin len(map). Len() int }
func New ¶
func New[K comparable, V any]() OMultiMap[K, V]
Create a new OMultiMap using default implementation, currently a OMultiMapLinked.
func NewOMultiMapLinked ¶
func NewOMultiMapLinked[K comparable, V any]() OMultiMap[K, V]
Create a new OMultiMapLinked.
func NewOMultiMapSync ¶
func NewOMultiMapSync[K comparable, V any]() OMultiMap[K, V]
type OMultiMapLinked ¶
type OMultiMapLinked[K comparable, V any] struct { // contains filtered or unexported fields }
OMultiMapLinked implements an OMultiMap using a linked list to navigate through the key/values in same order as originally inserted.
func (*OMultiMapLinked[K, V]) DeleteAll ¶
func (m *OMultiMapLinked[K, V]) DeleteAll(key K)
Delete all values stored by a giving key. Complexity: O(m) where m is the number of values pointing to the given key.
func (*OMultiMapLinked[K, V]) DeleteAt ¶
func (m *OMultiMapLinked[K, V]) DeleteAt(interfaceIt omap.OMapIterator[K, V]) error
Delete the value currently pointed by the iterator, returning a non-nil error if failed. Complexity: O(1).
func (*OMultiMapLinked[K, V]) GetValuesOf ¶
func (m *OMultiMapLinked[K, V]) GetValuesOf(key K) omap.OMapIterator[K, V]
Get an iterator over all values of a given key. Complexity: O(1).
func (*OMultiMapLinked[K, V]) Iterator ¶
func (m *OMultiMapLinked[K, V]) Iterator() omap.OMapIterator[K, V]
Return an iterator at the beginning of the map.
func (*OMultiMapLinked[K, V]) Len ¶
func (m *OMultiMapLinked[K, V]) Len() int
Returns the length of the map. Complexity: O(1).
func (OMultiMapLinked[K, V]) MarshalJSON ¶
func (m OMultiMapLinked[K, V]) MarshalJSON() ([]byte, error)
Implement json.Marshaler interface.
func (*OMultiMapLinked[K, V]) MustDeleteAt ¶
func (m *OMultiMapLinked[K, V]) MustDeleteAt(interfaceIt omap.OMapIterator[K, V])
Same as DeleteAt but with panic in case of failure. Complexity: O(1).
func (*OMultiMapLinked[K, V]) Put ¶
func (m *OMultiMapLinked[K, V]) Put(key K, values ...V)
Add a given key/value to the map. Complexity: O(1), for each value in values slice.
func (*OMultiMapLinked[K, V]) PutAfter ¶
func (m *OMultiMapLinked[K, V]) PutAfter(interfaceIt omap.OMapIterator[K, V], key K, value V) error
Add a given key/value to the map, after the entry pointed by iterator. Complexity: O(1).
func (*OMultiMapLinked[K, V]) String ¶
func (m *OMultiMapLinked[K, V]) String() string
Implement fmt.Stringer
func (*OMultiMapLinked[K, V]) UnmarshalJSON ¶
func (m *OMultiMapLinked[K, V]) UnmarshalJSON(b []byte) error
Implement json.Unmarshaler interface.
type OMultiMapLinkedIterator ¶
type OMultiMapLinkedIterator[K comparable, V any] struct { // contains filtered or unexported fields }
Iterator for OMultiMapLinked.
func (*OMultiMapLinkedIterator[K, V]) EOF ¶
func (it *OMultiMapLinkedIterator[K, V]) EOF() bool
func (*OMultiMapLinkedIterator[K, V]) IsValid ¶
func (it *OMultiMapLinkedIterator[K, V]) IsValid() bool
func (*OMultiMapLinkedIterator[K, V]) Key ¶
func (it *OMultiMapLinkedIterator[K, V]) Key() K
func (*OMultiMapLinkedIterator[K, V]) MoveBack ¶
func (it *OMultiMapLinkedIterator[K, V]) MoveBack() omap.OMapIterator[K, V]
func (*OMultiMapLinkedIterator[K, V]) MoveFront ¶
func (it *OMultiMapLinkedIterator[K, V]) MoveFront() omap.OMapIterator[K, V]
func (*OMultiMapLinkedIterator[K, V]) Next ¶
func (it *OMultiMapLinkedIterator[K, V]) Next() bool
func (*OMultiMapLinkedIterator[K, V]) Prev ¶
func (it *OMultiMapLinkedIterator[K, V]) Prev() bool
func (*OMultiMapLinkedIterator[K, V]) Value ¶
func (it *OMultiMapLinkedIterator[K, V]) Value() V
type OMultiMapLinkedValuesIterator ¶
type OMultiMapLinkedValuesIterator[K comparable, V any] struct { // contains filtered or unexported fields }
Values iterator for OMultiMapLinked.
func (*OMultiMapLinkedValuesIterator[K, V]) EOF ¶
func (it *OMultiMapLinkedValuesIterator[K, V]) EOF() bool
func (OMultiMapLinkedValuesIterator[K, V]) IsValid ¶
func (it OMultiMapLinkedValuesIterator[K, V]) IsValid() bool
func (*OMultiMapLinkedValuesIterator[K, V]) Key ¶
func (it *OMultiMapLinkedValuesIterator[K, V]) Key() K
func (*OMultiMapLinkedValuesIterator[K, V]) MoveBack ¶
func (it *OMultiMapLinkedValuesIterator[K, V]) MoveBack() omap.OMapIterator[K, V]
func (*OMultiMapLinkedValuesIterator[K, V]) MoveFront ¶
func (it *OMultiMapLinkedValuesIterator[K, V]) MoveFront() omap.OMapIterator[K, V]
func (*OMultiMapLinkedValuesIterator[K, V]) Next ¶
func (it *OMultiMapLinkedValuesIterator[K, V]) Next() bool
func (*OMultiMapLinkedValuesIterator[K, V]) Prev ¶
func (it *OMultiMapLinkedValuesIterator[K, V]) Prev() bool
func (*OMultiMapLinkedValuesIterator[K, V]) Value ¶
func (it *OMultiMapLinkedValuesIterator[K, V]) Value() V
type OMultiMapSync ¶
type OMultiMapSync[K comparable, V any] struct { // contains filtered or unexported fields }
Implements an OMultiMap with synchronization to be safely called from goroutines without worrying about synchronization.
Uses an OMultiMapLinked underneath, and behavior of functions and time complexity are the same. Uses a sync.RWMutex internally to make sure that reads can be run in parallel, while any write operation will block other operations.
func (*OMultiMapSync[K, V]) DeleteAll ¶
func (m *OMultiMapSync[K, V]) DeleteAll(key K)
func (*OMultiMapSync[K, V]) DeleteAt ¶
func (m *OMultiMapSync[K, V]) DeleteAt(interfaceIt omap.OMapIterator[K, V]) error
func (*OMultiMapSync[K, V]) GetValuesOf ¶
func (m *OMultiMapSync[K, V]) GetValuesOf(key K) omap.OMapIterator[K, V]
func (*OMultiMapSync[K, V]) Iterator ¶
func (m *OMultiMapSync[K, V]) Iterator() omap.OMapIterator[K, V]
func (*OMultiMapSync[K, V]) Len ¶
func (m *OMultiMapSync[K, V]) Len() int
func (*OMultiMapSync[K, V]) MarshalJSON ¶
func (m *OMultiMapSync[K, V]) MarshalJSON() ([]byte, error)
Implement json.Marshaler interface.
func (*OMultiMapSync[K, V]) MustDeleteAt ¶
func (m *OMultiMapSync[K, V]) MustDeleteAt(interfaceIt omap.OMapIterator[K, V])
func (*OMultiMapSync[K, V]) Put ¶
func (m *OMultiMapSync[K, V]) Put(key K, values ...V)
func (*OMultiMapSync[K, V]) PutAfter ¶
func (m *OMultiMapSync[K, V]) PutAfter(interfaceIt omap.OMapIterator[K, V], key K, value V) error
func (*OMultiMapSync[K, V]) String ¶
func (m *OMultiMapSync[K, V]) String() string
Implement fmt.Stringer
func (*OMultiMapSync[K, V]) UnmarshalJSON ¶
func (m *OMultiMapSync[K, V]) UnmarshalJSON(b []byte) error
Implement json.Unmarshaler interface.
type OMultiMapSyncIterator ¶
type OMultiMapSyncIterator[K comparable, V any] struct { // contains filtered or unexported fields }
func (*OMultiMapSyncIterator[K, V]) EOF ¶
func (it *OMultiMapSyncIterator[K, V]) EOF() bool
func (*OMultiMapSyncIterator[K, V]) IsValid ¶
func (it *OMultiMapSyncIterator[K, V]) IsValid() bool
func (*OMultiMapSyncIterator[K, V]) Key ¶
func (it *OMultiMapSyncIterator[K, V]) Key() K
func (*OMultiMapSyncIterator[K, V]) MoveBack ¶
func (it *OMultiMapSyncIterator[K, V]) MoveBack() omap.OMapIterator[K, V]
func (*OMultiMapSyncIterator[K, V]) MoveFront ¶
func (it *OMultiMapSyncIterator[K, V]) MoveFront() omap.OMapIterator[K, V]
func (*OMultiMapSyncIterator[K, V]) Next ¶
func (it *OMultiMapSyncIterator[K, V]) Next() bool
func (*OMultiMapSyncIterator[K, V]) Prev ¶
func (it *OMultiMapSyncIterator[K, V]) Prev() bool
func (*OMultiMapSyncIterator[K, V]) Value ¶
func (it *OMultiMapSyncIterator[K, V]) Value() V