Documentation
¶
Index ¶
- type RingMap
- func (m *RingMap) Back() *orderedmap.Element
- func (m *RingMap) Capacity() int
- func (m *RingMap) Delete(key interface{}) (didDelete bool)
- func (m *RingMap) Front() *orderedmap.Element
- func (m *RingMap) Get(key interface{}) (interface{}, bool)
- func (m *RingMap) GetOrDefault(key, defaultValue interface{}) interface{}
- func (m *RingMap) IsFull() bool
- func (m *RingMap) Keys() (keys []interface{})
- func (m *RingMap) Len() int
- func (m *RingMap) Put(key, value interface{}) bool
- func (m *RingMap) Set(key, value interface{}) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type RingMap ¶
type RingMap struct {
// contains filtered or unexported fields
}
RingMap the ordered map data structure
func NewRingMap ¶
NewRingMap creates a new ordered map with a maximum size
Example ¶
package main
import (
"fmt"
"github.com/prgsmall/ringmap"
)
var ringMapCapacity = 777
func main() {
m := ringmap.NewRingMap(ringMapCapacity)
m.Set("foo", "bar")
m.Set("qux", 1.23)
m.Set(123, true)
m.Delete("qux")
for _, key := range m.Keys() {
value, _ := m.Get(key)
fmt.Println(key, value)
}
}
Output:
func (*RingMap) Back ¶
func (m *RingMap) Back() *orderedmap.Element
Back will return the element that is the last (most recent Set element). If there are no elements this will return nil.
func (*RingMap) Delete ¶
Delete will remove a key from the map. It will return true if the key was removed (the key did exist).
func (*RingMap) Front ¶
func (m *RingMap) Front() *orderedmap.Element
Front will return the element that is the first (oldest Set element). If there are no elements this will return nil.
Example ¶
package main
import (
"fmt"
"github.com/prgsmall/ringmap"
)
var ringMapCapacity = 777
func main() {
m := ringmap.NewRingMap(ringMapCapacity)
m.Set(1, true)
m.Set(2, true)
for el := m.Front(); el != nil; el = el.Next() {
fmt.Println(el)
}
}
Output:
func (*RingMap) Get ¶
Get returns the value for a key. If the key does not exist, the second return parameter will be false and the value will be nil.
func (*RingMap) GetOrDefault ¶
func (m *RingMap) GetOrDefault(key, defaultValue interface{}) interface{}
GetOrDefault returns the value for a key. If the key does not exist, returns the default value instead.
func (*RingMap) Keys ¶
func (m *RingMap) Keys() (keys []interface{})
Keys returns all of the keys in the order they were inserted. If a key was replaced it will retain the same position. To ensure most recently set keys are always at the end you must always Delete before Set.
func (*RingMap) Put ¶
Put will set a value for a key. If the key already exists, it will be deleted from and a recreated at the end of the list. If the key was new, then true will be returned. The returned value will be false if the value was replaced (even if the value was the same). If a new key is being added and the map is full, then the front element will be deleted to make room for the new element.
func (*RingMap) Set ¶
Set will set (or replace) a value for a key. If the key was new, then true will be returned. The returned value will be false if the value was replaced (even if the value was the same). If a new key is being added and the map is full, then the front element will be deleted to make room for the new element.