omap

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

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

Go to latest
Published: Aug 24, 2017 License: MIT Imports: 2 Imported by: 1

README

Ordered Map for golang

Build Status

OrderedMap is a Python port of OrderedDict implemented in golang. Golang's builtin map purposefully randomizes the iteration of stored key/values. OrderedMap` struct preserves inserted key/value pairs; such that on iteration, key/value pairs are received in inserted (first in, first out) order.

Features

  • Full support Key/Value for all data types
  • Exposes an Iterator that iterates in order of insertion
  • Full Get/Set/Delete map interface
  • Supports Golang v1.3 through v1.7

Download and Install

go get https://github.com/cevaris/ordered_map.git

Examples

Create, Get, Set, Delete
package main

import (
    "fmt"
    "github.com/cevaris/ordered_map"
)

func main() {

    // Init new OrderedMap
    om := ordered_map.NewOrderedMap()

    // Set key
    om.Set("a", 1)
    om.Set("b", 2)
    om.Set("c", 3)
    om.Set("d", 4)

    // Same interface as builtin map
    if val, ok := om.Get("b"); ok == true {
        // Found key "b"
        fmt.Println(val)
    }

    // Delete a key
    om.Delete("c")

    // Failed Get lookup becase we deleted "c"
    if _, ok := om.Get("c"); ok == false {
        // Did not find key "c"
        fmt.Println("c not found")
    }
    
    fmt.Println(om)
}
Iterator
n := 100
om := ordered_map.NewOrderedMap()

for i := 0; i < n; i++ {
    // Insert data into OrderedMap
    om.Set(i, fmt.Sprintf("%d", i * i))
}

// Iterate though values
// - Values iteration are in insert order
// - Returned in a key/value pair struct
iter := om.IterFunc()
for kv, ok := iter(); ok; kv, ok = iter() {
    fmt.Println(kv, kv.Key, kv.Value)
}
Custom Structs
om := ordered_map.NewOrderedMap()
om.Set("one", &MyStruct{1, 1.1})
om.Set("two", &MyStruct{2, 2.2})
om.Set("three", &MyStruct{3, 3.3})

fmt.Println(om)
// Ouput: OrderedMap[one:&{1 1.1},  two:&{2 2.2},  three:&{3 3.3}, ]

For Development

Git clone project

git clone https://github.com/cevaris/ordered_map.git

Build and install project

make

Run tests

make test

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KVPair

type KVPair struct {
	Key   interface{}
	Value interface{}
}

KVPair represents tuple

func (*KVPair) Compare

func (k *KVPair) Compare(kv2 *KVPair) bool

Compare current key to another

func (*KVPair) String

func (k *KVPair) String() string

String representations of the key pair

type Map

type Map interface {
	Set(key interface{}, value interface{})
	Get(key interface{}) (interface{}, bool)
	Delete(key interface{})
	Iterator() func() (*KVPair, bool)
	Len() int
}

Map object

func New

func New() Map

New map object

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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