Documentation ¶
Overview ¶
Package orderedmap implements an ordered map, i.e. a map that also keeps track of the order in which keys were inserted.
All operations are constant-time.
Github repo: https://github.com/KusakabeSi/go-ordered-map
Example ¶
om := orderedmap.New() om.Set("foo", "bar") om.Set("bar", "baz") om.Set("coucou", "toi") fmt.Println("## Get operations: ##") fmt.Println(om.Get("foo")) fmt.Println(om.Get("i dont exist")) fmt.Println("## Iterating over pairs from oldest to newest: ##") for pair := om.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%s => %s\n", pair.Key, pair.Value) } fmt.Println("## Iterating over the 2 newest pairs: ##") i := 0 for pair := om.Newest(); pair != nil; pair = pair.Prev() { fmt.Printf("%s => %s\n", pair.Key, pair.Value) i++ if i >= 2 { break } }
Output: ## Get operations: ## bar true <nil> false ## Iterating over pairs from oldest to newest: ## foo => bar bar => baz coucou => toi ## Iterating over the 2 newest pairs: ## coucou => toi bar => baz
Index ¶
- type OrderedMap
- func (om *OrderedMap) Delete(key interface{}) (interface{}, bool)
- func (om *OrderedMap) Get(key interface{}) (interface{}, bool)
- func (om *OrderedMap) GetPair(key interface{}) *Pair
- func (om *OrderedMap) Len() int
- func (om *OrderedMap) Load(key interface{}) (interface{}, bool)
- func (om *OrderedMap) MoveAfter(key interface{}, mark_key interface{}) error
- func (om *OrderedMap) MoveBefore(key interface{}, mark_key interface{}) error
- func (om *OrderedMap) MoveToBack(key interface{}) error
- func (om *OrderedMap) MoveToFront(key interface{}) error
- func (om *OrderedMap) Newest() *Pair
- func (om *OrderedMap) Oldest() *Pair
- func (om *OrderedMap) Set(key interface{}, value interface{}) (interface{}, bool)
- func (om *OrderedMap) Store(key interface{}, value interface{}) (interface{}, bool)
- type Pair
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type OrderedMap ¶
type OrderedMap struct {
// contains filtered or unexported fields
}
func (*OrderedMap) Delete ¶
func (om *OrderedMap) Delete(key interface{}) (interface{}, bool)
Delete removes the key-value pair, and returns what `Get` would have returned on that key prior to the call to `Delete`.
func (*OrderedMap) Get ¶
func (om *OrderedMap) Get(key interface{}) (interface{}, bool)
Get looks for the given key, and returns the value associated with it, or nil if not found. The boolean it returns says whether the key is present in the map.
func (*OrderedMap) GetPair ¶ added in v0.2.1
func (om *OrderedMap) GetPair(key interface{}) *Pair
GetPair looks for the given key, and returns the pair associated with it, or nil if not found. The Pair struct can then be used to iterate over the ordered map from that point, either forward or backward.
func (*OrderedMap) Load ¶ added in v0.2.1
func (om *OrderedMap) Load(key interface{}) (interface{}, bool)
func (*OrderedMap) MoveAfter ¶ added in v0.2.1
func (om *OrderedMap) MoveAfter(key interface{}, mark_key interface{}) error
func (*OrderedMap) MoveBefore ¶ added in v0.2.1
func (om *OrderedMap) MoveBefore(key interface{}, mark_key interface{}) error
func (*OrderedMap) MoveToBack ¶ added in v0.2.1
func (om *OrderedMap) MoveToBack(key interface{}) error
func (*OrderedMap) MoveToFront ¶ added in v0.2.1
func (om *OrderedMap) MoveToFront(key interface{}) error
func (*OrderedMap) Newest ¶
func (om *OrderedMap) Newest() *Pair
Newest returns a pointer to the newest pair. It's meant to be used to iterate on the ordered map's pairs from the newest to the oldest, e.g.: for pair := orderedMap.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%v => %v\n", pair.Key, pair.Value) }
func (*OrderedMap) Oldest ¶
func (om *OrderedMap) Oldest() *Pair
Oldest returns a pointer to the oldest pair. It's meant to be used to iterate on the ordered map's pairs from the oldest to the newest, e.g.: for pair := orderedMap.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%v => %v\n", pair.Key, pair.Value) }
func (*OrderedMap) Set ¶
func (om *OrderedMap) Set(key interface{}, value interface{}) (interface{}, bool)
Set sets the key-value pair, and returns what `Get` would have returned on that key prior to the call to `Set`.
func (*OrderedMap) Store ¶ added in v0.2.1
func (om *OrderedMap) Store(key interface{}, value interface{}) (interface{}, bool)