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

org.jcodec.common.IntIntMap Maven / Gradle / Ivy

There is a newer version: 0.2.5
Show newest version
package org.jcodec.common;

import static java.lang.Integer.MIN_VALUE;

import java.util.Arrays;

/**
 * This class is part of JCodec ( www.jcodec.org ) This software is distributed
 * under FreeBSD License
 * 
 * @author Jay Codec
 * 
 */
public class IntIntMap {

    private static final int GROW_BY = 128;
    private int[] storage;
    private int size;

    public IntIntMap() {
        this.storage = createArray(GROW_BY);
        Arrays.fill(this.storage, MIN_VALUE);
    }

    public void put(int key, int val) {
        if (val == MIN_VALUE)
            throw new IllegalArgumentException("This implementation can not store " + MIN_VALUE);
        if (storage.length <= key) {
            int[] ns = createArray(storage.length + GROW_BY);
            System.arraycopy(storage, 0, ns, 0, storage.length);
            Arrays.fill(ns, storage.length, ns.length, MIN_VALUE);
            storage = ns;
        }
        if (storage[key] == MIN_VALUE)
            size++;
        storage[key] = val;
    }

    public int get(int key) {
        return key >= storage.length ? null : storage[key];
    }

    public int[] keys() {
        int[] result = new int[size];
        for (int i = 0, r = 0; i < storage.length; i++) {
            if (storage[i] != MIN_VALUE)
                result[r++] = i;
        }
        return result;
    }

    public void clear() {
        for (int i = 0; i < storage.length; i++)
            storage[i] = MIN_VALUE;
        size = 0;
    }

    public int size() {
        return size;
    }

    public void remove(int key) {
        if (storage[key] != Integer.MIN_VALUE)
            size--;
        storage[key] = MIN_VALUE;
    }

    public int[] values() {
        int[] result = createArray(size);
        for (int i = 0, r = 0; i < storage.length; i++) {
            if (storage[i] != MIN_VALUE)
                result[r++] = storage[i];
        }
        return result;
    }

    private int[] createArray(int size) {
        return new int[size];
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy