import jalview.viewmodel.annotationfilter.AnnotationFilterParameter.SearchableAnnotationField;
import java.util.ArrayList;
+import java.util.BitSet;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
*/
public class ColumnSelection
{
- /*
- * list of selected columns (not ordered)
- */
- Vector<Integer> selected = new Vector<Integer>();
+ private class IntList
+ {
+ /*
+ * list of selected columns (ordered by selection order, not column order)
+ */
+ private List<Integer> order = new ArrayList<Integer>();
+
+ /**
+ * 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<Integer> 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<int[]> 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);
+ }
+ }
+ IntList selected = new IntList();
/*
* list of hidden column [start, end] ranges; the list is maintained in
* ascending start column order
*/
public void addElement(int col)
{
- Integer column = new Integer(col);
- if (!selected.contains(column))
- {
- selected.addElement(column);
- }
+ selected.add(col);
}
/**
*/
public void clear()
{
- selected.removeAllElements();
+ selected.clear();
}
/**
*/
public void removeElement(int col)
{
- Integer colInt = new Integer(col);
-
- if (selected.contains(colInt))
- {
- // 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
- selected.removeElement(colInt);
- }
+ selected.remove(col);
}
/**
colInt = new Integer(i);
if (selected.contains(colInt))
{
- selected.removeElement(colInt);
+ selected.remove(colInt);
}
}
}
*/
public List<Integer> getSelected()
{
- return selected;
+ return selected.getList();
}
/**
public List<int[]> compensateForEdit(int start, int change)
{
List<int[]> deletedHiddenColumns = null;
- for (int i = 0; i < selected.size(); i++)
- {
- int temp = selected.get(i);
-
- if (temp >= start)
- {
- // if this ever changes to List.set(), swap parameter order!!
- selected.setElementAt(new Integer(temp - change), i);
- }
- }
+ selected.compensateForEdits(start, change);
if (hiddenColumns != null)
{
*/
private void compensateForDelEdits(int start, int change)
{
- for (int i = 0; i < selected.size(); i++)
- {
- int temp = selected.get(i);
- if (temp >= start)
- {
- // if this ever changes to List.set(), must swap parameter order!!!
- selected.setElementAt(new Integer(temp - change), i);
- }
- }
+ selected.compensateForEdits(start, change);
if (hiddenColumns != null)
{
// operations.
}
- private boolean pruneColumnList(final List<int[]> shifts,
- Vector<Integer> list)
- {
- int s = 0, t = shifts.size();
- int[] sr = shifts.get(s++);
- boolean pruned = false;
- int i = 0, j = list.size();
- while (i < j && s <= t)
- {
- int c = list.elementAt(i++).intValue();
- if (sr[0] <= c)
- {
- if (sr[1] + sr[0] >= c)
- { // sr[1] -ve means inseriton.
- list.removeElementAt(--i);
- j--;
- }
- else
- {
- if (s < t)
- {
- sr = shifts.get(s);
- }
- s++;
- }
- }
- }
- return pruned;
- }
-
/**
* remove any hiddenColumns or selected columns and shift remaining based on a
* series of position, range deletions.
}
if (selected != null && selected.size() > 0)
{
- pruneColumnList(shifts, selected);
+ selected.pruneColumnList(shifts);
if (selected != null && selected.size() == 0)
{
selected = null;
{
while (!selected.isEmpty())
{
- int column = selected.get(0).intValue();
+ int column = selected.elementAt(0);
hideColumns(column);
}
{
if (copy.selected != null)
{
- selected = new Vector<Integer>();
+ selected = new IntList();
for (int i = 0, j = copy.selected.size(); i < j; i++)
{
- selected.addElement(copy.selected.elementAt(i));
+ selected.add(copy.selected.elementAt(i));
}
}
if (copy.hiddenColumns != null)
{
if (hiddenColumns != null && isVisible(col.intValue()))
{
- if (!selected.contains(col))
- {
- selected.addElement(col);
- }
+ selected.add(col);
}
}
}
*/
public void setElementsFrom(ColumnSelection colsel)
{
- selected = new Vector<Integer>();
+ selected = new IntList();
if (colsel.selected != null && colsel.selected.size() > 0)
{
if (hiddenColumns != null && hiddenColumns.size() > 0)