package ch.tkuhn.memetools;

import java.util.ArrayList;
import java.util.List;

public class IntStringMap {

	private static final int MAX_POS = 2000000000;

	private List<String> data = new ArrayList<String>();
	private long[] startPos;
	private int[] length;

	private StringBuilder sb = new StringBuilder();

	private boolean frozen = false;

	public IntStringMap(int size) {
		startPos = new long[size];
		length = new int[size];
		sb.append(" ");
	}

	public void put(int key, String value) {
		if (frozen) {
			throw new RuntimeException("Frozen");
		}
		long s = MAX_POS*data.size() + sb.length();
		int l = value.length();
		sb.append(value);
		startPos[key] = s;
		length[key] = l;
		if (sb.length() > MAX_POS) {
			data.add(sb.toString());
			sb = new StringBuilder();
		}
	}

	public String get(int key) {
		if (!frozen) {
			throw new RuntimeException("Not yet frozen");
		}
		long s = startPos[key];
		if (s == 0) return null;
		int l = length[key];
		int subpos = (int) (s % MAX_POS);
		return data.get((int) (s / MAX_POS)).substring(subpos, subpos + l);
	}

	public void freeze() {
		frozen = true;
		data.add(sb.toString());
		sb = null;
	}

}

@@ -559,136 +559,30 @@
 %7D%0A%09%09
-long s = MAX_POS*data.size() + sb.length();%0A%09%09int l = value.length();%0A%09%09sb.append(value);%0A%09%09startPos%5Bkey%5D = s;%0A%09%09length%5Bkey%5D = l
+int l = value.length()
 ;%0A%09%09
@@ -597,17 +597,22 @@
 ength() 
-%3E
++ l %3E=
  MAX_POS
@@ -676,16 +676,122 @@
 ();%0A%09%09%7D%0A
+%09%09long s = MAX_POS*data.size() + sb.length();%0A%09%09sb.append(value);%0A%09%09startPos%5Bkey%5D = s;%0A%09%09length%5Bkey%5D = l;%0A
 %09%7D%0A%0A%09pub

Fix problems of IntStringMap