From 02b1326e4ac5595535ed65026fd7777a8fe7c3e2 Mon Sep 17 00:00:00 2001 From: ray Date: Thu, 6 May 2004 22:01:25 +0000 Subject: [PATCH] added getMinIndexes git-svn-id: https://samskivert.googlecode.com/svn/trunk@1426 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../java/com/samskivert/util/IntListUtil.java | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/IntListUtil.java b/projects/samskivert/src/java/com/samskivert/util/IntListUtil.java index a5d78415..684df911 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.5 2003/02/06 18:51:50 mdb Exp $ +// $Id: IntListUtil.java,v 1.6 2004/05/06 22:01:25 ray Exp $ package com.samskivert.util; @@ -395,6 +395,46 @@ public class IntListUtil return maxes; } + /** + * Returns an array of the indexes in the given array of values that + * have the minimum value in the array, or a zero-length array if the + * supplied array of values is null or zero-length. + */ + public static int[] getMinIndexes (int[] values) + { + int min = Integer.MAX_VALUE; + int num = 0; + int vcount = (values == null) ? 0 : values.length; + + for (int ii=0; ii < vcount; ii++) { + int value = values[ii]; + + if (value > min) { + // common case- stop checking things.. + continue; + + } else if (value < min) { + // new min + min = value; + num = 1; + + } else { + // another sighting of min + num++; + } + } + + // now find the indexes that have min + int[] mins = new int[num]; + for (int ii=0, pos=0; pos < num; ii++) { + if (values[ii] == min) { + mins[pos++] = ii; + } + } + + return mins; + } + /** * Creates a new list that will accomodate the specified index and * copies the contents of the old list to the first.