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

com.almondtools.util.map.CharIntMap Maven / Gradle / Ivy

There is a newer version: 0.2.22
Show newest version
package com.almondtools.util.map;

import java.util.Map;

public class CharIntMap {

	private HashFunction h;
	private char[] keys;
	private int[] values;
	private int defaultValue;

	public CharIntMap(HashFunction h, Map map, Integer defaultValue) {
		this.h = h;
		this.defaultValue = defaultValue;
		computeKeysAndValues(map);
	}

	private void computeKeysAndValues(Map map) {
		int len = map.size();
		if (len == 0) {
			keys = new char[1];
			values = new int[1];
			values[0] = defaultValue;
		} else {
			keys = new char[len];
			values = new int[len];
			for (Map.Entry entry : map.entrySet()) {
				char key = entry.getKey();
				int value = entry.getValue();

				int i = h.hash(key);

				keys[i] = key;
				values[i] = value;
			}
		}
	}

	public char[] keys() {
		return keys;
	}

	public int get(char value) {
		int i = h.hash(value);
		if (i >= keys.length) {
			return defaultValue;
		} else if (keys[i] == value) {
			return values[i];
		} else {
			return defaultValue;
		}
	}

	public int getDefaultValue() {
		return defaultValue;
	}

	public static class Builder extends MinimalPerfectMapBuilder {

		public Builder(Integer defaultValue) {
			super(defaultValue);
		}

		@Override
		public CharIntMap perfectMinimal() {
			try {
				computeFunctions(100, 1.15);
				return new CharIntMap(getH(), getEntries(), getDefaultValue());
			} catch (HashBuildException e) {
				return new Fallback(getEntries(), getDefaultValue());
			}
		}

	}

	private static class Fallback extends CharIntMap {

		private Map map;

		public Fallback(Map map, Integer defaultValue) {
			super(new HashFunction(new int[] { 0 }, 0, 0), map, defaultValue);
			this.map = map;
		}

		@Override
		public int get(char key) {
			Integer value = map.get(key);
			if (value == null) {
				return getDefaultValue();
			} else {
				return value;
			}
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy