JAL-3383 JAL-3253-applet shortcut binary search when position is out of
[jalview.git] / src / jalview / datamodel / features / FeatureStore.java
index 8db35a3..d832f4d 100644 (file)
@@ -33,6 +33,11 @@ public abstract class FeatureStore implements FeatureStoreI
 {
 
   /**
+   * track last start for quick insertion of ordered features
+   */
+  protected int lastStart = -1, lastContactStart = -1;
+
+  /**
    * Answers the 'length' of the feature, counting 0 for non-positional features
    * and 1 for contact features
    * 
@@ -87,12 +92,13 @@ public abstract class FeatureStore implements FeatureStoreI
     /*
      * locate the first entry in the list which does not precede the feature
      */
-    int pos = findFirstBegin(list, feature.begin);
+    int begin = feature.begin;
+    int pos = findFirstBegin(list, begin);
     int len = list.size();
     while (pos < len)
     {
       SequenceFeature sf = list.get(pos);
-      if (sf.begin > feature.begin)
+      if (sf.begin > begin)
       {
         return -1; // no match found
       }
@@ -268,6 +274,10 @@ public abstract class FeatureStore implements FeatureStoreI
 
     if (feature.isContactFeature())
     {
+      if (feature.begin > lastContactStart)
+      {
+        lastContactStart = feature.begin;
+      }
       addContactFeature(feature);
     }
     else if (feature.isNonPositional())
@@ -277,6 +287,10 @@ public abstract class FeatureStore implements FeatureStoreI
     else
     {
       addPositionalFeature(feature);
+      if (feature.begin > lastStart)
+      {
+        lastStart = feature.begin;
+      }
     }
 
     /*
@@ -374,10 +388,12 @@ public abstract class FeatureStore implements FeatureStoreI
     if (feature.isContactFeature())
     {
       return contactFeatureStarts != null
+              && feature.begin <= lastContactStart
               && listContains(contactFeatureStarts, feature);
     }
 
-    return features == null ? false : containsFeature(feature);
+    return features == null || feature.begin > lastStart ? false
+            : containsFeature(feature);
   }