Git Product home page Git Product logo

indextrie's Introduction

IndexTrie

Swift Package Manager Mac + Linux

This is a simple Swift-based Trie implementation which supports mappings between strings and their prefixes to sets of objects (often array indexes).

Background

Find more information on the Trie data structure here.

As an example, consider an array of companies that have a relationship to a set of programming languages or libraries:

["Google", "Apple", "Facebook", "Netflix"]

An association between programming languages / libraries to offsets into the above array might look as follows:

String Indices
"react" [2, 3]
"redux" [2, 3]
"angular" [0]
"piet" nil

After building the IndexTrie, we can perform prefix searches to find the set of matching indices (e.g. searching the IndexTrie for "re" will return indices 2,3)

The trie structure ensures minimal prefix duplication.

Installation

IndexTrie is distributed using the Swift Package Manager. To add it to a project, add it as a dependency in your Package.swift manifest:

let package = Package(
    ...
    dependencies: [
        .package(url: "https://github.com/buza/indextrie.git", from: "0.1.0")
    ],
    ...
)

Then import IndexTrie wherever you’d like to use it:

import IndexTrie

More information on how to use the Swift Package Manager, can be found here.

Examples

Simple usage:

import IndexTrie

let indexTrie = IndexTrie<Int>()
indexTrie.addString("abc", index:1)
indexTrie.addString("ghi", index:2)
indexTrie.addString("gab", index:3)

var indices = indexTrie.getIndices("ab")!    // [1]
indices = indexTrie.getIndices("hi")!        // nil
indices = indexTrie.getIndices("g")!         // [2,3]

Lazy evaluation:

Populating the IndexTrie can be expensive. IndexTrie supports specifying a lazy limit, which is a maximum depth to generate nodes up until, saving the remaining suffix in the node that will be used to generate the remainder of the trie if a node at this depth is visited.

import IndexTrie

let lazyTrie = IndexTrie<Int>(lazyLimit: 2)
indexTrie.addString("abcd", index:1)

//This returns the same index result [1] as in the previous example,
//but the IndexTrie will generate the remaining subtree during the lookup.
let indices = indexTrie.getIndices("abc")!

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.