com.almondtools.util.map.CharIntMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stringsandchars Show documentation
Show all versions of stringsandchars Show documentation
Searching and Matching Strings and Chars with efficient algorithms.
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;
}
}
}
}