Skip to content

prgsmall/ringmap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🔃 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 GoDoc

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.

About

An implementation of a ring map in golang

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages