int[] fromEnd, int fromRatio2) {\r
int startpos, endpos;\r
startpos = fromStart[0]; // first position in fromStart\r
- endpos = fromEnd[0] + fromEnd[2] * (fromRatio2 - 1); // last position in\r
- // fromEnd\r
+ endpos = fromEnd[0]; // last position in fromEnd\r
+ int endindx = (fromRatio2 - 1); // additional positions to get to last\r
+ // position from endpos\r
int intv = 0, intvSize = fromShifts2.size();\r
- int iv[], i = 0, fs = -1, fe = -1; // containing intervals\r
- while (intv < intvSize && (fs == -1 || fe == -1)) {\r
+ int iv[], i = 0, fs = -1, fe_s = -1, fe = -1; // containing intervals\r
+ // search intervals to locate ones containing startpos and count endindx\r
+ // positions on from endpos\r
+ while (intv < intvSize && (fs == -1 || fe == -1))\r
+ {\r
iv = (int[]) fromShifts2.elementAt(intv++);\r
- if (iv[0] <= iv[1]) {\r
- if (fs == -1 && startpos >= iv[0] && startpos <= iv[1]) {\r
+ if (fe_s > -1)\r
+ {\r
+ endpos = iv[0]; // start counting from beginning of interval\r
+ endindx--; // inclusive of endpos\r
+ }\r
+ if (iv[0] <= iv[1])\r
+ {\r
+ if (fs == -1 && startpos >= iv[0] && startpos <= iv[1])\r
+ {\r
fs = i;\r
}\r
- if (fe == -1 && endpos >= iv[0] && endpos <= iv[1]) {\r
+ if (endpos >= iv[0] && endpos <= iv[1])\r
+ {\r
+ if (fe_s == -1)\r
+ {\r
+ fe_s = i;\r
+ }\r
+ if (fe_s != -1)\r
+ {\r
+ if (endpos + endindx <= iv[1])\r
+ {\r
fe = i;\r
+ endpos = endpos + endindx; // end of end token is within this\r
+ // interval\r
}\r
- } else {\r
- if (fs == -1 && startpos <= iv[0] && startpos >= iv[1]) {\r
+ else\r
+ {\r
+ endindx -= iv[1] - endpos; // skip all this interval too\r
+ }\r
+ }\r
+ }\r
+ }\r
+ else\r
+ {\r
+ if (fs == -1 && startpos <= iv[0] && startpos >= iv[1])\r
+ {\r
fs = i;\r
}\r
- if (fe == -1 && endpos <= iv[0] && endpos >= iv[1]) {\r
+ if (endpos <= iv[0] && endpos >= iv[1])\r
+ {\r
+ if (fe_s == -1)\r
+ {\r
+ fe_s = i;\r
+ }\r
+ if (fe_s != -1)\r
+ {\r
+ if (endpos - endindx >= iv[1])\r
+ {\r
fe = i;\r
+ endpos = endpos - endindx; // end of end token is within this\r
+ // interval\r
+ }\r
+ else\r
+ {\r
+ endindx -= endpos - iv[1]; // skip all this interval too\r
+ }\r
+ }\r
}\r
}\r
i++;\r
if (fs == fe && fe == -1)\r
return null;\r
Vector ranges = new Vector();\r
- if (fs <= fe) {\r
+ if (fs <= fe)\r
+ {\r
intv = fs;\r
i = fs;\r
// truncate initial interval\r
iv = (int[]) fromShifts2.elementAt(intv++);\r
- iv = new int[] { iv[0], iv[1] };// clone\r
+ iv = new int[]\r
+ { iv[0], iv[1] };// clone\r
if (i == fs)\r
iv[0] = startpos;\r
- while (i != fe) {\r
+ while (i != fe)\r
+ {\r
ranges.addElement(iv); // add initial range\r
iv = (int[]) fromShifts2.elementAt(intv++); // get next interval\r
- iv = new int[] { iv[0], iv[1] };// clone\r
+ iv = new int[]\r
+ { iv[0], iv[1] };// clone\r
i++;\r
}\r
if (i == fe)\r
iv[1] = endpos;\r
ranges.addElement(iv); // add only - or final range\r
- } else {\r
+ }\r
+ else\r
+ {\r
// walk from end of interval.\r
i = fromShifts2.size() - 1;\r
- while (i > fs) {\r
+ while (i > fs)\r
+ {\r
i--;\r
}\r
iv = (int[]) fromShifts2.elementAt(i);\r
- iv = new int[] { iv[1], iv[0] };// reverse and clone\r
+ iv = new int[]\r
+ { iv[1], iv[0] };// reverse and clone\r
// truncate initial interval\r
- if (i == fs) {\r
+ if (i == fs)\r
+ {\r
iv[0] = startpos;\r
}\r
- while (--i != fe) { // fix apparent logic bug when fe==-1\r
+ while (--i != fe)\r
+ { // fix apparent logic bug when fe==-1\r
ranges.addElement(iv); // add (truncated) reversed interval\r
iv = (int[]) fromShifts2.elementAt(i);\r
- iv = new int[] { iv[1], iv[0] }; // reverse and clone\r
+ iv = new int[]\r
+ { iv[1], iv[0] }; // reverse and clone\r
}\r
- if (i == fe) {\r
+ if (i == fe)\r
+ {\r
// interval is already reversed\r
iv[1] = endpos;\r
}\r
}\r
// create array of start end intervals.\r
int[] range = null;\r
- if (ranges != null && ranges.size() > 0) {\r
+ if (ranges != null && ranges.size() > 0)\r
+ {\r
range = new int[ranges.size() * 2];\r
intv = 0;\r
intvSize = ranges.size();\r
i = 0;\r
- while (intv < intvSize) {\r
+ while (intv < intvSize)\r
+ {\r
iv = (int[]) ranges.elementAt(intv);\r
range[i++] = iv[0];\r
range[i++] = iv[1];\r
* @param fromS\r
* @param fromE\r
*/\r
- public static void testMap(MapList ml, int fromS, int fromE) {\r
- for (int from = 1; from <= 25; from++) {\r
+ public static void testMap(MapList ml, int fromS, int fromE)\r
+ {\r
+ for (int from = 1; from <= 25; from++)\r
+ {\r
int[] too = ml.shiftFrom(from);\r
System.out.print("ShiftFrom(" + from + ")==");\r
- if (too == null) {\r
+ if (too == null)\r
+ {\r
System.out.print("NaN\n");\r
- } else {\r
+ }\r
+ else\r
+ {\r
System.out.print(too[0] + " % " + too[1] + " (" + too[2] + ")");\r
System.out.print("\t+--+\t");\r
int[] toofrom = ml.shiftTo(too[0]);\r
- if (toofrom != null) {\r
- if (toofrom[0] != from) {\r
- System.err.println("Mapping not reflexive:" + from + " " + too[0]\r
- + "->" + toofrom[0]);\r
+ if (toofrom != null)\r
+ {\r
+ if (toofrom[0] != from)\r
+ {\r
+ System.err.println("Mapping not reflexive:" + from + " "\r
+ + too[0] + "->" + toofrom[0]);\r
}\r
- System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0] + " % "\r
- + toofrom[1] + " (" + toofrom[2] + ")");\r
- } else {\r
+ System.out.println("ShiftTo(" + too[0] + ")==" + toofrom[0]\r
+ + " % " + toofrom[1] + " (" + toofrom[2] + ")");\r
+ }\r
+ else\r
+ {\r
System.out.println("ShiftTo(" + too[0] + ")=="\r
+ "NaN! - not Bijective Mapping!");\r
}\r
int mmap[][] = ml.makeFromMap();\r
System.out.println("FromMap : (" + mmap[0][0] + " " + mmap[0][1] + " "\r
+ mmap[0][2] + " " + mmap[0][3] + " ");\r
- for (int i = 1; i <= mmap[1].length; i++) {\r
- if (mmap[1][i - 1] == -1) {\r
+ for (int i = 1; i <= mmap[1].length; i++)\r
+ {\r
+ if (mmap[1][i - 1] == -1)\r
+ {\r
System.out.print(i + "=XXX");\r
\r
- } else {\r
+ }\r
+ else\r
+ {\r
System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));\r
}\r
- if (i % 20 == 0) {\r
+ if (i % 20 == 0)\r
+ {\r
System.out.print("\n");\r
- } else {\r
+ }\r
+ else\r
+ {\r
System.out.print(",");\r
}\r
}\r
System.out.print("\nTest locateInFrom\n");\r
{\r
int f = mmap[0][2], t = mmap[0][3];\r
- while (f <= t) {\r
+ while (f <= t)\r
+ {\r
System.out.println("Range " + f + " to " + t);\r
int rng[] = ml.locateInFrom(f, t);\r
- if (rng != null) {\r
- for (int i = 0; i < rng.length; i++) {\r
+ if (rng != null)\r
+ {\r
+ for (int i = 0; i < rng.length; i++)\r
+ {\r
System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));\r
}\r
- } else {\r
+ }\r
+ else\r
+ {\r
System.out.println("No range!");\r
}\r
System.out.print("\nReversed\n");\r
rng = ml.locateInFrom(t, f);\r
- if (rng != null) {\r
- for (int i = 0; i < rng.length; i++) {\r
+ if (rng != null)\r
+ {\r
+ for (int i = 0; i < rng.length; i++)\r
+ {\r
System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));\r
}\r
- } else {\r
+ }\r
+ else\r
+ {\r
System.out.println("No range!");\r
}\r
System.out.print("\n");\r
mmap = ml.makeToMap();\r
System.out.println("ToMap : (" + mmap[0][0] + " " + mmap[0][1] + " "\r
+ mmap[0][2] + " " + mmap[0][3] + " ");\r
- for (int i = 1; i <= mmap[1].length; i++) {\r
- if (mmap[1][i - 1] == -1) {\r
+ for (int i = 1; i <= mmap[1].length; i++)\r
+ {\r
+ if (mmap[1][i - 1] == -1)\r
+ {\r
System.out.print(i + "=XXX");\r
\r
- } else {\r
+ }\r
+ else\r
+ {\r
System.out.print(i + "=" + (mmap[0][2] + mmap[1][i - 1]));\r
}\r
- if (i % 20 == 0) {\r
+ if (i % 20 == 0)\r
+ {\r
System.out.print("\n");\r
- } else {\r
+ }\r
+ else\r
+ {\r
System.out.print(",");\r
}\r
}\r
System.out.print("\nTest locateInTo\n");\r
{\r
int f = mmap[0][2], t = mmap[0][3];\r
- while (f <= t) {\r
+ while (f <= t)\r
+ {\r
System.out.println("Range " + f + " to " + t);\r
int rng[] = ml.locateInTo(f, t);\r
- if (rng != null) {\r
- for (int i = 0; i < rng.length; i++) {\r
+ if (rng != null)\r
+ {\r
+ for (int i = 0; i < rng.length; i++)\r
+ {\r
System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));\r
}\r
- } else {\r
+ }\r
+ else\r
+ {\r
System.out.println("No range!");\r
}\r
System.out.print("\nReversed\n");\r
rng = ml.locateInTo(t, f);\r
- if (rng != null) {\r
- for (int i = 0; i < rng.length; i++) {\r
+ if (rng != null)\r
+ {\r
+ for (int i = 0; i < rng.length; i++)\r
+ {\r
System.out.print(rng[i] + ((i % 2 == 0) ? "," : ";"));\r
}\r
- } else {\r
+ }\r
+ else\r
+ {\r
System.out.println("No range!");\r
}\r
f++;\r
\r
}\r
\r
- public static void main(String argv[]) {\r
- MapList ml = new MapList(new int[] { 1, 5, 10, 15, 25, 20 }, new int[] {\r
- 51, 1 }, 1, 3);\r
- MapList ml1 = new MapList(new int[] { 1, 3, 17, 4 }, new int[] { 51, 1 },\r
- 1, 3);\r
- MapList ml2 = new MapList(new int[] { 1, 60 }, new int[] { 1, 20 }, 3, 1);\r
+ public static void main(String argv[])\r
+ {\r
+ MapList ml = new MapList(new int[]\r
+ { 1, 5, 10, 15, 25, 20 }, new int[]\r
+ { 51, 1 }, 1, 3);\r
+ MapList ml1 = new MapList(new int[]\r
+ { 1, 3, 17, 4 }, new int[]\r
+ { 51, 1 }, 1, 3);\r
+ MapList ml2 = new MapList(new int[]\r
+ { 1, 60 }, new int[]\r
+ { 1, 20 }, 3, 1);\r
// test internal consistency\r
int to[] = new int[51];\r
MapList.testMap(ml, 1, 60);\r
+ MapList mldna = new MapList(new int[]\r
+ { 2, 2, 6, 8, 12, 16 }, new int[]\r
+ { 1, 3 }, 3, 1);\r
+ int[] frm = mldna.locateInFrom(1, 1);\r
+ testLocateFrom(mldna, 1, 1, new int[]\r
+ { 2, 2, 6, 7 });\r
+ MapList.testMap(mldna, 1, 3);\r
/*\r
* for (int from=1; from<=51; from++) { int[] too=ml.shiftTo(from); int[]\r
* toofrom=ml.shiftFrom(too[0]);\r
// working!\r
}\r
\r
+ private static void testLocateFrom(MapList mldna, int i, int j, int[] ks)\r
+ {\r
+ int[] frm = mldna.locateInFrom(i, j);\r
+ if (frm == ks || java.util.Arrays.equals(frm, ks))\r
+ {\r
+ System.out.println("Success test locate from " + i + " to " + j);\r
+ }\r
+ else\r
+ {\r
+ System.err.println("Failed test locate from " + i + " to " + j);\r
+ for (int c = 0; c < frm.length; c++)\r
+ {\r
+ System.err.print(frm[c] + ((c % 2 == 0) ? "," : ";"));\r
+ }\r
+ System.err.println("Expected");\r
+ for (int c = 0; c < ks.length; c++)\r
+ {\r
+ System.err.print(ks[c] + ((c % 2 == 0) ? "," : ";"));\r
+ }\r
+ }\r
+ }\r
+\r
/**\r
* \r
* @return a MapList whose From range is this maplist's To Range, and vice\r
* versa\r
*/\r
- public MapList getInverse() {\r
+ public MapList getInverse()\r
+ {\r
return new MapList(getToRanges(), getFromRanges(), getToRatio(),\r
getFromRatio());\r
}\r
+\r
+ /**\r
+ * test for containment rather than equivalence to another mapping\r
+ * @param map to be tested for containment\r
+ * @return true if local or mapped range map contains or is contained by this mapping \r
+ */\r
+ public boolean containsEither(boolean local, MapList map)\r
+ {\r
+ if (local)\r
+ {\r
+ return ((getFromLowest()>=map.getFromLowest() && getFromHighest()<=map.getFromHighest())\r
+ || (getFromLowest()<=map.getFromLowest() && getFromHighest()>=map.getFromHighest()));\r
+ } else {\r
+ return ((getToLowest()>=map.getToLowest() && getToHighest()<=map.getToHighest())\r
+ || (getToLowest()<=map.getToLowest() && getToHighest()>=map.getToHighest()));\r
+ }\r
+ }\r
}\r