tinymap

package module
v0.0.0-...-09e5e64 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2019 License: MIT Imports: 3 Imported by: 0

README

GoDoc Build Status

tinymap

Provides a simple to use interface that behaves like a HashMap.

Multiple Maps are exposed for use as well as structs that behave like Tuples.

This package is intended to be used with tinygo.

However it has some nice little structs, so feel free to use this as you wish.

It is very small, but helps with embedded programming.

🎉

Basic usage

Just one of the many Maps 😄

strMap := new(StrMap)

strMap.Set("foo", "bar")

val, err := strMap.Get("foo")

if err != nil {
  log.Print(err)
}

fmt.Print(val)

strMap.Delete("foo")
Benchmarks
$ ./scripts/bench.sh
+ go test -bench=.
goos: windows
goarch: amd64
pkg: github.com/selfup/tinymap
Benchmark_ByteMap_Get_Lower_Bound-4             50000000                25.7 ns/op
Benchmark_ByteMap_Get_Expected_Bound-4          50000000                29.3 ns/op
Benchmark_ByteMap_Get_Upper_Bound-4              3000000               530 ns/op
Benchmark_ByteMap_Set_Upper_Bound-4              5000000               302 ns/op
Benchmark_ByteMap_Delete_Upper_Bound-4          100000000               11.7 ns/op
Benchmark_IntMap_Get_Lower_Bound-4              1000000000               2.51 ns/op
Benchmark_IntMap_Get_Expected_Bound-4           300000000                4.19 ns/op
Benchmark_IntMap_Get_Upper_Bound-4              30000000                45.3 ns/op
Benchmark_IntMap_Set_Upper_Bound-4              1000000000               2.84 ns/op
Benchmark_IntMap_Delete_Upper_Bound-4           1000000000               2.09 ns/op
Benchmark_IntStrMap_Get_Lower_Bound-4           1000000000               2.69 ns/op
Benchmark_IntStrMap_Get_Expected_Bound-4        300000000                4.84 ns/op
Benchmark_IntStrMap_Get_Upper_Bound-4           30000000                55.8 ns/op
Benchmark_IntStrMap_Set_Upper_Bound-4           100000000               20.1 ns/op
Benchmark_IntStrMap_Delete_Upper_Bound-4        1000000000               2.07 ns/op
Benchmark_StrByteMap_Get_Lower_Bound-4          200000000                6.91 ns/op
Benchmark_StrByteMap_Get_Expected_Bound-4       100000000               14.8 ns/op
Benchmark_StrByteMap_Get_Upper_Bound-4           3000000               444 ns/op
Benchmark_StrByteMap_Set_Upper_Bound-4           5000000               253 ns/op
Benchmark_StrByteMap_Delete_Upper_Bound-4       200000000                6.45 ns/op
Benchmark_StrMap_Get_Lower_Bound-4              300000000                4.86 ns/op
Benchmark_StrMap_Get_Expected_Bound-4           300000000                4.95 ns/op
Benchmark_StrMap_Get_Upper_Bound-4               5000000               325 ns/op
Benchmark_StrMap_Set_Upper_Bound-4              10000000               196 ns/op
Benchmark_StrMap_Delete_Upper_Bound-4           200000000                6.33 ns/op
PASS
ok      github.com/selfup/tinymap       49.282s
Details
  1. Using an int as an index is fastest for lookups/comparisons.
  2. Using []byte as key is the slowest (makes sense)
  3. Lower Bound means the value being grabbed is the 1th element in a slice of 1 element.
  4. Expected Bound means the value is being grabbed at the 5th element in a slice of 5 elements.
  5. Upper Bound means the value being grabbed is the 100th element in a slice of 100 elements.
  6. Under the hood TinyMap uses slices to store Tuples.
  7. I have not yet added a catch block to prevent the slice to grow, but this should be used for small data sotrage 🙏

Documentation

Overview

Package tinymap provides a simple to use interface that behaves like a HashMap.

Multiple Maps are exposed for use as well as structs that behave like Tuples.

This package is intended to be used with tinygo.

However it has some nice little structs, so feel free to use this as you wish.

It is very small, but helps with embedded programming.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByteMap

type ByteMap struct {
	// Data is public but use carefully :)
	Data []ByteTuple
}

ByteMap stores ByteTuples

It behaves like a HashMap!

byteMap := new(ByteMap)
byteMap.Set([]byte("42"), []byte("9000"))
val, err := byteMap.Get([]byte("42"))

if err != nil {
  log.Print(err)
}

fmt.Print(val)

byteMap.Delete([]byte("42"))

func (*ByteMap) Delete

func (t *ByteMap) Delete(key []byte) bool

Delete removes Tuples. Returns true if deleted. Returns false if the key was not found.

func (ByteMap) Get

func (t ByteMap) Get(key []byte) ([]byte, error)

Get fetches Tuples by Key and returns their Val.

func (*ByteMap) Set

func (t *ByteMap) Set(key []byte, val []byte)

Set will update or add Tuples. If ByteTuple.Key already exists, only the ByteTuple.Val is updated. Otherwise a new ByteTuple is inserted into the Data slice.

type ByteTuple

type ByteTuple struct {
	Key []byte
	Val []byte
}

ByteTuple is a basic struct

ByteTuple{Key: []byte("42"), Val: []byte("9000")}

type IntMap

type IntMap struct {
	// Data is public but use carefully :)
	Data []IntTuple
}

IntMap stores IntTuples

It behaves like a HashMap!

intMap := new(IntMap)
intMap.Set(42, 9000)
val, err := intMap.Get(42)

if err != nil {
  log.Print(err)
}

fmt.Print(val)

intMap.Delete(42)

func (*IntMap) Delete

func (t *IntMap) Delete(key int) bool

Delete removes Tuples. Returns true if deleted. Returns false if the key was not found.

func (IntMap) Get

func (t IntMap) Get(key int) (int, error)

Get fetches Tuples by Key and returns their Val

func (*IntMap) Set

func (t *IntMap) Set(key int, val int)

Set will update or add Tuples. If IntTuple.Key already exists, only the IntTuple.Val is updated. Otherwise a new IntTuple is inserted into the Data slice.

type IntStrMap

type IntStrMap struct {
	// Data is public but use carefully :)
	Data []IntStrTuple
}

IntStrMap stores IntStrTuples

It behaves like a HashMap!

intStrMap := new(IntStrMap)
intStrMap.Set(42, "9000")
val, err := intStrMap.Get(42)

if err != nil {
  log.Print(err)
}

fmt.Print(val)

intStrMap.Delete(42)

func (*IntStrMap) Delete

func (t *IntStrMap) Delete(key int) bool

Delete removes Tuples. Returns true if deleted. Returns false if the key was not found.

func (IntStrMap) Get

func (t IntStrMap) Get(key int) (string, error)

Get fetches Tuples by Key and returns their Val

func (*IntStrMap) Set

func (t *IntStrMap) Set(key int, val string)

Set will update or add Tuples. If IntStrTuple.Key already exists, only the IntStrTuple.Val is updated. Otherwise a new IntStrTuple is inserted into the Data slice.

type IntStrTuple

type IntStrTuple struct {
	Key int
	Val string
}

IntStrTuple is a basic struct

IntStrTuple{Key: 42, Val: "9000"}

type IntTuple

type IntTuple struct {
	Key int
	Val int
}

IntTuple is a basic struct

IntTuple{Key: 42, Val: 9000}

type StrByteMap

type StrByteMap struct {
	// Data is public but use carefully :)
	Data []StrByteTuple
}

StrByteMap stores StrByteTuples

It behaves like a HashMap!

strByteMap := new(StrByteMap)
strByteMap.Set("foo", []byte("bar"))
val, err := strByteMap.Get("foo")

if err != nil {
  log.Print(err)
}

fmt.Print(val)

strByteMap.Delete("foo")

func (*StrByteMap) Delete

func (t *StrByteMap) Delete(key string) bool

Delete removes Tuples. Returns true if deleted. Returns false if the key was not found.

func (StrByteMap) Get

func (t StrByteMap) Get(key string) ([]byte, error)

Get fetches Tuples by Key and returns their Val

func (*StrByteMap) Set

func (t *StrByteMap) Set(key string, val []byte)

Set will update or add Tuples. If StrByteTuple.Key already exists, only the StrByteTuple.Val is updated. Otherwise a new StrByteTuple is inserted into the Data slice.

type StrByteTuple

type StrByteTuple struct {
	Key string
	Val []byte
}

StrByteTuple is a basic struct

StrByteTuple{Key: "foo", Val: "bar"}

type StrMap

type StrMap struct {
	// Data is public but use carefully :)
	Data []StrTuple
}

StrMap stores StrTuples

It behaves like a HashMap!

strMap := new(StrMap)
strMap.Set("foo", "bar")
val, err := strMap.Get("foo")

if err != nil {
  log.Print(err)
}

fmt.Print(val)

strMap.Delete("foo")

func (*StrMap) Delete

func (t *StrMap) Delete(key string) bool

Delete removes Tuples. Returns true if deleted. Returns false if the key was not found.

func (StrMap) Get

func (t StrMap) Get(key string) (string, error)

Get fetches Tuples by Key and returns their Val

func (*StrMap) Set

func (t *StrMap) Set(key string, val string)

Set will update or add Tuples. If StrTuple.Key already exists, only the StrTuple.Val is updated. Otherwise a new StrTuple is inserted into the Data slice.

type StrTuple

type StrTuple struct {
	Key string
	Val string
}

StrTuple is a basic struct

StrTuple{Key: "foo", Val: "bar"}

Jump to

Keyboard shortcuts

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