JAL-2759 Renamed BoundedHiddenColsIterator to HiddenColsIterator
[jalview.git] / src / jalview / datamodel / HiddenColumns.java
index a81d3bd..ef80b32 100644 (file)
@@ -26,6 +26,27 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
+/**
+ * This class manages the collection of hidden columns associated with an
+ * alignment. To iterate over the collection, or over visible columns/regions,
+ * use an iterator obtained from one of:
+ * 
+ * - getBoundedIterator: iterates over the hidden regions, within some bounds,
+ * returning absolute positions
+ * 
+ * - getBoundedStartIterator: iterates over the start positions of hidden
+ * regions, within some bounds, returning visible positions
+ * 
+ * - getVisContigsIterator: iterates over visible regions in a range, returning
+ * absolute positions
+ * 
+ * - getVisibleColsIterator: iterates over the visible *columns*
+ * 
+ * For performance reasons, provide bounds where possible.
+ * 
+ * @author kmourao
+ *
+ */
 public class HiddenColumns
 {
   private static final int HASH_MULTIPLIER = 31;
@@ -60,7 +81,8 @@ public class HiddenColumns
    * Methods which change the hiddenColumns collection. These methods should 
    * use a writeLock to prevent other threads accessing the hiddenColumns
    * collection while changes are being made. They should also reset the hidden
-   * columns cursor, and either update the hidden columns count, or set it to 0.
+   * columns cursor, and either update the hidden columns count, or set it to 0 
+   * (so that it will later be updated when needed).
    */
 
   /**
@@ -166,10 +188,28 @@ public class HiddenColumns
         wasAlreadyLocked = true;
       }
 
+      int previndex = 0;
+      int prevHiddenCount = 0;
+      int regionindex = 0;
       if (hiddenColumns == null)
       {
         hiddenColumns = new ArrayList<>();
       }
+      else
+      {
+        // set up cursor reset values
+        HiddenCursorPosition cursorPos = cursor.findRegionForColumn(start);
+        regionindex = cursorPos.getRegionIndex();
+
+        if (regionindex > 0)
+        {
+          // get previous index and hidden count for updating the cursor later
+          previndex = regionindex - 1;
+          int[] prevRegion = hiddenColumns.get(previndex);
+          prevHiddenCount = cursorPos.getHiddenSoFar()
+                  - (prevRegion[1] - prevRegion[0] + 1);
+        }
+      }
 
       /*
        * new range follows everything else; check first to avoid looping over whole hiddenColumns collection
@@ -186,18 +226,23 @@ public class HiddenColumns
          * appropriate
          */
         boolean added = false;
-        for (int i = 0; !added && i < hiddenColumns.size(); i++)
+        if (regionindex > 0)
+        {
+          added = insertRangeAtRegion(regionindex - 1, start, end);
+        }
+        if (!added && regionindex < hiddenColumns.size())
         {
-          added = insertRangeAtRegion(i, start, end);
-        } // for
+          insertRangeAtRegion(regionindex, start, end);
+        }
       }
-      if (!wasAlreadyLocked)
-      {
-        cursor.resetCursor(hiddenColumns);
 
-        // reset the number of columns so they will be recounted
-        numColumns = 0;
-      }
+      // reset the cursor to just before our insertion point: this saves
+      // a lot of reprocessing in large alignments
+      cursor.resetCursor(hiddenColumns, previndex, prevHiddenCount);
+
+      // reset the number of columns so they will be recounted
+      numColumns = 0;
+
     } finally
     {
       if (!wasAlreadyLocked)
@@ -265,6 +310,9 @@ public class HiddenColumns
           break;
         }
         region[1] = Math.max(nextRegion[1], end);
+
+        // in theory this is faster than hiddenColumns.remove(i+1)
+        // benchmarking results a bit ambivalent
         hiddenColumns.subList(i + 1, i + 2).clear();
       }
       added = true;
@@ -596,15 +644,14 @@ public class HiddenColumns
       {
         // numColumns is out of date, so recalculate
         int size = 0;
-        if (hiddenColumns != null)
+
+        Iterator<int[]> it = hiddenColumns.iterator();
+        while (it.hasNext())
         {
-          Iterator<int[]> it = hiddenColumns.iterator();
-          while (it.hasNext())
-          {
-            int[] range = it.next();
-            size += range[1] - range[0] + 1;
-          }
+          int[] range = it.next();
+          size += range[1] - range[0] + 1;
         }
+
         numColumns = size;
       }
 
@@ -697,7 +744,7 @@ public class HiddenColumns
 
       if (hiddenColumns != null)
       {
-        result += cursor.getHiddenOffset(column).getHiddenSoFar();
+        result += cursor.findRegionForVisColumn(column).getHiddenSoFar();
       }
 
       return result;
@@ -821,6 +868,8 @@ public class HiddenColumns
    * @param alPos
    *          the absolute (visible) alignmentPosition to find the next hidden
    *          column for
+   * @return the index of the next hidden column, or alPos if there is no next
+   *         hidden column
    */
   public int getHiddenBoundaryRight(int alPos)
   {
@@ -903,7 +952,9 @@ public class HiddenColumns
       if (regionindex > -1 && regionindex < hiddenColumns.size())
       {
         int[] region = hiddenColumns.get(regionindex);
-        if (column >= region[0] && column <= region[1])
+        // already know that column <= region[1] as cursor returns containing
+        // region or region to right
+        if (column >= region[0])
         {
           return false;
         }
@@ -1053,8 +1104,12 @@ public class HiddenColumns
    */
   public void makeVisibleAnnotation(AlignmentAnnotation alignmentAnnotation)
   {
-    makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
+    if (alignmentAnnotation != null
+            && alignmentAnnotation.annotations != null)
+    {
+      makeVisibleAnnotation(0, alignmentAnnotation.annotations.length,
             alignmentAnnotation);
+    }
   }
 
   /**
@@ -1078,7 +1133,8 @@ public class HiddenColumns
       int startFrom = start;
       int endAt = end;
 
-      if (alignmentAnnotation.annotations != null)
+      if (alignmentAnnotation != null
+              && alignmentAnnotation.annotations != null)
       {
         if (hiddenColumns != null && hiddenColumns.size() > 0)
         {
@@ -1163,6 +1219,9 @@ public class HiddenColumns
     try
     {
       LOCK.readLock().lock();
+
+      // we don't use getSize()>0 here because it has to iterate over
+      // the full hiddenColumns collection and so will be much slower
       return hiddenColumns != null && hiddenColumns.size() > 0;
     } finally
     {
@@ -1172,9 +1231,9 @@ public class HiddenColumns
 
   /**
    * 
-   * @return true if there are more than one set of columns hidden
+   * @return true if there is more than one hidden column region
    */
-  public boolean hasManyHiddenColumns()
+  public boolean hasMultiHiddenColumnRegions()
   {
     try
     {
@@ -1295,7 +1354,7 @@ public class HiddenColumns
         higestRange = (range[1] >= endPos) ? range : higestRange;
       }
 
-      if (lowestRange[0] == -1 && lowestRange[1] == -1)
+      if (lowestRange[0] == -1) // includes (lowestRange[1] == -1)
       {
         startPos = alignmentStartEnd[0];
       }
@@ -1304,7 +1363,7 @@ public class HiddenColumns
         startPos = lowestRange[1] + 1;
       }
 
-      if (higestRange[0] == -1 && higestRange[1] == -1)
+      if (higestRange[0] == -1) // includes (higestRange[1] == -1)
       {
         endPos = alignmentStartEnd[1];
       }
@@ -1351,16 +1410,6 @@ public class HiddenColumns
         {
           reveal = hiddenColumns.get(regionindex);
         }
-        // or try the next region
-        else
-        {
-          regionindex++;
-          if (regionindex < hiddenColumns.size()
-                  && hiddenColumns.get(regionindex)[0] == adjres + 1)
-          {
-            reveal = hiddenColumns.get(regionindex);
-          }
-        }
       }
       return reveal;
 
@@ -1378,7 +1427,7 @@ public class HiddenColumns
     try
     {
       LOCK.readLock().lock();
-      return new BoundedHiddenColsIterator(hiddenColumns);
+      return new HiddenColsIterator(hiddenColumns);
     } finally
     {
       LOCK.readLock().unlock();
@@ -1399,7 +1448,7 @@ public class HiddenColumns
     try
     {
       LOCK.readLock().lock();
-      return new BoundedHiddenColsIterator(start, end, hiddenColumns);
+      return new HiddenColsIterator(start, end, hiddenColumns);
     } finally
     {
       LOCK.readLock().unlock();
@@ -1466,35 +1515,14 @@ public class HiddenColumns
    * boundaries
    * 
    * @param start
-   *          first column, inclusive from 0, absolute value
-   * @param end
-   *          last column - not inclusive, absolute value
-   */
-  public Iterator<int[]> getVisContigsIterator(int start, int end)
-  {
-    try
-    {
-      LOCK.readLock().lock();
-      return new VisibleContigsIterator(start, end, hiddenColumns);
-    } finally
-    {
-      LOCK.readLock().unlock();
-    }
-  }
-
-  /**
-   * return an iterator over visible segments between the given start and end
-   * boundaries
-   * 
-   * @param start
-   *          (first column - inclusive from 0)
+   *          first column, inclusive from 0
    * @param end
-   *          (last column - inclusive)
+   *          last column - not inclusive
    * @param useVisibleCoords
    *          if true, start and end are visible column positions, not absolute
-   *          positions
+   *          positions*
    */
-  public Iterator<int[]> getVisibleBlocksIterator(int start, int end,
+  public Iterator<int[]> getVisContigsIterator(int start, int end,
           boolean useVisibleCoords)
   {
     int adjstart = start;
@@ -1508,9 +1536,7 @@ public class HiddenColumns
     try
     {
       LOCK.readLock().lock();
-
-      return new VisibleContigsIterator(adjstart, adjend + 1,
-              hiddenColumns);
+      return new VisibleContigsIterator(adjstart, adjend, hiddenColumns);
     } finally
     {
       LOCK.readLock().unlock();