X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FHiddenColumns.java;h=c40c1050040e841732de481b8922e4fcc25ebaa1;hb=65c6bb5c1deac2163c9e2a75287de5e59c8728a9;hp=2edb3f1d8905aa6a30854b146329200fcc5d72b8;hpb=d4c27a996e22436dd4c4d8f64f6341d5315ff621;p=jalview.git diff --git a/src/jalview/datamodel/HiddenColumns.java b/src/jalview/datamodel/HiddenColumns.java index 2edb3f1..c40c105 100644 --- a/src/jalview/datamodel/HiddenColumns.java +++ b/src/jalview/datamodel/HiddenColumns.java @@ -1,861 +1,704 @@ +/* + * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$) + * Copyright (C) $$Year-Rel$$ The Jalview Authors + * + * This file is part of Jalview. + * + * Jalview is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 + * of the License, or (at your option) any later version. + * + * Jalview is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Jalview. If not, see . + * The Jalview Authors are detailed in the 'AUTHORS' file. + */ package jalview.datamodel; -import jalview.util.Comparison; -import jalview.util.ShiftList; - import java.util.ArrayList; -import java.util.Collections; +import java.util.Arrays; +import java.util.BitSet; +import java.util.Iterator; import java.util.List; -import java.util.Vector; - +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * This class manages the collection of hidden columns associated with an + * alignment. To iterate over the collection, or over visible columns/regions, + * use an iterator obtained from one of: + * + * - getBoundedIterator: iterates over the hidden regions, within some bounds, + * returning *absolute* positions + * + * - getBoundedStartIterator: iterates over the start positions of hidden + * regions, within some bounds, returning *visible* positions + * + * - getVisContigsIterator: iterates over visible regions in a range, returning + * *absolute* positions + * + * - getVisibleColsIterator: iterates over the visible *columns* + * + * For performance reasons, provide bounds where possible. Note that column + * numbering begins at 0 throughout this class. + * + * @author kmourao + */ + +/* Implementation notes: + * + * Methods which change the hiddenColumns collection should use a writeLock to + * prevent other threads accessing the hiddenColumns collection while changes + * are being made. They should also reset the hidden columns cursor, and either + * update the hidden columns count, or set it to 0 (so that it will later be + * updated when needed). + * + * + * Methods which only need read access to the hidden columns collection should + * use a readLock to prevent other threads changing the hidden columns + * collection while it is in use. + */ public class HiddenColumns { + private static final int HASH_MULTIPLIER = 31; + + private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock(); + + /* + * Cursor which tracks the last used hidden columns region, and the number + * of hidden columns up to (but not including) that region. + */ + private HiddenColumnsCursor cursor = new HiddenColumnsCursor(); + + /* + * cache of the number of hidden columns: must be kept up to date by methods + * which add or remove hidden columns + */ + private int numColumns = 0; + /* * list of hidden column [start, end] ranges; the list is maintained in * ascending start column order */ - private Vector hiddenColumns; + private List hiddenColumns = new ArrayList<>(); /** - * This Method is used to return all the HiddenColumn regions - * - * @return empty list or List of hidden column intervals + * Constructor */ - public List getHiddenRegions() + public HiddenColumns() { - return hiddenColumns == null ? Collections. emptyList() - : hiddenColumns; } /** - * Find the number of hidden columns + * Copy constructor * - * @return number of hidden columns + * @param copy + * the HiddenColumns object to copy from */ - public int getSize() + public HiddenColumns(HiddenColumns copy) { - int size = 0; - if (hasHidden()) - { - for (int[] range : hiddenColumns) - { - size += range[1] - range[0] + 1; - } - } - return size; + this(copy, Integer.MIN_VALUE, Integer.MAX_VALUE, 0); } /** - * Answers if there are any hidden columns + * Copy constructor within bounds and with offset. Copies hidden column + * regions fully contained between start and end, and offsets positions by + * subtracting offset. * - * @return true if there are hidden columns - */ - public boolean hasHidden() - { - return (hiddenColumns != null) && (!hiddenColumns.isEmpty()); - } - - @Override - public boolean equals(Object obj) - { - if (!(obj instanceof HiddenColumns)) - { - return false; - } - HiddenColumns that = (HiddenColumns) obj; - - /* - * check hidden columns are either both null, or match - */ - if (this.hiddenColumns == null) - { - return (that.hiddenColumns == null); - } - if (that.hiddenColumns == null - || that.hiddenColumns.size() != this.hiddenColumns.size()) - { - return false; - } - int i = 0; - for (int[] thisRange : hiddenColumns) - { - int[] thatRange = that.hiddenColumns.get(i++); - if (thisRange[0] != thatRange[0] || thisRange[1] != thatRange[1]) - { - return false; - } - } - return true; - } - - /** - * Return absolute column index for a visible column index + * @param copy + * HiddenColumns instance to copy from + * @param start + * lower bound to copy from + * @param end + * upper bound to copy to + * @param offset + * offset to subtract from each region boundary position * - * @param column - * int column index in alignment view (count from zero) - * @return alignment column index for column */ - public int adjustForHiddenColumns(int column) + public HiddenColumns(HiddenColumns copy, int start, int end, int offset) { - int result = column; - if (hiddenColumns != null) + try { - for (int i = 0; i < hiddenColumns.size(); i++) + LOCK.writeLock().lock(); + if (copy != null) { - int[] region = hiddenColumns.elementAt(i); - if (result >= region[0]) + numColumns = 0; + Iterator it = copy.getBoundedIterator(start, end); + while (it.hasNext()) { - result += region[1] - region[0] + 1; + int[] region = it.next(); + // still need to check boundaries because iterator returns + // all overlapping regions and we need contained regions + if (region[0] >= start && region[1] <= end) + { + hiddenColumns.add( + new int[] + { region[0] - offset, region[1] - offset }); + numColumns += region[1] - region[0] + 1; + } } + cursor = new HiddenColumnsCursor(hiddenColumns); } + } finally + { + LOCK.writeLock().unlock(); } - return result; } /** - * Use this method to find out where a column will appear in the visible - * alignment when hidden columns exist. If the column is not visible, then the - * left-most visible column will always be returned. + * Adds the specified column range to the hidden columns collection * - * @param hiddenColumn - * the column index in the full alignment including hidden columns - * @return the position of the column in the visible alignment + * @param start + * start of range to add (absolute position in alignment) + * @param end + * end of range to add (absolute position in alignment) */ - public int findColumnPosition(int hiddenColumn) + public void hideColumns(int start, int end) { - int result = hiddenColumn; - if (hiddenColumns != null) + try { - int index = 0; - int[] region; - do + LOCK.writeLock().lock(); + + int previndex = 0; + int prevHiddenCount = 0; + int regionindex = 0; + if (!hiddenColumns.isEmpty()) { - region = hiddenColumns.elementAt(index++); - if (hiddenColumn > region[1]) + // set up cursor reset values + HiddenCursorPosition cursorPos = cursor.findRegionForColumn(start, false); + regionindex = cursorPos.getRegionIndex(); + + if (regionindex > 0) { - result -= region[1] + 1 - region[0]; + // get previous index and hidden count for updating the cursor later + previndex = regionindex - 1; + int[] prevRegion = hiddenColumns.get(previndex); + prevHiddenCount = cursorPos.getHiddenSoFar() + - (prevRegion[1] - prevRegion[0] + 1); } - } while ((hiddenColumn > region[1]) && (index < hiddenColumns.size())); + } - if (hiddenColumn >= region[0] && hiddenColumn <= region[1]) + // new range follows everything else; check first to avoid looping over + // whole hiddenColumns collection + if (hiddenColumns.isEmpty() + || start > hiddenColumns.get(hiddenColumns.size() - 1)[1]) + { + hiddenColumns.add(new int[] { start, end }); + numColumns += end - start + 1; + } + else { - // Here the hidden column is within a region, so - // we want to return the position of region[0]-1, adjusted for any - // earlier hidden columns. - // Calculate the difference between the actual hidden col position - // and region[0]-1, and then subtract from result to convert result from - // the adjusted hiddenColumn value to the adjusted region[0]-1 value - - // However, if the region begins at 0 we cannot return region[0]-1 - // just return 0 - if (region[0] == 0) + /* + * traverse existing hidden ranges and insert / amend / append as + * appropriate + */ + boolean added = false; + if (regionindex > 0) { - return 0; + added = insertRangeAtRegion(regionindex - 1, start, end); } - else + if (!added && regionindex < hiddenColumns.size()) { - return result - (hiddenColumn - region[0] + 1); + insertRangeAtRegion(regionindex, start, end); } } + + // reset the cursor to just before our insertion point: this saves + // a lot of reprocessing in large alignments + cursor = new HiddenColumnsCursor(hiddenColumns, previndex, + prevHiddenCount); + } finally + { + LOCK.writeLock().unlock(); } - return result; // return the shifted position after removing hidden columns. } /** - * Find the visible column which is a given visible number of columns to the - * left of another visible column. i.e. for a startColumn x, the column which - * is distance 1 away will be column x-1. + * Insert [start, range] at the region at index i in hiddenColumns, if + * feasible * - * @param visibleDistance - * the number of visible columns to offset by - * @param startColumn - * the column to start from - * @return the position of the column in the visible alignment + * @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 */ - public int subtractVisibleColumns(int visibleDistance, int startColumn) + private boolean insertRangeAtRegion(int i, int start, int end) { - int distance = visibleDistance; - - // in case startColumn is in a hidden region, move it to the left - int start = adjustForHiddenColumns(findColumnPosition(startColumn)); + boolean added = false; - // get index of hidden region to left of start - int index = getHiddenIndexLeft(start); - if (index == -1) + int[] region = hiddenColumns.get(i); + if (end < region[0] - 1) { - // no hidden regions to left of startColumn - return start - distance; + /* + * insert discontiguous preceding range + */ + hiddenColumns.add(i, new int[] { start, end }); + numColumns += end - start + 1; + added = true; } - - // walk backwards through the alignment subtracting the counts of visible - // columns from distance - int[] region; - int gap = 0; - int nextstart = start; - - while ((index > -1) && (distance - gap > 0)) + else if (end <= region[1]) { - // subtract the gap to right of region from distance - distance -= gap; - start = nextstart; - - // calculate the next gap - region = hiddenColumns.get(index); - gap = start - region[1]; - - // set start to just to left of current region - nextstart = region[0] - 1; - index--; + /* + * new range overlaps existing, or is contiguous preceding it - adjust + * start column + */ + int oldstart = region[0]; + region[0] = Math.min(region[0], start); + numColumns += oldstart - region[0]; // new columns are between old and + // adjusted starts + added = true; } - - if (distance - gap > 0) + else if (start <= region[1] + 1) { - // fell out of loop because there are no more hidden regions - distance -= gap; - return nextstart - distance; + /* + * new range overlaps existing, or is contiguous following it - adjust + * start and end columns + */ + insertRangeAtOverlap(i, start, end, region); + added = true; } - return start - distance; - + return added; } /** - * Use this method to determine where the next hiddenRegion starts + * Insert a range whose start position overlaps an existing region and/or is + * contiguous to the right of the region * - * @param hiddenRegion - * index of hidden region (counts from 0) - * @return column number in visible view + * @param i + * index to insert at + * @param start + * start of range to insert + * @param end + * end of range to insert + * @param region + * the overlapped/continued region */ - public int findHiddenRegionPosition(int hiddenRegion) + private void insertRangeAtOverlap(int i, int start, int end, int[] region) { - int result = 0; - if (hiddenColumns != null) - { - int index = 0; - int gaps = 0; - do - { - int[] region = hiddenColumns.elementAt(index); - if (hiddenRegion == 0) - { - return region[0]; - } - - gaps += region[1] + 1 - region[0]; - result = region[1] + 1; - index++; - } while (index <= hiddenRegion); - - result -= gaps; - } + int oldstart = region[0]; + int oldend = region[1]; + region[0] = Math.min(region[0], start); + region[1] = Math.max(region[1], end); - return result; - } + numColumns += oldstart - region[0]; - /** - * This method returns the rightmost limit of a region of an alignment with - * hidden columns. In otherwords, the next hidden column. - * - * @param index - * int - */ - public int getHiddenBoundaryRight(int alPos) - { - if (hiddenColumns != null) + /* + * also update or remove any subsequent ranges + * that are overlapped + */ + int endi = i; + while (endi < hiddenColumns.size() - 1) { - int index = 0; - do + int[] nextRegion = hiddenColumns.get(endi + 1); + if (nextRegion[0] > end + 1) { - int[] region = hiddenColumns.elementAt(index); - if (alPos < region[0]) - { - return region[0]; - } - - index++; - } while (index < hiddenColumns.size()); + /* + * gap to next hidden range - no more to update + */ + break; + } + numColumns -= nextRegion[1] - nextRegion[0] + 1; + region[1] = Math.max(nextRegion[1], end); + endi++; } - - return alPos; - + numColumns += region[1] - oldend; + hiddenColumns.subList(i + 1, endi + 1).clear(); } /** - * This method returns the leftmost limit of a region of an alignment with - * hidden columns. In otherwords, the previous hidden column. + * hide a list of ranges * - * @param index - * int + * @param ranges */ - public int getHiddenBoundaryLeft(int alPos) + public void hideList(List ranges) { - if (hiddenColumns != null) + try { - int index = hiddenColumns.size() - 1; - do + LOCK.writeLock().lock(); + for (int[] r : ranges) { - int[] region = hiddenColumns.elementAt(index); - if (alPos > region[1]) - { - return region[1]; - } + hideColumns(r[0], r[1]); + } + cursor = new HiddenColumnsCursor(hiddenColumns); - index--; - } while (index > -1); + } finally + { + LOCK.writeLock().unlock(); } - - return alPos; - } /** - * This method returns the index of the hidden region to the left of a column - * position. If the column is in a hidden region it returns the index of the - * region to the left. If there is no hidden region to the left it returns -1. - * - * @param pos - * int + * Unhides, and adds to the selection list, all hidden columns */ - private int getHiddenIndexLeft(int pos) + public void revealAllHiddenColumns(ColumnSelection sel) { - if (hiddenColumns != null) + try { - int index = hiddenColumns.size() - 1; - do + LOCK.writeLock().lock(); + + for (int[] region : hiddenColumns) { - int[] region = hiddenColumns.elementAt(index); - if (pos > region[1]) + for (int j = region[0]; j < region[1] + 1; j++) { - return index; + sel.addElement(j); } + } + hiddenColumns.clear(); + cursor = new HiddenColumnsCursor(hiddenColumns); + numColumns = 0; - index--; - } while (index > -1); + } finally + { + LOCK.writeLock().unlock(); } - - return -1; - } /** - * Adds the specified column range to the hidden columns + * Reveals, and marks as selected, the hidden column range with the given + * start column * * @param start - * @param end + * the start column to look for + * @param sel + * the column selection to add the hidden column range to */ - public void hideColumns(int start, int end) + public void revealHiddenColumns(int start, ColumnSelection sel) { - if (hiddenColumns == null) + try { - hiddenColumns = new Vector(); - } + LOCK.writeLock().lock(); - /* - * traverse existing hidden ranges and insert / amend / append as - * appropriate - */ - for (int i = 0; i < hiddenColumns.size(); i++) - { - int[] region = hiddenColumns.elementAt(i); - - if (end < region[0] - 1) - { - /* - * insert discontiguous preceding range - */ - hiddenColumns.insertElementAt(new int[] { start, end }, i); - return; - } - - if (end <= region[1]) + if (!hiddenColumns.isEmpty()) { - /* - * new range overlaps existing, or is contiguous preceding it - adjust - * start column - */ - region[0] = Math.min(region[0], start); - return; - } - - if (start <= region[1] + 1) - { - /* - * new range overlaps existing, or is contiguous following it - adjust - * start and end columns - */ - region[0] = Math.min(region[0], start); - region[1] = Math.max(region[1], end); + int regionIndex = cursor.findRegionForColumn(start, false) + .getRegionIndex(); - /* - * also update or remove any subsequent ranges - * that are overlapped - */ - while (i < hiddenColumns.size() - 1) + if (regionIndex != -1 && regionIndex != hiddenColumns.size()) { - int[] nextRegion = hiddenColumns.get(i + 1); - if (nextRegion[0] > end + 1) + // regionIndex is the region which either contains start + // or lies to the right of start + int[] region = hiddenColumns.get(regionIndex); + if (start == region[0]) { - /* - * gap to next hidden range - no more to update - */ - break; + for (int j = region[0]; j < region[1] + 1; j++) + { + sel.addElement(j); + } + int colsToRemove = region[1] - region[0] + 1; + hiddenColumns.remove(regionIndex); + numColumns -= colsToRemove; } - region[1] = Math.max(nextRegion[1], end); - hiddenColumns.remove(i + 1); } - return; } + } finally + { + LOCK.writeLock().unlock(); } - - /* - * remaining case is that the new range follows everything else - */ - hiddenColumns.addElement(new int[] { start, end }); } - public boolean isVisible(int column) + /** + * Output regions data as a string. String is in the format: + * reg0[0]reg0[1]reg1[0]reg1[1] ... regn[1] + * + * @param delimiter + * string to delimit regions + * @param betweenstring + * to put between start and end region values + * @return regions formatted according to delimiter and between strings + */ + public String regionsToString(String delimiter, String between) { - if (hiddenColumns != null) + try { - for (int[] region : hiddenColumns) + LOCK.readLock().lock(); + StringBuilder regionBuilder = new StringBuilder(); + + boolean first = true; + for (int[] range : hiddenColumns) { - if (column >= region[0] && column <= region[1]) + if (!first) { - return false; + regionBuilder.append(delimiter); } + else + { + first = false; + } + regionBuilder.append(range[0]).append(between).append(range[1]); + } - } - return true; + return regionBuilder.toString(); + } finally + { + LOCK.readLock().unlock(); + } } /** - * ColumnSelection + * Find the number of hidden columns + * + * @return number of hidden columns */ - public HiddenColumns() + public int getSize() { + return numColumns; } /** - * Copy constructor + * Get the number of distinct hidden regions * - * @param copy + * @return number of regions */ - public HiddenColumns(HiddenColumns copy) + public int getNumberOfRegions() { - if (copy != null) + try { - if (copy.hiddenColumns != null) - { - hiddenColumns = new Vector(copy.hiddenColumns.size()); - for (int i = 0, j = copy.hiddenColumns.size(); i < j; i++) - { - int[] rh, cp; - rh = copy.hiddenColumns.elementAt(i); - if (rh != null) - { - cp = new int[rh.length]; - System.arraycopy(rh, 0, cp, 0, rh.length); - hiddenColumns.addElement(cp); - } - } - } + LOCK.readLock().lock(); + return hiddenColumns.size(); + } finally + { + LOCK.readLock().unlock(); } } - /** - * propagate shift in alignment columns to column selection - * - * @param start - * beginning of edit - * @param left - * shift in edit (+ve for removal, or -ve for inserts) - */ - public List compensateForEdit(int start, int change, - ColumnSelection sel) + @Override + public boolean equals(Object obj) { - List deletedHiddenColumns = null; - - if (hiddenColumns != null) + try { - deletedHiddenColumns = new ArrayList(); - int hSize = hiddenColumns.size(); - for (int i = 0; i < hSize; i++) + LOCK.readLock().lock(); + + if (!(obj instanceof HiddenColumns)) { - int[] region = hiddenColumns.elementAt(i); - if (region[0] > start && start + change > region[1]) - { - deletedHiddenColumns.add(region); + return false; + } + HiddenColumns that = (HiddenColumns) obj; - hiddenColumns.removeElementAt(i); - i--; - hSize--; - continue; - } + /* + * check hidden columns are either both null, or match + */ - if (region[0] > start) - { - region[0] -= change; - region[1] -= change; - } + if (that.hiddenColumns.size() != this.hiddenColumns.size()) + { + return false; + } - if (region[0] < 0) + Iterator it = this.iterator(); + Iterator thatit = that.iterator(); + while (it.hasNext()) + { + if (!(Arrays.equals(it.next(), thatit.next()))) { - region[0] = 0; + return false; } - } + return true; - this.revealHiddenColumns(0, sel); + } finally + { + LOCK.readLock().unlock(); } - - return deletedHiddenColumns; } /** - * propagate shift in alignment columns to column selection special version of - * compensateForEdit - allowing for edits within hidden regions + * Return absolute column index for a visible column index * - * @param start - * beginning of edit - * @param left - * shift in edit (+ve for removal, or -ve for inserts) + * @param column + * int column index in alignment view (count from zero) + * @return alignment column index for column */ - public void compensateForDelEdits(int start, int change) + public int visibleToAbsoluteColumn(int column) { - if (hiddenColumns != null) + try { - for (int i = 0; i < hiddenColumns.size(); i++) - { - int[] region = hiddenColumns.elementAt(i); - if (region[0] >= start) - { - region[0] -= change; - } - if (region[1] >= start) - { - region[1] -= change; - } - if (region[1] < region[0]) - { - hiddenColumns.removeElementAt(i--); - } + LOCK.readLock().lock(); + int result = column; - if (region[0] < 0) - { - region[0] = 0; - } - if (region[1] < 0) - { - region[1] = 0; - } + if (!hiddenColumns.isEmpty()) + { + result += cursor.findRegionForColumn(column, true) + .getHiddenSoFar(); } + + return result; + } finally + { + LOCK.readLock().unlock(); } } /** - * return all visible segments between the given start and end boundaries + * Use this method to find out where a column will appear in the visible + * alignment when hidden columns exist. If the column is not visible, then the + * index of the next visible column on the left will be returned (or 0 if + * there is no visible column on the left) * - * @param start - * (first column inclusive from 0) - * @param end - * (last column - not inclusive) - * @return int[] {i_start, i_end, ..} where intervals lie in - * start<=i_start<=i_end