A memory-efficient trie for testing the existence/prefixes of string only(for now).

Overview

Succinct Trie

A memory-efficient trie for testing the existence/prefixes of string only(for now).

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)

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 in a depth-first search manner. 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

= 0 && domain[i] != '.' { i-- } next(k) if matched { return } i = tmp } else if c == domain[i] { i-- next(k) if matched { return } i++ } } }) return matched } 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">
// First build a trie with reversed domains because we usually match domains backwards
domains := []string{"*.example.com", "google.*"}

reversed := make([]string, len(domains))
for i, p := range domains {
    bytes := []byte(p)
    for i2, j := 0, len(bytes)-1; i2 < j; i2, j = i2+1, j-1 {
        bytes[i2], bytes[j] = bytes[j], bytes[i2]
    }
    reversed[i] = string(bytes)
}

trie := sutrie.BuildSuccinctTrie(reversed)

// Then let's define the matching method
search := func(domain string) bool {
    i := len(domain) - 1
    matched := false
    trie.Search(func(children []byte, isLeaf bool, next func(int)) {
        // Define ending condition
        if i < 0 {
            matched = isLeaf
            return
        }

        for k, c := range children {
            if c == '*' {
                tmp := i
                for i >= 0 && domain[i] != '.' {
                    i--
                }

                next(k)
                if matched {
                    return
                }

                i = tmp
            } else if c == domain[i] {
                i--

                next(k)
                if matched {
                    return
                }

                i++
            }
        }
    })

    return matched
}

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
You might also like...
A simple and efficient thread-safe sharded hashmap for Go

shardmap A simple and efficient thread-safe sharded hashmap for Go. This is an alternative to the standard Go map and sync.Map, and is optimized for w

Novel, efficient, and practical image compression with visually appealing results. 🤏 ✨
Novel, efficient, and practical image compression with visually appealing results. 🤏 ✨

Tiny Thumb 🤏 ✨ A novel, efficient, and practical method for lossy image compression, that produces visually appealing thumbnails. This technique is u

Golang string comparison and edit distance algorithms library, featuring : Levenshtein, LCS, Hamming, Damerau levenshtein (OSA and Adjacent transpositions algorithms), Jaro-Winkler, Cosine, etc...

Go-edlib : Edit distance and string comparison library Golang string comparison and edit distance algorithms library featuring : Levenshtein, LCS, Ham

Multi-String Pattern Matching Algorithm Using TrieHashNode

Multi-String Pattern Matching algorithm. This implementation is inspired from Aho-Corasick algorithm Getting Started modelA = mspm.NewModel("mspm_mode

Data structure and relevant algorithms for extremely fast prefix/fuzzy string searching.

Trie Data structure and relevant algorithms for extremely fast prefix/fuzzy string searching. Usage Create a Trie with: t := trie.New() Add Keys with:

Decode / encode XML to/from map[string]interface{} (or JSON); extract values with dot-notation paths and wildcards. Replaces x2j and j2x packages.

mxj - to/from maps, XML and JSON Decode/encode XML to/from map[string]interface{} (or JSON) values, and extract/modify values from maps by key or key-

Implementation of Boyer-Moore fast string search algorithm in Go

boyermoore Implementation of Boyer-Moore fast string search algorithm in Go

Convert json string to Golang struct

json-to-go-cli Convert json string to Golang struct How to install git clone https://github.com/tiancheng92/json-to-go-cli.git cd json-to-go-cli go bu

Fast in-memory key:value store/cache with TTL

MCache library go-mcache - this is a fast key:value storage. Its major advantage is that, being essentially a thread-safe . map[string]interface{} wit

Owner
野辺かない
野辺かない
A fast (5x) string keyed read-only map for Go - particularly good for keys using a small set of nearby runes.

faststringmap faststringmap is a fast read-only string keyed map for Go (golang). For our use case it is approximately 5 times faster than using Go's

The Sensible Code Company 30 Jan 8, 2023
Double-ARray Trie System for golang

Darts This is a GO implementation of Double-ARray Trie System. It's a clone of the C++ version Darts can be used as simple hash dictionary. You can al

Andy Song 95 Nov 17, 2022
Trie data structure implementation in Golang 🌳

Gotri Gotri is an Unicode character based Trie/prefix tree implementation in Go, with the suggestion/auto-complete feature for character searching. Si

Monir Zaman 9 Jun 17, 2022
Hairetsu: a TRIE implementation by double array

hairetsu hairetsu is a TRIE implementation by double array. alpha quality : thin

ajiyoshi 6 Mar 20, 2022
An app with Trie tree and Breve search Implementation CLI and HTTP both 🥳

Introduction LifeLongLearner project consists of two different parts. My English Vocabulary My Technical Book Notes All of them provided by me within

A.Samet İleri 15 Jul 1, 2022
Recursively searches a map[string]interface{} structure for another map[string]interface{} structure

msirecurse Recursively searches a map[string]interface{} structure for existence of a map[string]interface{} structure Motivation I wrote this package

Fred Moyer 1 Mar 3, 2022
Package mafsa implements Minimal Acyclic Finite State Automata in Go, essentially a high-speed, memory-efficient, Unicode-friendly set of strings.

MA-FSA for Go Package mafsa implements Minimal Acyclic Finite State Automata (MA-FSA) with Minimal Perfect Hashing (MPH). Basically, it's a set of str

SmartyStreets (Archives) 293 Oct 27, 2022
An in-memory string-interface{} map with various expiration options for golang

TTLCache - an in-memory cache with expiration TTLCache is a simple key/value cache in golang with the following functions: Expiration of items based o

Rene Kroon 569 Jan 8, 2023
An in-memory string-interface{} map with various expiration options for golang

TTLCache - an in-memory cache with expiration TTLCache is a simple key/value cache in golang with the following functions: Expiration of items based o

Rene Kroon 565 Dec 28, 2022
A Go library for an efficient implementation of a skip list: https://godoc.org/github.com/MauriceGit/skiplist

Fast Skiplist Implementation This Go-library implements a very fast and efficient Skiplist that can be used as direct substitute for a balanced tree o

Maurice Tollmien 240 Dec 30, 2022