net.maizegenetics.stats.math.OrderEnumerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tassel Show documentation
Show all versions of tassel Show documentation
TASSEL is a software package to evaluate traits associations, evolutionary patterns, and linkage
disequilibrium.
The newest version!
// OrderEnumerator.java
//
// (c) 1999-2002 PAL Development Core Team
//
// This package may be distributed under the
// terms of the Lesser GNU General Public License (LGPL)
package net.maizegenetics.stats.math;
/**
* A means for describing odering information, and Utilities for creating such Orderings
*
* @version $Id: OrderEnumerator.java,v 1.1 2007/01/12 03:26:15 tcasstevens Exp $
*
* @author Matthew Goode
*/
public interface OrderEnumerator {
/**
* If hasMore returns false reset should be called
*/
boolean hasMore();
/**
* The next value in the enumeration
*/
int getNext();
/**
* Reset back to starting state, may have a differnet number of values, and a different ordering after a reset!
*/
void reset();
public static interface OEFactory {
/**
* For generating an ordering from 0..size-1. Enumerator doesn't have to actually produce
*/
public OrderEnumerator createOrderEnumerator(int size);
}
//=====================================================================================================
//================================= Utilities, and hidden classes =====================================
//=====================================================================================================
public static class Utils {
private static final Constant ZERO = new Constant(0);
/**
* @param index The index to always return
* @return an OrderEnumerator object that always returns 'index'
*/
public static final OrderEnumerator getConstant(int index) {
return new Constant(index);
}
/**
* @param size the number of different indexes returned (between 0 and size-1)
* @return an OrderEnumerator object returns index in order between a certain range
*/
public static final OrderEnumerator getOrdered(int size) {
return new Ordered(size);
}
/**
* @param size the number of different indexes returned (between 0 and size-1)
* @return an OrderEnumerator object returns index in random order between a certain range (order changes with each reset)
*/
public static final OrderEnumerator getShuffled(int size) {
return new Shuffled(size);
}
/**
* @param primary The primary OrderEnumerator, one index is taken from this enumertor than an entire sequence of the secondary is taken
* @param secondar The primary OrderEnumerator, the entire sequence of a secondary enumerator is taken for every single index from the primary enumerator
*
* @return an OrderEnumerator object that combines two sub enumerators
*/
public static final OrderEnumerator getBiasAlternating(OrderEnumerator primary, OrderEnumerator secondary) {
return new BiasAlternate(primary,secondary);
}
/**
* @param primary The primary OrderEnumerator
* @param secondar The primary OrderEnumerator
*
* @return an OrderEnumerator object that combines two sub enumerators, by alternating between outputs
*/
public static final OrderEnumerator getAlternating(OrderEnumerator primary, OrderEnumerator secondary) {
return new Alternate(primary,secondary);
}
/**
* @return OrderEnumerator that always returns 0 (zero)
*/
public static final OrderEnumerator getZero() {
return ZERO;
}
/**
* @param minimum minmim value released
* @param range range of values released (that is values go between minimum (inclusive) and minimum+range(exclusive)
*
* @return an OrderEnumerator that is restricted in indexes it returns based on base Enumerator
*
*/
public static final OrderEnumerator getRestricted(OrderEnumerator toRestrict, int minimum, int range) {
return new Restricted(toRestrict,minimum, range);
}
/**
* @return OrderEnumerator that always returns 0 (zero)
*/
public static final OrderEnumerator getAdjusted(OrderEnumerator toAdjust, int adjustmentFactor) {
return new Adjust(toAdjust,adjustmentFactor);
}
//============================================================
//=================== Factory Stuff ==========================
/**
* @return OrderEnumerator that always returns 0 (zero)
*/
public static final OrderEnumerator.OEFactory getZeroFactory() {
return ZERO;
}
/**
* @param index The index to always return
* @return an OrderEnumerator object that always returns 'index'
*/
public static final OrderEnumerator.OEFactory getConstantFactory(int index) {
return new Constant(index);
}
/**
* @return an OrderEnumerator object returns index in order between a certain range
*/
public static final OrderEnumerator.OEFactory getOrderedFactory() {
return Ordered.Factory.INSTANCE;
}
/**
* @return an OrderEnumerator object returns index in random order between a certain range (order changes with each reset)
*/
public static final OrderEnumerator.OEFactory getShuffledFactory() {
return Shuffled.Factory.INSTANCE;
}
/**
* @param adjustmentFactor If to adjust returns x, adjusted will return x+adjustmentFactory (it's that simple)
* @return an OrderEnumerator that returns indexes adjusted from a base enumerator
*
*/
public static final OrderEnumerator.OEFactory getAdjustedFactory(OrderEnumerator.OEFactory toAdjust, int adjustmentFactor) {
return new Adjust.Factory(toAdjust,adjustmentFactor);
}
/**
* @param minimum minmim value released
* @param range range of values released (that is values go between minimum (inclusive) and minimum+range(exclusive)
*
* @return an OrderEnumerator that is restricted in indexes it returns based on base Enumerator
*
*/
public static final OrderEnumerator.OEFactory getRestrictedFactory(OrderEnumerator.OEFactory toRestrict, int minimum, int range) {
return new Restricted.Factory(toRestrict,minimum, range);
}
/**
* @return an OrderEnumerator object that alternates outputs between two base enumerator
*/
public static final OrderEnumerator.OEFactory getAlternatingFactory(OrderEnumerator.OEFactory primary, OrderEnumerator.OEFactory secondary) {
return new Alternate.Factory(primary,secondary);
}
/**
* @return an OrderEnumerator object that alternates outputs between two base enumerator
* (takes one from primary, than all from secondary, one from primary, all from secondary)
*/
public static final OrderEnumerator.OEFactory getBiasAlternatingFactory(OrderEnumerator.OEFactory primary, OrderEnumerator.OEFactory secondary) {
return new BiasAlternate.Factory(primary,secondary);
}
//=======================================================
/**
* Returns the same index ever call
*/
private static class Constant implements OrderEnumerator, OrderEnumerator.OEFactory {
int index_;
boolean hasMore_;
public Constant(int index) {
this.index_ = index;
}
public boolean hasMore() { return hasMore_; }
public int getNext() { hasMore_ = false; return index_; }
public void reset() { hasMore_ = true; }
/**
* For generating an ordering from 0..size-1. Enumerator doesn't have to actually produce
*/
public OrderEnumerator createOrderEnumerator(int size) {
return this;
}
} //End of Constant
//=======================================================
/**
* Incrementally returns indexes
*/
private static class Ordered implements OrderEnumerator {
int index_;
int size_;
public Ordered(int size) {
this.size_= size;
reset();
}
public boolean hasMore() {
return index_=minimum_&&i