{
int widthx = 1 + endx - startx;
Iterator<Integer> it = hidden.getBoundedStartIterator(startx,
- startx + widthx + 1, true);
+ startx + widthx + 1);
while (it.hasNext())
{
res = it.next() - startx;
g.setColor(Color.blue);
int res;
Iterator<Integer> it = hidden.getBoundedStartIterator(startRes,
- endx + 1, true);
+ endx + 1);
while (it.hasNext())
{
res = it.next() - startRes;
int hideEnd;
int last = start;
- Iterator<int[]> regions = hidden.getBoundedIterator(start, end, true);
+ Iterator<int[]> regions = hidden.getBoundedIterator(start, end);
while (regions.hasNext())
{
region = regions.next();
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
+import java.util.NoSuchElementException;
import java.util.Vector;
import java.util.concurrent.locks.ReentrantReadWriteLock;
if (copy != null)
{
hiddenColumns = new ArrayList<>();
- Iterator<int[]> it = copy.getBoundedIterator(start, end, true);
+ Iterator<int[]> it = copy.getBoundedIterator(start, end);
while (it.hasNext())
{
int[] region = it.next();
}
/**
- * Returns a copy of the vector of hidden regions, as an ArrayList. Before
- * using this method please consider if you really need access to the hidden
- * regions - a new (or existing!) method on HiddenColumns might be more
- * appropriate.
- *
- * @return hidden regions as an ArrayList of [start,end] pairs
- */
- public ArrayList<int[]> getHiddenColumnsCopy()
- {
- try
- {
- LOCK.readLock().lock();
- return copyHiddenRegionsToArrayList(0);
- } finally
- {
- LOCK.readLock().unlock();
- }
- }
-
- /**
* return all visible segments between the given start and end boundaries
*
* @param start
}
}
- public Iterator<int[]> getBoundedIterator(int start, int end,
- boolean useCopy)
+ public Iterator<int[]> getBoundedIterator(int start, int end)
+ {
+ return new BoundedHiddenColsIterator(start, end, true);
+ }
+
+ public Iterator<Integer> getBoundedStartIterator(int start, int end)
{
- return new BoundedHiddenColsIterator(start, end, useCopy);
+ return new BoundedStartRegionIterator(start, end, true);
}
- public Iterator<Integer> getBoundedStartIterator(int start, int end,
- boolean useCopy)
+ public Iterator<Integer> getVisibleColsIterator(int start, int end)
{
- return new BoundedStartRegionIterator(start, end, useCopy);
+ return new VisibleColsIterator(start, end, true);
}
/**
* @author kmourao
*
*/
-
-
class BoundedHiddenColsIterator implements Iterator<int[]>
{
-
private int start; // start position to iterate from
private int end; // end position to iterate to
return result;
}
}
+
+ public class VisibleColsIterator implements Iterator<Integer>
+ {
+ private int last;
+
+ private int current;
+
+ private int next;
+
+ private List<int[]> localHidden = new ArrayList<>();
+
+ private int lasthiddenregion;
+
+ public VisibleColsIterator(int firstcol, int lastcol, boolean useCopy)
+ {
+ last = lastcol;
+ current = firstcol;
+ next = firstcol;
+ lasthiddenregion = -1;
+
+ try
+ {
+ if (useCopy)
+ {
+ // assume that if useCopy is false the calling code has locked
+ // hiddenColumns
+ LOCK.readLock().lock();
+ }
+
+ if (hiddenColumns != null)
+ {
+ int i = 0;
+ for (i = 0; i < hiddenColumns.size(); ++i)
+ {
+ if (current >= hiddenColumns.get(i)[0]
+ && current <= hiddenColumns.get(i)[1])
+ {
+ // current is hidden, move to right
+ current = hiddenColumns.get(i)[1] + 1;
+ next = current;
+ }
+ if (current < hiddenColumns.get(i)[0])
+ {
+ break;
+ }
+ }
+
+ lasthiddenregion = i - 1;
+
+ for (i = hiddenColumns.size() - 1; i >= 0; --i)
+ {
+ if (last >= hiddenColumns.get(i)[0]
+ && last <= hiddenColumns.get(i)[1])
+ {
+ // last is hidden, move to left
+ last = hiddenColumns.get(i)[0] - 1;
+ }
+ if (last > hiddenColumns.get(i)[1])
+ {
+ break;
+ }
+ }
+
+ // make a local copy of the bit we need
+ i = lasthiddenregion + 1;
+ while (i < hiddenColumns.size()
+ && hiddenColumns.get(i)[0] <= last)
+ {
+ int[] region = new int[] { hiddenColumns.get(i)[0],
+ hiddenColumns.get(i)[1] };
+ localHidden.add(region);
+ i++;
+ }
+ lasthiddenregion = -1;
+ }
+ } finally
+ {
+ if (useCopy)
+ {
+ LOCK.readLock().unlock();
+ }
+ }
+ }
+
+ @Override
+ public boolean hasNext()
+ {
+ return next <= last;
+ }
+
+ @Override
+ public Integer next()
+ {
+ if (next > last)
+ {
+ throw new NoSuchElementException();
+ }
+ current = next;
+ if ((localHidden != null)
+ && (lasthiddenregion + 1 < localHidden.size()))
+ {
+ // still some more hidden regions
+ if (next + 1 < localHidden.get(lasthiddenregion + 1)[0])
+ {
+ // next+1 is still before the next hidden region
+ next++;
+ }
+ else if ((next + 1 >= localHidden.get(lasthiddenregion + 1)[0])
+ && (next + 1 <= localHidden.get(lasthiddenregion + 1)[1]))
+ {
+ // next + 1 is in the next hidden region
+ next = localHidden.get(lasthiddenregion + 1)[1] + 1;
+ lasthiddenregion++;
+ }
+ }
+ else
+ {
+ // finished with hidden regions, just increment normally
+ next++;
+ }
+ return current;
+ }
+
+ @Override
+ public void remove()
+ {
+ throw new UnsupportedOperationException();
+ }
+ }
}
@Override
public Iterator<Integer> iterator()
{
- return new VisibleColsIterator(start, end, hidden);
+ return hidden.getVisibleColsIterator(start, end);
}
@Override
+++ /dev/null
-/*
- * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
- * Copyright (C) $$Year-Rel$$ The Jalview Authors
- *
- * This file is part of Jalview.
- *
- * Jalview is free software: you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation, either version 3
- * of the License, or (at your option) any later version.
- *
- * Jalview is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty
- * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- * PURPOSE. See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
- * The Jalview Authors are detailed in the 'AUTHORS' file.
- */
-package jalview.datamodel;
-
-import java.util.Iterator;
-import java.util.List;
-import java.util.NoSuchElementException;
-
-/**
- * An iterator which iterates over all visible columns in an alignment
- *
- * @author kmourao
- *
- */
-public class VisibleColsIterator implements Iterator<Integer>
-{
- private int last;
-
- private int current;
-
- private int next;
-
- private List<int[]> hidden;
-
- private int lasthiddenregion;
-
- public VisibleColsIterator(int firstcol, int lastcol,
- HiddenColumns hiddenCols)
- {
- last = lastcol;
- current = firstcol;
- next = firstcol;
- hidden = hiddenCols.getHiddenColumnsCopy();
- lasthiddenregion = -1;
-
- if (hidden != null)
- {
- int i = 0;
- for (i = 0; i < hidden.size(); ++i)
- {
- if (current >= hidden.get(i)[0] && current <= hidden.get(i)[1])
- {
- // current is hidden, move to right
- current = hidden.get(i)[1] + 1;
- next = current;
- }
- if (current < hidden.get(i)[0])
- {
- break;
- }
- }
- lasthiddenregion = i - 1;
-
- for (i = hidden.size() - 1; i >= 0; --i)
- {
- if (last >= hidden.get(i)[0] && last <= hidden.get(i)[1])
- {
- // last is hidden, move to left
- last = hidden.get(i)[0] - 1;
- }
- if (last > hidden.get(i)[1])
- {
- break;
- }
- }
- }
- }
-
- @Override
- public boolean hasNext()
- {
- return next <= last;
- }
-
- @Override
- public Integer next()
- {
- if (next > last)
- {
- throw new NoSuchElementException();
- }
- current = next;
- if ((hidden != null) && (lasthiddenregion + 1 < hidden.size()))
- {
- // still some more hidden regions
- if (next + 1 < hidden.get(lasthiddenregion + 1)[0])
- {
- // next+1 is still before the next hidden region
- next++;
- }
- else if ((next + 1 >= hidden.get(lasthiddenregion + 1)[0])
- && (next + 1 <= hidden.get(lasthiddenregion + 1)[1]))
- {
- // next + 1 is in the next hidden region
- next = hidden.get(lasthiddenregion + 1)[1] + 1;
- lasthiddenregion++;
- }
- }
- else
- {
- // finished with hidden regions, just increment normally
- next++;
- }
- return current;
- }
-
- @Override
- public void remove()
- {
- throw new UnsupportedOperationException();
- }
-}
if (av.getShowHiddenMarkers())
{
Iterator<Integer> it = hidden.getBoundedStartIterator(startx,
- startx + widthx + 1, true);
+ startx + widthx + 1);
while (it.hasNext())
{
res = it.next() - startx;
HiddenColumns hidden = av.getAlignment().getHiddenColumns();
Iterator<Integer> it = hidden.getBoundedStartIterator(startRes,
- endx + 1, true);
+ endx + 1);
while (it.hasNext())
{
res = it.next() - startRes;
public void testBoundedIterator()
{
HiddenColumns h = new HiddenColumns();
- Iterator<int[]> it = h.getBoundedIterator(0, 10, false);
+ Iterator<int[]> it = h.getBoundedIterator(0, 10);
// no hidden columns = nothing to iterate over
assertFalse(it.hasNext());
// all regions are returned
h.hideColumns(3, 10);
h.hideColumns(14, 16);
- it = h.getBoundedIterator(0, 20, false);
+ it = h.getBoundedIterator(0, 20);
assertTrue(it.hasNext());
int[] next = it.next();
assertEquals(3, next[0]);
// [start,end] overlaps a region
// 1 region returned
- it = h.getBoundedIterator(5, 7, false);
+ it = h.getBoundedIterator(5, 7);
assertTrue(it.hasNext());
next = it.next();
assertEquals(3, next[0]);
// [start,end] fully contains 1 region and start of last
// - 2 regions returned
- it = h.getBoundedIterator(3, 15, false);
+ it = h.getBoundedIterator(3, 15);
assertTrue(it.hasNext());
next = it.next();
assertEquals(3, next[0]);
// [start,end] contains end of first region and whole of last region
// - 2 regions returned
- it = h.getBoundedIterator(4, 20, false);
+ it = h.getBoundedIterator(4, 20);
assertTrue(it.hasNext());
next = it.next();
assertEquals(3, next[0]);
public void testBoundedStartIterator()
{
HiddenColumns h = new HiddenColumns();
- Iterator<Integer> it = h.getBoundedStartIterator(0, 10, false);
+ Iterator<Integer> it = h.getBoundedStartIterator(0, 10);
// no hidden columns = nothing to iterate over
assertFalse(it.hasNext());
// all regions are returned
h.hideColumns(3, 10);
h.hideColumns(14, 16);
- it = h.getBoundedStartIterator(0, 20, false);
+ it = h.getBoundedStartIterator(0, 20);
assertTrue(it.hasNext());
int next = it.next();
assertEquals(3, next);
// [start,end] does not contain a start of a region
// no regions to iterate over
- it = h.getBoundedStartIterator(4, 5, false);
+ it = h.getBoundedStartIterator(4, 5);
assertFalse(it.hasNext());
// [start,end] fully contains 1 region and start of last
// - 2 regions returned
- it = h.getBoundedStartIterator(3, 7, false);
+ it = h.getBoundedStartIterator(3, 7);
assertTrue(it.hasNext());
next = it.next();
assertEquals(3, next);
// [start,end] contains whole of last region
// - 1 region returned
- it = h.getBoundedStartIterator(4, 20, false);
+ it = h.getBoundedStartIterator(4, 20);
assertTrue(it.hasNext());
next = it.next();
assertEquals(6, next);
import static org.testng.Assert.assertTrue;
+import java.util.Iterator;
import java.util.NoSuchElementException;
import org.testng.annotations.BeforeClass;
@Test(groups = { "Functional" })
public void testHasNextAndNextWithHidden()
{
- VisibleColsIterator it = new VisibleColsIterator(0, 6, hiddenCols);
+ Iterator<Integer> it = hiddenCols.getVisibleColsIterator(0, 6);
int count = 0;
while (it.hasNext())
{
@Test(groups = { "Functional" })
public void testHasNextAndNextNoHidden()
{
- VisibleColsIterator it2 = new VisibleColsIterator(0, 3,
- new HiddenColumns());
+ HiddenColumns test = new HiddenColumns();
+ Iterator<Integer> it2 = test.getVisibleColsIterator(0, 3);
int count = 0;
while (it2.hasNext())
{
@Test(groups = { "Functional" })
public void testHasNextAndNextStartHidden()
{
- VisibleColsIterator it3 = new VisibleColsIterator(0, 6,
- hiddenColsAtStart);
+ Iterator<Integer> it3 = hiddenColsAtStart.getVisibleColsIterator(0, 6);
int count = 0;
while (it3.hasNext())
{
@Test(groups = { "Functional" })
public void testHasNextAndNextEndHidden()
{
- VisibleColsIterator it4 = new VisibleColsIterator(0, 4, hiddenCols);
+ Iterator<Integer> it4 = hiddenCols.getVisibleColsIterator(0, 4);
int count = 0;
while (it4.hasNext())
{
expectedExceptions = { NoSuchElementException.class })
public void testLastNextWithHidden() throws NoSuchElementException
{
- VisibleColsIterator it = new VisibleColsIterator(0, 3, hiddenCols);
+ Iterator<Integer> it = hiddenCols.getVisibleColsIterator(0, 3);
while (it.hasNext())
{
it.next();
expectedExceptions = { NoSuchElementException.class })
public void testLastNextNoHidden() throws NoSuchElementException
{
- VisibleColsIterator it2 = new VisibleColsIterator(0, 3,
- new HiddenColumns());
+ HiddenColumns test = new HiddenColumns();
+ Iterator<Integer> it2 = test.getVisibleColsIterator(0, 3);
while (it2.hasNext())
{
it2.next();
expectedExceptions = { NoSuchElementException.class })
public void testLastNextStartHidden() throws NoSuchElementException
{
- VisibleColsIterator it3 = new VisibleColsIterator(0, 6,
- hiddenColsAtStart);
+ Iterator<Integer> it3 = hiddenColsAtStart.getVisibleColsIterator(0, 6);
while (it3.hasNext())
{
it3.next();
expectedExceptions = { NoSuchElementException.class })
public void testLastNextEndHidden() throws NoSuchElementException
{
- VisibleColsIterator it4 = new VisibleColsIterator(0, 4, hiddenCols);
+ Iterator<Integer> it4 = hiddenCols.getVisibleColsIterator(0, 4);
while (it4.hasNext())
{
it4.next();
expectedExceptions = { UnsupportedOperationException.class })
public void testRemove() throws UnsupportedOperationException
{
- VisibleColsIterator it = new VisibleColsIterator(0, 3, hiddenCols);
+ Iterator<Integer> it = hiddenCols.getVisibleColsIterator(0, 3);
it.remove();
}
}