sclass SortedStringMap_managed extends AbstractManagedObject { replace Addr with int. new TreeMap map; int physicalLength; // total array length is twice this. data is: String *key, String *value, ... int size; // our actual length // it's actually a union class Entry { int index; // also when not loaded S value; // only when loaded *(int *index) {} *(int *index, S *value) {} S value() { if (value == null) value = mem.readString(valueAddr(index)); ret value; } } int size() { ret l(map); } int arrayStart() { ret addr+2; } int keyAddr(int i) { ret arrayStart()+i*2; } int valueAddr(int i) { ret arrayStart()+i*2+1; } // read from memory *(ManagedIntObjects_v1 *mem, Addr *addr) { physicalLength = mem.get(addr); size = mem.get(addr+1); for i to size: map.put(mem.readString(keyAddr(i)), new Entry(valueAddr(i))); } *(ManagedIntObjects_v1 *mem, SS initialData) { SS sorted; if (initialData instanceof TreeMap) { map.setComparator(initialData.comparator()); sorted = initialData; } else sorted = new TreeMap(unnull(initialData), map.comparator()); physicalLength = l(sorted); alloc(objectSize()); mem.set(addr, physicalLength); int i = 0; for (S key, S value : sorted) map.put(key, new Entry(i++, value)); flush(); } void flush { ensureCapacityInternal(size()); mem.set(addr+1, size()); int i = 0; for (S key, Entry entry : map) { int keyPtr = mem.get(keyAddr(i)); if (!eq(mem.readString(keyPtr), key)) { mem.freeString(keyAddr(i)); // strings belong to map mem.set(keyAddr(i), mem.newString(key)); } int valuePtr = mem.get(valueAddr(i)); if (entry.index != i || entry.value != null && !eq(entry.value if (!eq(mem.readString(valuePtr), entry.value())) { mem.freeString(valueAddr(i)); // strings belong to map mem.set(valueAddr(i), mem.newString(key)); } map.put(key, new Entry(i++, value)); } } void ensureCapacityInternal(int minCapacity) { if (physicalLength < minCapacity) grow(minCapacity); } private void grow(int minCapacity) { int oldCapacity = physicalLength; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; long oldAddr = addr, oldSize = objectSize(); physicalLength = newCapacity; setAddr(mem.alloc(objectSize())); for (int i = 1; i < oldSize; i++) mem.set(addr+i, mem.get(oldAddr+i)); mem.free(oldAddr, oldSize); mem.set(addr, physicalLength); } // GC handling public void scanForCollection(IManagedObjectCollector gc) { gc.noteObject(addr, objectSize()); } int objectSize() { ret 2+physicalLength*2; } }