X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2Ffeatures%2FFeatureStoreImpl.java;h=d1d267073d0252a7e37ebfd4261e68f6483722bf;hb=99b6c1ca13af92cd8867971986a05664b0cd787c;hp=62832d7731dd5526581e5bd8959c1f6f953f0403;hpb=5b833c4ea17f7e45fe1532b36219fcbfba3c0e4a;p=jalview.git diff --git a/src/jalview/datamodel/features/FeatureStoreImpl.java b/src/jalview/datamodel/features/FeatureStoreImpl.java index 62832d7..d1d2670 100644 --- a/src/jalview/datamodel/features/FeatureStoreImpl.java +++ b/src/jalview/datamodel/features/FeatureStoreImpl.java @@ -46,6 +46,43 @@ public class FeatureStoreImpl extends FeatureStore } /** + * Add a contact feature to the lists that hold them ordered by start (first + * contact) and by end (second contact) position, ensuring the lists remain + * ordered, and returns true. This method allows duplicate features to be + * added, so test before calling to avoid this. + * + * @param feature + * @return + */ + @Override + protected synchronized boolean addContactFeature(SequenceFeature feature) + { + if (contactFeatureStarts == null) + { + contactFeatureStarts = new ArrayList<>(); + contactFeatureEnds = new ArrayList<>(); + } + + /* + * insert into list sorted by start (first contact position): + * binary search the sorted list to find the insertion point + */ + int insertPosition = findFirstBeginStatic(contactFeatureStarts, + feature.getBegin()); + contactFeatureStarts.add(insertPosition, feature); + + /* + * insert into list sorted by end (second contact position): + * binary search the sorted list to find the insertion point + */ + insertPosition = findFirstEndStatic(contactFeatureEnds, + feature.getEnd()); + contactFeatureEnds.add(insertPosition, feature); + + return true; + } + + /** * Adds contact features to the result list where either the second or the * first contact position lies within the target range * @@ -81,8 +118,7 @@ public class FeatureStoreImpl extends FeatureStore * find the first contact feature (if any) * whose end point is not before the target range */ - int index = BinarySearcher.findFirst(contactFeatureEnds, - f -> f.getEnd() >= from); + int index = findFirstEndStatic(contactFeatureEnds, from); int n = contactFeatureEnds.size(); while (index < n) @@ -136,8 +172,7 @@ public class FeatureStoreImpl extends FeatureStore private void findContactStartOverlaps(long from, long to, List result) { - int index = BinarySearcher.findFirst(contactFeatureStarts, - f -> f.getBegin() >= from); + int index = findFirstBegin(contactFeatureStarts, from); while (index < contactFeatureStarts.size()) { @@ -195,8 +230,30 @@ public class FeatureStoreImpl extends FeatureStore { result.addAll(((IntervalStoreI) features) .findOverlaps(start, end)); - // TODO Auto-generated method stub + } + + @Override + protected int findFirstBegin(List list, long pos) + { + return findFirstBeginStatic(list, pos); + } + private static int findFirstBeginStatic(List list, + long pos) + { + return BinarySearcher.findFirst(list, f -> f.getBegin() >= pos); + } + + @Override + protected int findFirstEnd(List list, long pos) + { + return findFirstEndStatic(list, pos); + } + + private static int findFirstEndStatic(List list, + long pos) + { + return BinarySearcher.findFirst(list, f -> f.getEnd() >= pos); } }