Class GapShuffler<T>
java.lang.Object
com.github.yellowstonegames.core.GapShuffler<T>
- Type Parameters:
T- the type of items to iterate over; ideally, the items are unique
- All Implemented Interfaces:
ISerializersNeeded, Iterable<T>, Iterator<T>
Meant to take a fixed-size set of items and produce a shuffled stream of them such that an element is never chosen in
quick succession; that is, there should always be a gap between the same item's occurrences. This is an Iterable of
T, not a Collection, because it can iterate without stopping, infinitely, unless you break out of a foreach loop that
iterates through one of these, or call the iterator's next() method only a limited number of times. Collections have
a size that can be checked, but Iterables can be infinite (and in this case, this one is).
You can serialize this with Fory without needing a serializer, as long as you have the classes in
You can serialize this with Fory without needing a serializer, as long as you have the classes in
getSerializersNeeded() registered. You probably need to register the concrete subclass
of EnhancedRandom you use here; the default subclass of EnhancedRandom used here is AceRandom, unless you
have provided your own with a different type. If you use JSON, a serializer is available in squidstore.grid, and if
you use Kryo, one is available in squidfreeze.grid .-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionGapShuffler(GapShuffler<T> other) Copy constructor; duplicatesother, you know the deal.GapShuffler(Collection<T> elements) Constructor that takes any Collection of T, shuffles it with an unseeded AceRandom, and can then iterate infinitely through mostly-random shuffles of the given collection.GapShuffler(Collection<T> items, com.github.tommyettinger.random.EnhancedRandom random) Constructor that takes any Collection of T, shuffles it with the given EnhancedRandom (such as anAceRandom,WhiskerRandomor, if you need compatibility with SquidLib 3.0.0, a SilkRNG from squidold), and can then iterate infinitely through mostly-random shuffles of the given collection.GapShuffler(Collection<T> items, com.github.tommyettinger.random.EnhancedRandom random, boolean shareRNG) Constructor that takes any Collection of T, shuffles it with the given EnhancedRandom (such as anAceRandom,WhiskerRandomor, if you need compatibility with SquidLib 3.0.0, a SilkRNG from squidold), and can then iterate infinitely through mostly-random shuffles of the given collection.GapShuffler(Collection<T> items, com.github.tommyettinger.random.EnhancedRandom random, int index, boolean shareRNG) Constructor that takes any Collection of T, shuffles it with the given EnhancedRandom (such as anAceRandom,WhiskerRandomor, if you need compatibility with SquidLib 3.0.0, a SilkRNG from squidold), and can then iterate infinitely through mostly-random shuffles of the given collection.GapShuffler(Collection<T> items, com.github.tommyettinger.random.EnhancedRandom random, int index, boolean shareRNG, boolean initialShuffle) Constructor that takes any Collection of T, optionally shuffles it with the given EnhancedRandom (only ifinitialShuffleis true), and can then iterate infinitely through mostly-random shuffles of the given collection.GapShuffler(Collection<T> elements, String seed) Constructor that takes any Collection of T, shuffles it with an AceRandom seeded byseed, and can then iterate infinitely through mostly-random shuffles of the given collection.GapShuffler(T single) GapShuffler(T[] elements) Constructor that takes any Collection of T, shuffles it with an unseeded AceRandom, and can then iterate infinitely through mostly-random shuffles of the given collection.GapShuffler(T[] items, com.github.tommyettinger.random.EnhancedRandom random) Constructor that takes any Collection of T, shuffles it with the given EnhancedRandom (such as an AceRandom or WhiskerRandom), and can then iterate infinitely through mostly-random shuffles of the given collection.GapShuffler(T[] items, com.github.tommyettinger.random.EnhancedRandom random, boolean shareRNG) Constructor that takes any Collection of T, shuffles it with the given EnhancedRandom (such as anAceRandom,WhiskerRandomor, if you need compatibility with SquidLib 3.0.0, a SilkRNG from squidold), and can then iterate infinitely through mostly-random shuffles of the given collection.GapShuffler(T[] elements, CharSequence seed) Constructor that takes any Collection of T, shuffles it with an AceRandom seeded with the given CharSequence (hashed 3 different ways byHasher), and can then iterate infinitely through mostly-random shuffles of the given collection. -
Method Summary
Modifier and TypeMethodDescriptionbooleanvoidfillInto(Collection<T> coll) The internal items used here are protected, but you can still use this method to put a shallow copy of them into some other Collection.intgetIndex()Allows getting (but not setting) the internal index this uses as it iterates through shuffled versions of the same collection indefinitely.com.github.tommyettinger.random.EnhancedRandomgetRNG()Gets a List of Class instances that must each be registered with a serialization library before this object can be successfully serialized or deserialized.booleanhasNext()Returnstrueif the iteration has more elements.iterator()Returns an infinite iterator over elements of typeT; the returned iterator is this object.next()Gets the next element of the infinite sequence of T this shuffles through.voidremove()Not supported.voidsetRNG(com.github.tommyettinger.random.EnhancedRandom random) Sets the EnhancedRandom this uses to shuffle the order of elements, always copying the given EnhancedRandom before using it.voidsetRNG(com.github.tommyettinger.random.EnhancedRandom random, boolean shareRNG) Sets the EnhancedRandom this uses to shuffle the order of elements, optionally sharing a reference between outside code and the internal EnhancedRandom (whenshareRNGis true).toString()Methods inherited from class Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Iterable
forEach, spliteratorMethods inherited from interface Iterator
forEachRemaining
-
Field Details
-
random
public com.github.tommyettinger.random.EnhancedRandom random -
elements
-
index
protected int index
-
-
Constructor Details
-
GapShuffler
public GapShuffler() -
GapShuffler
-
GapShuffler
Constructor that takes any Collection of T, shuffles it with an unseeded AceRandom, and can then iterate infinitely through mostly-random shuffles of the given collection. These shuffles are spaced so that a single element should always have a large amount of "gap" in order between one appearance and the next. It helps to keep the appearance of a gap if every item in elements is unique, but that is not necessary and does not affect how this works.- Parameters:
elements- a Collection of T that will not be modified
-
GapShuffler
Constructor that takes any Collection of T, shuffles it with an AceRandom seeded byseed, and can then iterate infinitely through mostly-random shuffles of the given collection. These shuffles are spaced so that a single element should always have a large amount of "gap" in order between one appearance and the next. It helps to keep the appearance of a gap if every item in elements is unique, but that is not necessary and does not affect how this works.- Parameters:
elements- a Collection of T that will not be modified
-
GapShuffler
Constructor that takes any Collection of T, shuffles it with the given EnhancedRandom (such as anAceRandom,WhiskerRandomor, if you need compatibility with SquidLib 3.0.0, a SilkRNG from squidold), and can then iterate infinitely through mostly-random shuffles of the given collection. These shuffles are spaced so that a single element should always have a large amount of "gap" in order between one appearance and the next. It helps to keep the appearance of a gap if every item in items is unique, but that is not necessary and does not affect how this works. The random parameter is copied so externally using it won't change the order this produces its values; the random field is used whenever the iterator needs to re-shuffle the internal ordering of items.- Parameters:
items- a Collection of T that will not be modifiedrandom- an EnhancedRandom, such as a WhiskerRandom or AceRandom; will be copied and not used directly
-
GapShuffler
public GapShuffler(Collection<T> items, com.github.tommyettinger.random.EnhancedRandom random, boolean shareRNG) Constructor that takes any Collection of T, shuffles it with the given EnhancedRandom (such as anAceRandom,WhiskerRandomor, if you need compatibility with SquidLib 3.0.0, a SilkRNG from squidold), and can then iterate infinitely through mostly-random shuffles of the given collection. These shuffles are spaced so that a single element should always have a large amount of "gap" in order between one appearance and the next. It helps to keep the appearance of a gap if every item in items is unique, but that is not necessary and does not affect how this works. The random parameter will be copied ifshareRNGis true, otherwise the reference will be shared (which could make the results of this GapShuffler depend on outside code, though it will always maintain a gap between identical elements if the elements are unique).- Parameters:
items- a Collection of T that will not be modifiedrandom- an EnhancedRandom, such as a WhiskerRandom; will be copied and not used directlyshareRNG- if false,randomwill be copied and no reference will be kept; if true,randomwill be shared with the outside code
-
GapShuffler
public GapShuffler(Collection<T> items, com.github.tommyettinger.random.EnhancedRandom random, int index, boolean shareRNG) Constructor that takes any Collection of T, shuffles it with the given EnhancedRandom (such as anAceRandom,WhiskerRandomor, if you need compatibility with SquidLib 3.0.0, a SilkRNG from squidold), and can then iterate infinitely through mostly-random shuffles of the given collection. These shuffles are spaced so that a single element should always have a large amount of "gap" in order between one appearance and the next. It helps to keep the appearance of a gap if every item in items is unique, but that is not necessary and does not affect how this works. The random parameter will be copied ifshareRNGis true, otherwise the reference will be shared (which could make the results of this GapShuffler depend on outside code, though it will always maintain a gap between identical elements if the elements are unique). This constructor takes anindexto allow duplicating an existing GapShuffler given the parts that are externally available (usingrandom,fillInto(Collection), andgetIndex()). To actually complete a (shallow) copy,shareRNGmust be false; to do a deep copy, you need to deep-copyitems.- Parameters:
items- a Collection of T that will not be modifiedrandom- an EnhancedRandom, such as a WhiskerRandom; will be copied and not used directlyindex- an index in the iteration, as obtained bygetIndex()shareRNG- if false,randomwill be copied and no reference will be kept; if true,randomwill be shared with the outside code
-
GapShuffler
public GapShuffler(Collection<T> items, com.github.tommyettinger.random.EnhancedRandom random, int index, boolean shareRNG, boolean initialShuffle) Constructor that takes any Collection of T, optionally shuffles it with the given EnhancedRandom (only ifinitialShuffleis true), and can then iterate infinitely through mostly-random shuffles of the given collection. These shuffles are spaced so that a single element should always have a large amount of "gap" in order between one appearance and the next. It helps to keep the appearance of a gap if every item in items is unique, but that is not necessary and does not affect how this works. The random parameter will be copied ifshareRNGis true, otherwise the reference will be shared (which could make the results of this GapShuffler depend on outside code, though it will always maintain a gap between identical elements if the elements are unique). This constructor takes anindexto allow duplicating an existing GapShuffler given the parts that are externally available (usingrandom,fillInto(Collection), andgetIndex()). To actually complete a (shallow) copy,shareRNGmust be false; to do a deep copy, you need to deep-copyitems. You can useinitialShuffleto copy items from another GapShuffler; most of the constructors here act like this does wheninitialShuffleis true, but the copy constructorGapShuffler(GapShuffler)does not (it uses this method).- Parameters:
items- a Collection of T that will not be modifiedrandom- an EnhancedRandom, such as a WhiskerRandom; will be copied and not used directlyindex- an index in the iteration, as obtained bygetIndex()shareRNG- if false,randomwill be copied and no reference will be kept; if true,randomwill be shared with the outside codeinitialShuffle- if true, this will shuffle its copy ofitems; if false, then items will be kept in the same order
-
GapShuffler
Copy constructor; duplicatesother, you know the deal. This does not share itsrandomfield withother.- Parameters:
other- another GapShuffler; its item type will also be used here
-
GapShuffler
Constructor that takes any Collection of T, shuffles it with an unseeded AceRandom, and can then iterate infinitely through mostly-random shuffles of the given collection. These shuffles are spaced so that a single element should always have a large amount of "gap" in order between one appearance and the next. It helps to keep the appearance of a gap if every item in elements is unique, but that is not necessary and does not affect how this works.- Parameters:
elements- an array of T that will not be modified
-
GapShuffler
Constructor that takes any Collection of T, shuffles it with an AceRandom seeded with the given CharSequence (hashed 3 different ways byHasher), and can then iterate infinitely through mostly-random shuffles of the given collection. These shuffles are spaced so that a single element should always have a large amount of "gap" in order between one appearance and the next. It helps to keep the appearance of a gap if every item in elements is unique, but that is not necessary and does not affect how this works.- Parameters:
elements- an array of T that will not be modified
-
GapShuffler
Constructor that takes any Collection of T, shuffles it with the given EnhancedRandom (such as an AceRandom or WhiskerRandom), and can then iterate infinitely through mostly-random shuffles of the given collection. These shuffles are spaced so that a single element should always have a large amount of "gap" in order between one appearance and the next. It helps to keep the appearance of a gap if every item in items is unique, but that is not necessary and does not affect how this works. The random parameter is copied so externally using it won't change the order this produces its values; the random field is used whenever the iterator needs to re-shuffle the internal ordering of items.- Parameters:
items- an array of T that will not be modifiedrandom- an EnhancedRandom, such as an AceRandom or WhiskerRandom; will be copied and not used directly
-
GapShuffler
public GapShuffler(T[] items, com.github.tommyettinger.random.EnhancedRandom random, boolean shareRNG) Constructor that takes any Collection of T, shuffles it with the given EnhancedRandom (such as anAceRandom,WhiskerRandomor, if you need compatibility with SquidLib 3.0.0, a SilkRNG from squidold), and can then iterate infinitely through mostly-random shuffles of the given collection. These shuffles are spaced so that a single element should always have at least one "gap" element between one appearance and the next. It helps to keep the appearance of a gap if every item in items is unique, but that is not necessary and does not affect how this works. The random parameter will be copied ifshareRNGis false, otherwise the reference will be shared (which could make the results of this GapShuffler depend on outside code, though it will always maintain a gap between identical elements if the elements are unique).- Parameters:
items- an array of T that will not be modifiedrandom- an EnhancedRandom, such as a WhiskerRandom; will be copied and not used directlyshareRNG- if false,randomwill be copied and no reference will be kept; if true,randomwill be shared with the outside code
-
-
Method Details
-
next
-
hasNext
-
remove
public void remove()Not supported.- Specified by:
removein interfaceIterator<T>- Throws:
UnsupportedOperationException- always throws this exception
-
iterator
Returns an infinite iterator over elements of typeT; the returned iterator is this object. You should be prepared to break out of any loops that use this once you've gotten enough elements! The remove() method is not supported by this iterator and hasNext() will always return true. -
getRNG
public com.github.tommyettinger.random.EnhancedRandom getRNG() -
setRNG
public void setRNG(com.github.tommyettinger.random.EnhancedRandom random) Sets the EnhancedRandom this uses to shuffle the order of elements, always copying the given EnhancedRandom before using it. Always reshuffles the order, which may eliminate a gap that should have been present, so treat the sequence before and after like separate GapShuffler objects.- Parameters:
random- an EnhancedRandom, such as a WhiskerRandom; always copied
-
setRNG
public void setRNG(com.github.tommyettinger.random.EnhancedRandom random, boolean shareRNG) Sets the EnhancedRandom this uses to shuffle the order of elements, optionally sharing a reference between outside code and the internal EnhancedRandom (whenshareRNGis true). Always reshuffles the order, which may eliminate a gap that should have been present, so treat the sequence before and after like separate GapShuffler objects.- Parameters:
random- an EnhancedRandom, such as a WhiskerRandom; optionally copiedshareRNG- if false,randomwill be copied and no reference will be kept; if true,randomwill be shared with the outside code
-
fillInto
The internal items used here are protected, but you can still use this method to put a shallow copy of them into some other Collection. If the typeTis mutable, changes to individual items will be reflected in this GapShuffler, so be careful in that case (if T is immutable, like if it is String, then there's nothing to need to be careful about). This copies each item in the GapShuffler's sequence once, in no particular order, but it may give a prediction of what items this will return in the future (or has already returned).- Parameters:
coll- a Collection that will have each of the possible items this can produce added into it, in no particular order
-
getIndex
public int getIndex()Allows getting (but not setting) the internal index this uses as it iterates through shuffled versions of the same collection indefinitely. This is mostly meant for serialization code to be able to replicate the state of this GapShuffler. This is mostly useful withfillInto(Collection)and some kind of insertion-ordered Collection (likeObjectList, which lets the index match an item in that Collection.- Returns:
- an index used internally to determine how far through a particular shuffle the iteration has gotten
-
toString
-
equals
-
getSerializersNeeded
Description copied from interface:ISerializersNeededGets a List of Class instances that must each be registered with a serialization library before this object can be successfully serialized or deserialized. This isGwtIncompatible; none of the serialization libraries this is meant for have any support for GWT.- Specified by:
getSerializersNeededin interfaceISerializersNeeded- Returns:
- a List of Class instances that must each be registered with a serialization library
-