ringmap

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2020 License: MIT Imports: 1 Imported by: 0

README

🔃 github.com/prgsmall/ringmap GoDoc Build Status

*RingMap is an implementation of an OrderedMap with a maximum capacity. When the maximum capacity is reached, adding an element to the RingMap will cause the Front element to be deleted to make room for the new element.

*RingMap wraps the OrderedMap data structure available here: https://github.com/elliotchance/orderedmap

Installation

go get -u github.com/prgsmall/ringmap

Basic Usage

*RingMap wraps the high performance OrderedMap that maintains amortized O(1) for Put, Set, Get, Delete and Len:

m := ringmap.NewRingMap()

m.Set("foo", "bar")
m.Set("qux", 1.23)
m.Set(123, true)

m.Delete("qux")

m.Put("zzz", "yyy") // Deletes if the key exists, then calls Set

Iterating

Be careful using Keys() as it will create a copy of all of the keys so it's only suitable for a small number of items:

for _, key := range m.Keys() {
	value, _:= m.Get(key)
	fmt.Println(key, value)
}

For larger maps you should use Front() or Back() to iterate per element:

// Iterate through all elements from oldest to newest:
for el := m.Front(); el != nil; el = el.Next() {
    fmt.Println(el.Key, el.Value)
}

// You can also use Back and Prev to iterate in reverse:
for el := m.Back(); el != nil; el = el.Prev() {
    fmt.Println(el.Key, el.Value)
}

The iterator is safe to use bidirectionally, and will return nil once it goes beyond the first or last item.

If the map is changing while the iteration is in-flight it may produce unexpected behavior.

Documentation

Index

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

func NewRingMap(capacity int) *RingMap

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)
	}
}

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) Capacity

func (m *RingMap) Capacity() int

Capacity returns the capacity of the map

func (*RingMap) Delete

func (m *RingMap) Delete(key interface{}) (didDelete bool)

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)
	}
}

func (*RingMap) Get

func (m *RingMap) Get(key interface{}) (interface{}, bool)

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) IsFull

func (m *RingMap) IsFull() bool

IsFull returns true if the number of elements in the map is Capacity()

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) Len

func (m *RingMap) Len() int

Len returns the number of elements in the map.

func (*RingMap) Put

func (m *RingMap) Put(key, value interface{}) bool

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

func (m *RingMap) Set(key, value interface{}) bool

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.

Jump to

Keyboard shortcuts

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