Skip to content

Repository files navigation

OrderedMap Build StatusGoDocGo Report Card

OrderedMap is a Go implentation of Python's OrderedDict class, a map that preserves the order of insertion, so key:value pairs can be iterated in the order they where added. It can also be used as a stack (LIFO) or queue (FIFO).

Installing

Install the package from command line with the following command

go get -u github.com/secnot/orderedmap

And then import in your source file

import"github.com/secnot/orderedmap"

Usage

Basic operations

package main
import (
"github.com/secnot/orderedmap""fmt"
)
funcmain() {
// Createom:=orderedmap.NewOrderedMap()
// Insertom.Set("John Smith", 44)
om.Set("Laura Paro", 39)
om.Set("Alison Rogers", 52)
// Updateom.Set("Alison Rogers", 51)
// Get ifage, ok:=om.Get("John Smith"); ok {
fmt.Printf("John Smith age: %v", age)
}
// Get the last key addedifname, age, ok:=om.GetLast() {
fmt.Printf("%v age: %v\n", name, age)
}
// Deleteom.Delete("John Smith")
}

Pop the last key from the map

key, value, ok:=om.PopLast()
// > Alison Rogers, 52, true

Iterate over the Map elements in insertion order

package main
import (
"github.com/secnot/orderedmap""fmt"
)
funcmain() {
om:=orderedmap.NewOrderedMap()
om.Set("John Smith", 44)
om.Set("Laura Paro", 39)
om.Set("Alison Rogers", 52)
// Iterateiter:=om.Iter()
forkey, value, ok:=iter.Next(); ok; key, value, ok=iter.Next() {
fmt.Printf("%v: %v", key, value)
}
// > John Smith: 44// > Laura Paro: 39// > Alison Rogers: 52
}

While iterating over an OrderedMap only three methods can be called safely, Get, Set, and Delete, with some limitations/gotchas for the last two.

Set can update the value of any existing key without problems, but new keys are inserted at the end of the Map so they will be also iterated over, this can cause bugs on innocent-looking code:

package main
import (
"github.com/secnot/orderedmap""fmt"
)
funcmain() {
om:=orderedmap.NewOrderedMap()
om.Set("John Smith", 44)
om.Set("Laura Paro", 39)
// This is an infinite loopiter:=om.Iter()
forname, age, ok:=iter.Next(); ok; name, age, ok=iter.Next() {
om.Set("Dr. "+name, age)
fmt.Printf("%v\n", name)
}
// Prints// > John Smith// > Laura Paro// > Dr. John Smith// > Dr. Laura Paro// > Dr. Dr. John Smith// > Dr. Dr. Laura Paro// .....// Iterating in reverse order avoids this problemiter=om.IterReverse()
forname, age, ok:=iter.Next(); ok; name, age, ok=iter.Next() {
om.Set("Dr. "+name, age)
} }

Delete is more restrictive and can only be used to delete the key being iterated over at that moment.

package main
import"github.com/secnot/orderedmap"funcmain() {
om:=orderedmap.NewOrderedMap()
om.Set("first", 1)
om.Set("second", 2)
om.Set("third", 3)
// This is safeiter:=om.Iter()
fork, _, ok:=iter.Next(); ok; k, _, ok=iter.Next() {
om.Delete(k)
}
// This is NOTiter=om.Iter()
for_, _, ok:=iter.Next(); ok; _, _, ok=iter.Next() {
om.Delete("another key")
}
} 

Lastly an OrderedMap can also be handled as a queue or a stack with:

  • GetLast | GetFirst : key:value for both ends of the queue or stack, without modifying the map
  • PopLast: Pop the value at the top of the Stack.
  • PopFirst: Pop next queue element
  • MoveLast | MoveFirst: Move elements to either end of the queue or stack

Documentation

TYPE

typeOrderedMapstruct {
// contains filtered or unexported fields
}

func NewOrderedMap

funcNewOrderedMap() *OrderedMap

Create an empty OrderedMap

func (*OrderedMap) Delete

func (*OrderedMap) Delete(keyinterface{})

Delete a key:value pair from the map.

func (*OrderedMap) Get

func (om*OrderedMap) Get(keyinterface{}) (valueinterface{}, okbool)

Get the value of an existing key, leaving the map unchanged

func (om *OrderedMap) GetFirst

func (om*OrderedMap) GetFirst() (keyinterface{}, valueinterface{}, okbool)

Get the key value for the beginning element, leaving the map unchanged

func (om *OrderedMap) GetLast

func (om*OrderedMap) GetLast() (keyinterface{}, valueinterface{}, okbool)

Get the key and value for the last element added, leaving the map unchanged

func (om *OrderedMap) Iter

func (om*OrderedMap) Iter() *MapIterator

Create a map iterator

func (om *OrderedMap) IterReverse

func (om*OrderedMap) IterReverse() *MapIterator

Create a reverse order map iterator

func (om *OrderedMap) Len

func (om*OrderedMap) Len() int

Return the number of elements in an OrderedMap

func (om *OrderedMap) Move

func (om*OrderedMap) Move(keyinterface{}, lastbool) (okbool)

Move an existing key to either the end of the OrderedMap

func (om *OrderedMap) MoveFirst

func (om*OrderedMap) MoveFirst(keyinterface{}) (okbool)

Shortcut to Move a key to the beginning of the map

func (om *OrderedMap) MoveLast

func (om*OrderedMap) MoveLast(keyinterface{}) (okbool)

Shortcut to Move a key to the end of the map

func (om *OrderedMap) Pop

func (om*OrderedMap) Pop(lastbool) (keyinterface{}, valueinterface{}, okbool)

Pop and return key:value for the newest or oldest element on the OrderedMap.

func (om *OrderedMap) PopFirst

func (om*OrderedMap) PopFirst() (keyinterface{}, valueinterface{}, okbool)

Shortcut to Pop the first element

func (om *OrderedMap) PopLast

func (om*OrderedMap) PopLast() (keyinterface{}, valueinterface{}, okbool)

Shortcut to Pop the last element

func (om *OrderedMap) Set

func (om*OrderedMap) Set(keyinterface{}, valueinterface{})

Sets the key value, if the key exists it overwrites the existing entry, and the original insertion position is left unchanged, otherwise the key is inserted at the end.

func (om *OrderedMap) String

func (om*OrderedMap) String() string

Stringer interface

Type

typeMapIteratorstruct {
// contains filtered or unexported fields
}

func (mi *MapIterator) Next

func (mi*MapIterator) Next() (keyinterface{}, valueinterface{}, okbool)

Return iterators next key:value pair until the map is exhausted

About

OrderedMap is a Go implentation of a map that preserves the order of insertion, so key/value pais can be iterated in the order they where added

Topics

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages