Git Product home page Git Product logo

sutrie's Introduction

Succinct Trie

"Sutrie" is an extremely memory-efficient prefix tree or trie data structure, which also boasts impressive time efficiency.

Install

go get -u github.com/nobekanai/sutrie

Documentation

A simple and common use case: querying whether the key appears in the dictionary or whether the prefix of the key is in the dictionary

keys := []string{"hat", "is", "it", "a"}
trie := sutrie.BuildSuccinctTrie(keys).Root()

lastUnmatch := trie.SearchPrefix("hatt")
println(lastUnmatch) // will print 3, same if you search "hat"

lastUnmatch = trie.SearchPrefix("ha")
println(lastUnmatch) // will print 0, because neither "ha" nor its prefix (that is "h") is in trie

Advanced Usage

You can customize trie's traversal process. For example, define a domain name lookup rule: *.example.com matches www.example.com and xxx.example.com but not xxx.www.example.com and example.com

func reverse(s string) string {
	bytes := []byte(s)
	for i, j := 0, len(bytes)-1; i < j; i, j = i+1, j-1 {
		bytes[i], bytes[j] = bytes[j], bytes[i]
	}
	return string(bytes)
}

func reverseSplit(domain string) []string {
	return strings.Split(reverse(domain), ".")
}

func main() {
	// First build a trie with reversed domains because we match domains backwards
	domains := []string{reverse("*.example.com"), reverse("google.*")}
	trie := sutrie.BuildSuccinctTrie(domains)

	// Then let's define matching rules
	var search func(node sutrie.Node, ds []string, idx int) bool
	search = func(node sutrie.Node, ds []string, idx int) bool {
		if !node.Exists() {
			return false
		}

		wildcard := node.Next('*')

		if idx == len(ds)-1 {
			return wildcard.Leaf() || node.Search(ds[idx]).Leaf()
		}

		if wildcard.Exists() && search(wildcard.Next('.'), ds, idx+1) {
			return true
		}

		return search(node.Search(ds[idx]).Next('.'), ds, idx+1)
	}

	Search := func(domain string) bool {
		return search(trie.Root(), reverseSplit(domain), 0)
	}

	// Finally we apply the rules to search for domain names
	println(Search("www.example.com"))     // true
	println(Search("xxx.example.com"))     // true
	println(Search("xxx.www.example.com")) // false
	println(Search("example.com"))         // false
	println(Search("google.io"))           // true
}

sutrie's People

Contributors

nobekanai avatar

Stargazers

 avatar  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.