@Override public short getRandomElement(ShortSet set) { if (set.size <= 0) { throw new UnsupportedOperationException("ShortSet cannot be empty when getting a random element"); } int n = super.nextIntHasty(set.size); short s = 0; ShortSet.ShortSetIterator ssi = set.iterator(); while (n-- >= 0 && ssi.hasNext) s = ssi.next(); ssi.reset(); return s; }
public void addAll (ShortSet set) { ensureCapacity(set.size); ShortSetIterator iterator = set.iterator(); while (iterator.hasNext) add(iterator.next()); }
/** * Returns a random element from the provided ShortSet. If the set is empty * then an exception is thrown. * * <p> * Requires iterating through a random amount of the elements in set, so performance depends on the size of set but * is likely to be decent. This is mostly meant for internal use, the same as ShortSet. * </p> * @param set the ShortSet to get an element from * @return the randomly selected element */ public short getRandomElement(ShortSet set) { if (set.size <= 0) { throw new UnsupportedOperationException("ShortSet cannot be empty when getting a random element"); } int n = nextInt(set.size); short s = 0; ShortSet.ShortSetIterator ssi = set.iterator(); while (n-- >= 0 && ssi.hasNext) s = ssi.next(); ssi.reset(); return s; }
/** * Gets a random short from this ShortSet, using the given {@link IRNG} to generate random values. * If this ShortSet is empty, throws an UnsupportedOperationException. This method operates in linear time, unlike * the random item retrieval methods in {@link OrderedSet} and {@link OrderedMap}, which take constant time. * @param rng an {@link IRNG}, such as {@link RNG} or {@link GWTRNG} * @return a random short from this ShortSet */ public short random(IRNG rng) { if (size <= 0) { throw new UnsupportedOperationException("ShortSet cannot be empty when getting a random element"); } int n = rng.nextInt(size); short s = 0; ShortSet.ShortSetIterator ssi = iterator(); while (n-- >= 0 && ssi.hasNext) s = ssi.next(); ssi.reset(); return s; }
/** * Returns a random element from the provided ShortSet. If the set is empty * then an exception is thrown. * <p> * <p> * Requires iterating through a random amount of the elements in set, so performance depends on the size of set but * is likely to be decent. This is mostly meant for internal use, the same as ShortSet. * </p> * * @param set the ShortSet to get an element from * @return the randomly selected element */ public short getRandomElement(ShortSet set) { if (set.size <= 0) { throw new UnsupportedOperationException("ShortSet cannot be empty when getting a random element"); } int n = nextInt(set.size); short s = 0; ShortSet.ShortSetIterator ssi = set.iterator(); while (n-- >= 0 && ssi.hasNext) s = ssi.next(); ssi.reset(); return s; }