Package com.github.tommyettinger.ds
Interface Arrangeable
- All Known Subinterfaces:
Arrangeable.ArrangeableList<T>,Ordered<T>,Ordered.OfBoolean,Ordered.OfByte,Ordered.OfChar,Ordered.OfDouble,Ordered.OfFloat,Ordered.OfInt,Ordered.OfLong,Ordered.OfShort
- All Known Implementing Classes:
BooleanBag,BooleanDeque,BooleanList,ByteBag,ByteDeque,ByteList,CaseInsensitiveOrderedMap,CaseInsensitiveOrderedSet,CharBag,CharDeque,CharList,DoubleBag,DoubleDeque,DoubleList,EnumFloatOrderedMap,EnumIntOrderedMap,EnumLongOrderedMap,EnumOrderedMap,EnumOrderedSet,FilteredIterableOrderedMap,FilteredIterableOrderedSet,FilteredStringOrderedMap,FilteredStringOrderedSet,FloatBag,FloatDeque,FloatList,HolderOrderedSet,IdentityObjectOrderedMap,IdentityOrderedSet,IntBag,IntDeque,IntFloatOrderedMap,IntIntOrderedMap,IntList,IntLongOrderedMap,IntObjectOrderedMap,IntOrderedSet,LongBag,LongDeque,LongFloatOrderedMap,LongIntOrderedMap,LongList,LongLongOrderedMap,LongObjectOrderedMap,LongOrderedSet,NumberedSet,NumberedSet.InternalMap,ObjectBag,ObjectDeque,ObjectFloatOrderedMap,ObjectIntOrderedMap,ObjectList,ObjectLongOrderedMap,ObjectObjectOrderedMap,ObjectOrderedSet,ShortBag,ShortDeque,ShortList
public interface Arrangeable
Indicates that a type can have its contents change position, without specifying the type of contents.
This can be used for primitive-backed collections as well as generic ones.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceAn empty interface that merges Arrangeable and java.util.List APIs. -
Method Summary
Modifier and TypeMethodDescriptiondefault voidrearrange(long seed) Rearranges this Arrangeable using the given seed to shuffle it.voidreverse()Reverses this Arrangeable in-place.default voidshuffle()Pseudo-randomly shuffles the order of this Arrangeable in-place, usingArrayTools.RANDOMas the unseeded random number generator.default voidPseudo-randomly shuffles the order of this Arrangeable in-place.intsize()Returns the number of elements in this Arrangeable.voidswap(int first, int second) Switches the ordering of positionsfirstandsecond, without changing any items beyond that.
-
Method Details
-
swap
void swap(int first, int second) Switches the ordering of positionsfirstandsecond, without changing any items beyond that. -
shuffle
default void shuffle()Pseudo-randomly shuffles the order of this Arrangeable in-place, usingArrayTools.RANDOMas the unseeded random number generator. -
shuffle
Pseudo-randomly shuffles the order of this Arrangeable in-place.- Parameters:
random- anyRandom, such asAlternateRandomor one from juniper
-
reverse
void reverse()Reverses this Arrangeable in-place. -
size
int size()Returns the number of elements in this Arrangeable. Often this is shared withCollection.size(), but isn't always.- Returns:
- the number of elements in this Arrangeable
-
rearrange
default void rearrange(long seed) Rearranges this Arrangeable using the given seed to shuffle it. The rearrangement is done in-place. You can reuse the seed on different calls to rearrange with Arrangeables of the same size, which will perform the same reordering steps on each Arrangeable (so if the first item in one Arrangeable became the fifth item after a call to rearrange(), reusing the seed in another same-size Arrangeable would also move its first item to become its fifth item).
If you don't need to reuse a seed, you should considershuffle(Random), which can be faster than this with a good enough Random subclass (likely not withRandom).
This usesHasher.randomize2Bounded(long, int)to randomize the swap positions. If you want to use a stronger randomization method, such asHasher.randomize3Bounded(long, int), use a faster one, such asHasher.randomize1Bounded(long, int), or just to inline this method manually, the code used here fits in one line:
for (int i = size() - 1; i > 0; i--) swap(i, Hasher.randomize2Bounded(++seed, i + 1));- Parameters:
seed- a (typically random) long seed to determine the shuffled order
-