X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FVisibleColsIterator.java;h=2fa27ede5d80702c6ddc680280ab21757b1f4053;hb=650bed7f2109092a55ca924039a03628fef2126b;hp=2ab61ff7e23802f909f560743d6c5c548bffed47;hpb=536a4a9c407a4c07b71e86e75135eaa2ba5e8850;p=jalview.git diff --git a/src/jalview/datamodel/VisibleColsIterator.java b/src/jalview/datamodel/VisibleColsIterator.java index 2ab61ff..2fa27ed 100644 --- a/src/jalview/datamodel/VisibleColsIterator.java +++ b/src/jalview/datamodel/VisibleColsIterator.java @@ -1,31 +1,13 @@ -/* - * 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 . - * The Jalview Authors are detailed in the 'AUTHORS' file. - */ package jalview.datamodel; +import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; /** - * An iterator which iterates over all visible columns in an alignment + * Iterator over the visible *columns* (not regions) as determined by the set of + * hidden columns. Uses a local copy of hidden columns. * * @author kmourao * @@ -38,49 +20,53 @@ public class VisibleColsIterator implements Iterator private int next; - private List hidden; + private List localHidden = new ArrayList<>(); - private int lasthiddenregion; + private int nexthiddenregion; - public VisibleColsIterator(int firstcol, int lastcol, - HiddenColumns hiddenCols) + VisibleColsIterator(int firstcol, int lastcol, List hiddenColumns) { last = lastcol; current = firstcol; next = firstcol; - hidden = hiddenCols.getListOfCols(); - lasthiddenregion = -1; + nexthiddenregion = 0; - if (hidden != null) + if (hiddenColumns != null) { int i = 0; - for (i = 0; i < hidden.size(); ++i) + for (i = 0; i < hiddenColumns.size() + && (current <= hiddenColumns.get(i)[0]); ++i) { - if (current >= hidden.get(i)[0] && current <= hidden.get(i)[1]) + if (current >= hiddenColumns.get(i)[0] + && current <= hiddenColumns.get(i)[1]) { // current is hidden, move to right - current = hidden.get(i)[1] + 1; + current = hiddenColumns.get(i)[1] + 1; next = current; - } - if (current < hidden.get(i)[0]) - { - break; + nexthiddenregion = i + 1; } } - lasthiddenregion = i - 1; - for (i = hidden.size() - 1; i >= 0; --i) + for (i = hiddenColumns.size() - 1; i >= 0 + && (last >= hiddenColumns.get(i)[1]); --i) { - if (last >= hidden.get(i)[0] && last <= hidden.get(i)[1]) + if (last >= hiddenColumns.get(i)[0] + && last <= hiddenColumns.get(i)[1]) { // last is hidden, move to left - last = hidden.get(i)[0] - 1; - } - if (last > hidden.get(i)[1]) - { - break; + last = hiddenColumns.get(i)[0] - 1; } } + + // make a local copy of the bit we need + i = nexthiddenregion; + 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++; + } } } @@ -98,20 +84,20 @@ public class VisibleColsIterator implements Iterator throw new NoSuchElementException(); } current = next; - if ((hidden != null) && (lasthiddenregion + 1 < hidden.size())) + if ((localHidden != null) && (nexthiddenregion < localHidden.size())) { // still some more hidden regions - if (next + 1 < hidden.get(lasthiddenregion + 1)[0]) + if (next + 1 < localHidden.get(nexthiddenregion)[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])) + else if ((next + 1 >= localHidden.get(nexthiddenregion)[0]) + && (next + 1 <= localHidden.get(nexthiddenregion)[1])) { // next + 1 is in the next hidden region - next = hidden.get(lasthiddenregion + 1)[1] + 1; - lasthiddenregion++; + next = localHidden.get(nexthiddenregion)[1] + 1; + nexthiddenregion++; } } else @@ -128,4 +114,3 @@ public class VisibleColsIterator implements Iterator throw new UnsupportedOperationException(); } } -