A prefix-enhanced map in Go

  • Build Status
  • Coverage Status
  • GoDoc

PrefixMap

PrefixMap is a prefix-enhanced map that eases the retrieval of values based on key prefixes.

Quick Start

Creating a PrefixMap

// creates the map object
prefixMap := prefixmap.New()

Inserting a value

 // inserts values 1, "value 2" and false for key 'someKey'
prefixMap.Insert("someKey", 1, "value 2", false)

// map now contains
//
// 'someKey' => [1, "value 2", false]

Replace values for key

prefixMap.Insert("key", "hello")

// map contents:
//
// 'key' => ["hello"]

prefixMap.Insert("key", "world")

// map contents:
//
// 'key' => ["hello", "world"]

// now replacing the contents for key
prefixMap.Replace("key", "new value")

// map contents:
//
// 'key' => ["new value"]

Checking if a key exists

prefixMap.Insert("key", "hello")

prefixMap.Contains("k") // #=> false
prefixMap.Contains("key") // #=> true
prefixMap.ContainsPrefix("k") // #=> true

Getting by key

prefixMap.Insert("foo", "bar", "baz", "quz")

data := prefixMap.Get("foo") // #=> [bar, baz, quz]

Getting by keys prefix

prefixMap.Insert("prefix1", "prefix1")
prefixMap.Insert("prefix2", "prefix2")
prefixMap.Insert("prefix3", "prefix3")

data := prefixMap.GetByPrefix("prefix") // #=> [prefix1, prefix2, prefix3]

Iterate over prefixes

PrefixMap exposes an EachPrefix method that executes a callback function against every prefix in the map. The prefixes are iterated over using a Depth First Search algorithm. At each iteration the given callback is invoked. The callback allows you to skip a branch iteration altogether if you're not satisfied with what you're looking for. Check out PrefixCallback documentation for more information.

prefixMap.EachPrefix(func(prefix Prefix) (bool, bool) {
    
    // do something with the current prefix
    doSomething(prefix.Key)
    
    // keep iterating
    return false, false
})

License

The code contained in this repository is provided as is under the terms of the MIT license as specified here.

Owner
Alessandro Diaferia
Software Engineer Follow me on Twitter: https://twitter.com/alediaferia
Alessandro Diaferia
Similar Resources

💯 Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving

Package validator implements value validations for structs and individual fields based on tags.

Nov 9, 2022

A Go library to iterate over potentially nested map keys using the visitor pattern

A Go library to iterate over potentially nested map keys using the visitor pattern

Mar 15, 2022

Go library for encoding native Go structures into generic map values.

wstructs origin: github.com/things-go/structs Go library for encoding native Go structures into generic map values. Installation Use go get. go ge

Jan 10, 2022

skipmap is a high-performance concurrent sorted map based on skip list. Up to 3x ~ 10x faster than sync.Map in the typical pattern.

skipmap is a high-performance concurrent sorted map based on skip list. Up to 3x ~ 10x faster than sync.Map in the typical pattern.

Introduction skipmap is a high-performance concurrent map based on skip list. In typical pattern(one million operations, 90%LOAD 9%STORE 1%DELETE), th

Jan 8, 2023

Recursively searches a map[string]interface{} structure for another map[string]interface{} structure

msirecurse Recursively searches a map[string]interface{} structure for existence of a map[string]interface{} structure Motivation I wrote this package

Mar 3, 2022

Grpc-gateway-map-null - gRPC Gateway test using nullable values in map

Demonstrate gRPC gateway behavior with nullable values in maps Using grpc-gatewa

Jan 6, 2022

Levenshtein distance and similarity metrics with customizable edit costs and Winkler-like bonus for common prefix.

A Go package for calculating the Levenshtein distance between two strings This package implements distance and similarity metrics for strings, based o

Dec 15, 2022

A prefix tree implementation in go

Trie (Prefix tree) This library is compatible with Go 1.11+ Please refer to CHANGELOG.md if you encounter breaking changes. Motivation Introduction Us

Nov 3, 2022

Data structure and relevant algorithms for extremely fast prefix/fuzzy string searching.

Trie Data structure and relevant algorithms for extremely fast prefix/fuzzy string searching. Usage Create a Trie with: t := trie.New() Add Keys with:

Dec 27, 2022

CoLog is a prefix-based leveled execution log for Go

CoLog is a prefix-based leveled execution log for Go

What's CoLog? CoLog is a prefix-based leveled execution log for Go. It's heavily inspired by Logrus and aims to offer similar features by parsing the

Dec 14, 2022

generate Wireguard keypairs with a given prefix string

wireguard-vanity-address Generate Wireguard keypairs with a given prefix string. The Wireguard VPN uses Curve25519 keypairs, and displays the Base64-e

Nov 9, 2022

MAC Address & Prefix Utility for Go

macaddr MAC Address & Prefix Utility for Go Installation go get -d github.com/thatmattlove/go-macaddr Usage Single MAC Address mac, err := macaddr.Pa

Feb 7, 2022

An enhanced http client for Golang

An enhanced http client for Golang

go-http-client An enhanced http client for Golang Documentation on go.dev 🔗 This package provides you a http client package for your http requests. Y

Dec 23, 2022

An enhanced HTTP client for Go

An enhanced HTTP client for Go

Heimdall Description Installation Usage Making a simple GET request Creating a hystrix-like circuit breaker Creating a hystrix-like circuit breaker wi

Jan 9, 2023

An Enhanced Go Experience For The Atom Editor

An Enhanced Go Experience For The Atom Editor

go-plus An Improved Go Experience For The Atom Editor Github: https://github.com/joefitzgerald/go-plus Atom: https://atom.io/packages/go-plus Overview

Dec 26, 2022

Enhanced PostgreSQL logical replication

pgcat - Enhanced postgresql logical replication Why pgcat? Architecture Build from source Install Run Conflict handling Table mapping Replication iden

Dec 21, 2022

An enhanced HTTP client for Go

An enhanced HTTP client for Go

Heimdall Description Installation Usage Making a simple GET request Creating a hystrix-like circuit breaker Creating a hystrix-like circuit breaker wi

Jan 2, 2023

A SimpleHTTPServer written in Go, enhanced with features and with a nice design

A SimpleHTTPServer written in Go, enhanced with features and with a nice design

goshs is a replacement for Python's SimpleHTTPServer. It allows uploading and downloading via HTTP/S with either self-signed certificate or user provi

Dec 28, 2022

digpro is a enhanced uber-go/dig

Introduction digpro is a enhanced uber-go/dig, inherit all feature of uber-go/dig and add the following features: Progressive

Oct 14, 2022
Comments
  • How this is different from Ternary search tree?

    How this is different from Ternary search tree?

    I just want to know how this is different from ternary search tree. And can u please provide some more explanation on how this prefix search works on Map.

Recursively searches a map[string]interface{} structure for another map[string]interface{} structure

msirecurse Recursively searches a map[string]interface{} structure for existence of a map[string]interface{} structure Motivation I wrote this package

Mar 3, 2022
Levenshtein distance and similarity metrics with customizable edit costs and Winkler-like bonus for common prefix.

A Go package for calculating the Levenshtein distance between two strings This package implements distance and similarity metrics for strings, based o

Dec 15, 2022
A prefix tree implementation in go

Trie (Prefix tree) This library is compatible with Go 1.11+ Please refer to CHANGELOG.md if you encounter breaking changes. Motivation Introduction Us

Nov 3, 2022
Data structure and relevant algorithms for extremely fast prefix/fuzzy string searching.

Trie Data structure and relevant algorithms for extremely fast prefix/fuzzy string searching. Usage Create a Trie with: t := trie.New() Add Keys with:

Dec 27, 2022
A thread safe map which has expiring key-value pairs

~ timedmap ~ A map which has expiring key-value pairs. go get github.com/zekroTJA/timedmap Intro This package allows to set values to a map which will

Dec 29, 2022
An in-memory string-interface{} map with various expiration options for golang

TTLCache - an in-memory cache with expiration TTLCache is a simple key/value cache in golang with the following functions: Expiration of items based o

Jan 8, 2023
Decode / encode XML to/from map[string]interface{} (or JSON); extract values with dot-notation paths and wildcards. Replaces x2j and j2x packages.

mxj - to/from maps, XML and JSON Decode/encode XML to/from map[string]interface{} (or JSON) values, and extract/modify values from maps by key or key-

Dec 22, 2022
A typed implementation of the Go sync.Map using code generation

syncmap A typed implementation of the Go sync.Map using code generation. Install go get -u github.com/a8m/syncmap@master Examples: Using CLI $ syncma

Dec 26, 2022
An in-memory string-interface{} map with various expiration options for golang

TTLCache - an in-memory cache with expiration TTLCache is a simple key/value cache in golang with the following functions: Expiration of items based o

Dec 28, 2022
A fast (5x) string keyed read-only map for Go - particularly good for keys using a small set of nearby runes.

faststringmap faststringmap is a fast read-only string keyed map for Go (golang). For our use case it is approximately 5 times faster than using Go's

Jan 8, 2023