All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.metafacture.commons.tries.CharMap Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2013, 2014 Deutsche Nationalbibliothek
 *
 * Licensed under the Apache License, Version 2.0 the "License";
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.metafacture.commons.tries;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * A {@link Map} with char as key. Used for set matching, tries etc.
 *
 * Important: It is optimized for size in memory. No extra
 * information for fast entry/keySet/values iteration etc. is held.
 *
 * @param  type of the values in the map
 * @author Markus Michael Geipel
 */
final class CharMap implements Map {

    private static final int INITIAL_CAPACITY = 2;
    private static final float LOAD_FACTOR = 1f;
    private Entry[] table;

    private int size;

    @SuppressWarnings("unchecked")
    CharMap() {
        table = new Entry[INITIAL_CAPACITY];
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size() == 0;
    }

    @Override
    public boolean containsKey(final Object key) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean containsValue(final Object value) {
        throw new UnsupportedOperationException();
    }

    @Override
    public V get(final Object key) {
        if (key instanceof Character) {
            final Character beite = (Character) key;
            return get(beite.charValue());
        }
        return null;
    }

    public V get(final char key) {

        Entry entry = table[key % table.length];
        while (entry != null) {
            if (entry.getKeyChar() == key) {
                return entry.getValue();
            }
            entry = entry.getNext();
        }
        return null;
    }

    @Override
    public V put(final Character key, final V value) {
        put(key.charValue(), value);
        return null;
    }

    public void put(final char key, final V value) {
        if (size > LOAD_FACTOR * table.length) {
            expand();
        }
        put(table, key, value);
        ++size;
    }

    public void put(final Entry[] currentTable, final char key, final V value) {
        final Entry newEntry = new Entry(key, value);

        Entry entry = currentTable[key % currentTable.length];

        if (entry == null) {
            currentTable[key % currentTable.length] = newEntry;
        }
        else {
            while (entry.getNext() != null) {
                if (entry.getKeyChar() == key) {
                    throw new IllegalStateException("Key '" + key + "' already used");
                }
                entry = entry.getNext();
            }
            entry.setNext(newEntry);
        }
    }

    private void expand() {
        final int newSize = 2 * table.length;
        @SuppressWarnings("unchecked")
        final Entry[] newTable = new Entry[newSize];

        for (final Entry entry : table) {
            Entry temp = entry;
            while (temp != null) {
                put(newTable, temp.getKeyChar(), temp.getValue());
                temp = temp.getNext();
            }
        }

        table = newTable;
    }

    @Override
    public V remove(final Object key) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void putAll(final Map map) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Set keySet() {
        final Set keys = new HashSet();
        for (int i = 0; i < table.length; ++i) {
            Entry entry = table[i];
            while (entry != null) {
                keys.add(entry.getKey());
                entry = entry.getNext();
            }
        }
        return keys;
    }

    @Override
    public Collection values() {
        final Set values = new HashSet();
        for (int i = 0; i < table.length; ++i) {
            Entry entry = table[i];
            while (entry != null) {
                values.add(entry.getValue());
                entry = entry.getNext();
            }
        }
        return values;
    }

    @Override
    public Set> entrySet() {
        final Set> entries = new HashSet>();
        for (int i = 0; i < table.length; ++i) {
            Entry entry = table[i];
            while (entry != null) {
                entries.add(entry);
                entry = entry.getNext();
            }
        }
        return entries;
    }

    /**
     * Entry in the map.
     *
     * @param  type of the value of the entry.
     */
    private static final class Entry implements Map.Entry {
        private final char key;
        private V value;
        private Entry next;

        Entry(final char key, final V value) {
            this.key = key;
            this.value = value;
        }

        public Entry getNext() {
            return next;
        }

        public char getKeyChar() {
            return key;
        }

        public void setNext(final Entry next) {
            this.next = next;
        }

        @Override
        public Character getKey() {
            return Character.valueOf(key);
        }

        @Override
        public V getValue() {
            return value;
        }

        @Override
        public V setValue(final V newValue) {
            final V old = value;
            value = newValue;
            return old;
        }

        @Override
        public String toString() {
            return key + "=" + value;
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy