Added a form of pickRandom that takes a List argument.

I just can't bear to see a useless object created (an Iterator) and
potentially a great many objects iterated past in order to find the
chosen random object when the original List is usually RandomAccess.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3366 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2005-02-24 06:45:25 +00:00
parent 44f2a29f4f
commit a107aef489
+49 -1
View File
@@ -1,5 +1,5 @@
//
// $Id: RandomUtil.java,v 1.11 2004/08/27 02:20:36 mdb Exp $
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
@@ -22,7 +22,9 @@
package com.threerings.util;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.RandomAccess;
import com.samskivert.util.IntListUtil;
@@ -142,6 +144,52 @@ public class RandomUtil
return (index >= values.length) ? null : values[index];
}
/**
* Picks a random object from the supplied List
*
* @return a randomly selected item.
*/
public static Object pickRandom (List values)
{
int size = values.size();
if (!(values instanceof RandomAccess)) {
return pickRandom(values.iterator(), size);
}
if (size == 0) {
throw new IllegalArgumentException(
"Must have at least one element [size=" + size + "]");
}
return values.get(getInt(size));
}
/**
* Picks a random object from the supplied List. The specified skip
* object will be skipped when selecting a random value. The skipped
* object must exist exactly once in the List.
*
* @return a randomly selected item.
*/
public static Object pickRandom (List values, Object skip)
{
int size = values.size();
if (!(values instanceof RandomAccess)) {
return pickRandom(values.iterator(), size, skip);
}
if (size < 2) {
throw new IllegalArgumentException(
"Must have at least one element [size=" + size + "]");
}
int pick = getInt(size - 1);
Object val = values.get(pick);
if (val == skip) {
val = values.get(pick + 1);
}
return val;
}
/**
* Picks a random object from the supplied iterator (which must
* iterate over exactly <code>count</code> objects.