Documentation ¶
Overview ¶
Package orderedmap provides an implementation of ordered map.
An ordered map guarantees that the iteration order preserves the original insertion order. If an existing key is updated later, that doesn't change its iteration order. But if a key was deleted then later inserted again, the iteration order reflects its last insertion order.
Example ¶
package main import ( "fmt" "go.yhsif.com/orderedmap" ) func main() { var m orderedmap.Map fmt.Println(m.Load("key1")) fmt.Println(m.LoadOrStore("key1", "value1")) fmt.Println(m.LoadOrStore("key1", "value2")) m.Store("key2", "value2") fmt.Println(m.Load("key2")) m.Store("key1", "new value1") fmt.Println(m.Load("key1")) fmt.Println("Range1:") m.Range(func(k, v interface{}) bool { fmt.Println(k, v) return true }) m.Delete("key1") m.Store("key1", "value1") fmt.Println("Range2:") m.Range(func(k, v interface{}) bool { fmt.Println(k, v) return true }) }
Output: <nil> false value1 false value1 true value2 true new value1 true Range1: key1 new value1 key2 value2 Range2: key2 value2 key1 value1
Index ¶
- type Interface
- type Map
- func (m *Map) Delete(key interface{})
- func (m *Map) Load(key interface{}) (value interface{}, ok bool)
- func (m *Map) LoadAndDelete(key interface{}) (value interface{}, loaded bool)
- func (m *Map) LoadOrStore(key, value interface{}) (actual interface{}, loaded bool)
- func (m *Map) Range(f func(key, value interface{}) bool)
- func (m *Map) Store(key, value interface{})
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Interface ¶
type Interface interface { Delete(key interface{}) Load(key interface{}) (value interface{}, ok bool) LoadAndDelete(key interface{}) (value interface{}, loaded bool) LoadOrStore(key, value interface{}) (actual interface{}, loaded bool) Range(f func(key, value interface{}) bool) Store(key, value interface{}) }
Interface defines the common interface between orderedmap.Map and sync.Map.
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map represents an ordered map.
An ordered map preserves the inserting order when iterating.
Underlying it's wrapping a sync.Map with a doubly linked list.
The interface is intentionally kept the same with sync.Map to be used interchangeably.
The zero value is an empty map ready to use. A map must not be copied after first use.
func (*Map) Delete ¶
func (m *Map) Delete(key interface{})
Delete deletes key from the map.
key must be hashable.
func (*Map) Load ¶
Load loads key from the map.
key must be hashable.
The ok result indicates whether the value is found in the map.
func (*Map) LoadAndDelete ¶ added in v0.2.0
LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.
func (*Map) LoadOrStore ¶
LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.
key must be hashable.