Giter Site home page Giter Site logo

lua-radixtree's Introduction

lua-radixtree

A radix tree matcher. It implements a lightweight tree search.

Why do I need that?

If a set of strings is given and you want to return all of the strings that start with "abc", you would need to iterate over all strings and call string.find, string.match or compare the substring. The radix tree does these things more sophisticated and therefore vastly reduces search time.

You can also use the radix tree to return a subset of elements that can then be evaluated further if the matching expressions provided by the radix tree do not cover the needed matching expressions completely.

example

local radix = require'radix'
local radixTree = radix.new()
radixTree.add('foo')
radixTree.add('bar')
local matches = radixTree.get_possible_matches({startsWith = "fo"}, false)
if matches then
  for path,_ in pairs(matches) do
    print(path)
  end
end

speed comparison

3.000.000 states are added and a lookup is performed.

  • added radixtree: 16.518508911133
  • lookup radixtree: 0.004925012588501
  • added table: 3.9087619781494
  • lookup table: 0.50004291534424

Obviously, adding strings to the radix-tree consumes more time (4x) than just pushing them into an array. A lookup from the radix tree is much faster (100x) than an ordinary string.match. If you rely on fast string compares the radix-tree is the way to go.

interface

Add elements to tree

add(string)

Remove elements from tree

remove(string)

Find in tree

get_possible_matches(Object, Boolean) returns array of matches and completeness identifier

The Object can have the following identifiers:

  • startsWith = string
  • contains = string
  • endsWith = string
  • equals = string

If only a set of these identifiers is present, the completeness identifier is "all" and the returned elements represent the complete match that e.g. a string.find or string.match would return when iterating over all added strings. More identifiers can be defined that are not evalueted by the tree but must be evaluated by the calling application. The tree than just reduces the search space and the completeness identifier is "partial". If identifiers are duplicated, only unknown identifiers are present or a case sensitive search shall be performed, the completeness identifier is "impossible" and nil is returned instead of a set of matches

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.