Git Product home page Git Product logo

triehard's Introduction

TrieHard

Stable

A generic Trie implementation in Java. TrieHard comes ready to create Tries of many types:
String, char[], byte[], int[], short[], long[], and java.nio.ByteBuffer

Code Example

Trie<String, Boolean> t = Tries.forStrings();

// Include
t.put( "java.lang.", true );
t.put( "java.io.", true );
t.put( "java.util.concurrent.", true );

// Exclude
t.put( "java.util.", false );
t.put( "java.lang.Boolean", false );

assertTrue( t.get( "java.lang.Integer" ) );
assertTrue( t.get( "java.lang.Long" ) );
assertFalse( t.get( "java.lang.Boolean" ) );
assertTrue( t.get( "java.io.InputStream" ) );
assertFalse( t.get( "java.util.ArrayList" ) );
assertTrue( t.get( "java.util.concurrent.ConcurrentHashMap" ) );

Performance

You can insert millions of keys/values into TrieHard in a second (average insert on all dictionary words is 300-400 nanoseconds) as well as retrieve millions of values in a second (average retrieval is 200-300 nanoseconds).

How does it work compared to other Tries?

A typical Trie implementation has an element (i.e. character) per node (a non-compact structure). The Trie implementation in this library is a compact Trie which saves space and is just as efficient.

What are the matching options and how do they work?

Given a Trie { "java.io." => 23 }...

  1. EXACT
    Only an equivalent "java.io." will result in 23.
  2. STARTS_WITH
    Any superset or equivalent of "java.io." will result in 23. I.E. "java.io.InputStream" is a STARTS_WITH match to the Trie.
  3. PARTIAL
    Any subset, superset, or equivalent of "java.io." will result in 23. I.E. "java" is a PARTIAL match to the Trie.

Builds

Use Cases

Auto-Complete

Trie<String, Integer> t = Tries.forInsensitiveStrings();
t.setDefaultMatch( TrieMatch.PARTIAL );
// Add all available values to the Trie
t.put( "world", 23 );
t.put( "worm", 45 );
t.put( "worry", 76 );
t.put( "why", -89 );
t.put( "women", 123 );
...
// Given user input, what are possible keys & values?
String userInput = "WO";
Set<Entry<String, Integer>> possible = t.nodes( userInput );
// possible = { world=>23, worm=>45, worry=>76, women=>123 }
...
// Use possible to display full keys and their values.

IP to Host Mapping

Trie<byte[], String> mapper = Tries.forBytes();
mapper.setDefaultMatch( TrieMatch.EXACT );
...
mapper.put( socketAddress.getAddress(), "google.com" );
...

// Given an IP, get the host name
String host = mapper.get( socketAddress.getAddress() );

To see other use cases, request one!

How do I create my own Trie type?

Implement the following interface and pass it into the constructor of Trie.

public interface TrieSequencer<S> 
{
   public int matches(S sequenceA, int indexA, S sequenceB, int indexB, int count);
   public int lengthOf(S sequence);
   public int hashOf(S sequence, int index);
}

triehard's People

Contributors

clickermonkey avatar hrgdavor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

triehard's Issues

Trie$AbstractIterator.findNext - Null pointer exception

The following line (Trie$AbstractIterator.findNext line1006):
if (indices[0] == root.children.capacity())
will throw an exception if root.children is null
Seems like just changing it into:
if (root.children == null || indices[0] == root.children.capacity())
will solve it

Store multiple values for one key in a Trie

Is there an extension similar to the put method, there I can add additional values in a java collection.
I know I can implement it for my own needs, but I think it would be good general extension for this project.

Something like TrieList<S, ArrayList<T>> with an append method which appends new entries to the list:

public T append( S query, T value )

and a get method which returns the list:

public ArrayList<T> get( Object sequence )

Can't find items in the trie

The following code return 2 options for the opt1 variable, 2 options for opt2 but ZERO options for opt3.
Seems like the code building the trie doesn't assume you can add term like "c" and "c d e" without putting "c d" inside as well.

    Trie<String, Integer> trie = Tries.forInsensitiveStrings();

    trie.setDefaultMatch(TrieMatch.STARTS_WITH);

    trie.put("a", 1);
    trie.put("a b", 2);
    trie.put("c", 3);
    trie.put("c d e", 4);

    Set<Map.Entry<String, Integer>> opt1 = trie.entrySet("a");
    Set<Map.Entry<String, Integer>> opt2 = trie.entrySet("c");
    Set<Map.Entry<String, Integer>> opt3 = trie.entrySet("c d");

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.