com.googlecode.concurrenttrees.radix.ConcurrentRadixTree Java Examples

The following examples show how to use com.googlecode.concurrenttrees.radix.ConcurrentRadixTree. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConcurrentSuffixTree.java    From concurrent-trees with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Iterable<KeyValuePair<O>> getKeyValuePairsForKeysEndingWith(final CharSequence suffix) {
    return new Iterable<KeyValuePair<O>>() {
        @Override
        public Iterator<KeyValuePair<O>> iterator() {
            return new LazyIterator<KeyValuePair<O>>() {
                Iterator<String> originalKeys = nullSafeIterator(radixTree.getValueForExactKey(suffix));

                @Override
                protected KeyValuePair<O> computeNext() {
                    String originalKey = null;
                    O value = null;
                    while (value == null) {
                        if (!originalKeys.hasNext()) {
                            return endOfData();
                        }
                        originalKey = originalKeys.next();
                        value = valueMap.get(originalKey);
                    }
                    return new ConcurrentRadixTree.KeyValuePairImpl<O>(originalKey, value);
                }
            };
        }
    };
}
 
Example #2
Source File: RadixTreeUsage.java    From concurrent-trees with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    RadixTree<Integer> tree = new ConcurrentRadixTree<Integer>(new DefaultCharArrayNodeFactory());

    tree.put("TEST", 1);
    tree.put("TOAST", 2);
    tree.put("TEAM", 3);

    System.out.println("Tree structure:");
    // PrettyPrintable is a non-public API for testing, prints semi-graphical representations of trees...
    PrettyPrinter.prettyPrint((PrettyPrintable) tree, System.out);

    System.out.println();
    System.out.println("Value for 'TEST' (exact match): " + tree.getValueForExactKey("TEST"));
    System.out.println("Value for 'TOAST' (exact match): " + tree.getValueForExactKey("TOAST"));
    System.out.println();
    System.out.println("Keys starting with 'T': " + Iterables.toString(tree.getKeysStartingWith("T")));
    System.out.println("Keys starting with 'TE': " + Iterables.toString(tree.getKeysStartingWith("TE")));
    System.out.println();
    System.out.println("Values for keys starting with 'TE': " + Iterables.toString(tree.getValuesForKeysStartingWith("TE")));
    System.out.println("Key-Value pairs for keys starting with 'TE': " + Iterables.toString(tree.getKeyValuePairsForKeysStartingWith("TE")));
    System.out.println();
    System.out.println("Keys closest to 'TEMPLE': " + Iterables.toString(tree.getClosestKeys("TEMPLE")));
}
 
Example #3
Source File: BuildShakespeareWordRadixTree.java    From concurrent-trees with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    ConcurrentRadixTree<WordValue> tree = new ConcurrentRadixTree<WordValue>(new DefaultCharArrayNodeFactory());
    for (String file : files) {
        Set<String> wordsInFile = IOUtil.loadWordsFromTextFileOnClasspath(file, true); // true = convert to lowercase
        for (String word : wordsInFile) {
            WordValue wordValue = tree.getValueForExactKey(word);
            if (wordValue == null) {
                wordValue = new WordValue(word);
                tree.put(word, wordValue); // not using concurrency support here
            }
            wordValue.manuscriptsContainingWord.add(file.replaceAll("/.*/.*/", "").replace(".txt", ""));
        }
    }

    final String radixTreePrinted = PrettyPrinter.prettyPrint(tree);
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JTextArea textArea = new JTextArea();
            textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 14));
            textArea.setText(radixTreePrinted);
            JScrollPane scrollPane = new JScrollPane(textArea);
            textArea.setEditable(false);
            JFrame frame = new JFrame("Shakespeare Radix Tree");
            frame.add(scrollPane);
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setSize(640, 480);
            frame.setVisible(true);
        }
    });

}
 
Example #4
Source File: IterablesUsage.java    From concurrent-trees with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    RadixTree<Integer> tree = new ConcurrentRadixTree<Integer>(new DefaultCharArrayNodeFactory());

    tree.put("TEST", 1);
    tree.put("TOAST", 2);
    tree.put("TEAM", 3);

    Iterable<CharSequence> keysStartingWithT    = tree.getKeysStartingWith("T");

    List<CharSequence> listOfKeysStartingWithT  = Iterables.toList  (keysStartingWithT);
    Set<CharSequence> setOfKeysStartingWithT    = Iterables.toSet   (keysStartingWithT);
    String toStringOfKeysStartingWithT          = Iterables.toString(keysStartingWithT);

    System.out.println("Keys starting with 'T': " + toStringOfKeysStartingWithT);
}
 
Example #5
Source File: RadixTreeIndex.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * Package-private constructor, used by static factory methods.
 */
protected RadixTreeIndex(Attribute<O, A> attribute, NodeFactory nodeFactory) {
    super(attribute, new HashSet<Class<? extends Query>>() {{
        add(Equal.class);
        add(In.class);
        add(StringStartsWith.class);
    }});
    this.nodeFactory = nodeFactory;
    this.tree = new ConcurrentRadixTree<StoredResultSet<O>>(nodeFactory);
}
 
Example #6
Source File: RadixTreeIndex.java    From cqengine with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void clear(QueryOptions queryOptions) {
    this.tree = new ConcurrentRadixTree<StoredResultSet<O>>(new DefaultCharArrayNodeFactory());
}
 
Example #7
Source File: TrieMemIndex.java    From sasi with Apache License 2.0 5 votes vote down vote up
private ConcurrentPrefixTrie(ColumnDefinition column)
{
    super(column);
    trie = new ConcurrentRadixTree<>(new SmartArrayBasedNodeFactory());
}
 
Example #8
Source File: IpConcurrentRadixTree.java    From onos with Apache License 2.0 5 votes vote down vote up
@Override
public void clear() {
    ipv4Tree = new ConcurrentRadixTree<>(new DefaultCharArrayNodeFactory());
    ipv6Tree = new ConcurrentRadixTree<>(new DefaultCharArrayNodeFactory());
}
 
Example #9
Source File: AutoCompleteProvider.java    From openvisualtraceroute with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Clear history
 */
public void clear() {
	_map.clear();
	_tree = new ConcurrentRadixTree<>(new DefaultByteArrayNodeFactory());
}
 
Example #10
Source File: ConcurrentSuffixTree.java    From concurrent-trees with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public Iterable<KeyValuePair<O>> getKeyValuePairsForKeysContaining(final CharSequence fragment) {
    return new Iterable<KeyValuePair<O>>() {
        @Override
        public Iterator<KeyValuePair<O>> iterator() {
            return new LazyIterator<KeyValuePair<O>>() {

                Iterator<Set<String>> originalKeysSets = radixTree.getValuesForKeysStartingWith(fragment).iterator();
                Iterator<String> keyIterator = Collections.<String>emptyList().iterator();

                // A given fragment can be contained many times within the same key, so track keys processed
                // so far, so that we can avoid re-processing the same key multiple times...
                Set<String> keysAlreadyProcessed = new HashSet<String>();

                @Override
                protected KeyValuePair<O> computeNext() {
                    String originalKey = null;
                    O value = null;
                    while (value == null) {
                        while (!keyIterator.hasNext()) {
                            if (!originalKeysSets.hasNext()) {
                                return endOfData();
                            }
                            keyIterator = originalKeysSets.next().iterator();
                        }
                        originalKey = keyIterator.next();

                        if (keysAlreadyProcessed.add(originalKey)) {
                            // Key was not in the already-processed set, so proceed with looking up the value...
                            value = valueMap.get(originalKey);

                            // value could still be null due to race condition if key/value was removed while
                            // iterating, hence if so, we loop again to find the next non-null key/value...
                        }
                    }
                    return new ConcurrentRadixTree.KeyValuePairImpl<O>(originalKey, value);
                }
            };
        }
    };
}