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

package ch.randelshofer.fastdoubleparser.chr;

import java.util.Arrays;

final class CharTrieNode
{
    private char[] chars;
    private CharTrieNode[] children;
    private boolean isEnd;
    
    public CharTrieNode() {
        this.chars = new char[0];
        this.children = new CharTrieNode[0];
    }
    
    public CharTrieNode insert(final char ch) {
        int index = this.indexOf(ch);
        if (index < 0) {
            index = this.chars.length;
            this.chars = Arrays.copyOf(this.chars, this.chars.length + 1);
            this.children = Arrays.copyOf(this.children, this.children.length + 1);
            this.chars[index] = ch;
            this.children[index] = new CharTrieNode();
        }
        return this.children[index];
    }
    
    public CharTrieNode get(final char ch) {
        final int index = this.indexOf(ch);
        return (index < 0) ? null : this.children[index];
    }
    
    private int indexOf(final char ch) {
        int index = -1;
        for (int i = 0; i < this.chars.length; ++i) {
            if (this.chars[i] == ch) {
                index = i;
            }
        }
        return index;
    }
    
    public void setEnd() {
        this.isEnd = true;
    }
    
    public boolean isEnd() {
        return this.isEnd;
    }
    
    public CharTrieNode insert(final char ch, final CharTrieNode forcedNode) {
        int index = this.indexOf(ch);
        if (index < 0) {
            index = this.chars.length;
            this.chars = Arrays.copyOf(this.chars, this.chars.length + 1);
            this.children = Arrays.copyOf(this.children, this.children.length + 1);
            this.chars[index] = ch;
            this.children[index] = forcedNode;
        }
        if (this.children[index] != forcedNode) {
            throw new AssertionError((Object)"trie is corrupt");
        }
        return this.children[index];
    }
}
