-
- public Iterator<int[]> iterator()
- {
- if (hiddenColumns != null)
- {
- int last = hiddenColumns.get(hiddenColumns.size() - 1)[1];
- return new BoundedHiddenColsIterator(0, last, true);
- }
- else
- {
- return new BoundedHiddenColsIterator(0, 0, true);
- }
- }
-
- public Iterator<int[]> getBoundedIterator(int start, int end,
- boolean useCopy)
- {
- return new BoundedHiddenColsIterator(start, end, useCopy);
- }
-
- public Iterator<Integer> getBoundedStartIterator(int start, int end,
- boolean useCopy)
- {
- return new BoundedStartRegionIterator(start, end, useCopy);
- }
-
- public Iterator<int[]> getBoundedVisRegionIterator(int start, int end)
- {
- return new BoundedVisRegionIterator(start, end, true);
- }
-
- /**
- * An iterator which iterates over hidden column regions in a range.
- *
- * @author kmourao
- *
- */
-
-
- class BoundedHiddenColsIterator implements Iterator<int[]>
- {
-
- private int start; // start position to iterate from
-
- private int end; // end position to iterate to
-
- // current index in hiddenColumns
- private int currentPosition = 0;
-
- // current column in hiddenColumns
- private int[] currentRegion;
-
- // whether to make a local copy of hiddenColumns
- private final boolean useCopy;
-
- // local copy or reference to hiddenColumns
- private List<int[]> localHidden;
-
- /**
- * Construct an iterator over hiddenColums bounded at
- * [lowerBound,upperBound]
- *
- * @param lowerBound
- * lower bound to iterate from
- * @param upperBound
- * upper bound to iterate to
- * @param useCopyCols
- * whether to make a local copy of hiddenColumns for iteration (set
- * to true if calling from outwith the HiddenColumns class)
- */
- BoundedHiddenColsIterator(int lowerBound, int upperBound,
- boolean useCopyCols)
- {
- start = lowerBound;
- end = upperBound;
- useCopy = useCopyCols;
-
- try
- {
- if (useCopy)
- {
- // assume that if useCopy is false the calling code has locked
- // hiddenColumns
- LOCK.readLock().lock();
- }
-
- if (hiddenColumns != null)
- {
- localHidden = new ArrayList<>();
-
- // iterate until a region overlaps with [start,end]
- int i = 0;
- while ((i < hiddenColumns.size())
- && (hiddenColumns.get(i)[1] < start))
- {
- i++;
- }
-
- // iterate from start to end, adding each hidden region. Positions are
- // absolute, and all regions which *overlap* [start,end] are added.
- while (i < hiddenColumns.size()
- && (hiddenColumns.get(i)[0] <= end))
- {
- int[] rh;
- int[] cp;
- rh = hiddenColumns.get(i);
- if (rh != null)
- {
- cp = new int[rh.length];
- System.arraycopy(rh, 0, cp, 0, rh.length);
- localHidden.add(cp);
- }
- i++;
- }
- }
- }
- finally
- {
- if (useCopy)
- {
- LOCK.readLock().unlock();
- }
- }
- }
-
- @Override
- public boolean hasNext()
- {
- return (localHidden != null)
- && (currentPosition < localHidden.size());
- }
-
- @Override
- public int[] next()
- {
- currentRegion = localHidden.get(currentPosition);
- currentPosition++;
- return currentRegion;
- }
- }
-
- class BoundedStartRegionIterator implements Iterator<Integer>
- {
-
- private int start; // start position to iterate from
-
- private int end; // end position to iterate to
-
- // current index in hiddenColumns
- private int currentPosition = 0;
-
- // local copy or reference to hiddenColumns
- private List<Integer> positions = null;
-
- /**
- * Construct an iterator over hiddenColums bounded at
- * [lowerBound,upperBound]
- *
- * @param lowerBound
- * lower bound to iterate from
- * @param upperBound
- * upper bound to iterate to
- * @param useCopyCols
- * whether to make a local copy of hiddenColumns for iteration (set
- * to true if calling from outwith the HiddenColumns class)
- */
- BoundedStartRegionIterator(int lowerBound, int upperBound,
- boolean useCopy)
- {
- start = lowerBound;
- end = upperBound;
-
- try
- {
- if (useCopy)
- {
- // assume that if useCopy is false the calling code has locked
- // hiddenColumns
- LOCK.readLock().lock();
- }
-
- if (hiddenColumns != null)
- {
- positions = new ArrayList<>(hiddenColumns.size());
-
- // navigate to start, keeping count of hidden columns
- int i = 0;
- int hiddenSoFar = 0;
- while ((i < hiddenColumns.size())
- && (hiddenColumns.get(i)[0] < start + hiddenSoFar))
- {
- int[] region = hiddenColumns.get(i);
- hiddenSoFar += region[1] - region[0] + 1;
- i++;
- }
-
- // iterate from start to end, adding start positions of each
- // hidden region. Positions are visible columns count, not absolute
- while (i < hiddenColumns.size()
- && (hiddenColumns.get(i)[0] <= end + hiddenSoFar))
- {
- int[] region = hiddenColumns.get(i);
- positions.add(region[0] - hiddenSoFar);
- hiddenSoFar += region[1] - region[0] + 1;
- i++;
- }
- }
- else
- {
- positions = new ArrayList<>();
- }
- } finally
- {
- if (useCopy)
- {
- LOCK.readLock().unlock();
- }
- }
- }
-
- @Override
- public boolean hasNext()
- {
- return (currentPosition < positions.size());
- }
-
- @Override
- public Integer next()
- {
- int result = positions.get(currentPosition);
- currentPosition++;
- return result;
- }
- }
-
- class BoundedVisRegionIterator implements Iterator<int[]>
- {
- private int start; // start position to iterate from
-
- private int end; // end position to iterate to
-
- // current region in visColumns
- private int[] currentRegion;
-
- // current index in visColumns
- private int currentPosition = 0;
-
- private List<int[]> vcontigs = null;
-
- /**
- * Construct an iterator over visibleColumn regions bounded at
- * [lowerBound,upperBound]
- *
- * @param lowerBound
- * lower bound to iterate from
- * @param upperBound
- * upper bound to iterate to
- * @param useCopyCols
- * whether to make a local copy for iteration (set to true if
- * calling from outwith the HiddenColumns class)
- */
- BoundedVisRegionIterator(int lowerBound, int upperBound,
- boolean useCopy)
- {
- try
- {
- start = lowerBound;
- end = upperBound;
-
- if (useCopy)
- {
- // assume that if useCopy is false the calling code has locked
- // hiddenColumns
- LOCK.readLock().lock();
- }
-
- int visStart = start;
-
- if (hiddenColumns != null)
- {
- vcontigs = new ArrayList<>(hiddenColumns.size() + 1);
-
- // navigate to start, keeping count of hidden columns
- int i = 0;
- int[] region = null;
- while ((i < hiddenColumns.size())
- && (hiddenColumns.get(i)[0] <= start))
- {
- i++;
- }
- // if there was a hidden region before (i>=1), and it ended after
- // start
- // and before end, adjust visStart to be just after that region
- if (i > 0)
- {
- region = hiddenColumns.get(i - 1);
- if ((region[1] > start) && (region[1] < end))
- {
- visStart = region[1] + 1;
- }
- else if (region[1] >= end)
- {
- // previous hidden region covers whole range [start,end]
- // early exit - vcontigs is empty
- return;
- }
- }
-
- // iterate from start to end, adding start positions of each
- // hidden region. Positions are visible columns count, not absolute
- while (i < hiddenColumns.size()
- && (hiddenColumns.get(i)[0] < end))
- {
- region = hiddenColumns.get(i);
- int[] prevVisibleRegion = new int[] { visStart, region[0] - 1 };
- vcontigs.add(prevVisibleRegion);
- visStart = region[1] + 1;
- i++;
- }
- // add on a final visible region if needed
- if (visStart <= end)
- {
- int[] lastRegion = new int[] { visStart, end };
- vcontigs.add(lastRegion);
- }
- }
- else
- {
- vcontigs = new ArrayList<>();
- int[] lastRegion = new int[] { start, end };
- vcontigs.add(lastRegion);
- }
- } finally
- {
- if (useCopy)
- {
- LOCK.readLock().unlock();
- }
- }
- }
-
- @Override
- public boolean hasNext()
- {
- return (currentPosition < vcontigs.size());
- }
-
- @Override
- public int[] next()
- {
- currentRegion = vcontigs.get(currentPosition);
- currentPosition++;
- return currentRegion;
- }
- }