- if (i % 20==0)
- System.out.print("\n");
- else
- System.out.print(",");
- }
- System.out.print("\n");
- }
- public static void main(String argv[]) {
- MapList ml=new MapList(new int[] { 1,5,10,15,25,20},
- new int[] { 51,1}, 1, 3);
- MapList ml1=new MapList(new int[] { 1,3,17,4},
- new int[] { 51,1}, 1, 3);
-
- // test internal consistency
- int to[] = new int[51];
- MapList.testMap(ml, 1, 25);
- /*
- for (int from=1; from<=51; from++) {
- int[] too=ml.shiftTo(from);
- int[] toofrom=ml.shiftFrom(too[0]);
- System.out.println("ShiftFrom("+from+")=="+too[0]+" % "+too[1]+"\t+-+\tShiftTo("+too[0]+")=="+toofrom[0]+" % "+toofrom[1]);
- }*/
- System.out.print("Success?\n"); // if we get here - something must be working!
+ if (fe_s != -1)
+ {
+ if (endpos - endindx >= iv[1])
+ {
+ fe = i;
+ endpos = endpos - endindx; // end of end token is within this
+ // interval
+ }
+ else
+ {
+ endindx -= endpos - iv[1]; // skip all this interval too
+ }
+ }
+ }
+ }
+ i++;
+ }
+ if (fs == fe && fe == -1)
+ {
+ return null;
+ }
+ List<int[]> ranges = new ArrayList<int[]>();
+ if (fs <= fe)
+ {
+ intv = fs;
+ i = fs;
+ // truncate initial interval
+ iv = shiftFrom.get(intv++);
+ iv = new int[] { iv[0], iv[1] };// clone
+ if (i == fs)
+ {
+ iv[0] = startpos;
+ }
+ while (i != fe)
+ {
+ ranges.add(iv); // add initial range
+ iv = shiftFrom.get(intv++); // get next interval
+ iv = new int[] { iv[0], iv[1] };// clone
+ i++;
+ }
+ if (i == fe)
+ {
+ iv[1] = endpos;
+ }
+ ranges.add(iv); // add only - or final range
+ }
+ else
+ {
+ // walk from end of interval.
+ i = shiftFrom.size() - 1;
+ while (i > fs)
+ {
+ i--;
+ }
+ iv = shiftFrom.get(i);
+ iv = new int[] { iv[1], iv[0] };// reverse and clone
+ // truncate initial interval
+ if (i == fs)
+ {
+ iv[0] = startpos;
+ }
+ while (--i != fe)
+ { // fix apparent logic bug when fe==-1
+ ranges.add(iv); // add (truncated) reversed interval
+ iv = shiftFrom.get(i);
+ iv = new int[] { iv[1], iv[0] }; // reverse and clone
+ }
+ if (i == fe)
+ {
+ // interval is already reversed
+ iv[1] = endpos;
+ }
+ ranges.add(iv); // add only - or final range
+ }
+ // create array of start end intervals.
+ int[] range = null;
+ if (ranges != null && ranges.size() > 0)
+ {
+ range = new int[ranges.size() * 2];
+ intv = 0;
+ intvSize = ranges.size();
+ i = 0;
+ while (intv < intvSize)
+ {
+ iv = ranges.get(intv);
+ range[i++] = iv[0];
+ range[i++] = iv[1];
+ ranges.set(intv++, null); // remove
+ }
+ }
+ return range;
+ }
+
+ /**
+ * get the 'initial' position of mpos in To
+ *
+ * @param mpos
+ * position in from
+ * @return position of first word in to reference frame
+ */
+ public int getToPosition(int mpos)
+ {
+ // TODO not used - remove??
+ int[] mp = shiftTo(mpos);
+ if (mp != null)
+ {
+ return mp[0];
+ }
+ return mpos;
+ }
+
+ /**
+ * get range of positions in To frame for the mpos word in From
+ *
+ * @param mpos
+ * position in From
+ * @return null or int[] first position in To for mpos, last position in to
+ * for Mpos
+ */
+ public int[] getToWord(int mpos)
+ {
+ int[] mp = shiftTo(mpos);
+ if (mp != null)
+ {
+ return new int[] { mp[0], mp[0] + mp[2] * (getFromRatio() - 1) };
+ }
+ return null;
+ }
+
+ /**
+ * get From position in the associated reference frame for position pos in the
+ * associated sequence.
+ *
+ * @param pos
+ * @return
+ */
+ public int getMappedPosition(int pos)
+ {
+ // TODO not used - remove??
+ int[] mp = shiftFrom(pos);
+ if (mp != null)
+ {
+ return mp[0];
+ }
+ return pos;
+ }
+
+ public int[] getMappedWord(int pos)
+ {
+ // TODO not used - remove??
+ int[] mp = shiftFrom(pos);
+ if (mp != null)
+ {
+ return new int[] { mp[0], mp[0] + mp[2] * (getToRatio() - 1) };
+ }
+ return null;
+ }
+
+ /**
+ *
+ * @return a MapList whose From range is this maplist's To Range, and vice
+ * versa
+ */
+ public MapList getInverse()
+ {
+ return new MapList(getToRanges(), getFromRanges(), getToRatio(),
+ getFromRatio());
+ }
+
+ /**
+ * test for containment rather than equivalence to another mapping
+ *
+ * @param map
+ * to be tested for containment
+ * @return true if local or mapped range map contains or is contained by this
+ * mapping
+ */
+ public boolean containsEither(boolean local, MapList map)
+ {
+ // TODO not used - remove?
+ if (local)
+ {
+ return ((getFromLowest() >= map.getFromLowest() && getFromHighest() <= map
+ .getFromHighest()) || (getFromLowest() <= map.getFromLowest() && getFromHighest() >= map
+ .getFromHighest()));
+ }
+ else
+ {
+ return ((getToLowest() >= map.getToLowest() && getToHighest() <= map
+ .getToHighest()) || (getToLowest() <= map.getToLowest() && getToHighest() >= map
+ .getToHighest()));
+ }
+ }
+
+ /**
+ * String representation - for debugging, not guaranteed not to change
+ */
+ @Override
+ public String toString()
+ {
+ StringBuilder sb = new StringBuilder(64);
+ sb.append("[");
+ for (int[] shift : fromShifts)
+ {
+ sb.append(" ").append(Arrays.toString(shift));
+ }
+ sb.append(" ] ");
+ sb.append(fromRatio).append(":").append(toRatio);
+ sb.append(" to [");
+ for (int[] shift : toShifts)
+ {
+ sb.append(" ").append(Arrays.toString(shift));
+ }
+ sb.append(" ]");
+ return sb.toString();