bytemap

package
v0.0.50 Latest Latest
Warning

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

Go to latest
Published: May 1, 2023 License: MIT Imports: 0 Imported by: 1

README

Bytemap

The bytemap package provides an implementation of a map that uses []byte keys, called ByteSliceMap. The map is implemented as a Trie data structure. It is based off of triemap, but adds support for generics.

Usage

To use the ByteSliceMap, first import the package:

import "github.com/synapsecns/sanguine/core/bytemap"

Then, create a new ByteSliceMap with the desired value type:

m := &bytemap.ByteSliceMap[string]{}
Inserting Values

Values can be inserted into the map using either []byte or string keys. To insert a value with a string key, use the PutString method:

m.PutString("key", "value")

To insert a value with a []byte key, use the Put method:

m.Put([]byte{0x01, 0x02}, "value")
Retrieving values

Values can be retrieved from the map using either []byte or string keys. To retrieve a value with a string key, use the GetString method:

value, exists := m.GetString("key")

To retrieve a value with a []byte key, use the Get method:

value, exists := m.Get([]byte{0x01, 0x02})

The exists return value indicates whether the key exists in the map.

Performance

The ByteSliceMap seems to perform worse than a regular map[string]V, even when casting []byte to string. Therefore, it may not be suitable for performance-critical applications.

Documentation

Overview

Package bytemap is an implementation of https://github.com/google/triemap/blob/main/bytemap.go with generic support for values

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByteSliceMap

type ByteSliceMap[V any] struct {
	// contains filtered or unexported fields
}

ByteSliceMap emulates `map[[]byte]V`, implemented as a Trie.

It seems to perform worse than `map[string]V` even when casting `string([]byte)`.

func (*ByteSliceMap[V]) Get

func (n *ByteSliceMap[V]) Get(s []byte) (value V, _ bool)

Get returns a value as mapped by the `[]byte` key and a boolean of whether the value exists in the map.

func (*ByteSliceMap[V]) GetString

func (n *ByteSliceMap[V]) GetString(s string) (V, bool)

GetString is a convenience method to get a value using a string key.

See: `Get`.

func (*ByteSliceMap[V]) Put

func (n *ByteSliceMap[V]) Put(s []byte, v V) *ByteSliceMap[V]

Put inserts a value into the `ByteMap` using `[]byte` as a key.

func (*ByteSliceMap[V]) PutString

func (n *ByteSliceMap[V]) PutString(s string, v V) *ByteSliceMap[V]

PutString is a convenience method to insert a value using a string key.

Jump to

Keyboard shortcuts

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