Explorar el Código

new: ArrayUtils#size
new: ArrayUtils#wrap(array) -> List
new: ArraySearch#binarySearch - support for primitive arrays

tests (60)

Ranides Atterwim hace 10 años
padre
commit
c62655a688

+ 15 - 21
assira/src/main/java/net/ranides/assira/collection/arrays/ArrayAllocator.java

@@ -92,7 +92,7 @@ public final class ArrayAllocator {
      * of <code>array</code>.
      */
     public static <K> K[] ensureCapacity(K[] array, int length) {
-        return ensureCapacity(array, length, sizeof(array));
+        return ensureCapacity(array, length, ArrayUtils.size(array));
     }
     
     /**
@@ -127,7 +127,7 @@ public final class ArrayAllocator {
      * of <code>array</code>.
      */
     public static <K> K[] ensureCapacity(K[] array, int length, int preserve) {
-        if (length > sizeof(array)) {
+        if (length > ArrayUtils.size(array)) {
             K target[] = forPrototype(array, length);
             System.arraycopy(array, 0, target, 0, preserve);
             return target;
@@ -152,7 +152,7 @@ public final class ArrayAllocator {
         "SuspiciousSystemArraycopy"
     })
     public static <A> A ensureCapacity(A array, int length, int preserve) {
-        int n = sizeof(array);
+        int n = ArrayUtils.size(array);
         if(preserve < 0) {
             preserve = n;
         }
@@ -178,7 +178,7 @@ public final class ArrayAllocator {
      * length.
      */
     public static <K> void ensureOffsetLength(K[] array, int offset, int length) {
-        ensureOffsetLength(sizeof(array), offset, length);
+        ensureOffsetLength(ArrayUtils.size(array), offset, length);
     }
     
     /**
@@ -195,7 +195,7 @@ public final class ArrayAllocator {
      * length.
      */
     public static void ensureOffsetLength(Object array, int offset, int length) {
-        ArrayAllocator.ensureOffsetLength(sizeof(array), offset, length);
+        ArrayAllocator.ensureOffsetLength(ArrayUtils.size(array), offset, length);
     }
 
     /**
@@ -238,7 +238,7 @@ public final class ArrayAllocator {
      * or <code>to</code> are greater than the array length or negative.
      */
     public static <K> void ensureFromTo(K[] array, int begin, int end) {
-        ensureFromTo(sizeof(array), begin, end);
+        ensureFromTo(ArrayUtils.size(array), begin, end);
     }
     
     /**
@@ -256,7 +256,7 @@ public final class ArrayAllocator {
      * or <code>to</code> are greater than the array length or negative.
      */
     public static void ensureFromTo(Object array, int begin, int end) {
-        ArrayAllocator.ensureFromTo(sizeof(array), begin, end);
+        ArrayAllocator.ensureFromTo(ArrayUtils.size(array), begin, end);
     }
     
     /**
@@ -304,7 +304,7 @@ public final class ArrayAllocator {
      *
      */
     public static <K> K[] grow(K[] array, int length) {
-        return grow(array, length, sizeof(array));
+        return grow(array, length, ArrayUtils.size(array));
     }
     
     /**
@@ -350,8 +350,9 @@ public final class ArrayAllocator {
      *
      */
     public static <K> K[] grow(K[] array, int length, int preserve) {
-        if (length > sizeof(array)) {
-            int newLength = (int) Math.min(Math.max(2L * sizeof(array), length), MAX_SIZE);
+        int n = ArrayUtils.size(array);
+        if (length > n) {
+            int newLength = (int) Math.min(Math.max(2L * n, length), MAX_SIZE);
             K target[] = forPrototype(array, newLength);
             System.arraycopy(array, 0, target, 0, preserve);
             return target;
@@ -383,7 +384,7 @@ public final class ArrayAllocator {
         "SuspiciousSystemArraycopy"
     })
     public static <A> A grow(A array, int length, int preserve) {
-        int n = sizeof(array);
+        int n = ArrayUtils.size(array);
         if(preserve < 0) {
             preserve = n;
         }
@@ -408,7 +409,7 @@ public final class ArrayAllocator {
      *
      */
     public static <K> K[] trim(K[] array, int length) {
-        if (length >= sizeof(array)) {
+        if (length >= ArrayUtils.size(array)) {
             return array;
         }
         K target[] = forPrototype(array, length);
@@ -431,19 +432,12 @@ public final class ArrayAllocator {
         "SuspiciousSystemArraycopy"
     })
     public static <A> A trim(A array, int length) {
-        if (length >= sizeof(array)) {
+        if (length >= ArrayUtils.size(array)) {
             return array;
         }
         A target = forPrototype(array, length);
         System.arraycopy(array, 0, target, 0, length);
         return target;
     }
-    
-    private static <K> int sizeof(K[] array) {
-        return null == array ? 0 : array.length;
-    }
-    
-    private static int sizeof(Object array) {
-        return null == array ? 0 : Array.getLength(array);
-    }
+
 }

+ 179 - 143
assira/src/main/java/net/ranides/assira/collection/arrays/ArraySearch.java

@@ -1,143 +1,179 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/assira
- */
-package net.ranides.assira.collection.arrays;
-
-import java.util.Comparator;
-
-@SuppressWarnings({
-    "PMD.AvoidReassigningParameters"
-})
-public final class ArraySearch {
-    
-    private ArraySearch() {
-        // utility class
-    }
-    
-    /**
-     * Searches a range of the specified array for the specified value using the
-     * binary search algorithm. The range must be sorted prior to making this
-     * call. If it is not sorted, the results are undefined. If the range
-     * contains multiple elements with the specified value, there is no
-     * guarantee which one will be found.
-     *
-     * @param array the array to be searched.
-     * @param begin the index of the first element (inclusive) to be searched.
-     * @param end the index of the last element (exclusive) to be searched.
-     * @param key the value to be searched for.
-     * @return index of the search key, if it is contained in the array;
-     * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
-     * point</i> is defined as the the point at which the value would be
-     * inserted into the array: the index of the first element greater than the
-     * key, or the length of the array, if all elements in the array are less
-     * than the specified key. Note that this guarantees that the return value
-     * will be &gt;= 0 if and only if the key is found.
-     * @see java.util.Arrays
-     */
-    public static <K extends Comparable<K>> int binarySearch(K[] array, int begin, int end, K key) {
-        K midVal;
-        end--;
-        while (begin <= end) {
-            int mid = (begin + end) >>> 1;
-            midVal = array[ mid];
-            int cmp = midVal.compareTo(key);
-            if (cmp < 0) {
-                begin = mid + 1;
-            } else if (cmp > 0) {
-                end = mid - 1;
-            } else {
-                return mid;
-            }
-        }
-        return -(begin + 1);
-    }
-
-    /**
-     * Searches an array for the specified value using the binary search
-     * algorithm. The range must be sorted prior to making this call. If it is
-     * not sorted, the results are undefined. If the range contains multiple
-     * elements with the specified value, there is no guarantee which one will
-     * be found.
-     *
-     * @param array the array to be searched.
-     * @param key the value to be searched for.
-     * @return index of the search key, if it is contained in the array;
-     * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
-     * point</i> is defined as the the point at which the value would be
-     * inserted into the array: the index of the first element greater than the
-     * key, or the length of the array, if all elements in the array are less
-     * than the specified key. Note that this guarantees that the return value
-     * will be &gt;= 0 if and only if the key is found.
-     * @see java.util.Arrays
-     */
-    public static <K extends Comparable<K>> int binarySearch(K[] array, K key) {
-        return binarySearch(array, 0, array.length, key);
-    }
-
-    /**
-     * Searches a range of the specified array for the specified value using the
-     * binary search algorithm and a specified comparator. The range must be
-     * sorted following the comparator prior to making this call. If it is not
-     * sorted, the results are undefined. If the range contains multiple
-     * elements with the specified value, there is no guarantee which one will
-     * be found.
-     *
-     * @param array the array to be searched.
-     * @param begin the index of the first element (inclusive) to be searched.
-     * @param end the index of the last element (exclusive) to be searched.
-     * @param key the value to be searched for.
-     * @param comp a comparator.
-     * @return index of the search key, if it is contained in the array;
-     * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
-     * point</i> is defined as the the point at which the value would be
-     * inserted into the array: the index of the first element greater than the
-     * key, or the length of the array, if all elements in the array are less
-     * than the specified key. Note that this guarantees that the return value
-     * will be &gt;= 0 if and only if the key is found.
-     * @see java.util.Arrays
-     */
-    public static <K> int binarySearch(K[] array, int begin, int end, K key, Comparator<K> comp) {
-        K midVal;
-        end--;
-        while (begin <= end) {
-            int mid = (begin + end) >>> 1;
-            midVal = array[ mid];
-            int cmp = comp.compare(midVal, key);
-            if (cmp < 0) {
-                begin = mid + 1;
-            } else if (cmp > 0) {
-                end = mid - 1;
-            } else {
-                return mid; // key found
-            }
-        }
-        return -(begin + 1);
-    }
-
-    /**
-     * Searches an array for the specified value using the binary search
-     * algorithm and a specified comparator. The range must be sorted following
-     * the comparator prior to making this call. If it is not sorted, the
-     * results are undefined. If the range contains multiple elements with the
-     * specified value, there is no guarantee which one will be found.
-     *
-     * @param array the array to be searched.
-     * @param key the value to be searched for.
-     * @param cmp a comparator.
-     * @return index of the search key, if it is contained in the array;
-     * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
-     * point</i> is defined as the the point at which the value would be
-     * inserted into the array: the index of the first element greater than the
-     * key, or the length of the array, if all elements in the array are less
-     * than the specified key. Note that this guarantees that the return value
-     * will be &gt;= 0 if and only if the key is found.
-     * @see java.util.Arrays
-     */
-    public static <K> int binarySearch(K[] array, K key, Comparator<K> cmp) {
-        return binarySearch(array, 0, array.length, key, cmp);
-    }
-
-}
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.collection.arrays;
+
+import java.util.Comparator;
+import java.util.List;
+import net.ranides.assira.generic.CompareUtils;
+
+@SuppressWarnings({
+    "PMD.AvoidReassigningParameters"
+})
+public final class ArraySearch {
+    
+    private ArraySearch() {
+        // utility class
+    }
+    
+    /**
+     * Searches an array for the specified value using the binary search
+     * algorithm. The range must be sorted prior to making this call. If it is
+     * not sorted, the results are undefined. If the range contains multiple
+     * elements with the specified value, there is no guarantee which one will
+     * be found.
+     *
+     * @param array the array to be searched.
+     * @param key the value to be searched for.
+     * @return index of the search key, if it is contained in the array;
+     * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
+     * point</i> is defined as the the point at which the value would be
+     * inserted into the array: the index of the first element greater than the
+     * key, or the length of the array, if all elements in the array are less
+     * than the specified key. Note that this guarantees that the return value
+     * will be &gt;= 0 if and only if the key is found.
+     * @see java.util.Arrays
+     */
+    public static <T extends Comparable<T>> int binarySearch(T[] array, T key) {
+        return binarySearch(array, 0, array.length, key);
+    }
+    
+    /**
+     * Searches a range of the specified array for the specified value using the
+     * binary search algorithm. The range must be sorted prior to making this
+     * call. If it is not sorted, the results are undefined. If the range
+     * contains multiple elements with the specified value, there is no
+     * guarantee which one will be found.
+     *
+     * @param array the array to be searched.
+     * @param begin the index of the first element (inclusive) to be searched.
+     * @param end the index of the last element (exclusive) to be searched.
+     * @param key the value to be searched for.
+     * @return index of the search key, if it is contained in the array;
+     * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
+     * point</i> is defined as the the point at which the value would be
+     * inserted into the array: the index of the first element greater than the
+     * key, or the length of the array, if all elements in the array are less
+     * than the specified key. Note that this guarantees that the return value
+     * will be &gt;= 0 if and only if the key is found.
+     * @see java.util.Arrays
+     */
+    public static <T extends Comparable<T>> int binarySearch(T[] array, int begin, int end, T key) {
+        T midVal;
+        end--;
+        while (begin <= end) {
+            int mid = (begin + end) >>> 1;
+            midVal = array[ mid];
+            int cmp = midVal.compareTo(key);
+            if (cmp < 0) {
+                begin = mid + 1;
+            } else if (cmp > 0) {
+                end = mid - 1;
+            } else {
+                return mid;
+            }
+        }
+        return -(begin + 1);
+    }
+    
+    /**
+     * Searches an array for the specified value using the binary search
+     * algorithm and a specified comparator. The range must be sorted following
+     * the comparator prior to making this call. If it is not sorted, the
+     * results are undefined. If the range contains multiple elements with the
+     * specified value, there is no guarantee which one will be found.
+     *
+     * @param array the array to be searched.
+     * @param key the value to be searched for.
+     * @param cmp a comparator.
+     * @return index of the search key, if it is contained in the array;
+     * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
+     * point</i> is defined as the the point at which the value would be
+     * inserted into the array: the index of the first element greater than the
+     * key, or the length of the array, if all elements in the array are less
+     * than the specified key. Note that this guarantees that the return value
+     * will be &gt;= 0 if and only if the key is found.
+     * @see java.util.Arrays
+     */
+    public static <T> int binarySearch(T[] array, T key, Comparator<T> cmp) {
+        return binarySearch(array, 0, array.length, key, cmp);
+    }
+
+    /**
+     * Searches a range of the specified array for the specified value using the
+     * binary search algorithm and a specified comparator. The range must be
+     * sorted following the comparator prior to making this call. If it is not
+     * sorted, the results are undefined. If the range contains multiple
+     * elements with the specified value, there is no guarantee which one will
+     * be found.
+     *
+     * @param array the array to be searched.
+     * @param begin the index of the first element (inclusive) to be searched.
+     * @param end the index of the last element (exclusive) to be searched.
+     * @param key the value to be searched for.
+     * @param comp a comparator.
+     * @return index of the search key, if it is contained in the array;
+     * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
+     * point</i> is defined as the the point at which the value would be
+     * inserted into the array: the index of the first element greater than the
+     * key, or the length of the array, if all elements in the array are less
+     * than the specified key. Note that this guarantees that the return value
+     * will be &gt;= 0 if and only if the key is found.
+     * @see java.util.Arrays
+     */
+    public static <T> int binarySearch(T[] array, int begin, int end, T key, Comparator<T> comp) {
+        T midVal;
+        end--;
+        while (begin <= end) {
+            int mid = (begin + end) >>> 1;
+            midVal = array[ mid];
+            int cmp = comp.compare(midVal, key);
+            if (cmp < 0) {
+                begin = mid + 1;
+            } else if (cmp > 0) {
+                end = mid - 1;
+            } else {
+                return mid; // key found
+            }
+        }
+        return -(begin + 1);
+    }
+    
+    public static <T> int binarySearch(Object array, T key) {
+        return binarySearch(array, 0, ArrayUtils.size(array), key);
+    }
+    
+    public static <T> int binarySearch(Object array, int begin, int end, T key) {
+        return binarySearchList(ArrayUtils.$wrap(array), begin, end, key, CompareUtils.comparator());
+    }
+    
+    public static <T> int binarySearch(Object array, T key, Comparator<T> comp) {
+        return binarySearch(array, 0, ArrayUtils.size(array), key, comp);
+    }
+    
+    public static <T> int binarySearch(Object array, int begin, int end, T key, Comparator<T> comp) {
+        return binarySearchList(ArrayUtils.$wrap(array), begin, end, key, comp);
+    }
+    
+    private static <T> int binarySearchList(List<T> array, int begin, int end, T key, Comparator<T> comp) {
+        T midVal;
+        end--;
+        while (begin <= end) {
+            int mid = (begin + end) >>> 1;
+            midVal = array.get(mid);
+            int cmp = comp.compare(midVal, key);
+            if (cmp < 0) {
+                begin = mid + 1;
+            } else if (cmp > 0) {
+                end = mid - 1;
+            } else {
+                return mid; // key found
+            }
+        }
+        return -(begin + 1);
+    }
+
+}

+ 224 - 0
assira/src/main/java/net/ranides/assira/collection/arrays/ArrayUtils.java

@@ -6,8 +6,12 @@
  */
 package net.ranides.assira.collection.arrays;
 
+import java.lang.reflect.Array;
+import java.util.AbstractList;
+import java.util.Arrays;
 import java.util.Comparator;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Random;
 import net.ranides.assira.collection.Swapper;
 
@@ -197,4 +201,224 @@ public final class ArrayUtils {
         shuffle(array.length, random, Swapper.swapper(array));
     }
     
+    public static <K> int size(K[] array) {
+        return null == array ? 0 : array.length;
+    }
+    
+    public static int size(Object array) {
+        return null == array ? 0 : Array.getLength(array);
+    }
+    
+    @SuppressWarnings("PMD.NPathComplexity")
+    public static List<?> wrap(Object array) {
+        Class<?> c = array.getClass().getComponentType();
+        if(byte.class.equals(c)) {
+            return new ByteList(array);
+        }
+        if(short.class.equals(c)) {
+            return new ShortList(array);
+        }
+        if(int.class.equals(c)) {
+            return new IntegerList(array);
+        }
+        if(long.class.equals(c)) {
+            return new LongList(array);
+        }
+        if(float.class.equals(c)) {
+            return new FloatList(array);
+        }
+        if(double.class.equals(c)) {
+            return new DoubleList(array);
+        }
+        if(char.class.equals(c)) {
+            return new CharacterList(array);
+        }
+        if(boolean.class.equals(c)) {
+            return new BooleanList(array);
+        }
+        return Arrays.asList((Object[])array);
+    }
+    
+    @SuppressWarnings("unchecked")
+    public static <T> List<T> $wrap(Object array) {
+        return (List<T>)wrap(array);
+    }
+    
+    private static abstract class ArrayWrapper<T> extends AbstractList<T> {
+
+        protected final Object array;
+
+        protected ArrayWrapper(Object array) {
+            this.array = array;
+        }
+
+        protected abstract void at(int index, T element);
+
+        protected abstract T at(int index);
+
+
+        @Override
+        public final T set(int index, T element) {
+            T prev = at(index);
+            at(index, element);
+            return prev;
+        }
+
+        @Override
+        public T get(int index) {
+            return at(index);
+        }
+
+        @Override
+        public int size() {
+            return Array.getLength(array);
+        }
+
+    }
+    
+    private static final class ByteList extends ArrayWrapper<Byte> {
+
+        public ByteList(Object array) {
+            super(array);
+        }
+
+        @Override
+        public void at(int index, Byte element) {
+            Array.setByte(array, index, element);
+        }
+
+        @Override
+        public Byte at(int index) {
+            return Array.getByte(array, index);
+        }
+    
+    }
+    
+    private static final class ShortList extends ArrayWrapper<Short> {
+
+        public ShortList(Object array) {
+            super(array);
+        }
+
+        @Override
+        public void at(int index, Short element) {
+            Array.setShort(array, index, element);
+        }
+
+        @Override
+        public Short at(int index) {
+            return Array.getShort(array, index);
+        }
+    
+    }
+    
+    private static final class IntegerList extends ArrayWrapper<Integer> {
+
+        public IntegerList(Object array) {
+            super(array);
+        }
+
+        @Override
+        public void at(int index, Integer element) {
+            Array.setInt(array, index, element);
+        }
+
+        @Override
+        public Integer at(int index) {
+            return Array.getInt(array, index);
+        }
+    
+    }
+    
+    private static final class LongList extends ArrayWrapper<Long> {
+
+        public LongList(Object array) {
+            super(array);
+        }
+
+        @Override
+        public void at(int index, Long element) {
+            Array.setLong(array, index, element);
+        }
+
+        @Override
+        public Long at(int index) {
+            return Array.getLong(array, index);
+        }
+    
+    }
+    
+    private static final class FloatList extends ArrayWrapper<Float> {
+
+        public FloatList(Object array) {
+            super(array);
+        }
+
+        @Override
+        public void at(int index, Float element) {
+            Array.setFloat(array, index, element);
+        }
+
+        @Override
+        public Float at(int index) {
+            return Array.getFloat(array, index);
+        }
+    
+    }
+    
+    private static final class DoubleList extends ArrayWrapper<Double> {
+
+        public DoubleList(Object array) {
+            super(array);
+        }
+
+        @Override
+        public void at(int index, Double element) {
+            Array.setDouble(array, index, element);
+        }
+
+        @Override
+        public Double at(int index) {
+            return Array.getDouble(array, index);
+        }
+    
+    }
+    
+    private static final class CharacterList extends ArrayWrapper<Character> {
+
+        public CharacterList(Object array) {
+            super(array);
+        }
+
+        @Override
+        public void at(int index, Character element) {
+            Array.setChar(array, index, element);
+        }
+
+        @Override
+        public Character at(int index) {
+            return Array.getChar(array, index);
+        }
+    
+    }
+    
+    private static final class BooleanList extends ArrayWrapper<Boolean> {
+
+        public BooleanList(Object array) {
+            super(array);
+        }
+
+        @Override
+        public void at(int index, Boolean element) {
+            Array.setBoolean(array, index, element);
+        }
+
+        @Override
+        public Boolean at(int index) {
+            return Array.getBoolean(array, index);
+        }
+    
+    }
+
+    
 }

+ 9 - 12
assira/src/main/java/net/ranides/assira/collection/arrays/IntArrayAllocator.java

@@ -32,7 +32,7 @@ public final class IntArrayAllocator {
      * of <code>array</code>.
      */
     public static int[] ensureCapacity(int[] array, int length) {
-        return ensureCapacity(array, length, sizeof(array));
+        return ensureCapacity(array, length, IntArrayUtils.size(array));
     }
     
     /**
@@ -49,7 +49,7 @@ public final class IntArrayAllocator {
      * of <code>array</code>.
      */
     public static int[] ensureCapacity(int[] array, int length, int preserve) {
-        if (length > sizeof(array)) {
+        if (length > IntArrayUtils.size(array)) {
             int t[] = new int[length];
             System.arraycopy(array, 0, t, 0, preserve);
             return t;
@@ -72,7 +72,7 @@ public final class IntArrayAllocator {
      * length.
      */
     public static void ensureOffsetLength(int[] array, int offset, int length) {
-        ArrayAllocator.ensureOffsetLength(sizeof(array), offset, length);
+        ArrayAllocator.ensureOffsetLength(IntArrayUtils.size(array), offset, length);
     }
     
     /**
@@ -90,7 +90,7 @@ public final class IntArrayAllocator {
      * or <code>to</code> are greater than the array length or negative.
      */
     public static void ensureFromTo(int[] array, int begin, int end) {
-        ArrayAllocator.ensureFromTo(sizeof(array), begin, end);
+        ArrayAllocator.ensureFromTo(IntArrayUtils.size(array), begin, end);
     }
     
     /**
@@ -112,7 +112,7 @@ public final class IntArrayAllocator {
      *
      */
     public static int[] grow(int[] array, int length) {
-        return grow(array, length, sizeof(array));
+        return grow(array, length, IntArrayUtils.size(array));
     }
 
     /**
@@ -136,8 +136,9 @@ public final class IntArrayAllocator {
      *
      */
     public static int[] grow(int[] array, int length, int preserve) {
-        if (length > sizeof(array)) {
-            int newLength = (int) Math.min(Math.max(2L * sizeof(array), length), ArrayAllocator.MAX_SIZE);
+        int n = IntArrayUtils.size(array);
+        if (length > n) {
+            int newLength = (int) Math.min(Math.max(2L * n, length), ArrayAllocator.MAX_SIZE);
             int t[] = new int[newLength];
             System.arraycopy(array, 0, t, 0, preserve);
             return t;
@@ -146,16 +147,12 @@ public final class IntArrayAllocator {
     }
     
     public static int[] trim(int[] array, int length) {
-        if (length >= sizeof(array)) {
+        if (length >= IntArrayUtils.size(array)) {
             return array;
         }
         int target[] = new int[length];
         System.arraycopy(array, 0, target, 0, length);
         return target;
     }
-    
-    private static int sizeof(int[] array) {
-        return null == array ? 0 : array.length;
-    }
 
 }

+ 30 - 29
assira/src/main/java/net/ranides/assira/collection/arrays/IntArraySearch.java

@@ -16,6 +16,28 @@ public final class IntArraySearch {
     private IntArraySearch() {
         // utility class
     }
+    
+    /**
+     * Searches an array for the specified value using the binary search
+     * algorithm. The range must be sorted prior to making this call. If it is
+     * not sorted, the results are undefined. If the range contains multiple
+     * elements with the specified value, there is no guarantee which one will
+     * be found.
+     *
+     * @param array the array to be searched.
+     * @param key the value to be searched for.
+     * @return index of the search key, if it is contained in the array;
+     * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
+     * point</i> is defined as the the point at which the value would be
+     * inserted into the array: the index of the first element greater than the
+     * key, or the length of the array, if all elements in the array are less
+     * than the specified key. Note that this guarantees that the return value
+     * will be &gt;= 0 if and only if the key is found.
+     * @see java.util.Arrays
+     */
+    public static int binarySearch(int[] array, int key) {
+        return binarySearch(array, 0, array.length, key);
+    }
    
     /**
      * Searches a range of the specified array for the specified value using the
@@ -53,16 +75,17 @@ public final class IntArraySearch {
         }
         return -(begin + 1);
     }
-
+    
     /**
      * Searches an array for the specified value using the binary search
-     * algorithm. The range must be sorted prior to making this call. If it is
-     * not sorted, the results are undefined. If the range contains multiple
-     * elements with the specified value, there is no guarantee which one will
-     * be found.
+     * algorithm and a specified comparator. The range must be sorted following
+     * the comparator prior to making this call. If it is not sorted, the
+     * results are undefined. If the range contains multiple elements with the
+     * specified value, there is no guarantee which one will be found.
      *
      * @param array the array to be searched.
      * @param key the value to be searched for.
+     * @param comp a comparator.
      * @return index of the search key, if it is contained in the array;
      * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
      * point</i> is defined as the the point at which the value would be
@@ -72,8 +95,8 @@ public final class IntArraySearch {
      * will be &gt;= 0 if and only if the key is found.
      * @see java.util.Arrays
      */
-    public static int binarySearch(int[] array, int key) {
-        return binarySearch(array, 0, array.length, key);
+    public static int binarySearch(int[] array, int key, IntComparator comp) {
+        return binarySearch(array, 0, array.length, key, comp);
     }
 
     /**
@@ -116,27 +139,5 @@ public final class IntArraySearch {
         return -(begin + 1);
     }
 
-    /**
-     * Searches an array for the specified value using the binary search
-     * algorithm and a specified comparator. The range must be sorted following
-     * the comparator prior to making this call. If it is not sorted, the
-     * results are undefined. If the range contains multiple elements with the
-     * specified value, there is no guarantee which one will be found.
-     *
-     * @param array the array to be searched.
-     * @param key the value to be searched for.
-     * @param comp a comparator.
-     * @return index of the search key, if it is contained in the array;
-     * otherwise, <samp>(-(<i>insertion point</i>) - 1)</samp>. The <i>insertion
-     * point</i> is defined as the the point at which the value would be
-     * inserted into the array: the index of the first element greater than the
-     * key, or the length of the array, if all elements in the array are less
-     * than the specified key. Note that this guarantees that the return value
-     * will be &gt;= 0 if and only if the key is found.
-     * @see java.util.Arrays
-     */
-    public static int binarySearch(int[] array, int key, IntComparator comp) {
-        return binarySearch(array, 0, array.length, key, comp);
-    }
     
 }

+ 4 - 0
assira/src/main/java/net/ranides/assira/collection/arrays/IntArrayUtils.java

@@ -184,4 +184,8 @@ public final class IntArrayUtils {
         ArrayUtils.shuffle(array.length, random, Swapper.swapper(array));
     }
     
+    public static int size(int[] array) {
+        return null == array ? 0 : array.length;
+    }
+    
 }

+ 92 - 0
assira/src/test/java/net/ranides/assira/collection/arrays/ArraySearchTest.java

@@ -0,0 +1,92 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/???
+ */
+package net.ranides.assira.collection.arrays;
+
+import java.util.Comparator;
+import net.ranides.assira.junit.QAssert;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class ArraySearchTest {
+    
+    @Test
+    public void testBinarySearch() {
+        Byte[] array1 = new Byte[]{1,2,5,6};
+        assertEquals(0, ArraySearch.binarySearch(array1, (byte)1));
+        assertEquals(1, ArraySearch.binarySearch(array1, (byte)2));
+        assertEquals(2, ArraySearch.binarySearch(array1, (byte)5));
+        assertEquals(3, ArraySearch.binarySearch(array1, (byte)6));
+        assertEquals(-5, ArraySearch.binarySearch(array1, (byte)9));
+        assertEquals(-1, ArraySearch.binarySearch(array1, (byte)0));
+        assertEquals(-3, ArraySearch.binarySearch(array1, (byte)3));
+        assertEquals(-3, ArraySearch.binarySearch(array1, (byte)4));
+        
+        QAssert.assertThrows(NullPointerException.class, ()->{
+            ArraySearch.binarySearch(array1, null);
+        });
+        
+    }
+
+    @Test
+    public void testBinarySearch_cmp() {
+        Byte[] array1 = new Byte[]{31,22,75,46};
+        Comparator<Byte> cmp = (a,b) -> (a%10-b%10);
+        assertEquals(0, ArraySearch.binarySearch(array1, (byte)1, cmp));
+        assertEquals(1, ArraySearch.binarySearch(array1, (byte)92, cmp));
+        assertEquals(2, ArraySearch.binarySearch(array1, (byte)95, cmp));
+        assertEquals(3, ArraySearch.binarySearch(array1, (byte)96, cmp));
+        assertEquals(-5, ArraySearch.binarySearch(array1, (byte)99, cmp));
+        assertEquals(-1, ArraySearch.binarySearch(array1, (byte)90, cmp));
+        assertEquals(-3, ArraySearch.binarySearch(array1, (byte)93, cmp));
+        assertEquals(-3, ArraySearch.binarySearch(array1, (byte)94, cmp));
+        
+        QAssert.assertThrows(NullPointerException.class, ()->{
+            ArraySearch.binarySearch(array1, null, cmp);
+        });
+    }
+    
+    @Test
+    public void testBinarySearch_pv() {
+        byte[] array1 = new byte[]{1,2,5,6};
+        assertEquals(0, ArraySearch.binarySearch(array1, (byte)1));
+        assertEquals(1, ArraySearch.binarySearch(array1, (byte)2));
+        assertEquals(2, ArraySearch.binarySearch(array1, (byte)5));
+        assertEquals(3, ArraySearch.binarySearch(array1, (byte)6));
+        assertEquals(-5, ArraySearch.binarySearch(array1, (byte)9));
+        assertEquals(-1, ArraySearch.binarySearch(array1, (byte)0));
+        assertEquals(-3, ArraySearch.binarySearch(array1, (byte)3));
+        assertEquals(-3, ArraySearch.binarySearch(array1, (byte)4));
+        
+        QAssert.assertThrows(NullPointerException.class, ()->{
+            ArraySearch.binarySearch(array1, null);
+        });
+    }
+
+    @Test
+    public void testBinarySearch_cmp_pv() {
+        byte[] array1 = new byte[]{31,22,75,46};
+        Comparator<Byte> cmp = (a,b) -> (a%10-b%10);
+        assertEquals(0, ArraySearch.binarySearch(array1, (byte)1, cmp));
+        assertEquals(1, ArraySearch.binarySearch(array1, (byte)92, cmp));
+        assertEquals(2, ArraySearch.binarySearch(array1, (byte)95, cmp));
+        assertEquals(3, ArraySearch.binarySearch(array1, (byte)96, cmp));
+        assertEquals(-5, ArraySearch.binarySearch(array1, (byte)99, cmp));
+        assertEquals(-1, ArraySearch.binarySearch(array1, (byte)90, cmp));
+        assertEquals(-3, ArraySearch.binarySearch(array1, (byte)93, cmp));
+        assertEquals(-3, ArraySearch.binarySearch(array1, (byte)94, cmp));
+        
+        QAssert.assertThrows(NullPointerException.class, ()->{
+            ArraySearch.binarySearch(array1, null, cmp);
+        });
+    }
+
+    
+}

+ 71 - 1
assira/src/test/java/net/ranides/assira/collection/arrays/ArrayUtilsTest.java

@@ -184,4 +184,74 @@ public class ArrayUtilsTest {
         assertArrayEquals(new String[]{"#", "#", "#", null}, out2);
     }
     
-}
+    @Test
+    public void testWrap() {
+        byte[] array1 = new byte[]{1,2,3};
+        List<Byte> list1 = ArrayUtils.$wrap(array1);
+        list1.set(1, (byte)7);
+        assertEquals((byte)7, array1[1]);
+        assertEquals((byte)1, (byte)list1.get(0));
+        assertEquals(3, list1.size());
+        
+        short[] array2 = new short[]{1,2,3};
+        List<Short> list2 = ArrayUtils.$wrap(array2);
+        list2.set(1, (short)7);
+        assertEquals((short)7, array2[1]);
+        assertEquals((short)1, (short)list2.get(0));
+        assertEquals(3, list2.size());
+        
+        
+        int[] array3 = new int[]{1,2,3};
+        List<Integer> list3 = ArrayUtils.$wrap(array3);
+        list3.set(1, (int)7);
+        assertEquals((int)7, array3[1]);
+        assertEquals((int)1, (int)list3.get(0));
+        assertEquals(3, list3.size());
+        
+        long[] array4 = new long[]{1,2,3};
+        List<Long> list4 = ArrayUtils.$wrap(array4);
+        list4.set(1, (long)7);
+        assertEquals((long)7, array4[1]);
+        assertEquals((long)1, (long)list4.get(0));
+        assertEquals(3, list4.size());
+        
+        float[] array5 = new float[]{1,2,3};
+        List<Float> list5 = ArrayUtils.$wrap(array5);
+        list5.set(1, (float)7);
+        assertEquals((float)7, array5[1], 0.1f);
+        assertEquals((float)1, (float)list5.get(0), 0.1f);
+        assertEquals(3, list5.size());
+        
+        double[] array6 = new double[]{1,2,3};
+        List<Double> list6 = ArrayUtils.$wrap(array6);
+        list6.set(1, (double)7);
+        assertEquals((double)7, array6[1], 0.1);
+        assertEquals((double)1, (double)list6.get(0), 0.1);
+        assertEquals(3, list6.size());
+        
+        char[] array7 = new char[]{1,2,3};
+        List<Character> list7 = ArrayUtils.$wrap(array7);
+        list7.set(1, (char)7);
+        assertEquals((char)7, array7[1]);
+        assertEquals((char)1, (char)list7.get(0));
+        assertEquals(3, list7.size());
+        
+        boolean[] array8 = new boolean[]{false, false, true, false};
+        List<Boolean> list8 = ArrayUtils.$wrap(array8);
+        list8.set(1, true);
+        assertEquals(true, array8[1]);
+        assertEquals(false, list8.get(0));
+        assertEquals(true, list8.get(1));
+        assertEquals(true, list8.get(2));
+        assertEquals(false, list8.get(3));
+        assertEquals(4, list8.size());
+        
+        String[] array9 = new String[]{"1","2","3"};
+        List<String> list9 = ArrayUtils.$wrap(array9);
+        list9.set(1, "!");
+        assertEquals("!", array9[1]);
+        assertEquals("1", list9.get(0));
+        assertEquals(3, list9.size());
+    }
+        
+}

+ 47 - 0
assira/src/test/java/net/ranides/assira/collection/arrays/IntArraySearchTest.java

@@ -0,0 +1,47 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/???
+ */
+package net.ranides.assira.collection.arrays;
+
+import net.ranides.assira.collection.IntComparator;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class IntArraySearchTest {
+    
+    @Test
+    public void testBinarySearch() {
+        int[] array1 = new int[]{1,2,5,6};
+        assertEquals(0, IntArraySearch.binarySearch(array1, 1));
+        assertEquals(1, IntArraySearch.binarySearch(array1, 2));
+        assertEquals(2, IntArraySearch.binarySearch(array1, 5));
+        assertEquals(3, IntArraySearch.binarySearch(array1, 6));
+        assertEquals(-5, IntArraySearch.binarySearch(array1, 9));
+        assertEquals(-1, IntArraySearch.binarySearch(array1, 0));
+        assertEquals(-3, IntArraySearch.binarySearch(array1, 3));
+        assertEquals(-3, IntArraySearch.binarySearch(array1, 4));
+        
+    }
+
+    @Test
+    public void testBinarySearch_cmp() {
+        int[] array1 = new int[]{31,22,75,46};
+        IntComparator cmp = (a,b) -> (a%10-b%10);
+        assertEquals(0, IntArraySearch.binarySearch(array1, 1, cmp));
+        assertEquals(1, IntArraySearch.binarySearch(array1, 92, cmp));
+        assertEquals(2, IntArraySearch.binarySearch(array1, 95, cmp));
+        assertEquals(3, IntArraySearch.binarySearch(array1, 96, cmp));
+        assertEquals(-5, IntArraySearch.binarySearch(array1, 99, cmp));
+        assertEquals(-1, IntArraySearch.binarySearch(array1, 90, cmp));
+        assertEquals(-3, IntArraySearch.binarySearch(array1, 93, cmp));
+        assertEquals(-3, IntArraySearch.binarySearch(array1, 94, cmp));
+    }
+    
+}