// Maximally compact implementation of such a thing
// Use SortedArrayBasedMap instead if keys are sortable
sclass MinimalMap extends CompactAbstractMap {
O[] entries;
*() {}
*(Map map) {
if (empty(map)) ret;
entries = new O[l(map)*2];
int i = 0;
for (A a, B b : map) {
entries[i++] = a;
entries[i++] = b;
}
}
public int size() { ret l(entries)/2; }
public Set> entrySet() {
ret toLinkedHashMap().entrySet();
}
int find(O o) {
if (entries != null)
for (int i = 0; i < entries.length; i += 2)
if (eq(entries[i], o))
ret i;
ret -1;
}
public bool containsKey(O o) {
ret find(o) >= 0;
}
@Override
public B get(O o) {
int i = find(o);
ret i >= 0 ? (B) entries[i+1] : null;
}
LinkedHashMap toLinkedHashMap() {
new LinkedHashMap map;
if (entries != null)
for (int i = 0; i < entries.length; i += 2)
map.put((A) entries[i], (B) entries[i+1]);
ret map;
}
@Override
public B put(A key, B value) {
int i = find(key);
// existing key?
if (i >= 0) {
B oldValue = (B) entries[i+1];
entries[i+1] = value;
ret oldValue;
}
// new key
int l = l(entries);
entries = resizeObjectArray(entries, l+2);
entries[l] = key;
entries[l+1] = value;
null;
}
@Override
public B remove(O key) {
int i = find(key);
if (i < 0) null;
B oldValue = (B) entries[i+1];
int l = l(entries);
entries = spliceObjectArray(entries, i, l+2, null);
ret oldValue;
}
}