// 
// Decompiled by Procyon v0.6.0
// 

package ch.randelshofer.fastdoubleparser.chr;

import java.util.Iterator;
import java.util.Set;

final class CharTrieOfFew implements CharTrie
{
    private CharTrieNode root;
    
    public CharTrieOfFew(final Set<String> set) {
        this.root = new CharTrieNode();
        for (final String str : set) {
            if (!str.isEmpty()) {
                this.add(str);
            }
        }
    }
    
    private void add(final String str) {
        CharTrieNode node = this.root;
        for (int i = 0; i < str.length(); ++i) {
            node = node.insert(str.charAt(i));
        }
        node.setEnd();
    }
    
    @Override
    public int match(final CharSequence str, final int startIndex, final int endIndex) {
        CharTrieNode node = this.root;
        int longestMatch = startIndex;
        for (int i = startIndex; i < endIndex; ++i) {
            node = node.get(str.charAt(i));
            if (node == null) {
                break;
            }
            longestMatch = (node.isEnd() ? (i + 1) : longestMatch);
        }
        return longestMatch - startIndex;
    }
    
    @Override
    public int match(final char[] str, final int startIndex, final int endIndex) {
        CharTrieNode node = this.root;
        int longestMatch = startIndex;
        for (int i = startIndex; i < endIndex; ++i) {
            node = node.get(str[i]);
            if (node == null) {
                break;
            }
            longestMatch = (node.isEnd() ? (i + 1) : longestMatch);
        }
        return longestMatch - startIndex;
    }
}
