JAL-2759 Moved alignment annotation code out of HiddenColumns
[jalview.git] / src / jalview / datamodel / VisibleColsIterator.java
index a82de93..2fa27ed 100644 (file)
@@ -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 <http://www.gnu.org/licenses/>.
- * 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<Integer>
 
   private int next;
 
-  private List<int[]> hidden;
+  private List<int[]> localHidden = new ArrayList<>();
 
-  private int lasthiddenregion;
+  private int nexthiddenregion;
 
-  public VisibleColsIterator(int firstcol, int lastcol,
-          HiddenColumns hiddenCols)
+  VisibleColsIterator(int firstcol, int lastcol, List<int[]> hiddenColumns)
   {
     last = lastcol;
     current = firstcol;
     next = firstcol;
-    hidden = hiddenCols.getHiddenColumnsCopy();
-    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<Integer>
       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<Integer>
     throw new UnsupportedOperationException();
   }
 }
-