From: Jim Procter Date: Mon, 4 Apr 2016 14:19:27 +0000 (+0100) Subject: JAL-2001 faster getMax/getMin selected column, with tests X-Git-Tag: Release_2_10_0~270^2^2~10 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=48538fac92d607c3b88b10b3d901539da00108e2;p=jalview.git JAL-2001 faster getMax/getMin selected column, with tests --- diff --git a/src/jalview/datamodel/ColumnSelection.java b/src/jalview/datamodel/ColumnSelection.java index 9795570..168e43f 100644 --- a/src/jalview/datamodel/ColumnSelection.java +++ b/src/jalview/datamodel/ColumnSelection.java @@ -169,6 +169,16 @@ public class ColumnSelection // lastly update the bitfield all at once selected.or(mask); } + + public int getMaxColumn() + { + return selected.length() - 1; + } + + public int getMinColumn() + { + return selected.get(0) ? 0 : selected.nextSetBit(0); + } } IntList selected = new IntList(); @@ -265,17 +275,11 @@ public class ColumnSelection */ public int getMax() { - int max = -1; - - for (int sel : getSelected()) + if (selected.isEmpty()) { - if (sel > max) - { - max = sel; - } + return -1; } - - return max; + return selected.getMaxColumn(); } /** @@ -285,17 +289,11 @@ public class ColumnSelection */ public int getMin() { - int min = 1000000000; - - for (int sel : getSelected()) + if (selected.isEmpty()) { - if (sel < min) - { - min = sel; - } + return 1000000000; } - - return min; + return selected.getMinColumn(); } /** diff --git a/test/jalview/datamodel/ColumnSelectionTest.java b/test/jalview/datamodel/ColumnSelectionTest.java index 698f259..0f08ceb 100644 --- a/test/jalview/datamodel/ColumnSelectionTest.java +++ b/test/jalview/datamodel/ColumnSelectionTest.java @@ -343,4 +343,36 @@ public class ColumnSelectionTest cs.invertColumnSelection(1, 9); assertEquals("[1, 4, 8]", cs.getSelected().toString()); } + + @Test(groups = { "Functional" }) + public void testMaxColumnSelection() + { + ColumnSelection cs = new ColumnSelection(); + cs.addElement(0); + cs.addElement(513); + cs.addElement(1); + assertEquals(513, cs.getMax()); + cs.removeElement(513); + assertEquals(1, cs.getMax()); + cs.removeElement(1); + assertEquals(0, cs.getMax()); + cs.addElement(512); + cs.addElement(513); + assertEquals(513, cs.getMax()); + + } + + @Test(groups = { "Functional" }) + public void testMinColumnSelection() + { + ColumnSelection cs = new ColumnSelection(); + cs.addElement(0); + cs.addElement(513); + cs.addElement(1); + assertEquals(0, cs.getMin()); + cs.removeElement(0); + assertEquals(1, cs.getMin()); + cs.addElement(0); + assertEquals(0, cs.getMin()); + } }