Git Product home page Git Product logo

go-trie's Introduction

go-trie

Build Status Coverage Status

A trie tree implementation in golang.

It runs faster than trie to regex approach more than 100 times (vs golang's regex engine; Please see the benchmark below).

Performance

Please see rival directory to benchmarks in other languages.

benchmark

 % make bench
 -             go-trie:   17.96 us/op (100000 times)
 - golang's trie2regex: 2482.64 us/op (  1000 times)
 -    php's trie2regex:  755.94 us/op ( 10000 times)
 -     v8's trie2regex:     4.2 us/op (100000 times)

zero heap allocation

Contains(string) and Match(string) operation do not allocate heaps:

% go test -benchmem -bench .
goos: linux
goarch: amd64
pkg: github.com/ledyba/go-trie/matchers/trie
BenchmarkUnmatchTrie-32                64830       18044 ns/op         0 B/op        0 allocs/op
PASS
ok    github.com/ledyba/go-trie/matchers/trie 4.086s

how to use?

package test

import (
  "testing"
  "github.com/ledyba/go-trie/matchers/trie"
)

func TestReadme(t *testing.T) {
	tr := trie.New() // Animes.
	// kirara
	tr.Add("NewGame!")
	tr.Add("School Live!")
	tr.Add("Urara Meirocho")
	tr.Add("Anne Happy")
	tr.Add("Kiniro Mosaic")
	tr.Add("Hanayamata")
	tr.Add("Is the order a rabbit?")
	tr.Add("Is the order a rabbit??")
	tr.Add("The Demon Girl Next Door")
	tr.Add("Hidamari Sketch")
	tr.Add("Blend S")
	tr.Add("Dōjin Work")
	tr.Add("Magic of Stella")
	// semi-kirara
	tr.Add("Yuki Yuna Is a Hero")
	tr.Add("Non Non Biyori")
	tr.Pack()

	// Match method 
	if tr.Match("NewGame!") == false {
		t.Error("NewGame! is a first season of the series.")
	}
	if tr.Match("NewGame!!") == false {
		t.Error("NewGame!! is a second season of the series.")
	}
	if tr.Match("NewGame") == true {
		t.Error("Not NewGame. NewGame\"!\"")
	}
	if tr.Match("Dojin Work") == true {
		t.Error("Not Dojin Work. \"Dōjin Work\"")
	}

	// Contains method
	if tr.Contains("I would like to eat udon with Fuu Inubozaki, a hero in \"Yuki Yuna Is a Hero\".") == false {
		t.Error("What????? Why????")
	}
	if tr.Contains("Alas, Ikaruga is going...") == true {
		t.Error("Ikaruga is a game. Not an animation.")
	}
}

You can see other examples in the unit test file.

LICENSE

Licensed under AGPLv3 or later.

This software uses materials from the wikipedia article 五稜郭 and ポケモン一覧, which is released under the Creative Commons Attribution-Share-Alike License 3.0.

go-trie's People

Contributors

ledyba avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

etsangsplk

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.