From: kiramt Date: Mon, 29 Jan 2018 14:37:30 +0000 (+0000) Subject: JAL-2759 Combine findRegionForColumn code in HiddenColumnsCursor X-Git-Tag: Release_2_10_4~55^2~1^2~18 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=ecdbc96815298ac853543640881ef8d2384b48a1;p=jalview.git JAL-2759 Combine findRegionForColumn code in HiddenColumnsCursor --- diff --git a/src/jalview/datamodel/HiddenColumns.java b/src/jalview/datamodel/HiddenColumns.java index 9a8669f..fd4a9b5 100644 --- a/src/jalview/datamodel/HiddenColumns.java +++ b/src/jalview/datamodel/HiddenColumns.java @@ -171,7 +171,8 @@ public class HiddenColumns if (!hiddenColumns.isEmpty()) { // set up cursor reset values - HiddenCursorPosition cursorPos = cursor.findRegionForColumn(start); + HiddenCursorPosition cursorPos = cursor.findRegionForColumn(start, + false); regionindex = cursorPos.getRegionIndex(); if (regionindex > 0) @@ -356,7 +357,7 @@ public class HiddenColumns if (!hiddenColumns.isEmpty()) { - int regionIndex = cursor.findRegionForColumn(start) + int regionIndex = cursor.findRegionForColumn(start, false) .getRegionIndex(); if (regionIndex != -1 && regionIndex != hiddenColumns.size()) @@ -544,7 +545,8 @@ public class HiddenColumns if (!hiddenColumns.isEmpty()) { - result += cursor.findRegionForVisColumn(column).getHiddenSoFar(); + result += cursor.findRegionForColumn(column, true) + .getHiddenSoFar(); } return result; @@ -574,7 +576,7 @@ public class HiddenColumns if (!hiddenColumns.isEmpty()) { HiddenCursorPosition cursorPos = cursor - .findRegionForColumn(hiddenColumn); + .findRegionForColumn(hiddenColumn, false); int index = cursorPos.getRegionIndex(); int hiddenBeforeCol = cursorPos.getHiddenSoFar(); @@ -656,7 +658,8 @@ public class HiddenColumns LOCK.readLock().lock(); if (!hiddenColumns.isEmpty()) { - int index = cursor.findRegionForColumn(alPos).getRegionIndex(); + int index = cursor.findRegionForColumn(alPos, false) + .getRegionIndex(); if (left && index > 0) { @@ -700,7 +703,8 @@ public class HiddenColumns { LOCK.readLock().lock(); - int regionindex = cursor.findRegionForColumn(column).getRegionIndex(); + int regionindex = cursor.findRegionForColumn(column, false) + .getRegionIndex(); if (regionindex > -1 && regionindex < hiddenColumns.size()) { int[] region = hiddenColumns.get(regionindex); @@ -846,7 +850,7 @@ public class HiddenColumns if (!hiddenColumns.isEmpty()) { - HiddenCursorPosition pos = cursor.findRegionForColumn(start); + HiddenCursorPosition pos = cursor.findRegionForColumn(start, false); int index = pos.getRegionIndex(); int startindex = index; // first index in hiddenColumns to remove @@ -863,7 +867,7 @@ public class HiddenColumns } } - pos = cursor.findRegionForColumn(end); + pos = cursor.findRegionForColumn(end, false); index = pos.getRegionIndex(); int endindex = index - 1; // last index in hiddenColumns to remove @@ -973,7 +977,7 @@ public class HiddenColumns if (!hiddenColumns.isEmpty()) { // look for a region ending just before adjres - int regionindex = cursor.findRegionForColumn(adjres - 1) + int regionindex = cursor.findRegionForColumn(adjres - 1, false) .getRegionIndex(); if (regionindex < hiddenColumns.size() && hiddenColumns.get(regionindex)[1] == adjres - 1) @@ -1055,7 +1059,7 @@ public class HiddenColumns // region we'll get the cursor pointing to the region before, which is // what we want HiddenCursorPosition pos = cursor - .findRegionForColumn(absoluteStart - 1); + .findRegionForColumn(absoluteStart - 1, false); return new BoundedStartRegionIterator(pos, start, end, hiddenColumns); diff --git a/src/jalview/datamodel/HiddenColumnsCursor.java b/src/jalview/datamodel/HiddenColumnsCursor.java index 3501fce..89c4f99 100644 --- a/src/jalview/datamodel/HiddenColumnsCursor.java +++ b/src/jalview/datamodel/HiddenColumnsCursor.java @@ -118,68 +118,10 @@ public class HiddenColumnsCursor * @return cursor pointing to hidden region containing the column (if hidden) * or to the right of the column (if visible) */ - protected HiddenCursorPosition findRegionForColumn(int column) + /*protected HiddenCursorPosition findRegionForColumn(int column) { - if (hiddenColumns.isEmpty()) - { - return null; - } - - HiddenCursorPosition oldpos = cursorPos; - int index = oldpos.getRegionIndex(); - int hiddenCount = oldpos.getHiddenSoFar(); - - if (index == hiddenColumns.size()) - { - // went past the end of hiddenColumns collection last time - index--; - int[] region = hiddenColumns.get(index); - hiddenCount -= region[1] - region[0] + 1; - } - - // this if statement excludes case where column is in current region - // - no changes needed - if (column < firstColumn) - { - index = 0; - hiddenCount = 0; - } - // column is after current region - else if (column > hiddenColumns.get(index)[1]) - { - // iterate from where we are now, if we're lucky we'll be close by - // (but still better than iterating from 0) - // stop when we find the region *before* column - // i.e. the next region starts after column or if not, ends after column - while ((index < hiddenColumns.size()) - && (column > hiddenColumns.get(index)[1])) - { - int[] region = hiddenColumns.get(index); - hiddenCount += region[1] - region[0] + 1; - index++; - } - } - // column is before current region - else if (column < hiddenColumns.get(index)[0]) - { - // column is before or in the previous region - while ((index > 0) && (hiddenColumns.get(index - 1)[1] >= column)) - { - index--; - int[] region = hiddenColumns.get(index); - hiddenCount -= region[1] - region[0] + 1; - } - } - - if (index != oldpos.getRegionIndex() - || hiddenCount != oldpos.getHiddenSoFar()) - { - cursorPos = new HiddenCursorPosition(index, - hiddenCount); - return cursorPos; - } - return oldpos; - } + return findRegionForColumn(column, false); + }*/ /** * Get the cursor pointing to the hidden region just after a visible column @@ -189,13 +131,37 @@ public class HiddenColumnsCursor * column is visible) * @return cursor pointing to hidden region to the right of the column */ - protected HiddenCursorPosition findRegionForVisColumn(int column) + /* protected HiddenCursorPosition findRegionForVisColumn(int column) + { + return findRegionForColumn(column, true); + }*/ + + /** + * Get the cursor pointing to the hidden region that column is within (if + * column is hidden) or which is to the right of column (if column is + * visible). If no hidden columns are to the right, returns a cursor pointing + * to an imaginary hidden region beyond the end of the hidden columns + * collection (this ensures the count of previous hidden columns is correct). + * If hidden columns is empty returns null. + * + * @param column + * index of column in visible or absolute coordinates + * @param useVisible + * true if column is in visible coordinates, false if absolute + * @return cursor pointing to hidden region containing the column (if hidden) + * or to the right of the column (if visible) + */ + protected HiddenCursorPosition findRegionForColumn(int column, + boolean useVisible) { if (hiddenColumns.isEmpty()) { return null; } + // used to add in hiddenColumns offset when working with visible columns + int offset = (useVisible ? 1 : 0); + HiddenCursorPosition oldpos = cursorPos; int index = oldpos.getRegionIndex(); int hiddenCount = oldpos.getHiddenSoFar(); @@ -205,38 +171,47 @@ public class HiddenColumnsCursor index = 0; hiddenCount = 0; } + + // column is after current region else if ((index < hiddenColumns.size()) - && (hiddenColumns.get(index)[0] <= column + hiddenCount)) + && (hiddenColumns.get(index)[0] <= column + + offset * hiddenCount)) { // iterate from where we are now, if we're lucky we'll be close by // (but still better than iterating from 0) + // stop when we find the region *before* column + // i.e. the next region starts after column or if not, ends after column while ((index < hiddenColumns.size()) - && (hiddenColumns.get(index)[0] <= column + hiddenCount)) + && (((useVisible && hiddenColumns.get(index)[0] <= column + + offset * hiddenCount)) + || (!useVisible + && hiddenColumns.get(index)[1] < column))) { int[] region = hiddenColumns.get(index); hiddenCount += region[1] - region[0] + 1; index++; } } + + // column is before current region else { - while ((index > 0) - && (hiddenColumns.get(index - 1)[1] >= column + hiddenCount)) + // column is before or in the previous region + while ((index > 0) && (hiddenColumns.get(index - 1)[1] >= column + + offset * hiddenCount)) { index--; int[] region = hiddenColumns.get(index); hiddenCount -= region[1] - region[0] + 1; } - } if (index != oldpos.getRegionIndex() || hiddenCount != oldpos.getHiddenSoFar()) { - cursorPos = new HiddenCursorPosition(index, - hiddenCount); - return cursorPos; + return new HiddenCursorPosition(index, hiddenCount); } return oldpos; } + } diff --git a/test/jalview/datamodel/HiddenColumnsCursorTest.java b/test/jalview/datamodel/HiddenColumnsCursorTest.java index 0deed08..3b1bc55 100644 --- a/test/jalview/datamodel/HiddenColumnsCursorTest.java +++ b/test/jalview/datamodel/HiddenColumnsCursorTest.java @@ -38,7 +38,7 @@ public class HiddenColumnsCursorTest { HiddenColumnsCursor cursor = new HiddenColumnsCursor(); - HiddenCursorPosition pos = cursor.findRegionForColumn(20); + HiddenCursorPosition pos = cursor.findRegionForColumn(20, false); assertNull(pos); List hidden = new ArrayList<>(); @@ -47,44 +47,44 @@ public class HiddenColumnsCursorTest cursor = new HiddenColumnsCursor(hidden); - int regionIndex = cursor.findRegionForColumn(126).getRegionIndex(); + int regionIndex = cursor.findRegionForColumn(126, false).getRegionIndex(); assertEquals(2, regionIndex); - regionIndex = cursor.findRegionForColumn(125).getRegionIndex(); + regionIndex = cursor.findRegionForColumn(125, false).getRegionIndex(); assertEquals(1, regionIndex); - regionIndex = cursor.findRegionForColumn(108).getRegionIndex(); + regionIndex = cursor.findRegionForColumn(108, false).getRegionIndex(); assertEquals(1, regionIndex); - regionIndex = cursor.findRegionForColumn(104).getRegionIndex(); + regionIndex = cursor.findRegionForColumn(104, false).getRegionIndex(); assertEquals(1, regionIndex); - regionIndex = cursor.findRegionForColumn(103).getRegionIndex(); + regionIndex = cursor.findRegionForColumn(103, false).getRegionIndex(); assertEquals(1, regionIndex); - regionIndex = cursor.findRegionForColumn(77).getRegionIndex(); + regionIndex = cursor.findRegionForColumn(77, false).getRegionIndex(); assertEquals(1, regionIndex); - regionIndex = cursor.findRegionForColumn(76).getRegionIndex(); + regionIndex = cursor.findRegionForColumn(76, false).getRegionIndex(); assertEquals(0, regionIndex); - regionIndex = cursor.findRegionForColumn(53).getRegionIndex(); + regionIndex = cursor.findRegionForColumn(53, false).getRegionIndex(); assertEquals(0, regionIndex); - regionIndex = cursor.findRegionForColumn(52).getRegionIndex(); + regionIndex = cursor.findRegionForColumn(52, false).getRegionIndex(); assertEquals(0, regionIndex); - regionIndex = cursor.findRegionForColumn(0).getRegionIndex(); + regionIndex = cursor.findRegionForColumn(0, false).getRegionIndex(); assertEquals(0, regionIndex); hidden.add(new int[] { 138, 155 }); cursor = new HiddenColumnsCursor(hidden); - regionIndex = cursor.findRegionForColumn(160).getRegionIndex(); + regionIndex = cursor.findRegionForColumn(160, false).getRegionIndex(); assertEquals(3, regionIndex); - regionIndex = cursor.findRegionForColumn(100).getRegionIndex(); + regionIndex = cursor.findRegionForColumn(100, false).getRegionIndex(); assertEquals(1, regionIndex); } @@ -96,7 +96,7 @@ public class HiddenColumnsCursorTest { HiddenColumnsCursor cursor = new HiddenColumnsCursor(); - HiddenCursorPosition pos = cursor.findRegionForVisColumn(20); + HiddenCursorPosition pos = cursor.findRegionForColumn(20, true); assertNull(pos); List hidden = new ArrayList<>(); @@ -105,23 +105,29 @@ public class HiddenColumnsCursorTest cursor = new HiddenColumnsCursor(hidden); - int offset = cursor.findRegionForVisColumn(80).getHiddenSoFar(); + int offset = cursor.findRegionForColumn(80, true).getHiddenSoFar(); assertEquals(46, offset); - offset = cursor.findRegionForVisColumn(79).getHiddenSoFar(); + offset = cursor.findRegionForColumn(79, true).getHiddenSoFar(); assertEquals(24, offset); - offset = cursor.findRegionForVisColumn(53).getHiddenSoFar(); + offset = cursor.findRegionForColumn(53, true).getHiddenSoFar(); assertEquals(24, offset); - offset = cursor.findRegionForVisColumn(52).getHiddenSoFar(); + offset = cursor.findRegionForColumn(52, true).getHiddenSoFar(); assertEquals(0, offset); - offset = cursor.findRegionForVisColumn(10).getHiddenSoFar(); + offset = cursor.findRegionForColumn(10, true).getHiddenSoFar(); assertEquals(0, offset); - offset = cursor.findRegionForVisColumn(0).getHiddenSoFar(); + offset = cursor.findRegionForColumn(0, true).getHiddenSoFar(); assertEquals(0, offset); + + offset = cursor.findRegionForColumn(79, true).getHiddenSoFar(); + assertEquals(24, offset); + + offset = cursor.findRegionForColumn(80, true).getHiddenSoFar(); + assertEquals(46, offset); } }