Code Monkey home page Code Monkey logo

trie's Introduction

trie

A Trie (Prefix Index) implementation in golang. It works fine with all unicode characters.

Documentation can be found at godoc.org.

Entries are reference counted: If you Add("foo") twice and Del("foo") it once it will still be found.

Build Status

Example

t := trie.NewTrie()
t.Add("foo")
t.Add("bar")
t.PrintDump()

// output:
//  I:f (-)
// - V:oo (1)
// --- $
//  I:b (-)
// - V:ar (1)
// --- $

t.Add("foo")
t.PrintDump()

// output:
//  I:f (-)
// - V:oo (2)
// --- $
//  I:b (-)
// - V:ar (1)
// --- $

fmt.Println(t.Has("foo"))
// output: true

fmt.Println(t.HasCount("foo"))
// output: true 2

fmt.Println(t.Has("foobar"))
// output: false

fmt.Println(t.Members())
// output: [foo(2) bar(1)]

t.Add("food")
t.Add("foobar")
t.Add("foot")
fmt.Println(t.HasPrefix("foo"))
// output: true

fmt.Println(t.PrefixMembers("foo"))
// output: [foo(2) food(1) foobar(1) foot(1)]

A Trie can be dumped into a file with

t.DumpToFile("/tmp/trie_foo")

And loaded with

t2, _ := trie.LoadFromFile("/tmp/trie_foo")
fmt.Println(t2.Members())
// output: [foo(2) food(1) foobar(1) foot(1) bar(1)]

An existing Trie can be merged with a stored one with

t3 := trie.NewTrie()
t3.Add("フー")
t3.Add("バー")
t3.Add("日本語")
fmt.Println(t3.Members())
// output: [フー(1) バー(1) 日本語(1)]

t3.MergeFromFile("/tmp/trie_foo")
fmt.Println(t3.Members())
// output: [フー(1) バー(1) 日本語(1) foo(2) food(1) foobar(1) foot(1) bar(1)]

trie's People

Contributors

fvbock avatar

Watchers

 avatar  avatar

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.