+ /**
+ * A class to hold an efficient representation of selected columns
+ */
+ private class IntList
+ {
+ /*
+ * list of selected columns (ordered by selection order, not column order)
+ */
+ private List<Integer> order;
+
+ /*
+ * an unmodifiable view of the selected columns list
+ */
+ private List<Integer> _uorder;
+
+ /**
+ * bitfield for column selection - allows quick lookup
+ */
+ private BitSet selected;
+
+ /**
+ * Constructor
+ */
+ IntList()
+ {
+ order = new ArrayList<>();
+ _uorder = Collections.unmodifiableList(order);
+ selected = new BitSet();
+ }
+
+ /**
+ * Copy constructor
+ *
+ * @param other
+ */
+ IntList(IntList other)
+ {
+ this();
+ if (other != null)
+ {
+ int j = other.size();
+ for (int i = 0; i < j; i++)
+ {
+ add(other.elementAt(i));
+ }
+ }
+ }
+
+ /**
+ * adds a new column i to the selection - only if i is not already selected
+ *
+ * @param i
+ */
+ void add(int i)
+ {
+ if (!selected.get(i))
+ {
+ order.add(Integer.valueOf(i));
+ selected.set(i);
+ }
+ }
+
+ void clear()
+ {
+ order.clear();
+ selected.clear();
+ }
+
+ void remove(int col)
+ {
+
+ Integer colInt = Integer.valueOf(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);
+ }
+ }
+
+ boolean contains(Integer colInt)
+ {
+ return selected.get(colInt);
+ }
+
+ boolean isEmpty()
+ {
+ return order.isEmpty();
+ }
+
+ /**
+ * Returns a read-only view of the selected columns list
+ *
+ * @return
+ */
+ List<Integer> getList()
+ {
+ return _uorder;
+ }
+
+ int size()
+ {
+ return order.size();
+ }
+
+ /**
+ * gets the column that was selected first, second or i'th
+ *
+ * @param i
+ * @return
+ */
+ 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
+ */
+ 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, Integer.valueOf(temp - change));
+ }
+ }
+ // lastly update the bitfield all at once
+ selected.or(mask);
+ }
+
+ boolean isSelected(int column)
+ {
+ return selected.get(column);
+ }
+
+ int getMaxColumn()
+ {
+ return selected.length() - 1;
+ }
+
+ int getMinColumn()
+ {
+ return selected.get(0) ? 0 : selected.nextSetBit(0);
+ }
+
+ /**
+ * @return a series of selection intervals along the range
+ */
+ List<int[]> getRanges()
+ {
+ List<int[]> 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;
+ }
+
+ @Override
+ public int hashCode()
+ {
+ // TODO Auto-generated method stub
+ return selected.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (obj instanceof IntList)
+ {
+ return ((IntList) obj).selected.equals(selected);
+ }
+ return false;
+ }
+ }