// you can arrange the entries in a self-chosen order
// so it's like a very compact LinkedHashMap
// Only good for very small maps
sclass SmallArrayBasedMap extends CompactAbstractMap {
O[] entries; // key, value, ... (any order of entries)
*() {}
*(Map map) {
int n = l(map);
if (n == 0) ret;
entries = new O[n*2];
int i = 0;
fOr (A key, B value : map) {
entries[i++] = key;
entries[i++] = value;
}
}
public int size() { ret l(entries)/2; }
public Set> entrySet() {
ret new AbstractSet>() {
public int size() { ret SmallArrayBasedMap.this.size(); }
public Iterator> iterator() {
ret new Iterator>() {
int idx;
public bool hasNext() {
ret idx < l(entries);
}
public Map.Entry next() {
SimpleEntry e = new SimpleEntry((A) entries[idx], (B) entries[idx+1]);
idx += 2;
ret e;
}
};
}
};
}
// returns actual index in entries array
int find(O key) {
int n = l(entries);
//if (n == 0) ret -1;
//int hash = _hashCode(key);
for (int i = 0; i < n; i += 2)
//if (_hashCode(entries[i]) == hash
if (eq(key, entries[i]))
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;
}
}