OSDN Git Service

handle size error, consistent variable names
authorJon Renner <rennerjc@gmail.com>
Fri, 4 Oct 2013 07:52:10 +0000 (15:52 +0800)
committerJon Renner <rennerjc@gmail.com>
Fri, 4 Oct 2013 07:52:10 +0000 (15:52 +0800)
gdx/src/com/badlogic/gdx/utils/Array.java
gdx/src/com/badlogic/gdx/utils/Select.java

index 8209a87..1f35ac8 100644 (file)
@@ -339,30 +339,30 @@ public class Array<T> implements Iterable<T> {
         * This might partially sort the Array.\r
         * @see Select\r
         * @param comparator used for comparison\r
-        * @param nthLowest rank of desired object according to comparison,\r
+        * @param kthLowest rank of desired object according to comparison,\r
         * n is based on ordinal numbers, not array indices.\r
         * for min value use 1, for max value use size of array, using 0 results in runtime exception.\r
         * @return the value of the Nth lowest ranked object.\r
         */\r
-       public T selectRanked(Comparator<T> comparator, int nthLowest) {\r
-               if (nthLowest < 1) {\r
+       public T selectRanked(Comparator<T> comparator, int kthLowest) {\r
+               if (kthLowest < 1) {\r
                        throw new GdxRuntimeException("nth_lowest must be greater than 0, 1 = first, 2 = second...");\r
                }\r
-               return Select.instance().select(items, comparator, nthLowest, size);\r
+               return Select.instance().select(items, comparator, kthLowest, size);\r
        }\r
 \r
        /** @see Array#selectRanked(java.util.Comparator, int)\r
        * @param comparator used for comparison\r
-        * @param nthLowest rank of desired object according to comparison,\r
+        * @param kthLowest rank of desired object according to comparison,\r
         * n is based on ordinal numbers, not array indices.\r
         * for min value use 1, for max value use size of array, using 0 results in runtime exception.\r
         * @return the index of the Nth lowest ranked object.\r
         */\r
-       public int selectRankedIndex(Comparator<T> comparator, int nthLowest) {\r
-               if (nthLowest < 1) {\r
+       public int selectRankedIndex(Comparator<T> comparator, int kthLowest) {\r
+               if (kthLowest < 1) {\r
                        throw new GdxRuntimeException("nth_lowest must be greater than 0, 1 = first, 2 = second...");\r
                }\r
-               return Select.instance().selectIndex(items, comparator, nthLowest, size);\r
+               return Select.instance().selectIndex(items, comparator, kthLowest, size);\r
        }\r
 \r
        public void reverse () {\r
index c98d9ba..515b36d 100644 (file)
@@ -31,7 +31,11 @@ public class Select {
        }
 
        public <T> int selectIndex(T[] items, Comparator<T> comp, int kthLowest, int size) {
-               if (size < 1) throw new GdxRuntimeException("cannot select from empty array (size < 1)");
+               if (size < 1) {
+                       throw new GdxRuntimeException("cannot select from empty array (size < 1)");
+               } else if (kthLowest > size) {
+                       throw new GdxRuntimeException("Kth rank is larger than size. k: " + kthLowest + ", size: " + size);
+               }
                int idx;
                // naive partial selection sort almost certain to outperform quickselect where n is min or max
                if (kthLowest == 1) {