From: kiramt Date: Mon, 9 Oct 2017 08:58:56 +0000 (+0100) Subject: JAL-2674 iterator updates X-Git-Tag: Release_2_10_4~55^2~1^2~80 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=787e3f5f12389d20f4c478d8333f4f459593a8bb;p=jalview.git JAL-2674 iterator updates --- diff --git a/benchmarking/README b/benchmarking/README index 60b94a9..93b9820 100644 --- a/benchmarking/README +++ b/benchmarking/README @@ -22,4 +22,7 @@ to install the jalview.jar file in the local maven repository. The pom.xml in th To get JSON output instead use: java -jar target/benchmarks.jar -rf json + To run a specific benchmark file use: + java -jar target/benchmarks.jar + JSON output can be viewed quickly by drag-dropping on http://jmh.morethan.io/ \ No newline at end of file diff --git a/benchmarking/src/main/java/org/jalview/HiddenColsIteratorsBenchmark.java b/benchmarking/src/main/java/org/jalview/HiddenColsIteratorsBenchmark.java index 1710c85..6d5863b 100644 --- a/benchmarking/src/main/java/org/jalview/HiddenColsIteratorsBenchmark.java +++ b/benchmarking/src/main/java/org/jalview/HiddenColsIteratorsBenchmark.java @@ -105,7 +105,7 @@ public class HiddenColsIteratorsBenchmark { int res = 0; int startx = tstate.visibleColumn; Iterator it = tstate.h.getBoundedStartIterator(startx, - startx+60, true); + startx+60); while (it.hasNext()) { res = it.next() - startx; @@ -123,7 +123,7 @@ public class HiddenColsIteratorsBenchmark { int blockEnd; int screenY = 0; Iterator it = tstate.h.getBoundedIterator(startx, - startx+60, true); + startx+60); while (it.hasNext()) { int[] region = it.next(); diff --git a/benchmarking/src/main/java/org/jalview/HiddenColumnsBenchmark.java b/benchmarking/src/main/java/org/jalview/HiddenColumnsBenchmark.java index aa4ad22..6e4742a 100644 --- a/benchmarking/src/main/java/org/jalview/HiddenColumnsBenchmark.java +++ b/benchmarking/src/main/java/org/jalview/HiddenColumnsBenchmark.java @@ -112,13 +112,6 @@ public class HiddenColumnsBenchmark return tstate.h.findColumnPosition(tstate.visibleColumn); } - /*@Benchmark - @BenchmarkMode({Mode.Throughput}) - public List benchFindHiddenRegionPositions(HiddenColsAndStartState tstate) - { - return tstate.h.findHiddenRegionPositions(); - }*/ - @Benchmark @BenchmarkMode({Mode.Throughput}) public int benchGetSize(HiddenColsAndStartState tstate) diff --git a/src/jalview/datamodel/HiddenColumns.java b/src/jalview/datamodel/HiddenColumns.java index fb1961e..399e083 100644 --- a/src/jalview/datamodel/HiddenColumns.java +++ b/src/jalview/datamodel/HiddenColumns.java @@ -133,15 +133,18 @@ public class HiddenColumns { LOCK.readLock().lock(); StringBuilder regionBuilder = new StringBuilder(); - Iterator it = new RegionsIterator(); - while (it.hasNext()) + if (hiddenColumns != null) { - int[] range = it.next(); - regionBuilder.append(delimiter).append(range[0]).append(between) - .append(range[1]); - if (!it.hasNext()) + Iterator it = hiddenColumns.iterator(); + while (it.hasNext()) { - regionBuilder.deleteCharAt(0); + int[] range = it.next(); + regionBuilder.append(delimiter).append(range[0]).append(between) + .append(range[1]); + if (!it.hasNext()) + { + regionBuilder.deleteCharAt(0); + } } } return regionBuilder.toString(); @@ -162,13 +165,15 @@ public class HiddenColumns { LOCK.readLock().lock(); int size = 0; - Iterator it = new RegionsIterator(); - while (it.hasNext()) + if (hiddenColumns != null) { - int[] range = it.next(); - size += range[1] - range[0] + 1; + Iterator it = hiddenColumns.iterator(); + while (it.hasNext()) + { + int[] range = it.next(); + size += range[1] - range[0] + 1; + } } - return size; } finally { @@ -224,7 +229,7 @@ public class HiddenColumns return false; } - Iterator it = new RegionsIterator(); + Iterator it = hiddenColumns.iterator(); Iterator thatit = that.iterator(); while (it.hasNext()) { @@ -256,13 +261,16 @@ public class HiddenColumns LOCK.readLock().lock(); int result = column; - Iterator it = new RegionsIterator(); - while (it.hasNext()) + if (hiddenColumns != null) { - int[] region = it.next(); - if (result >= region[0]) + Iterator it = hiddenColumns.iterator(); + while (it.hasNext()) { - result += region[1] - region[0] + 1; + int[] region = it.next(); + if (result >= region[0]) + { + result += region[1] - region[0] + 1; + } } } @@ -398,7 +406,7 @@ public class HiddenColumns LOCK.readLock().lock(); if (hiddenColumns != null) { - Iterator it = new RegionsIterator(); + Iterator it = hiddenColumns.iterator(); while (it.hasNext()) { int[] region = it.next(); @@ -504,6 +512,18 @@ public class HiddenColumns } } + /** + * Insert [start, range] at the region at index i in hiddenColumns, if + * feasible + * + * @param i + * index to insert at + * @param start + * start of range to insert + * @param end + * end of range to insert + * @return true if range was successfully inserted + */ private boolean insertRangeAtRegion(int i, int start, int end) { boolean added = false; @@ -550,7 +570,7 @@ public class HiddenColumns break; } region[1] = Math.max(nextRegion[1], end); - hiddenColumns.remove(i + 1); + hiddenColumns.subList(i + 1, i + 2).clear(); } added = true; } @@ -570,7 +590,7 @@ public class HiddenColumns { LOCK.readLock().lock(); - Iterator it = new RegionsIterator(); + Iterator it = new RegionsIterator(column, column); while (it.hasNext()) { int[] region = it.next(); @@ -670,7 +690,7 @@ public class HiddenColumns // Simply walk along the sequence whilst watching for hidden column // boundaries - Iterator regions = new RegionsIterator(); + Iterator regions = hiddenColumns.iterator(); int hideStart = seq.getLength(); int hideEnd = -1; int visPrev = 0; @@ -887,7 +907,7 @@ public class HiddenColumns try { LOCK.writeLock().lock(); - Iterator it = new RegionsIterator(); + Iterator it = hiddenColumns.iterator(); while (it.hasNext()) { int[] region = it.next(); @@ -896,7 +916,6 @@ public class HiddenColumns sel.addElement(j); } } - hiddenColumns = null; } finally { @@ -915,7 +934,7 @@ public class HiddenColumns try { LOCK.writeLock().lock(); - Iterator it = new RegionsIterator(); + Iterator it = new RegionsIterator(start, start); while (it.hasNext()) { int[] region = it.next(); @@ -925,8 +944,7 @@ public class HiddenColumns { sel.addElement(j); } - - hiddenColumns.remove(region); + it.remove(); break; } else if (start < region[0]) @@ -1009,7 +1027,7 @@ public class HiddenColumns // of // preceding visible gaps // update hidden columns at the same time - Iterator regions = new RegionsIterator(); + Iterator regions = hiddenColumns.iterator(); ArrayList newhidden = new ArrayList<>(); int numGapsBefore = 0; @@ -1114,7 +1132,7 @@ public class HiddenColumns { LOCK.readLock().lock(); int hashCode = 1; - Iterator it = new RegionsIterator(); + Iterator it = hiddenColumns.iterator(); while (it.hasNext()) { int[] hidden = it.next(); @@ -1166,7 +1184,7 @@ public class HiddenColumns { return; } - Iterator it = new RegionsIterator(); + Iterator it = hiddenColumns.iterator(); while (it.hasNext()) { int[] range = it.next(); @@ -1202,7 +1220,7 @@ public class HiddenColumns return new int[] { startPos, endPos }; } - Iterator it = new RegionsIterator(); + Iterator it = hiddenColumns.iterator(); while (it.hasNext()) { int[] range = it.next(); @@ -1360,7 +1378,7 @@ public class HiddenColumns } else { - return new VisibleContigsIterator(start, end - 1, true); + return new VisibleContigsIterator(start, end + 1, true); } } @@ -1384,38 +1402,12 @@ public class HiddenColumns // current column in hiddenColumns private int[] nextRegion = null; - // Constructor with bounds - RegionsIterator(int lowerBound, int upperBound) - { - init(lowerBound, upperBound); - } + private int[] currentRegion = null; - // Unbounded constructor - RegionsIterator() - { - if (hiddenColumns != null) - { - // iterator over full hiddenColumns collection - int last = hiddenColumns.get(hiddenColumns.size() - 1)[1]; - init(0, last); - } - else - { - // empty iterator - init(0, 0); - } - } + private int removedIndex = -1; - /** - * Construct an iterator over hiddenColums bounded at - * [lowerBound,upperBound] - * - * @param lowerBound - * lower bound to iterate from - * @param upperBound - * upper bound to iterate to - */ - private void init(int lowerBound, int upperBound) + // Constructor with bounds + RegionsIterator(int lowerBound, int upperBound) { start = lowerBound; end = upperBound; @@ -1446,7 +1438,7 @@ public class HiddenColumns @Override public int[] next() { - int[] region = nextRegion; + currentRegion = nextRegion; currentPosition++; if (currentPosition < hiddenColumns.size()) { @@ -1456,7 +1448,24 @@ public class HiddenColumns { nextRegion = null; } - return region; + return currentRegion; + } + + @Override + public void remove() + { + if ((currentRegion != null) && (removedIndex != currentPosition)) + { + currentPosition--; + hiddenColumns.subList(currentPosition, currentPosition + 1).clear(); + removedIndex = currentPosition; + } + else + { + // already removed element last returned by next() + // or next() has not yet been called + throw new IllegalStateException(); + } } }