X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fdatamodel%2FColumnSelection.java;h=c2327819c81923ce95e11d96da730e9544718cc4;hb=e224b72118c12cd77ae96f0f8d86c123e0416dc7;hp=c895b2f3c8b2a1c604f416bf585dbac5ff09e9d8;hpb=5655997c202bfcb4ab2778999d785490d3a7dbae;p=jalview.git diff --git a/src/jalview/datamodel/ColumnSelection.java b/src/jalview/datamodel/ColumnSelection.java index c895b2f..c232781 100644 --- a/src/jalview/datamodel/ColumnSelection.java +++ b/src/jalview/datamodel/ColumnSelection.java @@ -1,49 +1,229 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle - * - * This program 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 2 + * 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. - * - * This program 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. - * + * + * 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 this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + * 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 jalview.viewmodel.annotationfilter.AnnotationFilterParameter; +import jalview.viewmodel.annotationfilter.AnnotationFilterParameter.SearchableAnnotationField; -import java.util.*; +import java.util.ArrayList; +import java.util.BitSet; +import java.util.Collections; +import java.util.List; +import java.util.Vector; /** * NOTE: Columns are zero based. */ public class ColumnSelection { - Vector selected = new Vector(); + private class IntList + { + /* + * list of selected columns (ordered by selection order, not column order) + */ + private List order = new ArrayList(); + + /** + * bitfield for column selection - allows quick lookup + */ + private BitSet selected = new BitSet(); + + /** + * adds a new column i to the selection - only if i is not already selected + * + * @param i + */ + public void add(int i) + { + if (!selected.get(i)) + { + order.add(Integer.valueOf(i)); + selected.set(i); + } + } + + public void clear() + { + order.clear(); + selected.clear(); + } + + public void remove(int col) + { + + Integer colInt = new Integer(col); + + if (selected.get(col)) + { + // if this ever changes to List.remove(), ensure Integer not int + // argument + // as List.remove(int i) removes the i'th item which is wrong + order.remove(colInt); + selected.clear(col); + } + } + + public boolean contains(Integer colInt) + { + return selected.get(colInt); + } + + public boolean isEmpty() + { + return order.isEmpty(); + } + + public List getList() + { + return order; + } + + public int size() + { + return order.size(); + } + + /** + * gets the column that was selected first, second or i'th + * + * @param i + * @return + */ + public int elementAt(int i) + { + return order.get(i); + } + + protected boolean pruneColumnList(final List shifts) + { + int s = 0, t = shifts.size(); + int[] sr = shifts.get(s++); + boolean pruned = false; + int i = 0, j = order.size(); + while (i < j && s <= t) + { + int c = order.get(i++).intValue(); + if (sr[0] <= c) + { + if (sr[1] + sr[0] >= c) + { // sr[1] -ve means inseriton. + order.remove(--i); + selected.clear(c); + j--; + } + else + { + if (s < t) + { + sr = shifts.get(s); + } + s++; + } + } + } + return pruned; + } + + /** + * shift every selected column at or above start by change + * + * @param start + * - leftmost column to be shifted + * @param change + * - delta for shift + */ + public void compensateForEdits(int start, int change) + { + BitSet mask = new BitSet(); + for (int i = 0; i < order.size(); i++) + { + int temp = order.get(i); + + if (temp >= start) + { + // clear shifted bits and update List of selected columns + selected.clear(temp); + mask.set(temp - change); + order.set(i, new Integer(temp - change)); + } + } + // lastly update the bitfield all at once + selected.or(mask); + } + + public boolean isSelected(int column) + { + return selected.get(column); + } + + public int getMaxColumn() + { + return selected.length() - 1; + } + + public int getMinColumn() + { + return selected.get(0) ? 0 : selected.nextSetBit(0); + } - //Vector of int [] {startCol, endCol} - Vector hiddenColumns; + /** + * @return a series of selection intervals along the range + */ + public List getRanges() + { + List rlist = new ArrayList(); + if (selected.isEmpty()) + { + return rlist; + } + int next = selected.nextSetBit(0), clear = -1; + while (next != -1) + { + clear = selected.nextClearBit(next); + rlist.add(new int[] { next, clear - 1 }); + next = selected.nextSetBit(clear); + } + return rlist; + } + } + + IntList selected = new IntList(); + + /* + * list of hidden column [start, end] ranges; the list is maintained in + * ascending start column order + */ + Vector hiddenColumns; /** * Add a column to the selection - * - * @param col index of column + * + * @param col + * index of column */ public void addElement(int col) { - Integer column = new Integer(col); - if (!selected.contains(column)) - { - selected.addElement(column); - } + selected.add(col); } /** @@ -51,346 +231,376 @@ public class ColumnSelection */ public void clear() { - selected.removeAllElements(); + selected.clear(); } /** - * removes col from selection - * - * @param col index of column to be removed + * Removes value 'col' from the selection (not the col'th item) + * + * @param col + * index of column to be removed */ public void removeElement(int col) { - Integer colInt = new Integer(col); - - if (selected.contains(colInt)) - { - selected.removeElement(colInt); - } + selected.remove(col); } /** * removes a range of columns from the selection - * @param start int - first column in range to be removed - * @param end int - last col + * + * @param start + * int - first column in range to be removed + * @param end + * int - last col */ public void removeElements(int start, int end) { Integer colInt; - for(int i=start; i