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

package ch.randelshofer.fastdoubleparser.chr;

import java.util.Iterator;
import java.util.Collection;

final class CharToIntMap implements CharDigitSet, CharSet
{
    private Node[] table;
    
    public CharToIntMap(final Collection<Character> chars) {
        this(chars.size());
        int i = 0;
        for (final char ch : chars) {
            this.put(ch, i++);
        }
    }
    
    @Override
    public boolean containsKey(final char key) {
        return this.getOrDefault(key, -1) >= 0;
    }
    
    @Override
    public int toDigit(final char ch) {
        return this.getOrDefault(ch, 10);
    }
    
    public CharToIntMap(final int maxSize) {
        final int n = (-1 >>> Integer.numberOfLeadingZeros(maxSize * 2)) + 1;
        this.table = new Node[n];
    }
    
    public void put(final char key, final int value) {
        final int index = this.getIndex(key);
        Node found = this.table[index];
        if (found == null) {
            this.table[index] = new Node(key, value);
        }
        else {
            while (found.next != null && found.key != key) {
                found = found.next;
            }
            if (found.key == key) {
                found.value = value;
            }
            else {
                found.next = new Node(key, value);
            }
        }
    }
    
    private int getIndex(final char key) {
        return key & this.table.length - 1;
    }
    
    public int getOrDefault(final char key, final int defaultValue) {
        final int index = this.getIndex(key);
        for (Node found = this.table[index]; found != null; found = found.next) {
            if (found.key == key) {
                return found.value;
            }
        }
        return defaultValue;
    }
    
    private static class Node
    {
        char key;
        int value;
        Node next;
        
        public Node(final char key, final int value) {
            this.key = key;
            this.value = value;
        }
    }
}
