Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/acomagu/trie

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/acomagu/trie

  • v1.0.0
  • Source
  • Go
  • Socket score

Version published
Created
Source

trie: The fast and flexible Trie Tree implementation

GoDoc

The Trie Tree implementation in Go. It has flexible interface and works fast as Radix Tree implementation.

This is basically an implementation of the algorithm described in 簡単なトライ - LINE ENGINEERING. I really appreciate the amazing ideas and the clear and easy-to-understand explanation.

Benchmark

Run make bench to run it locally.

Exact Match

The task is to determine whether a string matches one of all Wikipedia titles.

PackageTimeObjects Allocated
acomagu/trie1090 ns/op0 allocs/op
sauerbraten/radix2445 ns/op0 allocs/op
dghubble/trie2576 ns/op0 allocs/op
hashicorp/go-immutable-radix3660 ns/op0 allocs/op
derekparker/trie4010 ns/op0 allocs/op
armon/go-radix11745 ns/op0 allocs/op
kkdai/radix18809 ns/op0 allocs/op
tchap/go-patricia/patricia21498 ns/op0 allocs/op

Longest Prefix

The task is to answer which of all Wikipedia titles can be the longest prefix of a string.

PackageTimeObjects Allocated
acomagu/trie140 ns/op0 allocs/op
hashicorp/go-immutable-radix159 ns/op0 allocs/op
tchap/go-patricia/patricia252 ns/op0 allocs/op
derekparker/trie2374 ns/op0 allocs/op
sauerbraten/radix3264938 ns/op0 allocs/op
armon/go-radix22129827 ns/op1 allocs/op

(dghubble/trie and kkdai/radix don't have way to do.)

Build

The task is to prepare Trie/Radix Tree with all of the Wikipedia titles.

PackageTimeObjects Allocated
sauerbraten/radix118959250 ns/op408564 allocs/op
acomagu/trie542902000 ns/op421906 allocs/op
dghubble/trie609406300 ns/op1136281 allocs/op
derekparker/trie1046705400 ns/op1801539 allocs/op
armon/go-radix1750312500 ns/op1446050 allocs/op
kkdai/radix2280362300 ns/op1742841 allocs/op
tchap/go-patricia/patricia2898335700 ns/op1150947 allocs/op
hashicorp/go-immutable-radix7614342400 ns/op45097986 allocs/op

Examples

The common preparation for each examples:

keys := [][]byte{
	[]byte("ab"),
	[]byte("abc"),
	[]byte("abd"),
}
values := []interface{}{1, 2, 3}
t := trie.New(keys, values)

New() takes keys and values as the arguments. values[i] is the value of the corresponding key, keys[i].

Exact Match

v, ok := t.Trace([]byte("abc")).Terminal()
fmt.Println(v, ok) // => 2 true

Playground

Longest Prefix

var v interface{}
var match bool
for _, c := range []byte("abcxxx") {
	if t = t.TraceByte(c); t == nil {
		break
	}
	if vv, ok := t.Terminal(); ok {
		v = vv
		match = true
	}
}

fmt.Println(v, match) // => 2 true

Playground

No special function to get longest prefix because it can be implemented yourself easily using the existing methods.

FAQs

Package last updated on 29 Sep 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc