orderedmap

package module
v0.0.0-...-a49937a Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2019 License: MIT Imports: 1 Imported by: 0

README

Ordered Map

This package implements an ordered map, using container/list and build-in map, it remembers the original insertion order of the keys, and primarily used in the situation of needing to pay attention to insertion order, for example first in first out.

All operations have O(1) time complexity.

Example

// Create a new map.
m := orderedmap.New()

// Sets item within map, sets 1 under key "a".
m.Set("a", 1)

// Retrieves item from map.
if elem, exist := m.Get("a"); exist {
  val := elem.(int)
  fmt.Println(val)
}

m.Set("b", 2)
m.Set("c", 3)

// Iterates on the map.
for e := m.First; e != nil; e = e.Next() {
  key := item.Key
  val := item.Value.(int)
  fmt.Printf("key: %v, value: %d\n", key, val)
}

// Deletes item under key "a".
m.Delete("a")

License

MIT (see LICENSE file)

Documentation

Overview

Package orderedmap implements an ordered map, using container/list and build-in map, it remembers the original insertion order of the keys.

All operations have O(1) time complexity.

To iterate over an ordered map (where m is a *OrderedMap):

for e:= m.Front; e != nil; e = e.Next() {
	key := e.Key
	value:= e.Value
	// do something with e.Value
}

If you want to delete element while iterating, MUST use the following pattern:

 var next *orderedmap.Element
	for e := m.First(); e != nil; e = next {
		key := e.Key
		// assign e.Next() to the next before deleting e
 	next = e.Next()
		m.Delete(key)
	}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ElemVal

type ElemVal struct {
	Key   string
	Value interface{}
}

ElemVal encapsulates the key-value pair which stores in the `Value` field of list element.

type Element

type Element struct {
	*ElemVal
	// contains filtered or unexported fields
}

Element encapsulates the underlying list element and the map's key-value pair, to provide a `Next` method for iterating.

func (*Element) Next

func (e *Element) Next() *Element

Next returns the next ordered map element or nil.

type OrderedMap

type OrderedMap struct {
	// contains filtered or unexported fields
}

OrderedMap holds key-value pairs and remembers the original insertion order of the keys.

`key` stores in the map, map's value is the element of the list. for the convenience of iteration, the `Value` of list element stores the aggregate data of `key` and `value`.

func New

func New() *OrderedMap

New return an instance of ordered map.

func (*OrderedMap) Delete

func (m *OrderedMap) Delete(key string)

Delete deletes an item from the ordered map.

func (*OrderedMap) First

func (m *OrderedMap) First() *Element

First returns the first element of ordered map or nil if the orded map is empty.

func (*OrderedMap) Get

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

Get retrieves an values from ordered map under given key. If no value was associated with the given key, will return false.

func (*OrderedMap) Len

func (m *OrderedMap) Len() int

Len returns the number of elements of ordered map m. The complexity is O(1).

func (*OrderedMap) Set

func (m *OrderedMap) Set(key string, value interface{})

Set sets the given value under the specified key.

Jump to

Keyboard shortcuts

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