FYI, i have come up with a few options.
the simplest way is to write the ArrayList to a simple array of objects using toArray like this:
static TTEvt evArray[];
public static void addUndoCheckpoint() {
long start = System.currentTimeMillis();
evArray = Doc.mEvList.toArray(new TTEvt[Doc.mEvList.size()]);
long end = System.currentTimeMillis();
Log.d("MainView", "addUndoCheckpoint time="+(end-start)+"mS");
}
public static void doUndo() {
Doc.mEvList.clear();
for(TTEvt ev : evArray)
Doc.mEvList.add(ev);
forceTotalRedraw();
}
UPDATE: i've just figured out the above code isn't really working because toArray does a new on an array of reference to objects only, NOT the objects them selves. so i would additional need to clone all the objects too which is obviously a lot of memory, and probably time. maybe the answer is the below, the slow option, using serialization but do it in an sync thread so its doesn't slow the UI down.
a more complicated way to go use gzip compression to save space like this
static byte[] undoBuf;
public static void addUndoCheckpoint() {
long start = System.currentTimeMillis();
//evArray = Doc.mEvList.toArray(new TTEvt[Doc.mEvList.size()]);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
ObjectOutputStream objectOut = new ObjectOutputStream(gzipOut);
for(TTEvt ev: Doc.mEvList)
objectOut.writeObject(ev);
objectOut.close();
undoBuf = baos.toByteArray();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long end = System.currentTimeMillis();
Log.d("MainView", "addUndoCheckpoint time="+(end-start)+"mS");
}
public static void doUndo() {
Doc.mEvList.clear();
//for(TTEvt ev : evArray)
// Doc.mEvList.add(ev);
try {
ByteArrayInputStream bais = new ByteArrayInputStream(undoBuf);
GZIPInputStream gzipIn;
gzipIn = new GZIPInputStream(bais);
ObjectInputStream objectIn = new ObjectInputStream(gzipIn);
while(objectIn.available()>0) {
TTEvt ev = (TTEvt) objectIn.readObject();
Doc.mEvList.add(ev);
}
objectIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
forceTotalRedraw();
}
the problem with this is its slow, almost 1 second for a data struct with about 5000 entries
this is only a 1 level undo at the moment, currently implementing a stack to keep multiple levels, and looking for a faster in memory compress