Save This Page
Home » mahout-collections-1.0-src » org.apache.mahout.math » [javadoc | source]
    1   /*
    2   Copyright 1999 CERN - European Organization for Nuclear Research.
    3   Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
    4   is hereby granted without fee, provided that the above copyright notice appear in all copies and 
    5   that both that copyright notice and this permission notice appear in supporting documentation. 
    6   CERN makes no representations about the suitability of this software for any purpose. 
    7   It is provided "as is" without expressed or implied warranty.
    8   */
    9   package org.apache.mahout.math;
   10   
   11   /**
   12    * Array manipulations; complements <tt>java.util.Arrays</tt>.
   13    *
   14    * @see java.util.Arrays
   15    * @see org.apache.mahout.math.Sorting
   16    *
   17    */
   18   public final class Arrays {
   19   
   20     private Arrays() {
   21     }
   22   
   23     /**
   24      * Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
   25      *
   26      * Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new
   27      * array with increased capacity containing the same elements, ensuring that it can hold at least the number of
   28      * elements specified by the minimum capacity argument.
   29      *
   30      * @param minCapacity the desired minimum capacity.
   31      */
   32     public static byte[] ensureCapacity(byte[] array, int minCapacity) {
   33       int oldCapacity = array.length;
   34       byte[] newArray;
   35       if (minCapacity > oldCapacity) {
   36         int newCapacity = (oldCapacity * 3) / 2 + 1;
   37         if (newCapacity < minCapacity) {
   38           newCapacity = minCapacity;
   39         }
   40   
   41         newArray = new byte[newCapacity];
   42         System.arraycopy(array, 0, newArray, 0, oldCapacity);
   43       } else {
   44         newArray = array;
   45       }
   46       return newArray;
   47     }
   48   
   49     /**
   50      * Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
   51      *
   52      * Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new
   53      * array with increased capacity containing the same elements, ensuring that it can hold at least the number of
   54      * elements specified by the minimum capacity argument.
   55      *
   56      * @param minCapacity the desired minimum capacity.
   57      */
   58     public static char[] ensureCapacity(char[] array, int minCapacity) {
   59       int oldCapacity = array.length;
   60       char[] newArray;
   61       if (minCapacity > oldCapacity) {
   62         int newCapacity = (oldCapacity * 3) / 2 + 1;
   63         if (newCapacity < minCapacity) {
   64           newCapacity = minCapacity;
   65         }
   66   
   67         newArray = new char[newCapacity];
   68         System.arraycopy(array, 0, newArray, 0, oldCapacity);
   69       } else {
   70         newArray = array;
   71       }
   72       return newArray;
   73     }
   74   
   75     /**
   76      * Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
   77      *
   78      * Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new
   79      * array with increased capacity containing the same elements, ensuring that it can hold at least the number of
   80      * elements specified by the minimum capacity argument.
   81      *
   82      * @param minCapacity the desired minimum capacity.
   83      */
   84     public static double[] ensureCapacity(double[] array, int minCapacity) {
   85       int oldCapacity = array.length;
   86       double[] newArray;
   87       if (minCapacity > oldCapacity) {
   88         int newCapacity = (oldCapacity * 3) / 2 + 1;
   89         if (newCapacity < minCapacity) {
   90           newCapacity = minCapacity;
   91         }
   92   
   93         newArray = new double[newCapacity];
   94         //for (int i = oldCapacity; --i >= 0; ) newArray[i] = array[i];
   95         System.arraycopy(array, 0, newArray, 0, oldCapacity);
   96       } else {
   97         newArray = array;
   98       }
   99       return newArray;
  100     }
  101   
  102     /**
  103      * Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
  104      *
  105      * Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new
  106      * array with increased capacity containing the same elements, ensuring that it can hold at least the number of
  107      * elements specified by the minimum capacity argument.
  108      *
  109      * @param minCapacity the desired minimum capacity.
  110      */
  111     public static float[] ensureCapacity(float[] array, int minCapacity) {
  112       int oldCapacity = array.length;
  113       float[] newArray;
  114       if (minCapacity > oldCapacity) {
  115         int newCapacity = (oldCapacity * 3) / 2 + 1;
  116         if (newCapacity < minCapacity) {
  117           newCapacity = minCapacity;
  118         }
  119   
  120         newArray = new float[newCapacity];
  121         System.arraycopy(array, 0, newArray, 0, oldCapacity);
  122       } else {
  123         newArray = array;
  124       }
  125       return newArray;
  126     }
  127   
  128     /**
  129      * Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
  130      *
  131      * Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new
  132      * array with increased capacity containing the same elements, ensuring that it can hold at least the number of
  133      * elements specified by the minimum capacity argument.
  134      *
  135      * @param minCapacity the desired minimum capacity.
  136      */
  137     public static int[] ensureCapacity(int[] array, int minCapacity) {
  138       int oldCapacity = array.length;
  139       int[] newArray;
  140       if (minCapacity > oldCapacity) {
  141         int newCapacity = (oldCapacity * 3) / 2 + 1;
  142         if (newCapacity < minCapacity) {
  143           newCapacity = minCapacity;
  144         }
  145   
  146         newArray = new int[newCapacity];
  147         System.arraycopy(array, 0, newArray, 0, oldCapacity);
  148       } else {
  149         newArray = array;
  150       }
  151       return newArray;
  152     }
  153   
  154     /**
  155      * Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
  156      *
  157      * Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new
  158      * array with increased capacity containing the same elements, ensuring that it can hold at least the number of
  159      * elements specified by the minimum capacity argument.
  160      *
  161      * @param minCapacity the desired minimum capacity.
  162      */
  163     public static long[] ensureCapacity(long[] array, int minCapacity) {
  164       int oldCapacity = array.length;
  165       long[] newArray;
  166       if (minCapacity > oldCapacity) {
  167         int newCapacity = (oldCapacity * 3) / 2 + 1;
  168         if (newCapacity < minCapacity) {
  169           newCapacity = minCapacity;
  170         }
  171   
  172         newArray = new long[newCapacity];
  173         System.arraycopy(array, 0, newArray, 0, oldCapacity);
  174       } else {
  175         newArray = array;
  176       }
  177       return newArray;
  178     }
  179   
  180     /**
  181      * Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
  182      *
  183      * Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new
  184      * array with increased capacity containing the same elements, ensuring that it can hold at least the number of
  185      * elements specified by the minimum capacity argument.
  186      *
  187      * @param minCapacity the desired minimum capacity.
  188      */
  189     public static Object[] ensureCapacity(Object[] array, int minCapacity) {
  190       int oldCapacity = array.length;
  191       Object[] newArray;
  192       if (minCapacity > oldCapacity) {
  193         int newCapacity = (oldCapacity * 3) / 2 + 1;
  194         if (newCapacity < minCapacity) {
  195           newCapacity = minCapacity;
  196         }
  197   
  198         newArray = new Object[newCapacity];
  199         System.arraycopy(array, 0, newArray, 0, oldCapacity);
  200       } else {
  201         newArray = array;
  202       }
  203       return newArray;
  204     }
  205   
  206     /**
  207      * Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
  208      *
  209      * Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new
  210      * array with increased capacity containing the same elements, ensuring that it can hold at least the number of
  211      * elements specified by the minimum capacity argument.
  212      *
  213      * @param minCapacity the desired minimum capacity.
  214      */
  215     public static short[] ensureCapacity(short[] array, int minCapacity) {
  216       int oldCapacity = array.length;
  217       short[] newArray;
  218       if (minCapacity > oldCapacity) {
  219         int newCapacity = (oldCapacity * 3) / 2 + 1;
  220         if (newCapacity < minCapacity) {
  221           newCapacity = minCapacity;
  222         }
  223   
  224         newArray = new short[newCapacity];
  225         System.arraycopy(array, 0, newArray, 0, oldCapacity);
  226       } else {
  227         newArray = array;
  228       }
  229       return newArray;
  230     }
  231   
  232     /**
  233      * Ensures that a given array can hold up to <tt>minCapacity</tt> elements.
  234      *
  235      * Returns the identical array if it can hold at least the number of elements specified. Otherwise, returns a new
  236      * array with increased capacity containing the same elements, ensuring that it can hold at least the number of
  237      * elements specified by the minimum capacity argument.
  238      *
  239      * @param minCapacity the desired minimum capacity.
  240      */
  241     public static boolean[] ensureCapacity(boolean[] array, int minCapacity) {
  242       int oldCapacity = array.length;
  243       boolean[] newArray;
  244       if (minCapacity > oldCapacity) {
  245         int newCapacity = (oldCapacity * 3) / 2 + 1;
  246         if (newCapacity < minCapacity) {
  247           newCapacity = minCapacity;
  248         }
  249   
  250         newArray = new boolean[newCapacity];
  251         System.arraycopy(array, 0, newArray, 0, oldCapacity);
  252       } else {
  253         newArray = array;
  254       }
  255       return newArray;
  256     }
  257   
  258     /**
  259      * Returns a string representation of the specified array.  The string representation consists of a list of the
  260      * arrays's elements, enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are separated by the characters
  261      * <tt>", "</tt> (comma and space).
  262      *
  263      * @return a string representation of the specified array.
  264      */
  265     public static String toString(byte[] array) {
  266       StringBuilder buf = new StringBuilder();
  267       buf.append('[');
  268       int maxIndex = array.length - 1;
  269       for (int i = 0; i <= maxIndex; i++) {
  270         buf.append(array[i]);
  271         if (i < maxIndex) {
  272           buf.append(", ");
  273         }
  274       }
  275       buf.append(']');
  276       return buf.toString();
  277     }
  278   
  279     /**
  280      * Returns a string representation of the specified array.  The string representation consists of a list of the
  281      * arrays's elements, enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are separated by the characters
  282      * <tt>", "</tt> (comma and space).
  283      *
  284      * @return a string representation of the specified array.
  285      */
  286     public static String toString(char[] array) {
  287       StringBuilder buf = new StringBuilder();
  288       buf.append('[');
  289       int maxIndex = array.length - 1;
  290       for (int i = 0; i <= maxIndex; i++) {
  291         buf.append(array[i]);
  292         if (i < maxIndex) {
  293           buf.append(", ");
  294         }
  295       }
  296       buf.append(']');
  297       return buf.toString();
  298     }
  299   
  300     /**
  301      * Returns a string representation of the specified array.  The string representation consists of a list of the
  302      * arrays's elements, enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are separated by the characters
  303      * <tt>", "</tt> (comma and space).
  304      *
  305      * @return a string representation of the specified array.
  306      */
  307     public static String toString(double[] array) {
  308       StringBuilder buf = new StringBuilder();
  309       buf.append('[');
  310       int maxIndex = array.length - 1;
  311       for (int i = 0; i <= maxIndex; i++) {
  312         buf.append(array[i]);
  313         if (i < maxIndex) {
  314           buf.append(", ");
  315         }
  316       }
  317       buf.append(']');
  318       return buf.toString();
  319     }
  320   
  321     /**
  322      * Returns a string representation of the specified array.  The string representation consists of a list of the
  323      * arrays's elements, enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are separated by the characters
  324      * <tt>", "</tt> (comma and space).
  325      *
  326      * @return a string representation of the specified array.
  327      */
  328     public static String toString(float[] array) {
  329       StringBuilder buf = new StringBuilder();
  330       buf.append('[');
  331       int maxIndex = array.length - 1;
  332       for (int i = 0; i <= maxIndex; i++) {
  333         buf.append(array[i]);
  334         if (i < maxIndex) {
  335           buf.append(", ");
  336         }
  337       }
  338       buf.append(']');
  339       return buf.toString();
  340     }
  341   
  342     /**
  343      * Returns a string representation of the specified array.  The string representation consists of a list of the
  344      * arrays's elements, enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are separated by the characters
  345      * <tt>", "</tt> (comma and space).
  346      *
  347      * @return a string representation of the specified array.
  348      */
  349     public static String toString(int[] array) {
  350       StringBuilder buf = new StringBuilder();
  351       buf.append('[');
  352       int maxIndex = array.length - 1;
  353       for (int i = 0; i <= maxIndex; i++) {
  354         buf.append(array[i]);
  355         if (i < maxIndex) {
  356           buf.append(", ");
  357         }
  358       }
  359       buf.append(']');
  360       return buf.toString();
  361     }
  362   
  363     /**
  364      * Returns a string representation of the specified array.  The string representation consists of a list of the
  365      * arrays's elements, enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are separated by the characters
  366      * <tt>", "</tt> (comma and space).
  367      *
  368      * @return a string representation of the specified array.
  369      */
  370     public static String toString(long[] array) {
  371       StringBuilder buf = new StringBuilder();
  372       buf.append('[');
  373       int maxIndex = array.length - 1;
  374       for (int i = 0; i <= maxIndex; i++) {
  375         buf.append(array[i]);
  376         if (i < maxIndex) {
  377           buf.append(", ");
  378         }
  379       }
  380       buf.append(']');
  381       return buf.toString();
  382     }
  383   
  384     /**
  385      * Returns a string representation of the specified array.  The string representation consists of a list of the
  386      * arrays's elements, enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are separated by the characters
  387      * <tt>", "</tt> (comma and space).
  388      *
  389      * @return a string representation of the specified array.
  390      */
  391     public static String toString(Object[] array) {
  392       StringBuilder buf = new StringBuilder();
  393       buf.append('[');
  394       int maxIndex = array.length - 1;
  395       for (int i = 0; i <= maxIndex; i++) {
  396         buf.append(array[i]);
  397         if (i < maxIndex) {
  398           buf.append(", ");
  399         }
  400       }
  401       buf.append(']');
  402       return buf.toString();
  403     }
  404   
  405     /**
  406      * Returns a string representation of the specified array.  The string representation consists of a list of the
  407      * arrays's elements, enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are separated by the characters
  408      * <tt>", "</tt> (comma and space).
  409      *
  410      * @return a string representation of the specified array.
  411      */
  412     public static String toString(short[] array) {
  413       StringBuilder buf = new StringBuilder();
  414       buf.append('[');
  415       int maxIndex = array.length - 1;
  416       for (int i = 0; i <= maxIndex; i++) {
  417         buf.append(array[i]);
  418         if (i < maxIndex) {
  419           buf.append(", ");
  420         }
  421       }
  422       buf.append(']');
  423       return buf.toString();
  424     }
  425   
  426     /**
  427      * Returns a string representation of the specified array.  The string representation consists of a list of the
  428      * arrays's elements, enclosed in square brackets (<tt>"[]"</tt>).  Adjacent elements are separated by the characters
  429      * <tt>", "</tt> (comma and space).
  430      *
  431      * @return a string representation of the specified array.
  432      */
  433     public static String toString(boolean[] array) {
  434       StringBuilder buf = new StringBuilder();
  435       buf.append('[');
  436       int maxIndex = array.length - 1;
  437       for (int i = 0; i <= maxIndex; i++) {
  438         buf.append(array[i]);
  439         if (i < maxIndex) {
  440           buf.append(", ");
  441         }
  442       }
  443       buf.append(']');
  444       return buf.toString();
  445     }
  446   
  447     /**
  448      * Ensures that the specified array cannot hold more than <tt>maxCapacity</tt> elements. An application can use this
  449      * operation to minimize array storage. <p> Returns the identical array if <tt>array.length &lt;= maxCapacity</tt>.
  450      * Otherwise, returns a new array with a length of <tt>maxCapacity</tt> containing the first <tt>maxCapacity</tt>
  451      * elements of <tt>array</tt>.
  452      *
  453      * @param maxCapacity the desired maximum capacity.
  454      */
  455     public static byte[] trimToCapacity(byte[] array, int maxCapacity) {
  456       if (array.length > maxCapacity) {
  457         byte[] oldArray = array;
  458         array = new byte[maxCapacity];
  459         System.arraycopy(oldArray, 0, array, 0, maxCapacity);
  460       }
  461       return array;
  462     }
  463   
  464     /**
  465      * Ensures that the specified array cannot hold more than <tt>maxCapacity</tt> elements. An application can use this
  466      * operation to minimize array storage. <p> Returns the identical array if <tt>array.length &lt;= maxCapacity</tt>.
  467      * Otherwise, returns a new array with a length of <tt>maxCapacity</tt> containing the first <tt>maxCapacity</tt>
  468      * elements of <tt>array</tt>.
  469      *
  470      * @param maxCapacity the desired maximum capacity.
  471      */
  472     public static char[] trimToCapacity(char[] array, int maxCapacity) {
  473       if (array.length > maxCapacity) {
  474         char[] oldArray = array;
  475         array = new char[maxCapacity];
  476         System.arraycopy(oldArray, 0, array, 0, maxCapacity);
  477       }
  478       return array;
  479     }
  480   
  481     /**
  482      * Ensures that the specified array cannot hold more than <tt>maxCapacity</tt> elements. An application can use this
  483      * operation to minimize array storage. <p> Returns the identical array if <tt>array.length &lt;= maxCapacity</tt>.
  484      * Otherwise, returns a new array with a length of <tt>maxCapacity</tt> containing the first <tt>maxCapacity</tt>
  485      * elements of <tt>array</tt>.
  486      *
  487      * @param maxCapacity the desired maximum capacity.
  488      */
  489     public static double[] trimToCapacity(double[] array, int maxCapacity) {
  490       if (array.length > maxCapacity) {
  491         double[] oldArray = array;
  492         array = new double[maxCapacity];
  493         System.arraycopy(oldArray, 0, array, 0, maxCapacity);
  494       }
  495       return array;
  496     }
  497   
  498     /**
  499      * Ensures that the specified array cannot hold more than <tt>maxCapacity</tt> elements. An application can use this
  500      * operation to minimize array storage. <p> Returns the identical array if <tt>array.length &lt;= maxCapacity</tt>.
  501      * Otherwise, returns a new array with a length of <tt>maxCapacity</tt> containing the first <tt>maxCapacity</tt>
  502      * elements of <tt>array</tt>.
  503      *
  504      * @param maxCapacity the desired maximum capacity.
  505      */
  506     public static float[] trimToCapacity(float[] array, int maxCapacity) {
  507       if (array.length > maxCapacity) {
  508         float[] oldArray = array;
  509         array = new float[maxCapacity];
  510         System.arraycopy(oldArray, 0, array, 0, maxCapacity);
  511       }
  512       return array;
  513     }
  514   
  515     /**
  516      * Ensures that the specified array cannot hold more than <tt>maxCapacity</tt> elements. An application can use this
  517      * operation to minimize array storage. <p> Returns the identical array if <tt>array.length &lt;= maxCapacity</tt>.
  518      * Otherwise, returns a new array with a length of <tt>maxCapacity</tt> containing the first <tt>maxCapacity</tt>
  519      * elements of <tt>array</tt>.
  520      *
  521      * @param maxCapacity the desired maximum capacity.
  522      */
  523     public static int[] trimToCapacity(int[] array, int maxCapacity) {
  524       if (array.length > maxCapacity) {
  525         int[] oldArray = array;
  526         array = new int[maxCapacity];
  527         System.arraycopy(oldArray, 0, array, 0, maxCapacity);
  528       }
  529       return array;
  530     }
  531   
  532     /**
  533      * Ensures that the specified array cannot hold more than <tt>maxCapacity</tt> elements. An application can use this
  534      * operation to minimize array storage. <p> Returns the identical array if <tt>array.length &lt;= maxCapacity</tt>.
  535      * Otherwise, returns a new array with a length of <tt>maxCapacity</tt> containing the first <tt>maxCapacity</tt>
  536      * elements of <tt>array</tt>.
  537      *
  538      * @param maxCapacity the desired maximum capacity.
  539      */
  540     public static long[] trimToCapacity(long[] array, int maxCapacity) {
  541       if (array.length > maxCapacity) {
  542         long[] oldArray = array;
  543         array = new long[maxCapacity];
  544         System.arraycopy(oldArray, 0, array, 0, maxCapacity);
  545       }
  546       return array;
  547     }
  548   
  549     /**
  550      * Ensures that the specified array cannot hold more than <tt>maxCapacity</tt> elements. An application can use this
  551      * operation to minimize array storage. <p> Returns the identical array if <tt>array.length &lt;= maxCapacity</tt>.
  552      * Otherwise, returns a new array with a length of <tt>maxCapacity</tt> containing the first <tt>maxCapacity</tt>
  553      * elements of <tt>array</tt>.
  554      *
  555      * @param maxCapacity the desired maximum capacity.
  556      */
  557     public static Object[] trimToCapacity(Object[] array, int maxCapacity) {
  558       if (array.length > maxCapacity) {
  559         Object[] oldArray = array;
  560         array = new Object[maxCapacity];
  561         System.arraycopy(oldArray, 0, array, 0, maxCapacity);
  562       }
  563       return array;
  564     }
  565   
  566     /**
  567      * Ensures that the specified array cannot hold more than <tt>maxCapacity</tt> elements. An application can use this
  568      * operation to minimize array storage. <p> Returns the identical array if <tt>array.length &lt;= maxCapacity</tt>.
  569      * Otherwise, returns a new array with a length of <tt>maxCapacity</tt> containing the first <tt>maxCapacity</tt>
  570      * elements of <tt>array</tt>.
  571      *
  572      * @param maxCapacity the desired maximum capacity.
  573      */
  574     public static short[] trimToCapacity(short[] array, int maxCapacity) {
  575       if (array.length > maxCapacity) {
  576         short[] oldArray = array;
  577         array = new short[maxCapacity];
  578         System.arraycopy(oldArray, 0, array, 0, maxCapacity);
  579       }
  580       return array;
  581     }
  582   
  583     /**
  584      * Ensures that the specified array cannot hold more than <tt>maxCapacity</tt> elements. An application can use this
  585      * operation to minimize array storage. <p> Returns the identical array if <tt>array.length &lt;= maxCapacity</tt>.
  586      * Otherwise, returns a new array with a length of <tt>maxCapacity</tt> containing the first <tt>maxCapacity</tt>
  587      * elements of <tt>array</tt>.
  588      *
  589      * @param maxCapacity the desired maximum capacity.
  590      */
  591     public static boolean[] trimToCapacity(boolean[] array, int maxCapacity) {
  592       if (array.length > maxCapacity) {
  593         boolean[] oldArray = array;
  594         array = new boolean[maxCapacity];
  595         System.arraycopy(oldArray, 0, array, 0, maxCapacity);
  596       }
  597       return array;
  598     }
  599   
  600     /**
  601      * {@link java.util.Arrays#copyOf} compatibility with Java 1.5.
  602      */
  603     public static byte[] copyOf(byte[] src, int length) {
  604         byte[] result = new byte [length];
  605         System.arraycopy(src, 0, result, 0, Math.min(length, src.length));
  606         return result;
  607     }
  608     
  609     /**
  610      * {@link java.util.Arrays#copyOf} compatibility with Java 1.5.
  611      */
  612     public static char[] copyOf(char[] src, int length) {
  613         char[] result = new char [length];
  614         System.arraycopy(src, 0, result, 0, Math.min(length, src.length));
  615         return result;
  616     }
  617     
  618     /**
  619      * {@link java.util.Arrays#copyOf} compatibility with Java 1.5.
  620      */
  621     public static short[] copyOf(short[] src, int length) {
  622         short[] result = new short [length];
  623         System.arraycopy(src, 0, result, 0, Math.min(length, src.length));
  624         return result;
  625     }
  626     
  627     /**
  628      * {@link java.util.Arrays#copyOf} compatibility with Java 1.5. 
  629      */
  630     public static int[] copyOf(int[] src, int length) {
  631         int[] result = new int [length];
  632         System.arraycopy(src, 0, result, 0, Math.min(length, src.length));
  633         return result;
  634     }
  635     
  636     /**
  637      * {@link java.util.Arrays#copyOf} compatibility with Java 1.5.
  638      */
  639     public static float[] copyOf(float[] src, int length) {
  640         float[] result = new float [length];
  641         System.arraycopy(src, 0, result, 0, Math.min(length, src.length));
  642         return result;
  643     }
  644   
  645     /**
  646      * {@link java.util.Arrays#copyOf} compatibility with Java 1.5.
  647      */
  648     public static double[] copyOf(double[] src, int length) {
  649         double[] result = new double [length];
  650         System.arraycopy(src, 0, result, 0, Math.min(length, src.length));
  651         return result;
  652     }
  653     
  654     /**
  655      * {@link java.util.Arrays#copyOf} compatibility with Java 1.5.
  656      */
  657     public static long[] copyOf(long[] src, int length) {
  658         long[] result = new long [length];
  659         System.arraycopy(src, 0, result, 0, Math.min(length, src.length));
  660         return result;
  661     }
  662   }

Save This Page
Home » mahout-collections-1.0-src » org.apache.mahout.math » [javadoc | source]