From d6aee14e800d4f841b80ae832ba890fee07ccc62 Mon Sep 17 00:00:00 2001 From: mdb Date: Tue, 4 Feb 2003 03:12:30 +0000 Subject: [PATCH] Moved a bunch of int[] specific stuff to IntListUtil. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1048 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/ArrayUtil.java | 110 +---------------- .../java/com/samskivert/util/IntListUtil.java | 112 +++++++++++++++++- 2 files changed, 111 insertions(+), 111 deletions(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/ArrayUtil.java b/projects/samskivert/src/java/com/samskivert/util/ArrayUtil.java index 7ab6e0f6..71b8796b 100644 --- a/projects/samskivert/src/java/com/samskivert/util/ArrayUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/ArrayUtil.java @@ -1,5 +1,5 @@ // -// $Id: ArrayUtil.java,v 1.23 2003/01/17 01:27:16 mdb Exp $ +// $Id: ArrayUtil.java,v 1.24 2003/02/04 03:12:30 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Walter Korman @@ -31,114 +31,6 @@ import com.samskivert.Log; */ public class ArrayUtil { - /** - * Returns the maximum value in the given array of values, or {@link - * Integer#MIN_VALUE} if the array is null or zero-length. - */ - public static int getMaxValue (int[] values) - { - int max = Integer.MIN_VALUE; - int vcount = (values == null) ? 0 : values.length; - for (int ii = 0; ii < vcount; ii++) { - if (values[ii] > max) { - // new max - max = values[ii]; - } - } - return max; - } - - /** - * Returns the minimum value in the given array of values, or {@link - * Integer#MAX_VALUE} if the array is null or zero-length. - */ - public static int getMinValue (int[] values) - { - int min = Integer.MAX_VALUE; - int vcount = (values == null) ? 0 : values.length; - for (int ii = 0; ii < vcount; ii++) { - if (values[ii] < min) { - // new min - min = values[ii]; - } - } - return min; - } - - /** - * Returns the index of the maximum value in the given array of - * values, or -1 if the array is null or - * zero-length. - */ - public static int getMaxValueIndex (int[] values) - { - if (values == null || values.length == 0) { - return -1; - } - - int idx = 0; - int max = values[idx]; - for (int ii = 1; ii < values.length; ii++) { - if (values[ii] > max) { - max = values[ii]; - idx = ii; - } - } - return idx; - } - - /** - * Returns an array of the indexes in the given array of values that - * have the maximum value in the array, or a zero-length array if the - * supplied array of values is null or zero-length. - */ - public static int[] getMaxIndexes (int[] values) - { - int max = Integer.MIN_VALUE; - int num = 0; - int vcount = (values == null) ? 0 : values.length; - - for (int ii=0; ii < vcount; ii++) { - int value = values[ii]; - - if (value < max) { - // common case- stop checking things.. - continue; - - } else if (value > max) { - // new max - max = value; - num = 1; - - } else { - // another sighting of max - num++; - } - } - - // now find the indexes that have max - int[] maxes = new int[num]; - for (int ii=0, pos=0; pos < num; ii++) { - if (values[ii] == max) { - maxes[pos++] = ii; - } - } - - return maxes; - } - - /** - * Returns the sum of the values in the supplied integer array. - */ - public static int sum (int[] values) - { - int sum = 0; - for (int ii = 0; ii < values.length; ii++) { - sum += values[ii]; - } - return sum; - } - /** * Looks for an element that is equal to the supplied value and * returns its index in the array. diff --git a/projects/samskivert/src/java/com/samskivert/util/IntListUtil.java b/projects/samskivert/src/java/com/samskivert/util/IntListUtil.java index 559119b4..8d365264 100644 --- a/projects/samskivert/src/java/com/samskivert/util/IntListUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/IntListUtil.java @@ -1,5 +1,5 @@ // -// $Id: IntListUtil.java,v 1.3 2002/11/12 05:23:41 mdb Exp $ +// $Id: IntListUtil.java,v 1.4 2003/02/04 03:12:30 mdb Exp $ package com.samskivert.util; @@ -290,7 +290,7 @@ public class IntListUtil /** * Returns the total of all of the values in the list. */ - public static int total (int[] list) + public static int sum (int[] list) { int total = 0, lsize = list.length; for (int ii = 0; ii < lsize; ii++) { @@ -299,6 +299,114 @@ public class IntListUtil return total; } + /** + * Returns the maximum value in the given array of values, or {@link + * Integer#MIN_VALUE} if the array is null or zero-length. + */ + public static int getMaxValue (int[] values) + { + int max = Integer.MIN_VALUE; + int vcount = (values == null) ? 0 : values.length; + for (int ii = 0; ii < vcount; ii++) { + if (values[ii] > max) { + // new max + max = values[ii]; + } + } + return max; + } + + /** + * Returns the minimum value in the given array of values, or {@link + * Integer#MAX_VALUE} if the array is null or zero-length. + */ + public static int getMinValue (int[] values) + { + int min = Integer.MAX_VALUE; + int vcount = (values == null) ? 0 : values.length; + for (int ii = 0; ii < vcount; ii++) { + if (values[ii] < min) { + // new min + min = values[ii]; + } + } + return min; + } + + /** + * Returns the index of the maximum value in the given array of + * values, or -1 if the array is null or + * zero-length. + */ + public static int getMaxValueIndex (int[] values) + { + if (values == null || values.length == 0) { + return -1; + } + + int idx = 0; + int max = values[idx]; + for (int ii = 1; ii < values.length; ii++) { + if (values[ii] > max) { + max = values[ii]; + idx = ii; + } + } + return idx; + } + + /** + * Creates a new array one larger than the supplied array and with the + * specified value inserted into the last slot. + */ + public static int[] append (int[] values, int value) + { + int[] nvalues = new int[values.length+1]; + System.arraycopy(values, 0, nvalues, 0, values.length); + nvalues[values.length] = value; + return nvalues; + } + + /** + * Returns an array of the indexes in the given array of values that + * have the maximum value in the array, or a zero-length array if the + * supplied array of values is null or zero-length. + */ + public static int[] getMaxIndexes (int[] values) + { + int max = Integer.MIN_VALUE; + int num = 0; + int vcount = (values == null) ? 0 : values.length; + + for (int ii=0; ii < vcount; ii++) { + int value = values[ii]; + + if (value < max) { + // common case- stop checking things.. + continue; + + } else if (value > max) { + // new max + max = value; + num = 1; + + } else { + // another sighting of max + num++; + } + } + + // now find the indexes that have max + int[] maxes = new int[num]; + for (int ii=0, pos=0; pos < num; ii++) { + if (values[ii] == max) { + maxes[pos++] = ii; + } + } + + return maxes; + } + /** * Creates a new list that will accomodate the specified index and * copies the contents of the old list to the first.