Code Monkey home page Code Monkey logo

trie's Introduction

trie

Build codecov Go Report Card GoDoc

A missing trie implementation for Go.

Installation

go get github.com/krasun/trie

Usage

Known limitations:

  1. Empty string keys are not supported. And functions won't return an error.
  2. Keys are not ordered, so do not expect any ordering.

Without any stress:

t := trie.New() // or trie.NewRuneTrie()  

t.Insert("apple")
t.Insert("alphabet")
t.Insert("banana")

t.Contains("apple") // true
t.Contains("app") // false
t.Contains("banana") // true
t.Contains("ban") // false

t.SearchByPrefix("a") // []string{"apple", "alphabet"}

t.StartsWith("a") // true

A goroutine-safe (thread-safe) trie

By default, rune-wise trie are not safe to use concurrently, but it is easy to make them safe. You can wrap any trie into the safe version by:

safeTrie := trie.Safe(trie.NewTrie())

// the same interface as for a regular trie

Tests

To run tests, just execute:

$ go test . -race

Benchmarking

Zero heap allocations for Contains function:

$ go test -bench=. -benchmem -benchtime=1000x
goos: darwin
goarch: amd64
op	       0 allocs/op
BenchmarkRuneTrieInsert-8          	    1000	      7196 ns/op	    6984 B/op	     129 allocs/op
BenchmarkRuneTrieContains-8        	    1000	       517 ns/op	       0 B/op	       0 allocs/op
BenchmarkWordHashSetInsert-8       	    1000	      1406 ns/op	    1100 B/op	       4 allocs/op
BenchmarkWordHashSetContains-8     	    1000	       178 ns/op	       0 B/op	       0 allocs/op
PASS

But the hash set (map[string]{}struct) is much much more efficient than the trie. Carefully weigh when to use the trie.

Applications

Use the trie in cases when it only outperforms hash tables or hash sets (map[string]{}struct):

  1. Search key by the prefix.

License

trie is released under the MIT license.

trie's People

Contributors

krasun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

trie's Issues

Improve documentation

In order to improve documentation:

  1. Add benchmarks and publish results
  2. Describe use cases
  3. Decide on goroutine-safety (thread-safety)
  4. Add CI
  5. Add labels
  6. Attach the code of conduct
  7. Add link to godoc

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.