JAL-2526 cache first/last residue column positions in cursor
[jalview.git] / src / jalview / datamodel / HiddenColumns.java
index 1abd04b..f0d99e5 100644 (file)
@@ -4,6 +4,7 @@ import jalview.util.Comparison;
 import jalview.util.ShiftList;
 
 import java.util.ArrayList;
+import java.util.BitSet;
 import java.util.Collections;
 import java.util.List;
 import java.util.Vector;
@@ -21,12 +22,35 @@ public class HiddenColumns
    * 
    * @return empty list or List of hidden column intervals
    */
-  public List<int[]> getListOfCols()
+  public List<int[]> getHiddenRegions()
   {
     return hiddenColumns == null ? Collections.<int[]> emptyList()
             : hiddenColumns;
   }
 
+  /**
+   * Find the number of hidden columns
+   * 
+   * @return number of hidden columns
+   */
+  public int getSize()
+  {
+    int size = 0;
+    if (hasHidden())
+    {
+      for (int[] range : hiddenColumns)
+      {
+        size += range[1] - range[0] + 1;
+      }
+    }
+    return size;
+  }
+
+  /**
+   * Answers if there are any hidden columns
+   * 
+   * @return true if there are hidden columns
+   */
   public boolean hasHidden()
   {
     return (hiddenColumns != null) && (!hiddenColumns.isEmpty());
@@ -544,7 +568,7 @@ public class HiddenColumns
     if (hiddenColumns != null && hiddenColumns.size() > 0)
     {
       List<int[]> visiblecontigs = new ArrayList<int[]>();
-      List<int[]> regions = getListOfCols();
+      List<int[]> regions = getHiddenRegions();
 
       int vstart = start;
       int[] region;
@@ -598,7 +622,7 @@ public class HiddenColumns
       for (i = 0; i < iSize; i++)
       {
         StringBuffer visibleSeq = new StringBuffer();
-        List<int[]> regions = getListOfCols();
+        List<int[]> regions = getHiddenRegions();
 
         int blockStart = start, blockEnd = end;
         int[] region;
@@ -671,7 +695,7 @@ public class HiddenColumns
 
     // Simply walk along the sequence whilst watching for hidden column
     // boundaries
-    List<int[]> regions = getListOfCols();
+    List<int[]> regions = getHiddenRegions();
     int spos = fpos, lastvispos = -1, rcount = 0, hideStart = seq
             .getLength(), hideEnd = -1;
     int visPrev = 0, visNext = 0, firstP = -1, lastP = -1;
@@ -765,7 +789,7 @@ public class HiddenColumns
       // then mangle the alignmentAnnotation annotation array
       Vector<Annotation[]> annels = new Vector<Annotation[]>();
       Annotation[] els = null;
-      List<int[]> regions = getListOfCols();
+      List<int[]> regions = getHiddenRegions();
       int blockStart = start, blockEnd = end;
       int[] region;
       int hideStart, hideEnd, w = 0;
@@ -1224,9 +1248,10 @@ public class HiddenColumns
   /**
    * Returns a hashCode built from hidden column ranges
    */
-  public int hashCode(int hc)
+  @Override
+  public int hashCode()
   {
-    int hashCode = hc;
+    int hashCode = 1;
     if (hiddenColumns != null)
     {
       for (int[] hidden : hiddenColumns)
@@ -1238,4 +1263,37 @@ public class HiddenColumns
     return hashCode;
   }
 
+  /**
+   * Hide columns corresponding to the marked bits
+   * 
+   * @param inserts
+   *          - columns map to bits starting from zero
+   */
+  public void hideMarkedBits(BitSet inserts)
+  {
+    for (int firstSet = inserts.nextSetBit(0), lastSet = 0; firstSet >= 0; firstSet = inserts
+            .nextSetBit(lastSet))
+    {
+      lastSet = inserts.nextClearBit(firstSet);
+      hideColumns(firstSet, lastSet - 1);
+    }
+  }
+
+  /**
+   * 
+   * @param inserts
+   *          BitSet where hidden columns will be marked
+   */
+  public void markHiddenRegions(BitSet inserts)
+  {
+    if (hiddenColumns == null)
+    {
+      return;
+    }
+    for (int[] range : hiddenColumns)
+    {
+      inserts.set(range[0], range[1] + 1);
+    }
+  }
+
 }