private static int findDataChunk( MJIEnv env, int this_ptr, long position, int chunk_size, FeatureExpr ctx) { ClassInfo dataRep = getDataRepresentationClassInfo(env, ctx); if (dataRep == null) { // will be reexecuted return 0; } // check if the file data is mapped, use mapped this_ptr if it exists this_ptr = getMapping(env, this_ptr, ctx); int prev_obj = MJIEnv.NULL; int cur_obj = env.getReferenceField(ctx, this_ptr, data_root).getValue(); long chunk_idx = position / chunk_size; while (cur_obj != MJIEnv.NULL && env.getLongField(cur_obj, chunk_index).getValue() < chunk_idx) { prev_obj = cur_obj; cur_obj = env.getReferenceField(ctx, cur_obj, next).getValue(); } if (cur_obj != MJIEnv.NULL && env.getLongField(cur_obj, chunk_index).getValue() == chunk_idx) { return cur_obj; } int result = env.newObject(ctx, dataRep); int int_array = env.newIntArray(chunk_size / INT_SIZE); env.setReferenceField(ctx, result, data, int_array); env.setLongField(ctx, result, chunk_index, new One<>(chunk_idx)); env.setReferenceField(ctx, result, next, cur_obj); if (prev_obj == MJIEnv.NULL) { env.setReferenceField(ctx, this_ptr, data_root, result); } else { env.setReferenceField(ctx, prev_obj, next, result); } return result; }
int appendString( FeatureExpr ctx, final MJIEnv env, final int objref, final Conditional<String> conditionalS) { Conditional<Integer> condAref = env.getReferenceField(ctx, objref, "value"); condAref.mapf( ctx, new VoidBiFunction<FeatureExpr, Integer>() { @Override public void apply(FeatureExpr ctx, final Integer aref) { conditionalS.mapf( ctx, new VoidBiFunction<FeatureExpr, String>() { @Override public void apply(FeatureExpr ctx, final String s) { final int slen = s.length(); final int alen = env.getArrayLength(ctx, aref); Conditional<Integer> count = env.getIntField(objref, "count"); count.mapf( ctx, new VoidBiFunction<FeatureExpr, Integer>() { @Override public void apply(FeatureExpr ctx, Integer count) { if (Conditional.isContradiction(ctx)) { return; } int i, j; int n = count + slen; if (n < alen) { for (i = count, j = 0; i < n; i++, j++) { env.setCharArrayElement(ctx, aref, i, new One<>(s.charAt(j))); } } else { int m = 3 * alen / 2; if (m < n) { m = n; } int arefNew = env.newCharArray(ctx, m); for (i = 0; i < count; i++) { env.setCharArrayElement( ctx, arefNew, i, env.getCharArrayElement(aref, i)); } for (j = 0; i < n; i++, j++) { env.setCharArrayElement(ctx, arefNew, i, new One<>(s.charAt(j))); } env.setReferenceField(ctx, objref, "value", arefNew); } env.setIntField(ctx, objref, "count", new One<>(n)); } }); } }); } }); return objref; }
@MJI public int delete__II__Ljava_lang_StringBuilder_2( final MJIEnv env, final int objref, final Integer beginIndex, final Integer endIndex, FeatureExpr ctx) { final Integer aref = env.getReferenceField(ctx, objref, "value").getValue(); Conditional<Integer> count = env.getIntField(objref, "count"); final int diff = endIndex - beginIndex; count.mapf( ctx, new VoidBiFunction<FeatureExpr, Integer>() { @Override public void apply(FeatureExpr ctx, Integer count) { for (int i = beginIndex, j = endIndex; i < count; i++, j++) { if (j < count) { env.setCharArrayElement(ctx, aref, i, env.getCharArrayElement(aref, j)); } else { env.setCharArrayElement(ctx, aref, i, new One<>(' ')); } } env.setIntField(ctx, objref, "count", new One<>(count - diff)); } }); return objref; }
private static byte getDataValue( MJIEnv env, int chunk_obj, long position, int chunk_size, FeatureExpr ctx) { int offset = (int) (position % chunk_size); int index = offset / INT_SIZE; int bit_shift = 8 * (offset % INT_SIZE); int int_array = env.getReferenceField(ctx, chunk_obj, data).getValue(); return (byte) (env.getIntArrayElement(int_array, index) >> bit_shift); }
@MJI public int substring__I__Ljava_lang_String_2( MJIEnv env, int objRef, int beginIndex, FeatureExpr ctx) { Integer aref = env.getReferenceField(ctx, objRef, "value").getValue(); char[] buf = env.getCharArrayObject(aref).getValue(); String obj = new String(buf); String result = obj.substring(beginIndex); return env.newString(ctx, result); }
public static int getName____Ljava_lang_String_2(MJIEnv env, int objRef) { FieldInfo fi = getFieldInfo(env, objRef); int nameRef = env.getReferenceField(objRef, "name"); if (nameRef == -1) { nameRef = env.newString(fi.getName()); env.setReferenceField(objRef, "name", nameRef); } return nameRef; }
@MJI public int getName____Ljava_lang_String_2(MJIEnv env, int objRef, FeatureExpr ctx) { FieldInfo fi = getFieldInfo(ctx, env, objRef); int nameRef = env.getReferenceField(ctx, objRef, "name").getValue(); if (nameRef == MJIEnv.NULL) { nameRef = env.newString(ctx, fi.getName()); env.setReferenceField(ctx, objRef, "name", nameRef); } return nameRef; }
private static void setDataValue( MJIEnv env, int chunk_obj, long position, byte data_value, int chunk_size, FeatureExpr ctx) { int offset = (int) (position % chunk_size); int index = offset / INT_SIZE; int bit_shift = 8 * (offset % INT_SIZE); int int_array = env.getReferenceField(ctx, chunk_obj, data).getValue(); int old_value = env.getIntArrayElement(int_array, index); env.setIntArrayElement( ctx, int_array, index, new One<>((old_value & ~(0xff << bit_shift)) | data_value << bit_shift)); }
@SuppressWarnings("deprecation") @MJI public Conditional<Integer> indexOf__Ljava_lang_String_2I__I( final MJIEnv env, int objref, final int str, final int fromIndex, FeatureExpr ctx) { Integer aref = env.getReferenceField(ctx, objref, "value").getValue(); Conditional<char[]> buf = env.getCharArrayObject(aref); return buf.mapf( ctx, new BiFunction<FeatureExpr, char[], Conditional<Integer>>() { @Override public Conditional<Integer> apply(FeatureExpr ctx, char[] buf) { String indexStr = env.getStringObject(ctx, str); return new One<>(new String(buf).indexOf(indexStr, fromIndex)); } }); }
@MJI public int substring__II__Ljava_lang_String_2( MJIEnv env, int objRef, final Conditional<Integer> beginIndex, final Conditional<Integer> endIndex, FeatureExpr ctx) { Integer aref = env.getReferenceField(ctx, objRef, "value").getValue(); Conditional<char[]> buf = env.getCharArrayObject(aref); Conditional<String> result = buf.mapf( ctx, new BiFunction<FeatureExpr, char[], Conditional<String>>() { @Override public Conditional<String> apply(FeatureExpr ctx, char[] buf) { String obj = new String(buf); return new One<>(obj.substring(beginIndex.getValue(), endIndex.getValue())); } }); return env.newString(ctx, result); }
@MJI public int toString____Ljava_lang_String_2(final MJIEnv env, final int objref, FeatureExpr ctx) { Conditional<Integer> aref = env.getReferenceField(ctx, objref, "value"); Conditional<String> s = aref.mapf( ctx, new BiFunction<FeatureExpr, Integer, Conditional<String>>() { @Override public Conditional<String> apply(FeatureExpr ctx, Integer aref) { Conditional<char[]> buf = env.getCharArrayObject(aref).simplify(ctx); return buf.mapf( ctx, new BiFunction<FeatureExpr, char[], Conditional<String>>() { @Override public Conditional<String> apply(FeatureExpr ctx, final char[] buf) { final Conditional<Integer> count = env.getIntField(objref, "count").simplify(ctx); return count.mapf( ctx, new BiFunction<FeatureExpr, Integer, Conditional<String>>() { @Override public Conditional<String> apply(FeatureExpr ctx, Integer count) { return new One<>(new String(buf, 0, count)); } }); } }); } }) .simplify(); return env.newString(ctx, s); }
// set the mapping during the constructor call @MJI public void setDataMap____V(MJIEnv env, int this_ptr, FeatureExpr ctx) { int fn_ptr = env.getReferenceField(ctx, this_ptr, "filename").getValue(); if (!File2DataMap.containsKey(new Integer(fn_ptr))) File2DataMap.put(new Integer(fn_ptr), new Integer(this_ptr)); }
// get the mapped object if one exists private static int getMapping(MJIEnv env, int this_ptr, FeatureExpr ctx) { int fn_ptr = env.getReferenceField(ctx, this_ptr, "filename").getValue(); Object o = File2DataMap.get(new Integer(fn_ptr)); if (o == null) return this_ptr; return ((Integer) o).intValue(); }