X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fintervalstore%2Fnonc%2FIntervalStore.java;h=55d29e84a7200b7246fdae195ed45bb40d6c581a;hb=113cc13d931734b2fc55e4b13474b6c863008e12;hp=23a9d1cc080d6e37ac08e513cfe0efdeffa4d1e7;hpb=28e9024f09a78a9625ed4defa2012bf342bec51e;p=jalview.git diff --git a/src/intervalstore/nonc/IntervalStore.java b/src/intervalstore/nonc/IntervalStore.java index 23a9d1c..55d29e8 100644 --- a/src/intervalstore/nonc/IntervalStore.java +++ b/src/intervalstore/nonc/IntervalStore.java @@ -268,7 +268,9 @@ public class IntervalStore } if (deleted > 0) + { finalizeDeletion(); + } if (!isTainted) { offsets = null; @@ -301,9 +303,13 @@ public class IntervalStore return false; } if (index < 0) + { index = -1 - index; + } else + { index++; + } } } @@ -313,7 +319,7 @@ public class IntervalStore { return false; } - + isSorted = false; } if (index == intervalCount) @@ -328,7 +334,9 @@ public class IntervalStore // System.out.println("stashed " + pt + " " + interval + " for " // + index + " " + intervals[index]); if (offsets == null) + { offsets = new int[capacity]; + } offsets[pt] = offsets[index]; @@ -344,7 +352,9 @@ public class IntervalStore private IntervalI[] finalizeAddition(IntervalI[] dest) { if (dest == null) + { dest = intervals; + } if (added == 0) { if (intervalCount > 0 && dest != intervals) @@ -362,16 +372,24 @@ public class IntervalStore { int pt0 = pt; while (--pt >= 0 && offsets[pt] == 0) + { ; + } if (pt < 0) + { pt = 0; + } int nOK = pt0 - pt; // shift upper intervals right ptShift -= nOK; if (nOK > 0) + { System.arraycopy(intervals, pt, dest, ptShift, nOK); + } if (added == 0) + { break; + } for (int offset = offsets[pt]; offset > 0; offset = offsets[offset]) { dest[--ptShift] = intervals[offset]; @@ -404,9 +422,13 @@ public class IntervalStore int r1 = interval.getEnd(); int end = intervalCount - 1; if (end < 0 || r0 < minStart) + { return -1; + } if (r0 > maxStart) + { return -1 - intervalCount; + } while (start <= end) { int mid = (start + end) >>> 1; @@ -423,25 +445,35 @@ public class IntervalStore IntervalI iv = intervals[mid]; if ((bsIgnore == null || !bsIgnore.get(mid)) && iv.equalsInterval(interval)) + { return mid; // found one; just scan up and down now, first checking the range, but // also checking other possible aspects of equivalence. + } for (int i = mid; ++i <= end;) { if ((iv = intervals[i]).getBegin() != r0 || iv.getEnd() != r1) + { break; + } if ((bsIgnore == null || !bsIgnore.get(i)) && iv.equalsInterval(interval)) + { return i; + } } for (int i = mid; --i >= start;) { if ((iv = intervals[i]).getBegin() != r0 || iv.getEnd() < r1) + { return -1 - ++i; + } if ((bsIgnore == null || !bsIgnore.get(i)) && iv.equalsInterval(interval)) + { return i; + } } return -1 - start; } @@ -501,7 +533,9 @@ public class IntervalStore private int compareRange(IntervalI t, long from, long to) { if (t == null) + { System.out.println("???"); + } int order = Long.signum(t.getBegin() - from); return (order == 0 ? Long.signum(bigendian ? to - t.getEnd() : t.getEnd() - to) @@ -512,7 +546,9 @@ public class IntervalStore public boolean contains(Object entry) { if (entry == null || intervalCount == 0) + { return false; + } if (!isSorted || deleted > 0) { sort(); @@ -525,6 +561,7 @@ public class IntervalStore ensureFinalized(); int index = binaryIdentitySearch(inner, null); if (index >= 0) + { while ((index = index - Math.abs(offsets[index])) >= 0) { if (intervals[index] == outer) @@ -532,19 +569,22 @@ public class IntervalStore return true; } } + } return false; } private void ensureFinalized() { - if (isTainted && intervalCount + added > 1) + if (isTainted) { if (!isSorted || added > 0 || deleted > 0) { sort(); } if (offsets == null || offsets.length < intervalCount) + { offsets = new int[intervalCount]; + } linkFeatures(); isTainted = false; } @@ -579,7 +619,7 @@ public class IntervalStore { result = new ArrayList<>(); } - switch (intervalCount) + switch (intervalCount + added) { case 0: return result; @@ -595,11 +635,15 @@ public class IntervalStore ensureFinalized(); if (from > maxEnd || to < minStart) + { return result; + } int index = binaryLastIntervalSearch(from, to, ret); int index1 = ret[0]; if (index1 < 0) + { return result; + } if (index1 > index + 1) { @@ -631,7 +675,9 @@ public class IntervalStore public IntervalI get(int i) { if (i < 0 || i >= intervalCount + added) + { return null; + } ensureFinalized(); return intervals[i]; } @@ -716,7 +762,7 @@ public class IntervalStore @Override public Iterator iterator() { - finalizeAddition(null); + ensureFinalized(); return new Iterator() { @@ -733,7 +779,9 @@ public class IntervalStore public T next() { if (next >= intervalCount) + { throw new NoSuchElementException(); + } return (T) intervals[next++]; } @@ -743,7 +791,9 @@ public class IntervalStore private void linkFeatures() { if (intervalCount == 0) + { return; + } maxEnd = intervals[0].getEnd(); offsets[0] = IntervalI.NOT_CONTAINED; if (intervalCount == 1) @@ -773,7 +823,7 @@ public class IntervalStore @Override public String prettyPrint() { - switch (intervalCount) + switch (intervalCount + added) { case 0: return ""; @@ -824,7 +874,9 @@ public class IntervalStore // if (addPt == intervalCount || offsets[pt] == 0) // return pt; if (pt >= 0 || added == 0 || pt == -1 - intervalCount) + { return pt; + } pt = -1 - pt; int start = interval.getBegin(); int end = interval.getEnd(); @@ -840,7 +892,9 @@ public class IntervalStore break; case 0: if (iv.equalsInterval(interval)) + { return pt; + } // fall through case 1: match = pt; @@ -853,7 +907,9 @@ public class IntervalStore { int i = intervalCount; while (--i >= 0 && !intervals[i].equalsInterval(interval)) + { ; + } return i; } } @@ -873,13 +929,19 @@ public class IntervalStore } int i = binaryIdentitySearch(interval, bsDeleted); if (i < 0) + { return false; + } if (deleted == 0) { if (bsDeleted == null) + { bsDeleted = new BitSet(intervalCount); + } else + { bsDeleted.clear(); + } } bsDeleted.set(i); deleted++; @@ -889,27 +951,39 @@ public class IntervalStore private void finalizeDeletion() { if (deleted == 0) + { return; + } // ......xxx.....xxxx.....xxxxx.... // ......^i,pt // ...... ....... // ............ - for (int i = bsDeleted.nextSetBit(0); i >= 0;) + for (int i = bsDeleted.nextSetBit(0), pt = i; i >= 0;) { - int pt = i; i = bsDeleted.nextClearBit(i + 1); int pt1 = bsDeleted.nextSetBit(i + 1); if (pt1 < 0) + { pt1 = intervalCount; - if (pt1 == pt) + } + int n = pt1 - i; + System.arraycopy(intervals, i, intervals, pt, n); + pt += n; + if (pt1 == intervalCount) + { + for (i = pt1; --i >= pt;) + { + intervals[i] = null; + } + intervalCount -= deleted; + deleted = 0; + bsDeleted.clear(); break; - System.arraycopy(intervals, i, intervals, pt, pt1 - i); + } i = pt1; } - intervalCount -= deleted; - deleted = 0; - bsDeleted.clear(); + } @@ -928,6 +1002,13 @@ public class IntervalStore return intervalCount + added - deleted; } + @Override + public Object[] toArray() + { + ensureFinalized(); + return super.toArray(); + } + /** * Sort intervals by start (lowest first) and end (highest first). */