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

package ch.randelshofer.fastdoubleparser.bte;

import java.util.Arrays;

final class ByteTrieNode
{
    private byte[] chars;
    private ByteTrieNode[] children;
    private boolean isEnd;
    
    public ByteTrieNode() {
        this.chars = new byte[0];
        this.children = new ByteTrieNode[0];
    }
    
    public ByteTrieNode insert(final byte 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 ByteTrieNode();
        }
        return this.children[index];
    }
    
    public ByteTrieNode insert(final byte ch, final ByteTrieNode 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];
    }
    
    public ByteTrieNode get(final byte ch) {
        final int index = this.indexOf(ch);
        return (index < 0) ? null : this.children[index];
    }
    
    private int indexOf(final byte 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;
    }
}
