Fixing Debug Branch by bringing it up to date!
This commit is contained in:
parent
6af0656216
commit
867b3b4d9a
10
.gitignore
vendored
10
.gitignore
vendored
@ -20,15 +20,7 @@ gradle-app.setting
|
|||||||
/bin/
|
/bin/
|
||||||
/storage/
|
/storage/
|
||||||
#Generated Code
|
#Generated Code
|
||||||
/src/main/java/speiger/src/collections/booleans/*
|
|
||||||
/src/main/java/speiger/src/collections/bytes/*
|
|
||||||
/src/main/java/speiger/src/collections/shorts/*
|
|
||||||
/src/main/java/speiger/src/collections/chars/*
|
|
||||||
/src/main/java/speiger/src/collections/ints/*
|
|
||||||
/src/main/java/speiger/src/collections/longs/*
|
|
||||||
/src/main/java/speiger/src/collections/floats/*
|
|
||||||
/src/main/java/speiger/src/collections/doubles/*
|
|
||||||
/src/main/java/speiger/src/collections/objects/*
|
|
||||||
|
|
||||||
#Generated Tests
|
#Generated Tests
|
||||||
/src/test/java/speiger/src/testers/booleans/*
|
/src/test/java/speiger/src/testers/booleans/*
|
||||||
|
|||||||
@ -0,0 +1,252 @@
|
|||||||
|
package speiger.src.collections.booleans.collections;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.AbstractCollection;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanConsumer;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanIterators;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanArrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract Type Specific Collection that reduces boxing/unboxing
|
||||||
|
*/
|
||||||
|
public abstract class AbstractBooleanCollection extends AbstractCollection<Boolean> implements BooleanCollection
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public abstract BooleanIterator iterator();
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean add(Boolean e) { return BooleanCollection.super.add(e); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(BooleanCollection c) {
|
||||||
|
boolean modified = false;
|
||||||
|
for(BooleanIterator iter = c.iterator();iter.hasNext();modified |= add(iter.nextBoolean()));
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanCollection copy() { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean contains(Object e) { return BooleanCollection.super.contains(e); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation of contains. This implementation iterates over the elements and returns true if the value match.
|
||||||
|
* @param e the element that should be searched for.
|
||||||
|
* @return true if the value was found.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean contains(boolean e) {
|
||||||
|
for(BooleanIterator iter = iterator();iter.hasNext();) { if(iter.nextBoolean() == e) return true; }
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean addAll(Collection<? extends Boolean> c)
|
||||||
|
{
|
||||||
|
return c instanceof BooleanCollection ? addAll((BooleanCollection)c) : super.addAll(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation of containsAll. This implementation iterates over all elements and checks all elements are present in the other collection.
|
||||||
|
* @param c the collection that should be checked if it contains all elements.
|
||||||
|
* @return true if all elements were found in the collection
|
||||||
|
* @throws java.lang.NullPointerException if the collection is null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean containsAll(BooleanCollection c) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
if(c.isEmpty()) return true;
|
||||||
|
for(BooleanIterator iter = c.iterator();iter.hasNext();)
|
||||||
|
if(!contains(iter.nextBoolean()))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean containsAll(Collection<?> c) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
return c instanceof BooleanCollection ? containsAll((BooleanCollection)c) : super.containsAll(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This implementation iterates over the elements of the collection and checks if they are stored in this collection
|
||||||
|
* @param c the elements that should be checked for
|
||||||
|
* @return true if any element is in this collection
|
||||||
|
* @throws java.lang.NullPointerException if the collection is null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean containsAny(Collection<?> c) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
if(c.isEmpty()) return false;
|
||||||
|
for(Object e : c)
|
||||||
|
if(contains(e))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This implementation iterates over the elements of the collection and checks if they are stored in this collection.
|
||||||
|
* @param c the elements that should be checked for
|
||||||
|
* @return true if any element is in this collection
|
||||||
|
* @throws java.lang.NullPointerException if the collection is null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean containsAny(BooleanCollection c) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
if(c.isEmpty()) return false;
|
||||||
|
for(BooleanIterator iter = c.iterator();iter.hasNext();)
|
||||||
|
if(contains(iter.nextBoolean()))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean remove(Object e) { return BooleanCollection.super.remove(e); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation of remove. This implementation iterates over the elements until it finds the element that is searched for or it runs out of elements.
|
||||||
|
* It stops after finding the first element
|
||||||
|
* @param e the element that is searched for
|
||||||
|
* @return true if the element was found and removed.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean remBoolean(boolean e) {
|
||||||
|
for(BooleanIterator iter = iterator();iter.hasNext();) {
|
||||||
|
if(iter.nextBoolean() == e) {
|
||||||
|
iter.remove();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation of removeAll. This Implementation iterates over all elements and removes them as they were found in the other collection.
|
||||||
|
* @param c the elements that should be deleted
|
||||||
|
* @return true if the collection was modified.
|
||||||
|
* @throws java.lang.NullPointerException if the collection is null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean removeAll(BooleanCollection c) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
if(c.isEmpty()) return false;
|
||||||
|
boolean modified = false;
|
||||||
|
for(BooleanIterator iter = iterator();iter.hasNext();) {
|
||||||
|
if(c.contains(iter.nextBoolean())) {
|
||||||
|
iter.remove();
|
||||||
|
modified = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeAll(BooleanCollection c, BooleanConsumer r) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
if(c.isEmpty()) return false;
|
||||||
|
Objects.requireNonNull(r);
|
||||||
|
boolean modified = false;
|
||||||
|
for(BooleanIterator iter = iterator();iter.hasNext();) {
|
||||||
|
boolean e = iter.nextBoolean();
|
||||||
|
if(c.contains(e)) {
|
||||||
|
r.accept(e);
|
||||||
|
iter.remove();
|
||||||
|
modified = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation of retainAll. This Implementation iterates over all elements and removes them as they were not found in the other collection.
|
||||||
|
* @param c the elements that should be kept
|
||||||
|
* @return true if the collection was modified.
|
||||||
|
* @throws java.lang.NullPointerException if the collection is null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean retainAll(BooleanCollection c) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
if(c.isEmpty()) {
|
||||||
|
boolean modified = !isEmpty();
|
||||||
|
clear();
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
boolean modified = false;
|
||||||
|
for(BooleanIterator iter = iterator();iter.hasNext();) {
|
||||||
|
if(!c.contains(iter.nextBoolean())) {
|
||||||
|
iter.remove();
|
||||||
|
modified = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean retainAll(BooleanCollection c, BooleanConsumer r) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
Objects.requireNonNull(r);
|
||||||
|
if(c.isEmpty()) {
|
||||||
|
boolean modified = !isEmpty();
|
||||||
|
forEach(r);
|
||||||
|
clear();
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
boolean modified = false;
|
||||||
|
for(BooleanIterator iter = iterator();iter.hasNext();) {
|
||||||
|
boolean e = iter.nextBoolean();
|
||||||
|
if(!c.contains(e)) {
|
||||||
|
r.accept(e);
|
||||||
|
iter.remove();
|
||||||
|
modified = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation of toArray that links to {@link #toBooleanArray(boolean[])} with a newly created array.
|
||||||
|
* @return an array containing all of the elements in this collection
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean[] toBooleanArray() {
|
||||||
|
if(isEmpty()) return BooleanArrays.EMPTY_ARRAY;
|
||||||
|
return toBooleanArray(new boolean[size()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation of toArray. This implementation iterates over all elements and unwraps them into primitive type.
|
||||||
|
* @param a array that the elements should be injected to. If null or to small a new array with the right size is created
|
||||||
|
* @return an array containing all of the elements in this collection
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean[] toBooleanArray(boolean[] a) {
|
||||||
|
if(a == null || a.length < size()) a = new boolean[size()];
|
||||||
|
BooleanIterators.unwrap(a, iterator());
|
||||||
|
if (a.length > size()) a[size()] = false;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,52 @@
|
|||||||
|
package speiger.src.collections.booleans.collections;
|
||||||
|
|
||||||
|
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
|
||||||
|
/**
|
||||||
|
* A Type-Specific {@link ObjectBidirectionalIterator} to reduce (un)boxing
|
||||||
|
*/
|
||||||
|
public interface BooleanBidirectionalIterator extends BooleanIterator, ObjectBidirectionalIterator<Boolean>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns true if the Iterator has a Previous element
|
||||||
|
* @return true if the Iterator has a Previous element
|
||||||
|
*/
|
||||||
|
public boolean hasPrevious();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Previous element of the iterator.
|
||||||
|
* @return the Previous element of the iterator.
|
||||||
|
* @throws java.util.NoSuchElementException if the iteration has no more elements
|
||||||
|
*/
|
||||||
|
public boolean previousBoolean();
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default Boolean previous() {
|
||||||
|
return Boolean.valueOf(previousBoolean());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
default int skip(int amount)
|
||||||
|
{
|
||||||
|
return BooleanIterator.super.skip(amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverses the Given amount of elements if possible. A Optimization function to reverse elements faster if the implementation allows it.
|
||||||
|
* @param amount the amount of elements that should be reversed
|
||||||
|
* @return the amount of elements that were reversed
|
||||||
|
*/
|
||||||
|
public default int back(int amount) {
|
||||||
|
if(amount < 0) throw new IllegalStateException("Can't go forward");
|
||||||
|
int i = 0;
|
||||||
|
for(;i<amount && hasPrevious();previousBoolean(),i++);
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,231 @@
|
|||||||
|
package speiger.src.collections.booleans.collections;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanConsumer;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanSplititerators;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanCollections;
|
||||||
|
import speiger.src.collections.utils.ISizeProvider;
|
||||||
|
import speiger.src.collections.utils.SanityChecks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific {@link Collection} that reduces (un)boxing
|
||||||
|
*/
|
||||||
|
public interface BooleanCollection extends Collection<Boolean>, BooleanIterable, ISizeProvider
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type-Specific add function to reduce (un)boxing
|
||||||
|
* @param o the element that should be added
|
||||||
|
* @return true if the element was added to the collection
|
||||||
|
*/
|
||||||
|
public boolean add(boolean o);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific addAll function to reduce (un)boxing
|
||||||
|
* @param c the collection of elements that should be added
|
||||||
|
* @return true if elements were added into the collection
|
||||||
|
*/
|
||||||
|
public boolean addAll(BooleanCollection c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific Array based addAll method to reduce the amount of Wrapping
|
||||||
|
* @param e the elements that should be added
|
||||||
|
* @return if the collection was modified
|
||||||
|
*/
|
||||||
|
public default boolean addAll(boolean... e) { return addAll(e, 0, e.length); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific Array based addAll method to reduce the amount of Wrapping
|
||||||
|
* @param e the elements that should be added
|
||||||
|
* @param length how many elements of the array should be added
|
||||||
|
* @return if the collection was modified
|
||||||
|
*/
|
||||||
|
public default boolean addAll(boolean[] e, int length) { return addAll(e, 0, length); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific Array based addAll method to reduce the amount of Wrapping
|
||||||
|
* @param e the elements that should be added
|
||||||
|
* @param offset where to start within the array
|
||||||
|
* @param length how many elements of the array should be added
|
||||||
|
* @return if the collection was modified
|
||||||
|
*/
|
||||||
|
public default boolean addAll(boolean[] e, int offset, int length) {
|
||||||
|
if(length <= 0) return false;
|
||||||
|
SanityChecks.checkArrayCapacity(e.length, offset, length);
|
||||||
|
boolean added = false;
|
||||||
|
for(int i = 0;i<length;i++) {
|
||||||
|
if(add(e[offset+i])) added = true;
|
||||||
|
}
|
||||||
|
return added;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific contains function to reduce (un)boxing
|
||||||
|
* @param o the element that is checked for
|
||||||
|
* @return true if the element is found in the collection
|
||||||
|
*/
|
||||||
|
public boolean contains(boolean o);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific containsAll function to reduce (un)boxing
|
||||||
|
* @param c the collection of elements that should be tested for
|
||||||
|
* @return true if all the element is found in the collection
|
||||||
|
*/
|
||||||
|
public boolean containsAll(BooleanCollection c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific containsAny function to reduce (un)boxing
|
||||||
|
* @param c the collection of elements that should be tested for
|
||||||
|
* @return true if any element was found
|
||||||
|
*/
|
||||||
|
public boolean containsAny(BooleanCollection c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if any element of the Collection is found in the provided collection.
|
||||||
|
* A Small Optimization function to find out of any element is present when comparing collections and not all of them.
|
||||||
|
* @param c the collection of elements that should be tested for
|
||||||
|
* @return true if any element was found.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public boolean containsAny(Collection<?> c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific remove function that reduces (un)boxing.
|
||||||
|
* @param o the element that should be removed
|
||||||
|
* @return true if the element was removed
|
||||||
|
* @see Collection#remove(Object)
|
||||||
|
*/
|
||||||
|
public boolean remBoolean(boolean o);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific removeAll function that reduces (un)boxing.
|
||||||
|
* @param c the collection of elements that should be removed
|
||||||
|
* @return true if any element was removed
|
||||||
|
* @see Collection#removeAll(Collection)
|
||||||
|
*/
|
||||||
|
public boolean removeAll(BooleanCollection c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific removeAll function that reduces (un)boxing.
|
||||||
|
* It also notifies the remover of which exact element is going to be removed.
|
||||||
|
* @param c the collection of elements that should be removed
|
||||||
|
* @param r elements that got removed
|
||||||
|
* @return true if any element was removed
|
||||||
|
* @see Collection#removeAll(Collection)
|
||||||
|
*/
|
||||||
|
public boolean removeAll(BooleanCollection c, BooleanConsumer r);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific retainAll function that reduces (un)boxing.
|
||||||
|
* @param c the collection of elements that should be kept
|
||||||
|
* @return true if any element was removed
|
||||||
|
* @see Collection#retainAll(Collection)
|
||||||
|
*/
|
||||||
|
public boolean retainAll(BooleanCollection c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific retainAll function that reduces (un)boxing.
|
||||||
|
* It also notifies the remover of which exact element is going to be removed.
|
||||||
|
* @param c the collection of elements that should be kept
|
||||||
|
* @param r elements that got removed
|
||||||
|
* @return true if any element was removed
|
||||||
|
* @see Collection#retainAll(Collection)
|
||||||
|
*/
|
||||||
|
public boolean retainAll(BooleanCollection c, BooleanConsumer r);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to collect all elements
|
||||||
|
* @param collection that the elements should be inserted to
|
||||||
|
* @param <E> the collection type
|
||||||
|
* @return the input with the desired elements
|
||||||
|
*/
|
||||||
|
default <E extends BooleanCollection> E pour(E collection) {
|
||||||
|
collection.addAll(this);
|
||||||
|
return collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Function that does a shallow clone of the Collection itself.
|
||||||
|
* This function is more optimized then a copy constructor since the Collection does not have to be unsorted/resorted.
|
||||||
|
* It can be compared to Cloneable but with less exception risk
|
||||||
|
* @return a Shallow Copy of the collection
|
||||||
|
* @note Wrappers and view collections will not support this feature
|
||||||
|
*/
|
||||||
|
public BooleanCollection copy();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific toArray function that delegates to {@link #toBooleanArray(boolean[])} with a newly created array.
|
||||||
|
* @return an array containing all of the elements in this collection
|
||||||
|
* @see Collection#toArray()
|
||||||
|
*/
|
||||||
|
public boolean[] toBooleanArray();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific toArray function that reduces (un)boxing.
|
||||||
|
* @param a array that the elements should be injected to. If null or to small a new array with the right size is created
|
||||||
|
* @return an array containing all of the elements in this collection
|
||||||
|
* @see Collection#toArray(Object[])
|
||||||
|
*/
|
||||||
|
public boolean[] toBooleanArray(boolean[] a);
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default boolean add(Boolean o) { return add(o.booleanValue()); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default boolean contains(Object o) { return o != null && contains(((Boolean)o).booleanValue()); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default boolean remove(Object o) { return o != null && remBoolean(((Boolean)o).booleanValue()); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a Type-Specific Iterator to reduce (un)boxing
|
||||||
|
* @return a iterator of the collection
|
||||||
|
* @see Collection#iterator()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Wrapped Collection that is Synchronized
|
||||||
|
* @return a new Collection that is synchronized
|
||||||
|
* @see BooleanCollections#synchronize
|
||||||
|
*/
|
||||||
|
public default BooleanCollection synchronize() { return BooleanCollections.synchronize(this); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Wrapped Collection that is Synchronized
|
||||||
|
* @param mutex is the controller of the synchronization block
|
||||||
|
* @return a new Collection Wrapper that is synchronized
|
||||||
|
* @see BooleanCollections#synchronize
|
||||||
|
*/
|
||||||
|
public default BooleanCollection synchronize(Object mutex) { return BooleanCollections.synchronize(this, mutex); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Wrapped Collection that is unmodifiable
|
||||||
|
* @return a new Collection Wrapper that is unmodifiable
|
||||||
|
* @see BooleanCollections#unmodifiable
|
||||||
|
*/
|
||||||
|
public default BooleanCollection unmodifiable() { return BooleanCollections.unmodifiable(this); }
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Type Splititerator to reduce boxing/unboxing
|
||||||
|
* @return type specific splititerator
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
default BooleanSplititerator spliterator() { return BooleanSplititerators.createSplititerator(this, 0); }
|
||||||
|
}
|
||||||
@ -0,0 +1,333 @@
|
|||||||
|
package speiger.src.collections.booleans.collections;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanConsumer;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanComparator;
|
||||||
|
import speiger.src.collections.objects.collections.ObjectIterable;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanFunction;
|
||||||
|
import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer;
|
||||||
|
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanPredicate;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanBooleanUnaryOperator;
|
||||||
|
import speiger.src.collections.booleans.lists.BooleanList;
|
||||||
|
import speiger.src.collections.booleans.lists.BooleanArrayList;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanArrays;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanAsyncBuilder;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanSplititerators;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanIterables;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanIterators;
|
||||||
|
import speiger.src.collections.utils.ISizeProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific {@link Iterable} that reduces (un)boxing
|
||||||
|
*/
|
||||||
|
public interface BooleanIterable extends Iterable<Boolean>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns an iterator over elements of type {@code T}.
|
||||||
|
*
|
||||||
|
* @return an Iterator.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
BooleanIterator iterator();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific foreach function that reduces (un)boxing
|
||||||
|
*
|
||||||
|
* @implSpec
|
||||||
|
* <p>The default implementation behaves as if:
|
||||||
|
* <pre>{@code
|
||||||
|
* iterator().forEachRemaining(action);
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param action The action to be performed for each element
|
||||||
|
* @throws NullPointerException if the specified action is null
|
||||||
|
* @see Iterable#forEach(Consumer)
|
||||||
|
*/
|
||||||
|
default void forEach(BooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
iterator().forEachRemaining(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
@Override
|
||||||
|
default void forEach(Consumer<? super Boolean> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
iterator().forEachRemaining(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Indexed forEach implementation that allows you to keep track of how many elements were already iterated over.
|
||||||
|
* @param action The action to be performed for each element
|
||||||
|
* @throws java.lang.NullPointerException if the specified action is null
|
||||||
|
*/
|
||||||
|
public default void forEachIndexed(IntBooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
int index = 0;
|
||||||
|
for(BooleanIterator iter = iterator();iter.hasNext();action.accept(index++, iter.nextBoolean()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to reduce Lambda usage and allow for more method references, since these are faster/cleaner.
|
||||||
|
* @param input the object that should be included
|
||||||
|
* @param action The action to be performed for each element
|
||||||
|
* @param <E> the generic type of the Object
|
||||||
|
* @throws java.lang.NullPointerException if the specified action is null
|
||||||
|
*/
|
||||||
|
default <E> void forEach(E input, ObjectBooleanConsumer<E> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
iterator().forEachRemaining(input, action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Type Splititerator to reduce boxing/unboxing
|
||||||
|
* @return type specific splititerator
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
default BooleanSplititerator spliterator() { return BooleanSplititerators.createUnknownSplititerator(iterator(), 0); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Async Builder for moving work of the thread.
|
||||||
|
* It is not designed to split the work to multithreaded work, so using this keep it singlethreaded, but it allows to be moved to another thread.
|
||||||
|
* @see BooleanAsyncBuilder
|
||||||
|
* @return a AsyncBuilder
|
||||||
|
*/
|
||||||
|
default BooleanAsyncBuilder asAsync() {
|
||||||
|
return new BooleanAsyncBuilder(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to convert a Iterable to something else.
|
||||||
|
* @param mapper the mapping function
|
||||||
|
* @param <E> The return type.
|
||||||
|
* @return a new Iterable that returns the desired result
|
||||||
|
*/
|
||||||
|
default <E> ObjectIterable<E> map(BooleanFunction<E> mapper) {
|
||||||
|
return BooleanIterables.map(this, mapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to convert a Iterable to something else.
|
||||||
|
* @param mapper the flatMapping function
|
||||||
|
* @param <V> The return type supplier.
|
||||||
|
* @param <E> The return type.
|
||||||
|
* @return a new Iterable that returns the desired result
|
||||||
|
* @note does not support toBooleanArray optimizations.
|
||||||
|
*/
|
||||||
|
default <E, V extends Iterable<E>> ObjectIterable<E> flatMap(BooleanFunction<V> mapper) {
|
||||||
|
return BooleanIterables.flatMap(this, mapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to convert a Iterable to something else.
|
||||||
|
* @param mapper the flatMapping function
|
||||||
|
* @param <E> The return type.
|
||||||
|
* @return a new Iterable that returns the desired result
|
||||||
|
* @note does not support toBooleanArray optimizations.
|
||||||
|
*/
|
||||||
|
default <E> ObjectIterable<E> arrayflatMap(BooleanFunction<E[]> mapper) {
|
||||||
|
return BooleanIterables.arrayFlatMap(this, mapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to filter out unwanted elements
|
||||||
|
* @param filter the elements that should be kept.
|
||||||
|
* @return a Iterable that filtered out all unwanted elements
|
||||||
|
* @note does not support toBooleanArray optimizations.
|
||||||
|
*/
|
||||||
|
default BooleanIterable filter(BooleanPredicate filter) {
|
||||||
|
return BooleanIterables.filter(this, filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to filter out duplicated elements
|
||||||
|
* @return a Iterable that filtered out all duplicated elements
|
||||||
|
* @note does not support toBooleanArray optimizations.
|
||||||
|
*/
|
||||||
|
default BooleanIterable distinct() {
|
||||||
|
return BooleanIterables.distinct(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to repeat elements a desired amount of times
|
||||||
|
* @param repeats how many times the elements should be repeated
|
||||||
|
* @return a Iterable that is repeating multiple times
|
||||||
|
*/
|
||||||
|
default BooleanIterable repeat(int repeats) {
|
||||||
|
return BooleanIterables.repeat(this, repeats);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to limit the amount of elements
|
||||||
|
* @param limit the amount of elements it should be limited to
|
||||||
|
* @return a Iterable that is limited in length
|
||||||
|
*/
|
||||||
|
default BooleanIterable limit(long limit) {
|
||||||
|
return BooleanIterables.limit(this, limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to sort the elements
|
||||||
|
* @param sorter that sorts the elements.
|
||||||
|
* @return a Iterable that is sorted
|
||||||
|
*/
|
||||||
|
default BooleanIterable sorted(BooleanComparator sorter) {
|
||||||
|
return BooleanIterables.sorted(this, sorter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to preview elements before they are iterated through
|
||||||
|
* @param action the action that should be applied
|
||||||
|
* @return a Peeked Iterable
|
||||||
|
*/
|
||||||
|
default BooleanIterable peek(BooleanConsumer action) {
|
||||||
|
return BooleanIterables.peek(this, action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to collect all elements
|
||||||
|
* @param collection that the elements should be inserted to
|
||||||
|
* @param <E> the collection type
|
||||||
|
* @return the input with the desired elements
|
||||||
|
*/
|
||||||
|
default <E extends BooleanCollection> E pour(E collection) {
|
||||||
|
BooleanIterators.pour(iterator(), collection);
|
||||||
|
return collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that reduces the usage of streams and allows to collect all elements as a ArrayList
|
||||||
|
* @return a new ArrayList of all elements
|
||||||
|
*/
|
||||||
|
default BooleanList pourAsList() {
|
||||||
|
return pour(new BooleanArrayList());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that reduces the usage of streams and allows to collect all elements as a Array
|
||||||
|
* @return a new Array of all elements
|
||||||
|
*/
|
||||||
|
default boolean[] toBooleanArray() {
|
||||||
|
ISizeProvider prov = ISizeProvider.of(this);
|
||||||
|
if(prov != null) {
|
||||||
|
int size = prov.size();
|
||||||
|
if(size >= 0) {
|
||||||
|
boolean[] array = new boolean[size];
|
||||||
|
BooleanIterators.unwrap(array, iterator());
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return BooleanArrays.pour(iterator());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to reduce stream usage that allows to filter for any matches.
|
||||||
|
* @param filter that should be applied
|
||||||
|
* @return true if any matches were found
|
||||||
|
*/
|
||||||
|
default boolean matchesAny(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(BooleanIterator iter = iterator();iter.hasNext();) {
|
||||||
|
if(filter.test(iter.nextBoolean())) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to reduce stream usage that allows to filter for no matches.
|
||||||
|
* @param filter that should be applied
|
||||||
|
* @return true if no matches were found
|
||||||
|
*/
|
||||||
|
default boolean matchesNone(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(BooleanIterator iter = iterator();iter.hasNext();) {
|
||||||
|
if(filter.test(iter.nextBoolean())) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to reduce stream usage that allows to filter for all matches.
|
||||||
|
* @param filter that should be applied
|
||||||
|
* @return true if all matches.
|
||||||
|
*/
|
||||||
|
default boolean matchesAll(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(BooleanIterator iter = iterator();iter.hasNext();) {
|
||||||
|
if(!filter.test(iter.nextBoolean())) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to reduce stream usage that allows to filter for the first match.
|
||||||
|
* @param filter that should be applied
|
||||||
|
* @return the found value or the null equivalent variant.
|
||||||
|
*/
|
||||||
|
default boolean findFirst(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(BooleanIterator iter = iterator();iter.hasNext();) {
|
||||||
|
boolean entry = iter.nextBoolean();
|
||||||
|
if(filter.test(entry)) return entry;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the
|
||||||
|
* elements of this Iterable
|
||||||
|
* @param operator the operation that should be applied
|
||||||
|
* @param identity the start value
|
||||||
|
* @return the reduction result, returns identity if nothing was found
|
||||||
|
*/
|
||||||
|
default boolean reduce(boolean identity, BooleanBooleanUnaryOperator operator) {
|
||||||
|
Objects.requireNonNull(operator);
|
||||||
|
boolean state = identity;
|
||||||
|
for(BooleanIterator iter = iterator();iter.hasNext();) {
|
||||||
|
state = operator.applyAsBoolean(state, iter.nextBoolean());
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the
|
||||||
|
* elements of this Iterable
|
||||||
|
* @param operator the operation that should be applied
|
||||||
|
* @return the reduction result, returns null value if nothing was found
|
||||||
|
*/
|
||||||
|
default boolean reduce(BooleanBooleanUnaryOperator operator) {
|
||||||
|
Objects.requireNonNull(operator);
|
||||||
|
boolean state = false;
|
||||||
|
boolean empty = true;
|
||||||
|
for(BooleanIterator iter = iterator();iter.hasNext();) {
|
||||||
|
if(empty) {
|
||||||
|
empty = false;
|
||||||
|
state = iter.nextBoolean();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
state = operator.applyAsBoolean(state, iter.nextBoolean());
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to reduce stream usage that allows to count the valid elements.
|
||||||
|
* @param filter that should be applied
|
||||||
|
* @return the amount of Valid Elements
|
||||||
|
*/
|
||||||
|
default int count(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
int result = 0;
|
||||||
|
for(BooleanIterator iter = iterator();iter.hasNext();) {
|
||||||
|
if(filter.test(iter.nextBoolean())) result++;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,88 @@
|
|||||||
|
package speiger.src.collections.booleans.collections;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanConsumer;
|
||||||
|
|
||||||
|
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific {@link Iterator} that reduces (un)boxing
|
||||||
|
*/
|
||||||
|
public interface BooleanIterator extends Iterator<Boolean>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns the next element in the iteration.
|
||||||
|
*
|
||||||
|
* @return the next element in the iteration
|
||||||
|
* @throws java.util.NoSuchElementException if the iteration has no more elements
|
||||||
|
* @see Iterator#next()
|
||||||
|
*/
|
||||||
|
public boolean nextBoolean();
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default Boolean next() { return Boolean.valueOf(nextBoolean()); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs the given action for each remaining element until all elements
|
||||||
|
* have been processed or the action throws an exception. Actions are
|
||||||
|
* performed in the order of iteration, if that order is specified.
|
||||||
|
* Exceptions thrown by the action are relayed to the caller.
|
||||||
|
*
|
||||||
|
* @implSpec
|
||||||
|
* <p>The default implementation behaves as if:
|
||||||
|
* <pre>{@code
|
||||||
|
* while (hasNext()) action.accept(nextBoolean());
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param action The action to be performed for each element
|
||||||
|
* @throws java.lang.NullPointerException if the specified action is null
|
||||||
|
* @see Iterator#forEachRemaining(Consumer)
|
||||||
|
*/
|
||||||
|
public default void forEachRemaining(BooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
while(hasNext()) { action.accept(nextBoolean()); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
@Override
|
||||||
|
default void forEachRemaining(Consumer<? super Boolean> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
forEachRemaining(action::accept);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to reduce Lambda usage and allow for more method references, since these are faster/cleaner.
|
||||||
|
* @param input the object that should be included
|
||||||
|
* @param action The action to be performed for each element
|
||||||
|
* @param <E> the generic type of the Object
|
||||||
|
* @throws java.lang.NullPointerException if the specified action is null
|
||||||
|
*/
|
||||||
|
default <E> void forEachRemaining(E input, ObjectBooleanConsumer<E> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
while(hasNext()) { action.accept(input, nextBoolean()); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Skips the Given amount of elements if possible. A Optimization function to skip elements faster if the implementation allows it.
|
||||||
|
* @param amount the amount of elements that should be skipped
|
||||||
|
* @return the amount of elements that were skipped
|
||||||
|
*/
|
||||||
|
default int skip(int amount) {
|
||||||
|
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
|
||||||
|
int i = 0;
|
||||||
|
for(;i<amount && hasNext();nextBoolean(), i++);
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
package speiger.src.collections.booleans.collections;
|
||||||
|
|
||||||
|
import java.util.Spliterator.OfPrimitive;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanConsumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Split-Iterator that reduces boxing/unboxing
|
||||||
|
* It fills the gaps of the java and uses this collection interfaces
|
||||||
|
*/
|
||||||
|
public interface BooleanSplititerator extends OfPrimitive<Boolean, BooleanConsumer, BooleanSplititerator>, BooleanIterator
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
default void forEachRemaining(BooleanConsumer action) { BooleanIterator.super.forEachRemaining(action); }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default void forEachRemaining(Consumer<? super Boolean> action) { BooleanIterator.super.forEachRemaining(action); }
|
||||||
|
}
|
||||||
@ -0,0 +1,85 @@
|
|||||||
|
package speiger.src.collections.booleans.collections;
|
||||||
|
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
|
import speiger.src.collections.utils.Stack;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific {@link Stack} that reduces (un)boxing
|
||||||
|
*/
|
||||||
|
public interface BooleanStack
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Inserts a given Object on top of the stack
|
||||||
|
* @param e the Object to insert
|
||||||
|
* @see Stack#push(Object)
|
||||||
|
*/
|
||||||
|
public void push(boolean e);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function that pushes the top element on top of the stack again.
|
||||||
|
* @throws NoSuchElementException if the stack is empty
|
||||||
|
*/
|
||||||
|
public default void pushTop() {
|
||||||
|
push(top());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the Object on top of the stack.
|
||||||
|
* @return the element that is on top of the stack
|
||||||
|
* @throws ArrayIndexOutOfBoundsException if the stack is empty
|
||||||
|
* @see Stack#pop()
|
||||||
|
*/
|
||||||
|
public boolean pop();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides the Object on top of the stack
|
||||||
|
* @return the element that is on top of the stack
|
||||||
|
* @throws ArrayIndexOutOfBoundsException if the stack is empty
|
||||||
|
* @see Stack#top()
|
||||||
|
*/
|
||||||
|
public default boolean top() {
|
||||||
|
return peek(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides the Selected Object from the stack.
|
||||||
|
* Top to bottom
|
||||||
|
* @param index of the element that should be provided
|
||||||
|
* @return the element that was requested
|
||||||
|
* @throws ArrayIndexOutOfBoundsException if the index is out of bounds
|
||||||
|
* @see Stack#peek(int)
|
||||||
|
*/
|
||||||
|
public boolean peek(int index);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears the stack
|
||||||
|
*/
|
||||||
|
public void clear();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides the amount of elements currently in the stack
|
||||||
|
* @return amount of elements in the list
|
||||||
|
*/
|
||||||
|
public int size();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return if the stack is empty
|
||||||
|
*/
|
||||||
|
public default boolean isEmpty() {
|
||||||
|
return size() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A method to drop the contents of the Stack without clearing the stack
|
||||||
|
* @return the contents of the stack into a seperate array.
|
||||||
|
*/
|
||||||
|
public default boolean[] toBooleanArray() { return toBooleanArray(new boolean[size()]); }
|
||||||
|
/**
|
||||||
|
* A method to drop the contents of the Stack without clearing the stack
|
||||||
|
* @param input where the elements should be inserted to. If it does not fit then it creates a new appropiatly created array
|
||||||
|
* @return the contents of the stack into a seperate array.
|
||||||
|
* @note if the Type is generic then a Object Array is created instead of a Type Array
|
||||||
|
*/
|
||||||
|
public boolean[] toBooleanArray(boolean[] input);
|
||||||
|
}
|
||||||
@ -0,0 +1,70 @@
|
|||||||
|
package speiger.src.collections.booleans.functions;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type-Specific Class for Comparator to reduce (un)boxing
|
||||||
|
*/
|
||||||
|
public interface BooleanComparator extends Comparator<Boolean>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Type-Specific compare function to reduce (un)boxing
|
||||||
|
* @param o1 the first object to be compared.
|
||||||
|
* @param o2 the second object to be compared.
|
||||||
|
* @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
|
||||||
|
* @see Comparator#compare(Object, Object)
|
||||||
|
*/
|
||||||
|
int compare(boolean o1, boolean o2);
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default int compare(Boolean o1, Boolean o2) {
|
||||||
|
return compare(o1.booleanValue(), o2.booleanValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Wrapper function to convert a Non-Type-Specific Comparator to a Type-Specific-Comparator
|
||||||
|
* @param c comparator to convert
|
||||||
|
* @return the wrapper of the comparator
|
||||||
|
* @throws NullPointerException if the comparator is null
|
||||||
|
*/
|
||||||
|
public static BooleanComparator of(Comparator<Boolean> c) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
return (K, V) -> c.compare(Boolean.valueOf(K), Boolean.valueOf(V));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public default BooleanComparator reversed() {
|
||||||
|
return new Reversed(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Reversed Comparator to reduce boxing/unboxing
|
||||||
|
*/
|
||||||
|
static class Reversed implements BooleanComparator
|
||||||
|
{
|
||||||
|
BooleanComparator original;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* default constructor
|
||||||
|
* @param original that is going to be reversed
|
||||||
|
*/
|
||||||
|
public Reversed(BooleanComparator original) {
|
||||||
|
this.original = original;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int compare(boolean o1, boolean o2) {
|
||||||
|
return original.compare(o2, o1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanComparator reversed() {
|
||||||
|
return original;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
package speiger.src.collections.booleans.functions;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
/**
|
||||||
|
* Type-Specific Consumer interface that reduces (un)boxing and allows to merge other consumer types into this interface
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface BooleanConsumer extends Consumer<Boolean>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Type-Specific function to reduce (un)boxing.
|
||||||
|
* Performs this operation on the given argument.
|
||||||
|
*
|
||||||
|
* @param t the input argument
|
||||||
|
*/
|
||||||
|
void accept(boolean t);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type Specific sequencing method to reduce boxing/unboxing.
|
||||||
|
* @param after a operation that should be performed afterwards
|
||||||
|
* @return a sequenced consumer that does 2 operations
|
||||||
|
* @throws NullPointerException if after is null
|
||||||
|
*/
|
||||||
|
public default BooleanConsumer andThen(BooleanConsumer after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return T -> {accept(T); after.accept(T);};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default void accept(Boolean t) { accept(t.booleanValue()); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default BooleanConsumer andThen(Consumer<? super Boolean> after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return T -> {accept(T); after.accept(Boolean.valueOf(T));};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
package speiger.src.collections.booleans.functions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type-Specific Supplier interface that reduces (un)boxing and allows to merge other consumer types into this interface
|
||||||
|
*/
|
||||||
|
public interface BooleanSupplier
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return the supplied value
|
||||||
|
*/
|
||||||
|
public boolean getAsBoolean();
|
||||||
|
}
|
||||||
@ -0,0 +1,87 @@
|
|||||||
|
package speiger.src.collections.booleans.functions;
|
||||||
|
|
||||||
|
import java.util.concurrent.RunnableFuture;
|
||||||
|
import java.util.concurrent.CancellationException;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* A Type Specific Task interface that allows you to keep track of the task that is currently running.<br>
|
||||||
|
* It extends Runnable future and supports said functions but also provides quality of life functions like:<br>
|
||||||
|
*
|
||||||
|
* - isSuccesfull: which allows to detect if the task was completed properly and not interrupted or crashed.
|
||||||
|
* - pause/resume: which allows to pause/resume the task at any moment, making it easier to create thread-safe actions.
|
||||||
|
*/
|
||||||
|
public interface BooleanTask extends RunnableFuture<Boolean> {
|
||||||
|
/**
|
||||||
|
* Helper function to detect if the task is currently paused.
|
||||||
|
* @return true if paused
|
||||||
|
*/
|
||||||
|
public boolean isPaused();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pauses the task, which lets the thread finish without completing the task.
|
||||||
|
* Tasks are written in the way where they can pause without any issues.
|
||||||
|
* This won't be instant, as this function is applied asynchronous and doesn't check if the thread paused.
|
||||||
|
* So make sure it had the time to pause.
|
||||||
|
*/
|
||||||
|
public void pause();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pauses the task, which lets the thread finish without completing the task.
|
||||||
|
* Tasks are written in the way where they can pause without any issues.
|
||||||
|
* This won't be instant, as this function is applied asynchronous.
|
||||||
|
* It will await the pausing of the task.
|
||||||
|
*/
|
||||||
|
public void awaitPausing();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Continues the task if it wasn't already completed.
|
||||||
|
* This is done by resubmitting the task to the executor provided.
|
||||||
|
*/
|
||||||
|
public void resume();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Quality of life function that allows to detect if no cancellation/exception was applied to this task and it completed on its own.
|
||||||
|
* @return true if it was properly completed
|
||||||
|
*/
|
||||||
|
public boolean isSuccessful();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific get method that allows to reduce (un)boxing of primtives.
|
||||||
|
*
|
||||||
|
* Waits if necessary for the computation to complete, and then
|
||||||
|
* retrieves its result.
|
||||||
|
*
|
||||||
|
* @return the computed result as primitive
|
||||||
|
* @throws CancellationException if the computation was cancelled
|
||||||
|
* @throws ExecutionException if the computation threw an exception
|
||||||
|
* @throws InterruptedException if the current thread was interrupted
|
||||||
|
* while waiting
|
||||||
|
*/
|
||||||
|
public boolean getBoolean() throws InterruptedException, ExecutionException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Waits if necessary for at most the given time for the computation
|
||||||
|
* to complete, and then retrieves its result, if available.
|
||||||
|
*
|
||||||
|
* @param timeout the maximum time to wait
|
||||||
|
* @param unit the time unit of the timeout argument
|
||||||
|
* @return the computed result as primitive
|
||||||
|
* @throws CancellationException if the computation was cancelled
|
||||||
|
* @throws ExecutionException if the computation threw an exception
|
||||||
|
* @throws InterruptedException if the current thread was interrupted while waiting
|
||||||
|
* @throws TimeoutException if the wait timed out
|
||||||
|
*/
|
||||||
|
public boolean getBoolean(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default Boolean get() throws InterruptedException, ExecutionException { return Boolean.valueOf(getBoolean()); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default Boolean get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { return Boolean.valueOf(getBoolean(timeout, unit)); }
|
||||||
|
}
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.consumer;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific BiConsumer class to reduce boxing/unboxing and that fills the gaps that java has.
|
||||||
|
*/
|
||||||
|
public interface BooleanBooleanConsumer extends BiConsumer<Boolean, Boolean>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specific operation method to reduce boxing/unboxing
|
||||||
|
* Performs this operation on the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first input argument
|
||||||
|
* @param v the second input argument
|
||||||
|
*/
|
||||||
|
void accept(boolean k, boolean v);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type Specific sequencing method to reduce boxing/unboxing.
|
||||||
|
* @param after a operation that should be performed afterwards
|
||||||
|
* @return a sequenced biconsumer that does 2 operations
|
||||||
|
* @throws NullPointerException if after is null
|
||||||
|
*/
|
||||||
|
public default BooleanBooleanConsumer andThen(BooleanBooleanConsumer after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(K, V);};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default void accept(Boolean k, Boolean v) { accept(k.booleanValue(), v.booleanValue()); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default BooleanBooleanConsumer andThen(BiConsumer<? super Boolean, ? super Boolean> after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(Boolean.valueOf(K), Boolean.valueOf(V));};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.consumer;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific BiConsumer class to reduce boxing/unboxing and that fills the gaps that java has.
|
||||||
|
*/
|
||||||
|
public interface BooleanByteConsumer extends BiConsumer<Boolean, Byte>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specific operation method to reduce boxing/unboxing
|
||||||
|
* Performs this operation on the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first input argument
|
||||||
|
* @param v the second input argument
|
||||||
|
*/
|
||||||
|
void accept(boolean k, byte v);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type Specific sequencing method to reduce boxing/unboxing.
|
||||||
|
* @param after a operation that should be performed afterwards
|
||||||
|
* @return a sequenced biconsumer that does 2 operations
|
||||||
|
* @throws NullPointerException if after is null
|
||||||
|
*/
|
||||||
|
public default BooleanByteConsumer andThen(BooleanByteConsumer after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(K, V);};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default void accept(Boolean k, Byte v) { accept(k.booleanValue(), v.byteValue()); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default BooleanByteConsumer andThen(BiConsumer<? super Boolean, ? super Byte> after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(Boolean.valueOf(K), Byte.valueOf(V));};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.consumer;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific BiConsumer class to reduce boxing/unboxing and that fills the gaps that java has.
|
||||||
|
*/
|
||||||
|
public interface BooleanCharConsumer extends BiConsumer<Boolean, Character>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specific operation method to reduce boxing/unboxing
|
||||||
|
* Performs this operation on the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first input argument
|
||||||
|
* @param v the second input argument
|
||||||
|
*/
|
||||||
|
void accept(boolean k, char v);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type Specific sequencing method to reduce boxing/unboxing.
|
||||||
|
* @param after a operation that should be performed afterwards
|
||||||
|
* @return a sequenced biconsumer that does 2 operations
|
||||||
|
* @throws NullPointerException if after is null
|
||||||
|
*/
|
||||||
|
public default BooleanCharConsumer andThen(BooleanCharConsumer after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(K, V);};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default void accept(Boolean k, Character v) { accept(k.booleanValue(), v.charValue()); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default BooleanCharConsumer andThen(BiConsumer<? super Boolean, ? super Character> after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(Boolean.valueOf(K), Character.valueOf(V));};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.consumer;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific BiConsumer class to reduce boxing/unboxing and that fills the gaps that java has.
|
||||||
|
*/
|
||||||
|
public interface BooleanDoubleConsumer extends BiConsumer<Boolean, Double>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specific operation method to reduce boxing/unboxing
|
||||||
|
* Performs this operation on the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first input argument
|
||||||
|
* @param v the second input argument
|
||||||
|
*/
|
||||||
|
void accept(boolean k, double v);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type Specific sequencing method to reduce boxing/unboxing.
|
||||||
|
* @param after a operation that should be performed afterwards
|
||||||
|
* @return a sequenced biconsumer that does 2 operations
|
||||||
|
* @throws NullPointerException if after is null
|
||||||
|
*/
|
||||||
|
public default BooleanDoubleConsumer andThen(BooleanDoubleConsumer after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(K, V);};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default void accept(Boolean k, Double v) { accept(k.booleanValue(), v.doubleValue()); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default BooleanDoubleConsumer andThen(BiConsumer<? super Boolean, ? super Double> after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(Boolean.valueOf(K), Double.valueOf(V));};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.consumer;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific BiConsumer class to reduce boxing/unboxing and that fills the gaps that java has.
|
||||||
|
*/
|
||||||
|
public interface BooleanFloatConsumer extends BiConsumer<Boolean, Float>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specific operation method to reduce boxing/unboxing
|
||||||
|
* Performs this operation on the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first input argument
|
||||||
|
* @param v the second input argument
|
||||||
|
*/
|
||||||
|
void accept(boolean k, float v);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type Specific sequencing method to reduce boxing/unboxing.
|
||||||
|
* @param after a operation that should be performed afterwards
|
||||||
|
* @return a sequenced biconsumer that does 2 operations
|
||||||
|
* @throws NullPointerException if after is null
|
||||||
|
*/
|
||||||
|
public default BooleanFloatConsumer andThen(BooleanFloatConsumer after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(K, V);};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default void accept(Boolean k, Float v) { accept(k.booleanValue(), v.floatValue()); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default BooleanFloatConsumer andThen(BiConsumer<? super Boolean, ? super Float> after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(Boolean.valueOf(K), Float.valueOf(V));};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.consumer;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific BiConsumer class to reduce boxing/unboxing and that fills the gaps that java has.
|
||||||
|
*/
|
||||||
|
public interface BooleanIntConsumer extends BiConsumer<Boolean, Integer>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specific operation method to reduce boxing/unboxing
|
||||||
|
* Performs this operation on the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first input argument
|
||||||
|
* @param v the second input argument
|
||||||
|
*/
|
||||||
|
void accept(boolean k, int v);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type Specific sequencing method to reduce boxing/unboxing.
|
||||||
|
* @param after a operation that should be performed afterwards
|
||||||
|
* @return a sequenced biconsumer that does 2 operations
|
||||||
|
* @throws NullPointerException if after is null
|
||||||
|
*/
|
||||||
|
public default BooleanIntConsumer andThen(BooleanIntConsumer after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(K, V);};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default void accept(Boolean k, Integer v) { accept(k.booleanValue(), v.intValue()); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default BooleanIntConsumer andThen(BiConsumer<? super Boolean, ? super Integer> after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(Boolean.valueOf(K), Integer.valueOf(V));};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.consumer;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific BiConsumer class to reduce boxing/unboxing and that fills the gaps that java has.
|
||||||
|
*/
|
||||||
|
public interface BooleanLongConsumer extends BiConsumer<Boolean, Long>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specific operation method to reduce boxing/unboxing
|
||||||
|
* Performs this operation on the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first input argument
|
||||||
|
* @param v the second input argument
|
||||||
|
*/
|
||||||
|
void accept(boolean k, long v);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type Specific sequencing method to reduce boxing/unboxing.
|
||||||
|
* @param after a operation that should be performed afterwards
|
||||||
|
* @return a sequenced biconsumer that does 2 operations
|
||||||
|
* @throws NullPointerException if after is null
|
||||||
|
*/
|
||||||
|
public default BooleanLongConsumer andThen(BooleanLongConsumer after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(K, V);};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default void accept(Boolean k, Long v) { accept(k.booleanValue(), v.longValue()); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default BooleanLongConsumer andThen(BiConsumer<? super Boolean, ? super Long> after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(Boolean.valueOf(K), Long.valueOf(V));};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,50 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.consumer;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific BiConsumer class to reduce boxing/unboxing and that fills the gaps that java has.
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
*/
|
||||||
|
public interface BooleanObjectConsumer<V> extends BiConsumer<Boolean, V>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specific operation method to reduce boxing/unboxing
|
||||||
|
* Performs this operation on the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first input argument
|
||||||
|
* @param v the second input argument
|
||||||
|
*/
|
||||||
|
void accept(boolean k, V v);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type Specific sequencing method to reduce boxing/unboxing.
|
||||||
|
* @param after a operation that should be performed afterwards
|
||||||
|
* @return a sequenced biconsumer that does 2 operations
|
||||||
|
* @throws NullPointerException if after is null
|
||||||
|
*/
|
||||||
|
public default BooleanObjectConsumer<V> andThen(BooleanObjectConsumer<V> after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(K, V);};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default void accept(Boolean k, V v) { accept(k.booleanValue(), v); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default BooleanObjectConsumer<V> andThen(BiConsumer<? super Boolean, ? super V> after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(Boolean.valueOf(K), V);};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.consumer;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific BiConsumer class to reduce boxing/unboxing and that fills the gaps that java has.
|
||||||
|
*/
|
||||||
|
public interface BooleanShortConsumer extends BiConsumer<Boolean, Short>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specific operation method to reduce boxing/unboxing
|
||||||
|
* Performs this operation on the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first input argument
|
||||||
|
* @param v the second input argument
|
||||||
|
*/
|
||||||
|
void accept(boolean k, short v);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type Specific sequencing method to reduce boxing/unboxing.
|
||||||
|
* @param after a operation that should be performed afterwards
|
||||||
|
* @return a sequenced biconsumer that does 2 operations
|
||||||
|
* @throws NullPointerException if after is null
|
||||||
|
*/
|
||||||
|
public default BooleanShortConsumer andThen(BooleanShortConsumer after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(K, V);};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default void accept(Boolean k, Short v) { accept(k.booleanValue(), v.shortValue()); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default BooleanShortConsumer andThen(BiConsumer<? super Boolean, ? super Short> after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return (K, V) -> {accept(K, V); after.accept(Boolean.valueOf(K), Short.valueOf(V));};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Function interface that reduces boxing/unboxing and fills the gaps of interfaces that are missing.
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Boolean2BooleanFunction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Type Specific get function to reduce boxing/unboxing
|
||||||
|
* @param k the value that should be processed
|
||||||
|
* @return the result of the function
|
||||||
|
*/
|
||||||
|
public boolean test(boolean k);
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Function interface that reduces boxing/unboxing and fills the gaps of interfaces that are missing.
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Boolean2ByteFunction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Type Specific get function to reduce boxing/unboxing
|
||||||
|
* @param k the value that should be processed
|
||||||
|
* @return the result of the function
|
||||||
|
*/
|
||||||
|
public byte applyAsByte(boolean k);
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Function interface that reduces boxing/unboxing and fills the gaps of interfaces that are missing.
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Boolean2CharFunction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Type Specific get function to reduce boxing/unboxing
|
||||||
|
* @param k the value that should be processed
|
||||||
|
* @return the result of the function
|
||||||
|
*/
|
||||||
|
public char applyAsChar(boolean k);
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Function interface that reduces boxing/unboxing and fills the gaps of interfaces that are missing.
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Boolean2DoubleFunction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Type Specific get function to reduce boxing/unboxing
|
||||||
|
* @param k the value that should be processed
|
||||||
|
* @return the result of the function
|
||||||
|
*/
|
||||||
|
public double applyAsDouble(boolean k);
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Function interface that reduces boxing/unboxing and fills the gaps of interfaces that are missing.
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Boolean2FloatFunction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Type Specific get function to reduce boxing/unboxing
|
||||||
|
* @param k the value that should be processed
|
||||||
|
* @return the result of the function
|
||||||
|
*/
|
||||||
|
public float applyAsFloat(boolean k);
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Function interface that reduces boxing/unboxing and fills the gaps of interfaces that are missing.
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Boolean2IntFunction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Type Specific get function to reduce boxing/unboxing
|
||||||
|
* @param k the value that should be processed
|
||||||
|
* @return the result of the function
|
||||||
|
*/
|
||||||
|
public int applyAsInt(boolean k);
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Function interface that reduces boxing/unboxing and fills the gaps of interfaces that are missing.
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Boolean2LongFunction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Type Specific get function to reduce boxing/unboxing
|
||||||
|
* @param k the value that should be processed
|
||||||
|
* @return the result of the function
|
||||||
|
*/
|
||||||
|
public long applyAsLong(boolean k);
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Function interface that reduces boxing/unboxing and fills the gaps of interfaces that are missing.
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Boolean2ObjectFunction<V>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Type Specific get function to reduce boxing/unboxing
|
||||||
|
* @param k the value that should be processed
|
||||||
|
* @return the result of the function
|
||||||
|
*/
|
||||||
|
public V apply(boolean k);
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Function interface that reduces boxing/unboxing and fills the gaps of interfaces that are missing.
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface Boolean2ShortFunction
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Type Specific get function to reduce boxing/unboxing
|
||||||
|
* @param k the value that should be processed
|
||||||
|
* @return the result of the function
|
||||||
|
*/
|
||||||
|
public short applyAsShort(boolean k);
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Unary Operator to support Compute/Merge functions with type specific methods to reduce boxing/unboxing
|
||||||
|
*/
|
||||||
|
public interface BooleanBooleanUnaryOperator extends BiFunction<Boolean, Boolean, Boolean>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specifc apply method to reduce boxing/unboxing.
|
||||||
|
* Applies this function to the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first function argument
|
||||||
|
* @param v the second function argument
|
||||||
|
* @return the function result
|
||||||
|
*/
|
||||||
|
public boolean applyAsBoolean(boolean k, boolean v);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public default Boolean apply(Boolean k, Boolean v) { return Boolean.valueOf(applyAsBoolean(k.booleanValue(), v.booleanValue())); }
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Unary Operator to support Compute/Merge functions with type specific methods to reduce boxing/unboxing
|
||||||
|
*/
|
||||||
|
public interface BooleanByteUnaryOperator extends BiFunction<Boolean, Byte, Byte>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specifc apply method to reduce boxing/unboxing.
|
||||||
|
* Applies this function to the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first function argument
|
||||||
|
* @param v the second function argument
|
||||||
|
* @return the function result
|
||||||
|
*/
|
||||||
|
public byte applyAsByte(boolean k, byte v);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public default Byte apply(Boolean k, Byte v) { return Byte.valueOf(applyAsByte(k.booleanValue(), v.byteValue())); }
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Unary Operator to support Compute/Merge functions with type specific methods to reduce boxing/unboxing
|
||||||
|
*/
|
||||||
|
public interface BooleanCharUnaryOperator extends BiFunction<Boolean, Character, Character>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specifc apply method to reduce boxing/unboxing.
|
||||||
|
* Applies this function to the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first function argument
|
||||||
|
* @param v the second function argument
|
||||||
|
* @return the function result
|
||||||
|
*/
|
||||||
|
public char applyAsChar(boolean k, char v);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public default Character apply(Boolean k, Character v) { return Character.valueOf(applyAsChar(k.booleanValue(), v.charValue())); }
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Unary Operator to support Compute/Merge functions with type specific methods to reduce boxing/unboxing
|
||||||
|
*/
|
||||||
|
public interface BooleanDoubleUnaryOperator extends BiFunction<Boolean, Double, Double>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specifc apply method to reduce boxing/unboxing.
|
||||||
|
* Applies this function to the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first function argument
|
||||||
|
* @param v the second function argument
|
||||||
|
* @return the function result
|
||||||
|
*/
|
||||||
|
public double applyAsDouble(boolean k, double v);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public default Double apply(Boolean k, Double v) { return Double.valueOf(applyAsDouble(k.booleanValue(), v.doubleValue())); }
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Unary Operator to support Compute/Merge functions with type specific methods to reduce boxing/unboxing
|
||||||
|
*/
|
||||||
|
public interface BooleanFloatUnaryOperator extends BiFunction<Boolean, Float, Float>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specifc apply method to reduce boxing/unboxing.
|
||||||
|
* Applies this function to the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first function argument
|
||||||
|
* @param v the second function argument
|
||||||
|
* @return the function result
|
||||||
|
*/
|
||||||
|
public float applyAsFloat(boolean k, float v);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public default Float apply(Boolean k, Float v) { return Float.valueOf(applyAsFloat(k.booleanValue(), v.floatValue())); }
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Function interface that reduces boxing/unboxing and fills the gaps of interfaces that are missing.
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface BooleanFunction<V>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Type Specific get function to reduce boxing/unboxing
|
||||||
|
* @param k the value that should be processed
|
||||||
|
* @return the result of the function
|
||||||
|
*/
|
||||||
|
public V apply(boolean k);
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Unary Operator to support Compute/Merge functions with type specific methods to reduce boxing/unboxing
|
||||||
|
*/
|
||||||
|
public interface BooleanIntUnaryOperator extends BiFunction<Boolean, Integer, Integer>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specifc apply method to reduce boxing/unboxing.
|
||||||
|
* Applies this function to the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first function argument
|
||||||
|
* @param v the second function argument
|
||||||
|
* @return the function result
|
||||||
|
*/
|
||||||
|
public int applyAsInt(boolean k, int v);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public default Integer apply(Boolean k, Integer v) { return Integer.valueOf(applyAsInt(k.booleanValue(), v.intValue())); }
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Unary Operator to support Compute/Merge functions with type specific methods to reduce boxing/unboxing
|
||||||
|
*/
|
||||||
|
public interface BooleanLongUnaryOperator extends BiFunction<Boolean, Long, Long>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specifc apply method to reduce boxing/unboxing.
|
||||||
|
* Applies this function to the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first function argument
|
||||||
|
* @param v the second function argument
|
||||||
|
* @return the function result
|
||||||
|
*/
|
||||||
|
public long applyAsLong(boolean k, long v);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public default Long apply(Boolean k, Long v) { return Long.valueOf(applyAsLong(k.booleanValue(), v.longValue())); }
|
||||||
|
}
|
||||||
@ -0,0 +1,23 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Unary Operator to support Compute/Merge functions with type specific methods to reduce boxing/unboxing
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
*/
|
||||||
|
public interface BooleanObjectUnaryOperator<V> extends BiFunction<Boolean, V, V>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specifc apply method to reduce boxing/unboxing.
|
||||||
|
* Applies this function to the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first function argument
|
||||||
|
* @param v the second function argument
|
||||||
|
* @return the function result
|
||||||
|
*/
|
||||||
|
public V apply(boolean k, V v);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public default V apply(Boolean k, V v) { return apply(k.booleanValue(), v); }
|
||||||
|
}
|
||||||
@ -0,0 +1,98 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Function interface that reduces boxing/unboxing and fills the gaps of interfaces that are missing.
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface BooleanPredicate
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Type Specific get function to reduce boxing/unboxing
|
||||||
|
* @param k the value that should be processed
|
||||||
|
* @return the result of the function
|
||||||
|
*/
|
||||||
|
public boolean test(boolean k);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Default function that returns the input provided.
|
||||||
|
* @return a input returning function
|
||||||
|
*/
|
||||||
|
public static BooleanPredicate identity() {
|
||||||
|
return T -> T;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a composed function that first applies the {@code before}
|
||||||
|
* function to its input, and then applies this function to the result.
|
||||||
|
* If evaluation of either function throws an exception, it is relayed to
|
||||||
|
* the caller of the composed function.
|
||||||
|
*
|
||||||
|
* @param before the function that should be used first
|
||||||
|
* @return a composed function with a different starting function.
|
||||||
|
*/
|
||||||
|
public default BooleanPredicate compose(BooleanPredicate before) {
|
||||||
|
Objects.requireNonNull(before);
|
||||||
|
return T -> test(before.test(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a composed function that first applies this function to
|
||||||
|
* its input, and then applies the {@code after} function to the result.
|
||||||
|
* If evaluation of either function throws an exception, it is relayed to
|
||||||
|
* the caller of the composed function.
|
||||||
|
*
|
||||||
|
* @param after the function that should be used last
|
||||||
|
* @return a composed function with a different starting function.
|
||||||
|
*/
|
||||||
|
public default BooleanPredicate andThen(BooleanPredicate after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return T -> after.test(test(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Always true function that may be useful if you don't need to process information or just want a default.
|
||||||
|
* @return a default returning function
|
||||||
|
*/
|
||||||
|
public static BooleanPredicate alwaysTrue() {
|
||||||
|
return T -> true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Always false function that may be useful if you don't need to process information or just want a default.
|
||||||
|
* @return a default returning function
|
||||||
|
*/
|
||||||
|
public static BooleanPredicate alwaysFalse() {
|
||||||
|
return T -> false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type specific and-function helper function that reduces boxing/unboxing
|
||||||
|
* @param other the other function that should be merged with.
|
||||||
|
* @return a function that compares values in a and comparason
|
||||||
|
*/
|
||||||
|
public default BooleanPredicate andType(BooleanPredicate other) {
|
||||||
|
Objects.requireNonNull(other);
|
||||||
|
return T -> test(T) && other.test(T);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A type specific inverter function
|
||||||
|
* @return the same function but inverts the result
|
||||||
|
*/
|
||||||
|
public default BooleanPredicate negate() {
|
||||||
|
return T -> !test(T);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type specific or-function helper function that reduces boxing/unboxing
|
||||||
|
* @param other the other function that should be merged with.
|
||||||
|
* @return a function that compares values in a or comparason
|
||||||
|
*/
|
||||||
|
public default BooleanPredicate orType(BooleanPredicate other) {
|
||||||
|
Objects.requireNonNull(other);
|
||||||
|
return T -> test(T) || other.test(T);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
package speiger.src.collections.booleans.functions.function;
|
||||||
|
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Unary Operator to support Compute/Merge functions with type specific methods to reduce boxing/unboxing
|
||||||
|
*/
|
||||||
|
public interface BooleanShortUnaryOperator extends BiFunction<Boolean, Short, Short>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type Specifc apply method to reduce boxing/unboxing.
|
||||||
|
* Applies this function to the given arguments.
|
||||||
|
*
|
||||||
|
* @param k the first function argument
|
||||||
|
* @param v the second function argument
|
||||||
|
* @return the function result
|
||||||
|
*/
|
||||||
|
public short applyAsShort(boolean k, short v);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public default Short apply(Boolean k, Short v) { return Short.valueOf(applyAsShort(k.booleanValue(), v.shortValue())); }
|
||||||
|
}
|
||||||
@ -0,0 +1,651 @@
|
|||||||
|
package speiger.src.collections.booleans.lists;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ListIterator;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.RandomAccess;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.collections.AbstractBooleanCollection;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanCollection;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanIterator;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanSplititerator;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanSplititerators;
|
||||||
|
import speiger.src.collections.utils.SanityChecks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract implementation of the {@link BooleanList} interface.
|
||||||
|
*/
|
||||||
|
public abstract class AbstractBooleanList extends AbstractBooleanCollection implements BooleanList
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation of add function that delegates to {@link List#add(int, Object)}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean add(boolean e) {
|
||||||
|
add(size(), e);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public void add(int index, Boolean element) {
|
||||||
|
add(index, element.booleanValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation that iterates over the elements and adds them.
|
||||||
|
* @param c the elements that wants to be added
|
||||||
|
* @return true if the list was modified
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean addAll(BooleanCollection c) {
|
||||||
|
return addAll(size(), c);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation that iterates over the elements and adds them.
|
||||||
|
* @param c the elements that wants to be added
|
||||||
|
* @return true if the list was modified
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean addAll(BooleanList c) {
|
||||||
|
return addAll(size(), c);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean addAll(Collection<? extends Boolean> c) {
|
||||||
|
return c instanceof BooleanCollection ? addAll((BooleanCollection)c) : addAll(size(), c);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The IndexOf implementation iterates over all elements and compares them to the search value.
|
||||||
|
* @param o the value that the index is searched for.
|
||||||
|
* @return index of the value that was searched for. -1 if not found
|
||||||
|
* @note it is highly suggested not to use this with Primitives because of boxing. But it is still supported because of ObjectComparason that are custom objects and allow to find the contents.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public int indexOf(Object o) {
|
||||||
|
BooleanListIterator iter = listIterator();
|
||||||
|
if(o == null) return -1;
|
||||||
|
while(iter.hasNext()) {
|
||||||
|
if(Objects.equals(o, Boolean.valueOf(iter.nextBoolean())))
|
||||||
|
return iter.previousIndex();
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The lastIndexOf implementation iterates over all elements and compares them to the search value.
|
||||||
|
* @param o the value that the index is searched for.
|
||||||
|
* @return the last index of the value that was searched for. -1 if not found
|
||||||
|
* @note it is highly suggested not to use this with Primitives because of boxing. But it is still supported because of ObjectComparason that are custom objects and allow to find the contents.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public int lastIndexOf(Object o) {
|
||||||
|
BooleanListIterator iter = listIterator(size());
|
||||||
|
if(o == null) return -1;
|
||||||
|
while(iter.hasPrevious()) {
|
||||||
|
if(Objects.equals(o, Boolean.valueOf(iter.previousBoolean())))
|
||||||
|
return iter.nextIndex();
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The indexOf implementation iterates over all elements and compares them to the search value.
|
||||||
|
* @param e the value that the index is searched for.
|
||||||
|
* @return index of the value that was searched for. -1 if not found
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int indexOf(boolean e) {
|
||||||
|
BooleanListIterator iter = listIterator();
|
||||||
|
while(iter.hasNext()) {
|
||||||
|
if(iter.nextBoolean() == e)
|
||||||
|
return iter.previousIndex();
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The lastIndexOf implementation iterates over all elements and compares them to the search value.
|
||||||
|
* @param e the value that the index is searched for.
|
||||||
|
* @return the last index of the value that was searched for. -1 if not found
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int lastIndexOf(boolean e) {
|
||||||
|
BooleanListIterator iter = listIterator(size());
|
||||||
|
while(iter.hasPrevious()) {
|
||||||
|
if(iter.previousBoolean() == e)
|
||||||
|
return iter.nextIndex();
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean swapRemoveBoolean(boolean e) {
|
||||||
|
int index = indexOf(e);
|
||||||
|
if(index == -1) return false;
|
||||||
|
swapRemove(index);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares if the list are the same.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (o == this)
|
||||||
|
return true;
|
||||||
|
if (!(o instanceof List))
|
||||||
|
return false;
|
||||||
|
List<?> l = (List<?>)o;
|
||||||
|
if(l.size() != size()) return false;
|
||||||
|
if(l instanceof BooleanList)
|
||||||
|
{
|
||||||
|
BooleanListIterator e1 = listIterator();
|
||||||
|
BooleanListIterator e2 = ((BooleanList)l).listIterator();
|
||||||
|
while (e1.hasNext() && e2.hasNext()) {
|
||||||
|
if(!(e1.nextBoolean() == e2.nextBoolean()))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return !(e1.hasNext() || e2.hasNext());
|
||||||
|
}
|
||||||
|
ListIterator<Boolean> e1 = listIterator();
|
||||||
|
ListIterator<?> e2 = l.listIterator();
|
||||||
|
while (e1.hasNext() && e2.hasNext()) {
|
||||||
|
if(!Objects.equals(e1.next(), e2.next()))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return !(e1.hasNext() || e2.hasNext());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates the hashcode based on the values stored in the list.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int hashCode = 1;
|
||||||
|
BooleanListIterator i = listIterator();
|
||||||
|
while(i.hasNext())
|
||||||
|
hashCode = 31 * hashCode + Boolean.hashCode(i.nextBoolean());
|
||||||
|
return hashCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanList subList(int fromIndex, int toIndex) {
|
||||||
|
SanityChecks.checkArrayCapacity(size(), fromIndex, toIndex-fromIndex);
|
||||||
|
return new SubList(this, 0, fromIndex, toIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanList reversed() {
|
||||||
|
return new ReversedList(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator() {
|
||||||
|
return listIterator(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanListIterator listIterator() {
|
||||||
|
return listIterator(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanListIterator listIterator(int index) {
|
||||||
|
if(index < 0 || index > size()) throw new IndexOutOfBoundsException();
|
||||||
|
return new BooleanListIter(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void size(int size) {
|
||||||
|
while(size > size()) add(false);
|
||||||
|
while(size < size()) removeBoolean(size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AbstractBooleanList copy() { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
private class ReversedList extends AbstractBooleanList
|
||||||
|
{
|
||||||
|
final AbstractBooleanList list;
|
||||||
|
|
||||||
|
public ReversedList(AbstractBooleanList list) {
|
||||||
|
this.list = list;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(int index, boolean e) {
|
||||||
|
list.add(list.size() - index - 1, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, BooleanCollection c) {
|
||||||
|
return addCollection(index, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, BooleanList c) {
|
||||||
|
if(c instanceof RandomAccess) {
|
||||||
|
for(int i = 0,m=c.size();i<m;i++) {
|
||||||
|
list.add(list.size() - index - i - 1, c.getBoolean(i));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return addCollection(index, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean addCollection(int index, BooleanCollection c) {
|
||||||
|
int i = 0;
|
||||||
|
for(BooleanIterator iter = c.iterator();iter.hasNext();i++) {
|
||||||
|
list.add(list.size() - index - i - 1, iter.nextBoolean());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, Collection<? extends Boolean> c) {
|
||||||
|
int i = 0;
|
||||||
|
for(Iterator<? extends Boolean> iter = c.iterator();iter.hasNext();i++) {
|
||||||
|
list.add(list.size() - index - i - 1, iter.next());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBoolean(int index) {
|
||||||
|
return list.getBoolean(list.size() - index - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean set(int index, boolean e) {
|
||||||
|
return list.set(list.size() - index - 1, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeBoolean(int index) {
|
||||||
|
return list.removeBoolean(list.size() - index - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addElements(int from, boolean[] a, int offset, int length) {
|
||||||
|
for(int i = 0,m=length;i<m;i++) {
|
||||||
|
list.add(list.size() - from - i - 1, a[i+offset]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] getElements(int from, boolean[] a, int offset, int length) {
|
||||||
|
return reverse(list.getElements(list.size() - from - 1, a, offset, length));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeElements(int from, int to) {
|
||||||
|
list.removeElements(list.size() - to - 1, list.size() - from - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean swapRemove(int index) {
|
||||||
|
return list.swapRemove(list.size() - index - 1);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public boolean[] extractElements(int from, int to) {
|
||||||
|
return reverse(list.extractElements(list.size() - to - 1, list.size() - from - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return list.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
list.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanList reversed() {
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean[] reverse(boolean[] data) {
|
||||||
|
for (int i = 0, mid = data.length >> 1, j = data.length - 1; i < mid; i++, j--) {
|
||||||
|
boolean t = data[i];
|
||||||
|
data[i] = data[j];
|
||||||
|
data[j] = t;
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private class SubList extends AbstractBooleanList
|
||||||
|
{
|
||||||
|
final AbstractBooleanList list;
|
||||||
|
final int parentOffset;
|
||||||
|
final int offset;
|
||||||
|
int size;
|
||||||
|
|
||||||
|
public SubList(AbstractBooleanList list, int offset, int from, int to) {
|
||||||
|
this.list = list;
|
||||||
|
this.parentOffset = from;
|
||||||
|
this.offset = offset + from;
|
||||||
|
this.size = to - from;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(int index, boolean element) {
|
||||||
|
checkAddSubRange(index);
|
||||||
|
list.add(parentOffset+index, element);
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, Collection<? extends Boolean> c) {
|
||||||
|
checkAddSubRange(index);
|
||||||
|
int add = c.size();
|
||||||
|
if(add <= 0) return false;
|
||||||
|
list.addAll(parentOffset+index, c);
|
||||||
|
this.size += add;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, BooleanCollection c) {
|
||||||
|
checkAddSubRange(index);
|
||||||
|
int add = c.size();
|
||||||
|
if(add <= 0) return false;
|
||||||
|
list.addAll(parentOffset+index, c);
|
||||||
|
this.size += add;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, BooleanList c) {
|
||||||
|
checkAddSubRange(index);
|
||||||
|
int add = c.size();
|
||||||
|
if(add <= 0) return false;
|
||||||
|
list.addAll(parentOffset+index, c);
|
||||||
|
this.size += add;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addElements(int from, boolean[] a, int offset, int length) {
|
||||||
|
checkAddSubRange(from);
|
||||||
|
if(length <= 0) return;
|
||||||
|
list.addElements(parentOffset+from, a, offset, length);
|
||||||
|
this.size += length;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] getElements(int from, boolean[] a, int offset, int length) {
|
||||||
|
SanityChecks.checkArrayCapacity(size, from, length);
|
||||||
|
SanityChecks.checkArrayCapacity(a.length, offset, length);
|
||||||
|
return list.getElements(from+parentOffset, a, offset, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeElements(int from, int to) {
|
||||||
|
if(to-from <= 0) return;
|
||||||
|
checkSubRange(from);
|
||||||
|
checkAddSubRange(to);
|
||||||
|
list.removeElements(from+parentOffset, to+parentOffset);
|
||||||
|
size -= to - from;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] extractElements(int from, int to) {
|
||||||
|
checkSubRange(from);
|
||||||
|
checkAddSubRange(to);
|
||||||
|
boolean[] result = list.extractElements(from+parentOffset, to+parentOffset);
|
||||||
|
size -= result.length;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBoolean(int index) {
|
||||||
|
checkSubRange(index);
|
||||||
|
return list.getBoolean(parentOffset+index);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean set(int index, boolean element) {
|
||||||
|
checkSubRange(index);
|
||||||
|
return list.set(parentOffset+index, element);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean swapRemove(int index) {
|
||||||
|
checkSubRange(index);
|
||||||
|
if(index == size-1) {
|
||||||
|
boolean result = list.removeBoolean(parentOffset+size-1);
|
||||||
|
size--;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
boolean result = list.set(index+parentOffset, list.getBoolean(parentOffset+size-1));
|
||||||
|
list.removeBoolean(parentOffset+size-1);
|
||||||
|
size--;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeBoolean(int index) {
|
||||||
|
checkSubRange(index);
|
||||||
|
boolean result = list.removeBoolean(index+parentOffset);
|
||||||
|
size--;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanSplititerator spliterator() { return BooleanSplititerators.createSplititerator(this, 16464); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanListIterator listIterator(int index) {
|
||||||
|
if(index < 0 || index > size()) throw new IndexOutOfBoundsException();
|
||||||
|
return new SubListIterator(this, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanList subList(int fromIndex, int toIndex) {
|
||||||
|
SanityChecks.checkArrayCapacity(size, fromIndex, toIndex-fromIndex);
|
||||||
|
return new SubList(this, offset, fromIndex, toIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void checkSubRange(int index) {
|
||||||
|
if (index < 0 || index >= size)
|
||||||
|
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void checkAddSubRange(int index) {
|
||||||
|
if (index < 0 || index > size)
|
||||||
|
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class SubListIterator implements BooleanListIterator
|
||||||
|
{
|
||||||
|
AbstractBooleanList list;
|
||||||
|
int index;
|
||||||
|
int lastReturned = -1;
|
||||||
|
|
||||||
|
SubListIterator(AbstractBooleanList list, int index) {
|
||||||
|
this.list = list;
|
||||||
|
this.index = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return index < list.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean nextBoolean() {
|
||||||
|
if(!hasNext()) throw new NoSuchElementException();
|
||||||
|
int i = index++;
|
||||||
|
return list.getBoolean((lastReturned = i));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasPrevious() {
|
||||||
|
return index > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean previousBoolean() {
|
||||||
|
if(!hasPrevious()) throw new NoSuchElementException();
|
||||||
|
index--;
|
||||||
|
return list.getBoolean((lastReturned = index));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int nextIndex() {
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int previousIndex() {
|
||||||
|
return index-1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove() {
|
||||||
|
if(lastReturned == -1) throw new IllegalStateException();
|
||||||
|
list.removeBoolean(lastReturned);
|
||||||
|
index = lastReturned;
|
||||||
|
lastReturned = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(boolean e) {
|
||||||
|
if(lastReturned == -1) throw new IllegalStateException();
|
||||||
|
list.set(lastReturned, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(boolean e) {
|
||||||
|
list.add(index, e);
|
||||||
|
index++;
|
||||||
|
lastReturned = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int skip(int amount) {
|
||||||
|
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
|
||||||
|
int steps = Math.min(amount, size() - index);
|
||||||
|
index += steps;
|
||||||
|
if(steps > 0) lastReturned = Math.min(index-1, size()-1);
|
||||||
|
return steps;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int back(int amount) {
|
||||||
|
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
|
||||||
|
int steps = Math.min(amount, index);
|
||||||
|
index -= steps;
|
||||||
|
if(steps > 0) lastReturned = Math.min(index, size()-1);
|
||||||
|
return steps;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class BooleanListIter implements BooleanListIterator {
|
||||||
|
int index;
|
||||||
|
int lastReturned = -1;
|
||||||
|
|
||||||
|
BooleanListIter(int index) {
|
||||||
|
this.index = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return index < size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean nextBoolean() {
|
||||||
|
if(!hasNext()) throw new NoSuchElementException();
|
||||||
|
int i = index++;
|
||||||
|
return getBoolean((lastReturned = i));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasPrevious() {
|
||||||
|
return index > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean previousBoolean() {
|
||||||
|
if(!hasPrevious()) throw new NoSuchElementException();
|
||||||
|
index--;
|
||||||
|
return getBoolean((lastReturned = index));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int nextIndex() {
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int previousIndex() {
|
||||||
|
return index-1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove() {
|
||||||
|
if(lastReturned == -1) throw new IllegalStateException();
|
||||||
|
AbstractBooleanList.this.removeBoolean(lastReturned);
|
||||||
|
index = lastReturned;
|
||||||
|
lastReturned = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(boolean e) {
|
||||||
|
if(lastReturned == -1) throw new IllegalStateException();
|
||||||
|
AbstractBooleanList.this.set(lastReturned, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(boolean e) {
|
||||||
|
AbstractBooleanList.this.add(index, e);
|
||||||
|
index++;
|
||||||
|
lastReturned = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int skip(int amount) {
|
||||||
|
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
|
||||||
|
int steps = Math.min(amount, size() - index);
|
||||||
|
index += steps;
|
||||||
|
if(steps > 0) lastReturned = Math.min(index-1, size()-1);
|
||||||
|
return steps;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int back(int amount) {
|
||||||
|
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
|
||||||
|
int steps = Math.min(amount, index);
|
||||||
|
index -= steps;
|
||||||
|
if(steps > 0) lastReturned = Math.min(index, size()-1);
|
||||||
|
return steps;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,910 @@
|
|||||||
|
package speiger.src.collections.booleans.lists;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanCollection;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanStack;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanIterator;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanComparator;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanConsumer;
|
||||||
|
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanPredicate;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanBooleanUnaryOperator;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanArrays;
|
||||||
|
import speiger.src.collections.objects.utils.ObjectArrays;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanIterators;
|
||||||
|
import speiger.src.collections.booleans.utils.IBooleanArray;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanSplititerator;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanSplititerators;
|
||||||
|
import speiger.src.collections.utils.SanityChecks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific Array-based implementation of list that is written to reduce (un)boxing
|
||||||
|
*
|
||||||
|
* <p>This implementation is optimized to improve how data is processed with interfaces like {@link IBooleanArray}, {@link BooleanStack}
|
||||||
|
* and with optimized functions that use type-specific implementations for primitives and optimized logic for bulkactions.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class BooleanArrayList extends AbstractBooleanList implements IBooleanArray, BooleanStack
|
||||||
|
{
|
||||||
|
static final int DEFAULT_ARRAY_SIZE = 10;
|
||||||
|
|
||||||
|
/** The backing array */
|
||||||
|
protected transient boolean[] data;
|
||||||
|
/** The current size of the elements stored in the backing array */
|
||||||
|
protected int size = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new ArrayList with a Empty array.
|
||||||
|
*/
|
||||||
|
public BooleanArrayList() {
|
||||||
|
data = BooleanArrays.EMPTY_ARRAY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new ArrayList with the specific requested size
|
||||||
|
* @param size the minimum initial size of the Backing array
|
||||||
|
*/
|
||||||
|
public BooleanArrayList(int size) {
|
||||||
|
if(size < 0) throw new IllegalStateException("Size has to be 0 or greater");
|
||||||
|
data = new boolean[size];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new ArrayList a copy with the contents of the Collection.
|
||||||
|
* @param c the elements that should be added into the list
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public BooleanArrayList(Collection<? extends Boolean> c) {
|
||||||
|
this(c.size());
|
||||||
|
size = BooleanIterators.unwrap(data, c.iterator());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new ArrayList a copy with the contents of the Collection.
|
||||||
|
* @param c the elements that should be added into the list
|
||||||
|
*/
|
||||||
|
public BooleanArrayList(BooleanCollection c) {
|
||||||
|
this(c.size());
|
||||||
|
size = BooleanIterators.unwrap(data, c.iterator());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new ArrayList a copy with the contents of the List.
|
||||||
|
* @param l the elements that should be added into the list
|
||||||
|
*/
|
||||||
|
public BooleanArrayList(BooleanList l) {
|
||||||
|
this(l.size());
|
||||||
|
size = l.size();
|
||||||
|
l.getElements(0, data, 0, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new ArrayList with a Copy of the array
|
||||||
|
* @param a the array that should be copied
|
||||||
|
*/
|
||||||
|
public BooleanArrayList(boolean... a) {
|
||||||
|
this(a, 0, a.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new ArrayList with a Copy of the array with a custom length
|
||||||
|
* @param a the array that should be copied
|
||||||
|
* @param length the desired length that should be copied
|
||||||
|
*/
|
||||||
|
public BooleanArrayList(boolean[] a, int length) {
|
||||||
|
this(a, 0, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new ArrayList with a Copy of the array with in the custom range.
|
||||||
|
* @param a the array that should be copied
|
||||||
|
* @param offset the starting offset of where the array should be copied from
|
||||||
|
* @param length the desired length that should be copied
|
||||||
|
* @throws IllegalStateException if offset is smaller then 0
|
||||||
|
* @throws IllegalStateException if the offset + length exceeds the array length
|
||||||
|
*/
|
||||||
|
public BooleanArrayList(boolean[] a, int offset, int length) {
|
||||||
|
this(length);
|
||||||
|
SanityChecks.checkArrayCapacity(a.length, offset, length);
|
||||||
|
System.arraycopy(a, offset, data, 0, length);
|
||||||
|
size = length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a wrapped arraylist that uses the array as backing array
|
||||||
|
* @param a elements that should be wrapped
|
||||||
|
* @return a Wrapped list using the input array
|
||||||
|
*/
|
||||||
|
public static BooleanArrayList wrap(boolean... a) {
|
||||||
|
return wrap(a, a.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a wrapped arraylist that uses the array as backing array and a custom fill size
|
||||||
|
* @param a elements that should be wrapped
|
||||||
|
* @param length the size of the elements within the array
|
||||||
|
* @return a Wrapped list using the input array
|
||||||
|
*/
|
||||||
|
public static BooleanArrayList wrap(boolean[] a, int length) {
|
||||||
|
SanityChecks.checkArrayCapacity(a.length, 0, length);
|
||||||
|
BooleanArrayList list = new BooleanArrayList();
|
||||||
|
list.data = a;
|
||||||
|
list.size = length;
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends the specified element to the end of this list.
|
||||||
|
*
|
||||||
|
* @param e element to be appended to this list
|
||||||
|
* @return true (as specified by {@link Collection#add})
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean add(boolean e) {
|
||||||
|
grow(size + 1);
|
||||||
|
data[size++] = e;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends the specified element to the end of this Stack.
|
||||||
|
* @param e element to be appended to this Stack
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void push(boolean e) {
|
||||||
|
add(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends the specified element to the index of the list
|
||||||
|
* @param index the index where to append the element to
|
||||||
|
* @param e the element to append to the list
|
||||||
|
* @throws IndexOutOfBoundsException if index is outside of the lists range
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void add(int index, boolean e) {
|
||||||
|
checkAddRange(index);
|
||||||
|
grow(size + 1);
|
||||||
|
if(index != size) System.arraycopy(data, index, data, index+1, size - index);
|
||||||
|
data[index] = e;
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends the specified elements to the index of the list.
|
||||||
|
* This function may delegate to more appropriate function if necessary
|
||||||
|
* @param index the index where to append the elements to
|
||||||
|
* @param c the elements to append to the list
|
||||||
|
* @throws IndexOutOfBoundsException if index is outside of the lists range
|
||||||
|
* @throws NullPointerException if collection contains a null element
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean addAll(int index, Collection<? extends Boolean> c) {
|
||||||
|
if(c instanceof BooleanCollection) return addAll(index, (BooleanCollection)c);
|
||||||
|
int add = c.size();
|
||||||
|
if(add <= 0) return false;
|
||||||
|
if(c.contains(null)) throw new NullPointerException();
|
||||||
|
grow(size + add);
|
||||||
|
if(index != size) System.arraycopy(data, index, data, index+add, size - index);
|
||||||
|
size+=add;
|
||||||
|
Iterator<? extends Boolean> iter = c.iterator();
|
||||||
|
while(add-- != 0) data[index++] = iter.next().booleanValue();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends the specified elements to the index of the list.
|
||||||
|
* This function may delegate to more appropriate function if necessary
|
||||||
|
* @param index the index where to append the elements to
|
||||||
|
* @param c the elements to append to the list
|
||||||
|
* @throws IndexOutOfBoundsException if index is outside of the lists range
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, BooleanCollection c) {
|
||||||
|
if(c instanceof BooleanList) return addAll(index, (BooleanList)c);
|
||||||
|
int add = c.size();
|
||||||
|
if(add <= 0) return false;
|
||||||
|
grow(size + add);
|
||||||
|
if(index != size) System.arraycopy(data, index, data, index+add, size - index);
|
||||||
|
size+=add;
|
||||||
|
BooleanIterator iter = c.iterator();
|
||||||
|
while(add-- != 0) data[index++] = iter.nextBoolean();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends the specified elements to the index of the list.
|
||||||
|
* @param index the index where to append the elements to
|
||||||
|
* @param c the elements to append to the list
|
||||||
|
* @throws IndexOutOfBoundsException if index is outside of the lists range
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, BooleanList c) {
|
||||||
|
int add = c.size();
|
||||||
|
if(add <= 0) return false;
|
||||||
|
checkAddRange(index);
|
||||||
|
grow(size + add);
|
||||||
|
if(index != size) System.arraycopy(data, index, data, index+add, size - index);
|
||||||
|
size+=add;
|
||||||
|
c.getElements(0, data, index, c.size());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(boolean[] e, int offset, int length) {
|
||||||
|
if(length <= 0) return false;
|
||||||
|
SanityChecks.checkArrayCapacity(e.length, offset, length);
|
||||||
|
grow(size + length);
|
||||||
|
System.arraycopy(e, offset, data, size, length);
|
||||||
|
size+=length;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends the specified array elements to the index of the list.
|
||||||
|
* @param from the index where to append the elements to
|
||||||
|
* @param a the elements to append to the list
|
||||||
|
* @param offset where to start ino the array
|
||||||
|
* @param length the amount of elements to insert
|
||||||
|
* @throws IndexOutOfBoundsException if index is outside of the lists range
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void addElements(int from, boolean[] a, int offset, int length) {
|
||||||
|
if(length <= 0) return;
|
||||||
|
checkAddRange(from);
|
||||||
|
SanityChecks.checkArrayCapacity(a.length, offset, length);
|
||||||
|
grow(size + length);
|
||||||
|
if(from != size) System.arraycopy(data, from, data, from+length, size - from);
|
||||||
|
size+=length;
|
||||||
|
System.arraycopy(a, offset, data, from, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to fast fetch elements from the list
|
||||||
|
* @param from index where the list should be fetching elements from
|
||||||
|
* @param a the array where the values should be inserted to
|
||||||
|
* @param offset the startIndex of where the array should be written to
|
||||||
|
* @param length the number of elements the values should be fetched from
|
||||||
|
* @return the inputArray
|
||||||
|
* @throws NullPointerException if the array is null
|
||||||
|
* @throws IndexOutOfBoundsException if from is outside of the lists range
|
||||||
|
* @throws IllegalStateException if offset or length are smaller then 0 or exceed the array length
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean[] getElements(int from, boolean[] a, int offset, int length) {
|
||||||
|
SanityChecks.checkArrayCapacity(size, from, length);
|
||||||
|
SanityChecks.checkArrayCapacity(a.length, offset, length);
|
||||||
|
System.arraycopy(data, from, a, offset, length);
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* a function to fast remove elements from the list.
|
||||||
|
* @param from the start index of where the elements should be removed from (inclusive)
|
||||||
|
* @param to the end index of where the elements should be removed to (exclusive)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void removeElements(int from, int to) {
|
||||||
|
checkRange(from);
|
||||||
|
checkAddRange(to);
|
||||||
|
int length = to - from;
|
||||||
|
if(length <= 0) return;
|
||||||
|
if(to != size) System.arraycopy(data, to, data, from, size - to);
|
||||||
|
size -= length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to fast extract elements out of the list, this removes the elements that were fetched.
|
||||||
|
* @param from the start index of where the elements should be fetched from (inclusive)
|
||||||
|
* @param to the end index of where the elements should be fetched to (exclusive)
|
||||||
|
* @return a array of the elements that were fetched
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean[] extractElements(int from, int to) {
|
||||||
|
int length = to - from;
|
||||||
|
if(length <= 0) return BooleanArrays.EMPTY_ARRAY;
|
||||||
|
boolean[] a = new boolean[length];
|
||||||
|
System.arraycopy(data, from, a, 0, length);
|
||||||
|
if(to != size) System.arraycopy(data, to, data, from, size - to);
|
||||||
|
size -= length;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to find if the Element is present in this list.
|
||||||
|
* @param o the element that is searched for
|
||||||
|
* @return if the element was found.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean contains(Object o) {
|
||||||
|
return indexOf(o) != -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to find the index of a given element
|
||||||
|
* @param o the element that is searched for
|
||||||
|
* @return the index of the element if found. (if not found then -1)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public int indexOf(Object o) {
|
||||||
|
if(o == null) return -1;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(Objects.equals(o, Boolean.valueOf(data[i]))) return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to find the last index of a given element
|
||||||
|
* @param o the element that is searched for
|
||||||
|
* @return the last index of the element if found. (if not found then -1)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public int lastIndexOf(Object o) {
|
||||||
|
if(o == null) return -1;
|
||||||
|
for(int i = size - 1;i>=0;i--) {
|
||||||
|
if(Objects.equals(o, Boolean.valueOf(data[i]))) return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific implementation of the Collection#contains function.
|
||||||
|
* @param e the element that is searched for.
|
||||||
|
* @return if the element was found
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean contains(boolean e) {
|
||||||
|
return indexOf(e) != -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific function to find the index of a given element
|
||||||
|
* @param e the element that is searched for
|
||||||
|
* @return the index of the element if found. (if not found then -1)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int indexOf(boolean e) {
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(data[i] == e) return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific function to find the last index of a given element
|
||||||
|
* @param e the element that is searched for
|
||||||
|
* @return the last index of the element if found. (if not found then -1)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int lastIndexOf(boolean e) {
|
||||||
|
for(int i = size - 1;i>=0;i--) {
|
||||||
|
if(data[i] == e) return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts the elements specified by the Natural order either by using the Comparator or the elements
|
||||||
|
* @param c the sorter of the elements, can be null
|
||||||
|
* @see java.util.List#sort(java.util.Comparator)
|
||||||
|
* @see BooleanArrays#stableSort(boolean[], BooleanComparator)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void sort(BooleanComparator c) {
|
||||||
|
if(c != null) BooleanArrays.stableSort(data, size, c);
|
||||||
|
else BooleanArrays.stableSort(data, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts the elements specified by the Natural order either by using the Comparator or the elements using a unstable sort
|
||||||
|
* @param c the sorter of the elements, can be null
|
||||||
|
* @see java.util.List#sort(java.util.Comparator)
|
||||||
|
* @see BooleanArrays#unstableSort(boolean[], BooleanComparator)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void unstableSort(BooleanComparator c) {
|
||||||
|
if(c != null) BooleanArrays.unstableSort(data, size, c);
|
||||||
|
else BooleanArrays.unstableSort(data, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific get function to reduce (un)boxing
|
||||||
|
* @param index the index of the element to fetch
|
||||||
|
* @return the value of the requested index
|
||||||
|
* @throws IndexOutOfBoundsException if the index is out of range
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean getBoolean(int index) {
|
||||||
|
checkRange(index);
|
||||||
|
return data[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides the Selected Object from the stack.
|
||||||
|
* Top to bottom
|
||||||
|
* @param index of the element that should be provided
|
||||||
|
* @return the element that was requested
|
||||||
|
* @throws ArrayIndexOutOfBoundsException if the index is out of bounds
|
||||||
|
* @see speiger.src.collections.utils.Stack#peek(int)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean peek(int index) {
|
||||||
|
checkRange((size() - 1) - index);
|
||||||
|
return data[(size() - 1) - index];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides the Underlying Array in the Implementation
|
||||||
|
* @return underlying Array
|
||||||
|
* @throws ClassCastException if the return type does not match the underlying array. (Only for Object Implementations)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean[] elements() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific foreach function that reduces (un)boxing
|
||||||
|
*
|
||||||
|
* @implSpec
|
||||||
|
* <p>The default implementation behaves as if:
|
||||||
|
* <pre>{@code
|
||||||
|
* for(int i = 0;i<size;i++)
|
||||||
|
* action.accept(data[i]);
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param action The action to be performed for each element
|
||||||
|
* @throws NullPointerException if the specified action is null
|
||||||
|
* @see Iterable#forEach(java.util.function.Consumer)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
for(int i = 0;i<size;i++)
|
||||||
|
action.accept(data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E> void forEach(E input, ObjectBooleanConsumer<E> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
for(int i = 0;i<size;i++)
|
||||||
|
action.accept(input, data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matchesAny(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(filter.test(data[i])) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matchesNone(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(filter.test(data[i])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matchesAll(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(!filter.test(data[i])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean findFirst(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(filter.test(data[i])) return data[i];
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean reduce(boolean identity, BooleanBooleanUnaryOperator operator) {
|
||||||
|
Objects.requireNonNull(operator);
|
||||||
|
boolean state = identity;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
state = operator.applyAsBoolean(state, data[i]);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean reduce(BooleanBooleanUnaryOperator operator) {
|
||||||
|
Objects.requireNonNull(operator);
|
||||||
|
boolean state = false;
|
||||||
|
boolean empty = true;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(empty) {
|
||||||
|
empty = false;
|
||||||
|
state = data[i];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
state = operator.applyAsBoolean(state, data[i]);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int count(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
int result = 0;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(filter.test(data[i])) result++;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific set function to reduce (un)boxing
|
||||||
|
* @param index the index of the element to set
|
||||||
|
* @param e the value that should be set
|
||||||
|
* @return the previous element
|
||||||
|
* @throws IndexOutOfBoundsException if the index is out of range
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean set(int index, boolean e) {
|
||||||
|
checkRange(index);
|
||||||
|
boolean old = data[index];
|
||||||
|
data[index] = e;
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to replace all values in the list
|
||||||
|
* @param o the action to replace the values
|
||||||
|
* @throws NullPointerException if o is null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public void replaceAll(UnaryOperator<Boolean> o) {
|
||||||
|
Objects.requireNonNull(o);
|
||||||
|
for(int i = 0;i<size;i++)
|
||||||
|
data[i] = o.apply(Boolean.valueOf(data[i])).booleanValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific remove function to reduce (un)boxing
|
||||||
|
* @param index the index of the element to fetch
|
||||||
|
* @return the value of the requested index
|
||||||
|
* @throws IndexOutOfBoundsException if the index is out of range
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean removeBoolean(int index) {
|
||||||
|
checkRange(index);
|
||||||
|
boolean old = data[index];
|
||||||
|
size--;
|
||||||
|
if(index != size) System.arraycopy(data, index+1, data, index, size - index);
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean swapRemove(int index) {
|
||||||
|
checkRange(index);
|
||||||
|
boolean old = data[index];
|
||||||
|
size--;
|
||||||
|
data[index] = data[size];
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation of remove. This implementation iterates over the elements until it finds the element that is searched for or it runs out of elements.
|
||||||
|
* It stops after finding the first element
|
||||||
|
* @param type the element that is searched for
|
||||||
|
* @return true if the element was found and removed.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean remBoolean(boolean type) {
|
||||||
|
int index = indexOf(type);
|
||||||
|
if(index == -1) return false;
|
||||||
|
removeBoolean(index);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific pop function to reduce (un)boxing
|
||||||
|
* @return the value of the requested index
|
||||||
|
* @throws IndexOutOfBoundsException if the index is out of range
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean pop() {
|
||||||
|
return removeBoolean(size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to remove all elements that were provided in the other collection
|
||||||
|
* This function might delegate to a more appropriate function if necessary
|
||||||
|
* @param c the elements that should be removed
|
||||||
|
* @return true if the collection was modified
|
||||||
|
* @throws NullPointerException if the collection is null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean removeAll(Collection<?> c) {
|
||||||
|
if(c.isEmpty()) return false;
|
||||||
|
boolean modified = false;
|
||||||
|
int j = 0;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(!c.contains(Boolean.valueOf(data[i]))) data[j++] = data[i];
|
||||||
|
else modified = true;
|
||||||
|
}
|
||||||
|
size = j;
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to retain all elements that were provided in the other collection
|
||||||
|
* This function might delegate to a more appropriate function if necessary
|
||||||
|
* @param c the elements that should be kept. If empty, BooleanArrayList#clear is called.
|
||||||
|
* @return true if the collection was modified
|
||||||
|
* @throws NullPointerException if the collection is null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean retainAll(Collection<?> c) {
|
||||||
|
if(c.isEmpty()) {
|
||||||
|
boolean modifed = size > 0;
|
||||||
|
clear();
|
||||||
|
return modifed;
|
||||||
|
}
|
||||||
|
boolean modified = false;
|
||||||
|
int j = 0;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(c.contains(Boolean.valueOf(data[i]))) data[j++] = data[i];
|
||||||
|
else modified = true;
|
||||||
|
}
|
||||||
|
size = j;
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A optimized List#removeIf(Predicate) that more quickly removes elements from the list then the ArrayList implementation
|
||||||
|
* @param filter the filter to remove elements
|
||||||
|
* @return true if the list was modified
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean removeIf(Predicate<? super Boolean> filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
boolean modified = false;
|
||||||
|
int j = 0;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(!filter.test(Boolean.valueOf(data[i]))) data[j++] = data[i];
|
||||||
|
else modified = true;
|
||||||
|
}
|
||||||
|
size = j;
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to remove all elements that were provided in the other collection
|
||||||
|
* @param c the elements that should be removed
|
||||||
|
* @return true if the collection was modified
|
||||||
|
* @throws NullPointerException if the collection is null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean removeAll(BooleanCollection c) {
|
||||||
|
if(c.isEmpty()) return false;
|
||||||
|
boolean modified = false;
|
||||||
|
int j = 0;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(!c.contains(data[i])) data[j++] = data[i];
|
||||||
|
else modified = true;
|
||||||
|
}
|
||||||
|
size = j;
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeAll(BooleanCollection c, BooleanConsumer r) {
|
||||||
|
if(c.isEmpty()) return false;
|
||||||
|
int j = 0;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(!c.contains(data[i])) data[j++] = data[i];
|
||||||
|
else r.accept(data[i]);
|
||||||
|
}
|
||||||
|
boolean modified = j != size;
|
||||||
|
size = j;
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to retain all elements that were provided in the other collection
|
||||||
|
* This function might delegate to a more appropriate function if necessary
|
||||||
|
* @param c the elements that should be kept. If empty, BooleanArrayList#clear is called.
|
||||||
|
* @return true if the collection was modified
|
||||||
|
* @throws NullPointerException if the collection is null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean retainAll(BooleanCollection c) {
|
||||||
|
if(c.isEmpty()) {
|
||||||
|
boolean modifed = size > 0;
|
||||||
|
clear();
|
||||||
|
return modifed;
|
||||||
|
}
|
||||||
|
boolean modified = false;
|
||||||
|
int j = 0;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(c.contains(data[i])) data[j++] = data[i];
|
||||||
|
else modified = true;
|
||||||
|
}
|
||||||
|
size = j;
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean retainAll(BooleanCollection c, BooleanConsumer r) {
|
||||||
|
if(c.isEmpty()) {
|
||||||
|
boolean modifed = size > 0;
|
||||||
|
forEach(r);
|
||||||
|
clear();
|
||||||
|
return modifed;
|
||||||
|
}
|
||||||
|
int j = 0;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(c.contains(data[i])) data[j++] = data[i];
|
||||||
|
else r.accept(data[i]);
|
||||||
|
}
|
||||||
|
boolean modified = j != size;
|
||||||
|
size = j;
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A toArray implementation that ensures the Array itself is a Object.
|
||||||
|
* @return a Array of the elements in the list
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public Object[] toArray() {
|
||||||
|
if(size == 0) return ObjectArrays.EMPTY_ARRAY;
|
||||||
|
Object[] obj = new Object[size];
|
||||||
|
for(int i = 0;i<size;i++)
|
||||||
|
obj[i] = Boolean.valueOf(data[i]);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A toArray implementation that ensures the Array itself is a Object.
|
||||||
|
* @param a original array. If null a Object array with the right size is created. If to small the Array of the same type is created with the right size
|
||||||
|
* @return a Array of the elements in the list
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public <E> E[] toArray(E[] a) {
|
||||||
|
if(a == null) a = (E[])new Object[size];
|
||||||
|
else if(a.length < size) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size);
|
||||||
|
for(int i = 0;i<size;i++)
|
||||||
|
a[i] = (E)Boolean.valueOf(data[i]);
|
||||||
|
if (a.length > size) a[size] = null;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] toBooleanArray(boolean[] a) {
|
||||||
|
if(a.length < size) a = new boolean[size];
|
||||||
|
System.arraycopy(data, 0, a, 0, size);
|
||||||
|
if (a.length > size) a[size] = false;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to return the size of the list
|
||||||
|
* @return the size of elements in the list
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to ensure the elements are within the requested size.
|
||||||
|
* If smaller then the stored elements they get removed as needed.
|
||||||
|
* If bigger it is ensured that enough room is provided depending on the implementation
|
||||||
|
* @param size the requested amount of elements/room for elements
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void size(int size) {
|
||||||
|
if(size > data.length)
|
||||||
|
data = Arrays.copyOf(data, size);
|
||||||
|
else if(size < size() && size >= 0)
|
||||||
|
Arrays.fill(data, size, size(), false);
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to clear all elements in the list.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trims the original collection down to the size of the current elements or the requested size depending which is bigger
|
||||||
|
* @param size the requested trim size.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean trim(int size) {
|
||||||
|
if(size > size() || size() == data.length) return false;
|
||||||
|
int value = Math.max(size, size());
|
||||||
|
data = value == 0 ? BooleanArrays.EMPTY_ARRAY : Arrays.copyOf(data, value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trims the collection down to the requested size and clears all elements while doing so
|
||||||
|
* @param size the amount of elements that should be allowed
|
||||||
|
* @note this will enforce minimum size of the collection itself
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void clearAndTrim(int size) {
|
||||||
|
if(data.length <= size) {
|
||||||
|
clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
data = size == 0 ? BooleanArrays.EMPTY_ARRAY : new boolean[size];
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Increases the capacity of this implementation instance, if necessary,
|
||||||
|
* to ensure that it can hold at least the number of elements specified by
|
||||||
|
* the minimum capacity argument.
|
||||||
|
*
|
||||||
|
* @param size the desired minimum capacity
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void ensureCapacity(int size) {
|
||||||
|
grow(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanArrayList copy() {
|
||||||
|
BooleanArrayList list = new BooleanArrayList();
|
||||||
|
list.data = Arrays.copyOf(data, data.length);
|
||||||
|
list.size = size;
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void grow(int capacity) {
|
||||||
|
if(capacity <= data.length) return;
|
||||||
|
data = Arrays.copyOf(data, data == BooleanArrays.EMPTY_ARRAY ? Math.max(DEFAULT_ARRAY_SIZE, capacity) : (int)Math.max(Math.min((long)data.length + (data.length >> 1), SanityChecks.MAX_ARRAY_SIZE), capacity));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void checkRange(int index) {
|
||||||
|
if (index < 0 || index >= size)
|
||||||
|
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void checkAddRange(int index) {
|
||||||
|
if (index < 0 || index > size)
|
||||||
|
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Type Splititerator to reduce boxing/unboxing
|
||||||
|
* @return type specific splititerator
|
||||||
|
* @note characteristics are ordered, sized, subsized
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public BooleanSplititerator spliterator() { return BooleanSplititerators.createArraySplititerator(data, size, 16464); }
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,455 @@
|
|||||||
|
package speiger.src.collections.booleans.lists;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Comparator;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanCollection;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanSplititerator;
|
||||||
|
import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanComparator;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanArrays;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanLists;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanSplititerators;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific List interface that reduces boxing/unboxing and adds a couple extra quality of life features
|
||||||
|
*/
|
||||||
|
public interface BooleanList extends BooleanCollection, List<Boolean>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type-Specific add Function to reduce (un)boxing
|
||||||
|
* @param e the element to add
|
||||||
|
* @return true if the list was modified
|
||||||
|
* @see List#add(Object)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean add(boolean e);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific add Function to reduce (un)boxing
|
||||||
|
* @param e the element to add
|
||||||
|
* @param index index at which the specified element is to be inserted
|
||||||
|
* @see List#add(int, Object)
|
||||||
|
*/
|
||||||
|
public void add(int index, boolean e);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that will only add elements if it is not present.
|
||||||
|
* @param e the element to add
|
||||||
|
* @return true if the list was modified
|
||||||
|
*/
|
||||||
|
public default boolean addIfAbsent(boolean e) {
|
||||||
|
if(indexOf(e) == -1) return add(e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that will only add elements if it is present.
|
||||||
|
* @param e the element to add
|
||||||
|
* @return true if the list was modified
|
||||||
|
*/
|
||||||
|
public default boolean addIfPresent(boolean e) {
|
||||||
|
if(indexOf(e) != -1) return add(e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific addAll Function to reduce (un)boxing
|
||||||
|
* @param c the elements that need to be added
|
||||||
|
* @param index index at which the specified elements is to be inserted
|
||||||
|
* @return true if the list was modified
|
||||||
|
* @see java.util.List#addAll(int, java.util.Collection)
|
||||||
|
*/
|
||||||
|
public boolean addAll(int index, BooleanCollection c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific and optimized addAll function that allows a faster transfer of elements
|
||||||
|
* @param c the elements that need to be added
|
||||||
|
* @return true if the list was modified
|
||||||
|
*/
|
||||||
|
public boolean addAll(BooleanList c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific and optimized addAll function that allows a faster transfer of elements
|
||||||
|
* @param c the elements that need to be added
|
||||||
|
* @param index index at which the specified elements is to be inserted
|
||||||
|
* @return true if the list was modified
|
||||||
|
*/
|
||||||
|
public boolean addAll(int index, BooleanList c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method that returns the first element of a List.
|
||||||
|
* This function was introduced due to how annoying it is to get/remove the last element of a list.
|
||||||
|
* This simplifies this process a bit.
|
||||||
|
* @return first element of the list
|
||||||
|
*/
|
||||||
|
public default boolean getFirstBoolean() {
|
||||||
|
return getBoolean(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method that returns the last element of a List.
|
||||||
|
* This function was introduced due to how annoying it is to get/remove the last element of a list.
|
||||||
|
* This simplifies this process a bit.
|
||||||
|
* @return last element of the list
|
||||||
|
*/
|
||||||
|
public default boolean getLastBoolean() {
|
||||||
|
return getBoolean(size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method that removes and returns the first element of a List.
|
||||||
|
* This function was introduced due to how annoying it is to get/remove the last element of a list.
|
||||||
|
* This simplifies this process a bit.
|
||||||
|
* @return first element of the list and removes it
|
||||||
|
*/
|
||||||
|
public default boolean removeFirstBoolean() {
|
||||||
|
return removeBoolean(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper method that removes and returns the last element of a List.
|
||||||
|
* This function was introduced due to how annoying it is to get/remove the last element of a list.
|
||||||
|
* This simplifies this process a bit.
|
||||||
|
* @return last element of the list and removes it
|
||||||
|
*/
|
||||||
|
public default boolean removeLastBoolean() {
|
||||||
|
return removeBoolean(size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific get function to reduce (un)boxing
|
||||||
|
* @param index the index of the value that is requested
|
||||||
|
* @return the value at the given index
|
||||||
|
* @throws IndexOutOfBoundsException if the index is not within the list range
|
||||||
|
* @see List#get(int)
|
||||||
|
*/
|
||||||
|
public boolean getBoolean(int index);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific set function to reduce (un)boxing
|
||||||
|
* @param index index of the element to replace
|
||||||
|
* @param e element to be stored at the specified position
|
||||||
|
* @return the element previously at the specified position
|
||||||
|
* @throws IndexOutOfBoundsException if the index is not within the list range
|
||||||
|
* @see List#set(int, Object)
|
||||||
|
*/
|
||||||
|
public boolean set(int index, boolean e);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific remove function to reduce (un)boxing
|
||||||
|
* @param index the index of the element to be removed
|
||||||
|
* @return the element previously at the specified position
|
||||||
|
* @see List#remove(int)
|
||||||
|
*/
|
||||||
|
public boolean removeBoolean(int index);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific indexOf function to reduce (un)boxing
|
||||||
|
* @param e the element that is searched for
|
||||||
|
* @return the index of the element if found. (if not found then -1)
|
||||||
|
* @note does not support null values
|
||||||
|
*/
|
||||||
|
public int indexOf(boolean e);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific lastIndexOf function to reduce (un)boxing
|
||||||
|
* @param e the element that is searched for
|
||||||
|
* @return the lastIndex of the element if found. (if not found then -1)
|
||||||
|
* @note does not support null values
|
||||||
|
*/
|
||||||
|
public int lastIndexOf(boolean e);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to fast add elements to the list
|
||||||
|
* @param a the elements that should be added
|
||||||
|
* @throws IndexOutOfBoundsException if from is outside of the lists range
|
||||||
|
*/
|
||||||
|
public default void addElements(boolean... a) { addElements(size(), a, 0, a.length); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to fast add elements to the list
|
||||||
|
* @param from the index where the elements should be added into the list
|
||||||
|
* @param a the elements that should be added
|
||||||
|
* @throws IndexOutOfBoundsException if from is outside of the lists range
|
||||||
|
*/
|
||||||
|
public default void addElements(int from, boolean... a) { addElements(from, a, 0, a.length); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to fast add elements to the list
|
||||||
|
* @param from the index where the elements should be added into the list
|
||||||
|
* @param a the elements that should be added
|
||||||
|
* @param offset the start index of the array should be read from
|
||||||
|
* @param length how many elements should be read from
|
||||||
|
* @throws IndexOutOfBoundsException if from is outside of the lists range
|
||||||
|
*/
|
||||||
|
public void addElements(int from, boolean[] a, int offset, int length);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to fast fetch elements from the list
|
||||||
|
* @param from index where the list should be fetching elements from
|
||||||
|
* @param a the array where the values should be inserted to
|
||||||
|
* @return the inputArray
|
||||||
|
* @throws NullPointerException if the array is null
|
||||||
|
* @throws IndexOutOfBoundsException if from is outside of the lists range
|
||||||
|
* @throws IllegalStateException if offset or length are smaller then 0 or exceed the array length
|
||||||
|
*/
|
||||||
|
public default boolean[] getElements(int from, boolean[] a) { return getElements(from, a, 0, a.length); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to fast fetch elements from the list
|
||||||
|
* @param from index where the list should be fetching elements from
|
||||||
|
* @param a the array where the values should be inserted to
|
||||||
|
* @param offset the startIndex of where the array should be written to
|
||||||
|
* @param length the number of elements the values should be fetched from
|
||||||
|
* @return the inputArray
|
||||||
|
* @throws NullPointerException if the array is null
|
||||||
|
* @throws IndexOutOfBoundsException if from is outside of the lists range
|
||||||
|
* @throws IllegalStateException if offset or length are smaller then 0 or exceed the array length
|
||||||
|
*/
|
||||||
|
public boolean[] getElements(int from, boolean[] a, int offset, int length);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* a function to fast remove elements from the list.
|
||||||
|
* @param from the start index of where the elements should be removed from (inclusive)
|
||||||
|
* @param to the end index of where the elements should be removed to (exclusive)
|
||||||
|
*/
|
||||||
|
public void removeElements(int from, int to);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Highly Optimized remove function that removes the desired element.
|
||||||
|
* But instead of shifting the elements to the left it moves the last element to the removed space.
|
||||||
|
* @param index the index of the element to be removed
|
||||||
|
* @return the element previously at the specified position
|
||||||
|
*/
|
||||||
|
public boolean swapRemove(int index);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Highly Optimized remove function that removes the desired element.
|
||||||
|
* But instead of shifting the elements to the left it moves the last element to the removed space.
|
||||||
|
* @param e the element that should be removed
|
||||||
|
* @return true if the element was removed
|
||||||
|
*/
|
||||||
|
public boolean swapRemoveBoolean(boolean e);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to fast extract elements out of the list, this removes the elements that were fetched.
|
||||||
|
* @param from the start index of where the elements should be fetched from (inclusive)
|
||||||
|
* @param to the end index of where the elements should be fetched to (exclusive)
|
||||||
|
* @return a array of the elements that were fetched
|
||||||
|
*/
|
||||||
|
public boolean[] extractElements(int from, int to);
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default void sort(Comparator<? super Boolean> c) {
|
||||||
|
sort((K, V) -> c.compare(Boolean.valueOf(K), Boolean.valueOf(V)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts the elements specified by the Natural order either by using the Comparator or the elements
|
||||||
|
* @param c the sorter of the elements, can be null
|
||||||
|
* @see java.util.List#sort(Comparator)
|
||||||
|
* @see BooleanArrays#stableSort(boolean[], BooleanComparator)
|
||||||
|
*/
|
||||||
|
public default void sort(BooleanComparator c) {
|
||||||
|
boolean[] array = toBooleanArray();
|
||||||
|
if(c != null) BooleanArrays.stableSort(array, c);
|
||||||
|
else BooleanArrays.stableSort(array);
|
||||||
|
BooleanListIterator iter = listIterator();
|
||||||
|
for (int i = 0,m=size();i<m && iter.hasNext();i++) {
|
||||||
|
iter.nextBoolean();
|
||||||
|
iter.set(array[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @param c the sorter of the elements, can be null
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public default void unstableSort(Comparator<? super Boolean> c) {
|
||||||
|
unstableSort((K, V) -> c.compare(Boolean.valueOf(K), Boolean.valueOf(V)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts the elements specified by the Natural order either by using the Comparator or the elements using a unstable sort
|
||||||
|
* @param c the sorter of the elements, can be null
|
||||||
|
* @see java.util.List#sort(Comparator)
|
||||||
|
* @see BooleanArrays#unstableSort(boolean[], BooleanComparator)
|
||||||
|
*/
|
||||||
|
public default void unstableSort(BooleanComparator c) {
|
||||||
|
boolean[] array = toBooleanArray();
|
||||||
|
if(c != null) BooleanArrays.unstableSort(array, c);
|
||||||
|
else BooleanArrays.unstableSort(array);
|
||||||
|
BooleanListIterator iter = listIterator();
|
||||||
|
for (int i = 0,m=size();i<m && iter.hasNext();i++) {
|
||||||
|
iter.nextBoolean();
|
||||||
|
iter.set(array[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Indexed forEach implementation that allows you to keep track of how many elements were already iterated over.
|
||||||
|
* @param action The action to be performed for each element
|
||||||
|
* @throws java.lang.NullPointerException if the specified action is null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public default void forEachIndexed(IntBooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
for(int i = 0,m=size();i<m;action.accept(i, getBoolean(i++)));
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* A Type-Specific Iterator of listIterator
|
||||||
|
* @see java.util.List#listIterator
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public BooleanListIterator listIterator();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific Iterator of listIterator
|
||||||
|
* @see java.util.List#listIterator(int)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public BooleanListIterator listIterator(int index);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific List of subList
|
||||||
|
* @see java.util.List#subList(int, int)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public BooleanList subList(int from, int to);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific List Helper that shows all elements in reverse.
|
||||||
|
* @return a list wrapper that has all elements reversed!
|
||||||
|
*/
|
||||||
|
public BooleanList reversed();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Wrapped List that is Synchronized
|
||||||
|
* @return a new List that is synchronized
|
||||||
|
* @see BooleanLists#synchronize
|
||||||
|
*/
|
||||||
|
public default BooleanList synchronize() { return BooleanLists.synchronize(this); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Wrapped List that is Synchronized
|
||||||
|
* @param mutex is the controller of the synchronization block
|
||||||
|
* @return a new List Wrapper that is synchronized
|
||||||
|
* @see BooleanLists#synchronize
|
||||||
|
*/
|
||||||
|
public default BooleanList synchronize(Object mutex) { return BooleanLists.synchronize(this, mutex); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Wrapped List that is unmodifiable
|
||||||
|
* @return a new List Wrapper that is unmodifiable
|
||||||
|
* @see BooleanLists#unmodifiable
|
||||||
|
*/
|
||||||
|
public default BooleanList unmodifiable() { return BooleanLists.unmodifiable(this); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to ensure the elements are within the requested size.
|
||||||
|
* If smaller then the stored elements they get removed as needed.
|
||||||
|
* If bigger it is ensured that enough room is provided depending on the implementation
|
||||||
|
* @param size the requested amount of elements/room for elements
|
||||||
|
*/
|
||||||
|
public void size(int size);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanList copy();
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default boolean add(Boolean e) {
|
||||||
|
return BooleanCollection.super.add(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default Boolean get(int index) {
|
||||||
|
return Boolean.valueOf(getBoolean(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default Boolean set(int index, Boolean e) {
|
||||||
|
return Boolean.valueOf(set(index, e.booleanValue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default int indexOf(Object o) {
|
||||||
|
return indexOf(((Boolean)o).booleanValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default int lastIndexOf(Object o) {
|
||||||
|
return lastIndexOf(((Boolean)o).booleanValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default boolean contains(Object o) {
|
||||||
|
return BooleanCollection.super.contains(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default boolean remove(Object o) {
|
||||||
|
return BooleanCollection.super.remove(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default Boolean remove(int index) {
|
||||||
|
return Boolean.valueOf(removeBoolean(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Type Splititerator to reduce boxing/unboxing
|
||||||
|
* @return type specific splititerator
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
default BooleanSplititerator spliterator() { return BooleanSplititerators.createSplititerator(this, 0); }
|
||||||
|
}
|
||||||
@ -0,0 +1,65 @@
|
|||||||
|
package speiger.src.collections.booleans.lists;
|
||||||
|
|
||||||
|
import java.util.ListIterator;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanBidirectionalIterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific ListIterator that reduces boxing/unboxing
|
||||||
|
*/
|
||||||
|
public interface BooleanListIterator extends ListIterator<Boolean>, BooleanBidirectionalIterator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Primitive set function to reduce (un)boxing
|
||||||
|
* @param e the element that should replace the last returned value
|
||||||
|
* @see ListIterator#set(Object)
|
||||||
|
*/
|
||||||
|
public void set(boolean e);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Primitive set function to reduce (un)boxing
|
||||||
|
* @param e the element that should be inserted before the last returned value
|
||||||
|
* @see ListIterator#set(Object)
|
||||||
|
*/
|
||||||
|
public void add(boolean e);
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default Boolean previous() {
|
||||||
|
return BooleanBidirectionalIterator.super.previous();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default Boolean next() {
|
||||||
|
return BooleanBidirectionalIterator.super.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default void set(Boolean e) {
|
||||||
|
set(e.booleanValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default void add(Boolean e) {
|
||||||
|
add(e.booleanValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,482 @@
|
|||||||
|
package speiger.src.collections.booleans.lists;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanCollection;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanComparator;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanConsumer;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanArrays;
|
||||||
|
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanPredicate;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanBooleanUnaryOperator;
|
||||||
|
import speiger.src.collections.objects.utils.ObjectArrays;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanIterators;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanSplititerator;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanSplititerators;
|
||||||
|
import speiger.src.collections.utils.SanityChecks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific Immutable implementation of list that is written to reduce (un)boxing
|
||||||
|
*/
|
||||||
|
public class ImmutableBooleanList extends AbstractBooleanList
|
||||||
|
{
|
||||||
|
/** The backing array */
|
||||||
|
protected transient boolean[] data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new Immutable copy of the contents of the Collection.
|
||||||
|
* @param c the elements that should be added into the list
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public ImmutableBooleanList(Collection<? extends Boolean> c) {
|
||||||
|
data = BooleanArrays.pour(BooleanIterators.wrap(c.iterator()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new Immutable copy of the contents of the Collection.
|
||||||
|
* @param c the elements that should be added into the list
|
||||||
|
*/
|
||||||
|
public ImmutableBooleanList(BooleanCollection c) {
|
||||||
|
data = BooleanArrays.pour(c.iterator());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new Immutable copy of the contents of the List.
|
||||||
|
* @param l the elements that should be added into the list
|
||||||
|
*/
|
||||||
|
public ImmutableBooleanList(BooleanList l) {
|
||||||
|
boolean[] temp = new boolean[l.size()];
|
||||||
|
l.getElements(0, temp, 0, l.size());
|
||||||
|
data = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new Immutable copy of the contents of the Array.
|
||||||
|
* @param a the array that should be copied
|
||||||
|
*/
|
||||||
|
public ImmutableBooleanList(boolean... a) {
|
||||||
|
this(a, 0, a.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new ImmutableList copy of the array with a custom length
|
||||||
|
* @param a the array that should be copied
|
||||||
|
* @param length the desired length that should be copied
|
||||||
|
*/
|
||||||
|
public ImmutableBooleanList(boolean[] a, int length) {
|
||||||
|
this(a, 0, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new ImmutableList copy of the array with in the custom range.
|
||||||
|
* @param a the array that should be copied
|
||||||
|
* @param offset the starting offset of where the array should be copied from
|
||||||
|
* @param length the desired length that should be copied
|
||||||
|
* @throws IllegalStateException if offset is smaller then 0
|
||||||
|
* @throws IllegalStateException if the offset + length exceeds the array length
|
||||||
|
*/
|
||||||
|
public ImmutableBooleanList(boolean[] a, int offset, int length) {
|
||||||
|
SanityChecks.checkArrayCapacity(a.length, offset, length);
|
||||||
|
data = Arrays.copyOfRange(a, offset, offset+length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean add(boolean e) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public void add(int index, boolean e) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean addAll(int index, Collection<? extends Boolean> c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, BooleanCollection c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, BooleanList c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean addAll(boolean[] e, int offset, int length) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public void addElements(int from, boolean[] a, int offset, int length) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] getElements(int from, boolean[] a, int offset, int length) {
|
||||||
|
SanityChecks.checkArrayCapacity(a.length, offset, length);
|
||||||
|
SanityChecks.checkArrayCapacity(data.length, from, length);
|
||||||
|
System.arraycopy(data, from, a, offset, length);
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeElements(int from, int to) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean[] extractElements(int from, int to) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to find if the Element is present in this list.
|
||||||
|
* @param o the element that is searched for
|
||||||
|
* @return if the element was found.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean contains(Object o) {
|
||||||
|
return indexOf(o) != -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to find the index of a given element
|
||||||
|
* @param o the element that is searched for
|
||||||
|
* @return the index of the element if found. (if not found then -1)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public int indexOf(Object o) {
|
||||||
|
if(o == null) return -1;
|
||||||
|
for(int i = 0,m=data.length;i<m;i++) {
|
||||||
|
if(Objects.equals(o, Boolean.valueOf(data[i]))) return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to find the last index of a given element
|
||||||
|
* @param o the element that is searched for
|
||||||
|
* @return the last index of the element if found. (if not found then -1)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public int lastIndexOf(Object o) {
|
||||||
|
if(o == null) return -1;
|
||||||
|
for(int i = data.length - 1;i>=0;i--) {
|
||||||
|
if(Objects.equals(o, Boolean.valueOf(data[i]))) return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific implementation of the Collection#contains function.
|
||||||
|
* @param e the element that is searched for.
|
||||||
|
* @return if the element was found
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean contains(boolean e) {
|
||||||
|
return indexOf(e) != -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific function to find the index of a given element
|
||||||
|
* @param e the element that is searched for
|
||||||
|
* @return the index of the element if found. (if not found then -1)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int indexOf(boolean e) {
|
||||||
|
for(int i = 0,m=data.length;i<m;i++) {
|
||||||
|
if(data[i] == e) return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific function to find the last index of a given element
|
||||||
|
* @param e the element that is searched for
|
||||||
|
* @return the last index of the element if found. (if not found then -1)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int lastIndexOf(boolean e) {
|
||||||
|
for(int i = data.length - 1;i>=0;i--) {
|
||||||
|
if(data[i] == e) return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sort(BooleanComparator c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public void unstableSort(BooleanComparator c) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific get function to reduce (un)boxing
|
||||||
|
* @param index the index of the element to fetch
|
||||||
|
* @return the value of the requested index
|
||||||
|
* @throws IndexOutOfBoundsException if the index is out of range
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean getBoolean(int index) {
|
||||||
|
checkRange(index);
|
||||||
|
return data[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ImmutableBooleanList copy() {
|
||||||
|
return new ImmutableBooleanList(Arrays.copyOf(data, data.length));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific foreach function that reduces (un)boxing
|
||||||
|
*
|
||||||
|
* @implSpec
|
||||||
|
* <p>The default implementation behaves as if:
|
||||||
|
* <pre>{@code
|
||||||
|
* for(int i = 0;i<size;i++)
|
||||||
|
* action.accept(data[i]);
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param action The action to be performed for each element
|
||||||
|
* @throws NullPointerException if the specified action is null
|
||||||
|
* @see Iterable#forEach(java.util.function.Consumer)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
for(int i = 0,m=data.length;i<m;i++)
|
||||||
|
action.accept(data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E> void forEach(E input, ObjectBooleanConsumer<E> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
for(int i = 0,m=data.length;i<m;i++)
|
||||||
|
action.accept(input, data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matchesAny(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0,m=data.length;i<m;i++) {
|
||||||
|
if(filter.test(data[i])) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matchesNone(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0,m=data.length;i<m;i++) {
|
||||||
|
if(filter.test(data[i])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matchesAll(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0,m=data.length;i<m;i++) {
|
||||||
|
if(!filter.test(data[i])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean findFirst(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0,m=data.length;i<m;i++) {
|
||||||
|
if(filter.test(data[i])) return data[i];
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean reduce(boolean identity, BooleanBooleanUnaryOperator operator) {
|
||||||
|
Objects.requireNonNull(operator);
|
||||||
|
boolean state = identity;
|
||||||
|
for(int i = 0,m=data.length;i<m;i++) {
|
||||||
|
state = operator.applyAsBoolean(state, data[i]);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean reduce(BooleanBooleanUnaryOperator operator) {
|
||||||
|
Objects.requireNonNull(operator);
|
||||||
|
boolean state = false;
|
||||||
|
boolean empty = true;
|
||||||
|
for(int i = 0,m=data.length;i<m;i++) {
|
||||||
|
if(empty) {
|
||||||
|
empty = false;
|
||||||
|
state = data[i];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
state = operator.applyAsBoolean(state, data[i]);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int count(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
int result = 0;
|
||||||
|
for(int i = 0,m=data.length;i<m;i++) {
|
||||||
|
if(filter.test(data[i])) result++;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanListIterator listIterator(int index) {
|
||||||
|
if(index < 0 || index > size()) throw new IndexOutOfBoundsException();
|
||||||
|
return new BooleanListIter(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean set(int index, boolean e) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public void replaceAll(UnaryOperator<Boolean> o) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean removeBoolean(int index) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean swapRemove(int index) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean remBoolean(boolean type) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean removeAll(Collection<?> c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean retainAll(Collection<?> c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean removeIf(Predicate<? super Boolean> filter) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean removeAll(BooleanCollection c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean retainAll(BooleanCollection c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean removeAll(BooleanCollection c, BooleanConsumer r) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean retainAll(BooleanCollection c, BooleanConsumer r) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A toArray implementation that ensures the Array itself is a Object.
|
||||||
|
* @return a Array of the elements in the list
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public Object[] toArray() {
|
||||||
|
if(data.length == 0) return ObjectArrays.EMPTY_ARRAY;
|
||||||
|
Object[] obj = new Object[data.length];
|
||||||
|
for(int i = 0,m=data.length;i<m;i++)
|
||||||
|
obj[i] = Boolean.valueOf(data[i]);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A toArray implementation that ensures the Array itself is a Object.
|
||||||
|
* @param a original array. If null a Object array with the right size is created. If to small the Array of the same type is created with the right size
|
||||||
|
* @return a Array of the elements in the list
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public <E> E[] toArray(E[] a) {
|
||||||
|
if(a == null) a = (E[])new Object[data.length];
|
||||||
|
else if(a.length < data.length) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), data.length);
|
||||||
|
for(int i = 0,m=data.length;i<m;i++)
|
||||||
|
a[i] = (E)Boolean.valueOf(data[i]);
|
||||||
|
if (a.length > data.length) a[data.length] = null;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] toBooleanArray(boolean[] a) {
|
||||||
|
if(a.length < data.length) a = new boolean[data.length];
|
||||||
|
System.arraycopy(data, 0, a, 0, data.length);
|
||||||
|
if (a.length > data.length) a[data.length] = false;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function to return the size of the list
|
||||||
|
* @return the size of elements in the list
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return data.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void size(int size) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public void clear() { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
protected void checkRange(int index) {
|
||||||
|
if (index < 0 || index >= data.length)
|
||||||
|
throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + data.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Type Splititerator to reduce boxing/unboxing
|
||||||
|
* @return type specific splititerator
|
||||||
|
* @note characteristics are ordered, sized, subsized
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public BooleanSplititerator spliterator() { return BooleanSplititerators.createArraySplititerator(data, data.length, 16464); }
|
||||||
|
|
||||||
|
private class BooleanListIter implements BooleanListIterator {
|
||||||
|
int index;
|
||||||
|
|
||||||
|
BooleanListIter(int index) {
|
||||||
|
this.index = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return index < size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean nextBoolean() {
|
||||||
|
if(!hasNext()) throw new NoSuchElementException();
|
||||||
|
return getBoolean(index++);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasPrevious() {
|
||||||
|
return index > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean previousBoolean() {
|
||||||
|
if(!hasPrevious()) throw new NoSuchElementException();
|
||||||
|
return getBoolean(--index);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int nextIndex() {
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int previousIndex() {
|
||||||
|
return index-1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove() { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(boolean e) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(boolean e) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int skip(int amount) {
|
||||||
|
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
|
||||||
|
int steps = Math.min(amount, size() - index);
|
||||||
|
index += steps;
|
||||||
|
return steps;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int back(int amount) {
|
||||||
|
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
|
||||||
|
int steps = Math.min(amount, index);
|
||||||
|
index -= steps;
|
||||||
|
return steps;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,132 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanBooleanImmutablePair;
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanBooleanMutablePair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key Value Pair Interface that allows to reduce boxing/unboxing.
|
||||||
|
*/
|
||||||
|
public interface BooleanBooleanPair
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Empty Reference for Immutable Pairs
|
||||||
|
*/
|
||||||
|
public static final BooleanBooleanPair EMPTY = new BooleanBooleanImmutablePair();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return empty Immutable Pair
|
||||||
|
*/
|
||||||
|
public static BooleanBooleanPair of() {
|
||||||
|
return EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @return Immutable Pair of Key
|
||||||
|
*/
|
||||||
|
public static BooleanBooleanPair ofKey(boolean key) {
|
||||||
|
return new BooleanBooleanImmutablePair(key, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Immutable Pair of Value
|
||||||
|
*/
|
||||||
|
public static BooleanBooleanPair ofValue(boolean value) {
|
||||||
|
return new BooleanBooleanImmutablePair(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Immutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static BooleanBooleanPair of(boolean key, boolean value) {
|
||||||
|
return new BooleanBooleanImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be immutably copied
|
||||||
|
* @return a Immutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static BooleanBooleanPair of(BooleanBooleanPair pair) {
|
||||||
|
return new BooleanBooleanImmutablePair(pair.getBooleanKey(), pair.getBooleanValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return empty Mutable Pair
|
||||||
|
*/
|
||||||
|
public static BooleanBooleanPair mutable() {
|
||||||
|
return new BooleanBooleanMutablePair();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @return Mutable Pair of key
|
||||||
|
*/
|
||||||
|
public static BooleanBooleanPair mutableKey(boolean key) {
|
||||||
|
return new BooleanBooleanMutablePair(key, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Mutable Pair of value
|
||||||
|
*/
|
||||||
|
public static BooleanBooleanPair mutableValue(boolean value) {
|
||||||
|
return new BooleanBooleanMutablePair(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Mutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static BooleanBooleanPair mutable(boolean key, boolean value) {
|
||||||
|
return new BooleanBooleanMutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be copied
|
||||||
|
* @return a Mutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static BooleanBooleanPair mutable(BooleanBooleanPair pair) {
|
||||||
|
return new BooleanBooleanMutablePair(pair.getBooleanKey(), pair.getBooleanValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Key of the Pair.
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key.
|
||||||
|
*/
|
||||||
|
public BooleanBooleanPair setBooleanKey(boolean key);
|
||||||
|
/**
|
||||||
|
* @return the Key of the Pair
|
||||||
|
*/
|
||||||
|
public boolean getBooleanKey();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Value of the Pair.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new value.
|
||||||
|
*/
|
||||||
|
public BooleanBooleanPair setBooleanValue(boolean value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the Value of the Pair
|
||||||
|
*/
|
||||||
|
public boolean getBooleanValue();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets key and value of the Pair
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key and value.
|
||||||
|
*/
|
||||||
|
public BooleanBooleanPair set(boolean key, boolean value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clones the Pair if it is mutable.
|
||||||
|
* @return a New Mutable Instance if it is mutable
|
||||||
|
*/
|
||||||
|
public BooleanBooleanPair shallowCopy();
|
||||||
|
}
|
||||||
@ -0,0 +1,132 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanByteImmutablePair;
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanByteMutablePair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key Value Pair Interface that allows to reduce boxing/unboxing.
|
||||||
|
*/
|
||||||
|
public interface BooleanBytePair
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Empty Reference for Immutable Pairs
|
||||||
|
*/
|
||||||
|
public static final BooleanBytePair EMPTY = new BooleanByteImmutablePair();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return empty Immutable Pair
|
||||||
|
*/
|
||||||
|
public static BooleanBytePair of() {
|
||||||
|
return EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @return Immutable Pair of Key
|
||||||
|
*/
|
||||||
|
public static BooleanBytePair ofKey(boolean key) {
|
||||||
|
return new BooleanByteImmutablePair(key, (byte)0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Immutable Pair of Value
|
||||||
|
*/
|
||||||
|
public static BooleanBytePair ofValue(byte value) {
|
||||||
|
return new BooleanByteImmutablePair(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Immutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static BooleanBytePair of(boolean key, byte value) {
|
||||||
|
return new BooleanByteImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be immutably copied
|
||||||
|
* @return a Immutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static BooleanBytePair of(BooleanBytePair pair) {
|
||||||
|
return new BooleanByteImmutablePair(pair.getBooleanKey(), pair.getByteValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return empty Mutable Pair
|
||||||
|
*/
|
||||||
|
public static BooleanBytePair mutable() {
|
||||||
|
return new BooleanByteMutablePair();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @return Mutable Pair of key
|
||||||
|
*/
|
||||||
|
public static BooleanBytePair mutableKey(boolean key) {
|
||||||
|
return new BooleanByteMutablePair(key, (byte)0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Mutable Pair of value
|
||||||
|
*/
|
||||||
|
public static BooleanBytePair mutableValue(byte value) {
|
||||||
|
return new BooleanByteMutablePair(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Mutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static BooleanBytePair mutable(boolean key, byte value) {
|
||||||
|
return new BooleanByteMutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be copied
|
||||||
|
* @return a Mutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static BooleanBytePair mutable(BooleanBytePair pair) {
|
||||||
|
return new BooleanByteMutablePair(pair.getBooleanKey(), pair.getByteValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Key of the Pair.
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key.
|
||||||
|
*/
|
||||||
|
public BooleanBytePair setBooleanKey(boolean key);
|
||||||
|
/**
|
||||||
|
* @return the Key of the Pair
|
||||||
|
*/
|
||||||
|
public boolean getBooleanKey();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Value of the Pair.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new value.
|
||||||
|
*/
|
||||||
|
public BooleanBytePair setByteValue(byte value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the Value of the Pair
|
||||||
|
*/
|
||||||
|
public byte getByteValue();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets key and value of the Pair
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key and value.
|
||||||
|
*/
|
||||||
|
public BooleanBytePair set(boolean key, byte value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clones the Pair if it is mutable.
|
||||||
|
* @return a New Mutable Instance if it is mutable
|
||||||
|
*/
|
||||||
|
public BooleanBytePair shallowCopy();
|
||||||
|
}
|
||||||
@ -0,0 +1,132 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanCharImmutablePair;
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanCharMutablePair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key Value Pair Interface that allows to reduce boxing/unboxing.
|
||||||
|
*/
|
||||||
|
public interface BooleanCharPair
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Empty Reference for Immutable Pairs
|
||||||
|
*/
|
||||||
|
public static final BooleanCharPair EMPTY = new BooleanCharImmutablePair();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return empty Immutable Pair
|
||||||
|
*/
|
||||||
|
public static BooleanCharPair of() {
|
||||||
|
return EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @return Immutable Pair of Key
|
||||||
|
*/
|
||||||
|
public static BooleanCharPair ofKey(boolean key) {
|
||||||
|
return new BooleanCharImmutablePair(key, (char)0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Immutable Pair of Value
|
||||||
|
*/
|
||||||
|
public static BooleanCharPair ofValue(char value) {
|
||||||
|
return new BooleanCharImmutablePair(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Immutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static BooleanCharPair of(boolean key, char value) {
|
||||||
|
return new BooleanCharImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be immutably copied
|
||||||
|
* @return a Immutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static BooleanCharPair of(BooleanCharPair pair) {
|
||||||
|
return new BooleanCharImmutablePair(pair.getBooleanKey(), pair.getCharValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return empty Mutable Pair
|
||||||
|
*/
|
||||||
|
public static BooleanCharPair mutable() {
|
||||||
|
return new BooleanCharMutablePair();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @return Mutable Pair of key
|
||||||
|
*/
|
||||||
|
public static BooleanCharPair mutableKey(boolean key) {
|
||||||
|
return new BooleanCharMutablePair(key, (char)0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Mutable Pair of value
|
||||||
|
*/
|
||||||
|
public static BooleanCharPair mutableValue(char value) {
|
||||||
|
return new BooleanCharMutablePair(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Mutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static BooleanCharPair mutable(boolean key, char value) {
|
||||||
|
return new BooleanCharMutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be copied
|
||||||
|
* @return a Mutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static BooleanCharPair mutable(BooleanCharPair pair) {
|
||||||
|
return new BooleanCharMutablePair(pair.getBooleanKey(), pair.getCharValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Key of the Pair.
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key.
|
||||||
|
*/
|
||||||
|
public BooleanCharPair setBooleanKey(boolean key);
|
||||||
|
/**
|
||||||
|
* @return the Key of the Pair
|
||||||
|
*/
|
||||||
|
public boolean getBooleanKey();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Value of the Pair.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new value.
|
||||||
|
*/
|
||||||
|
public BooleanCharPair setCharValue(char value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the Value of the Pair
|
||||||
|
*/
|
||||||
|
public char getCharValue();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets key and value of the Pair
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key and value.
|
||||||
|
*/
|
||||||
|
public BooleanCharPair set(boolean key, char value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clones the Pair if it is mutable.
|
||||||
|
* @return a New Mutable Instance if it is mutable
|
||||||
|
*/
|
||||||
|
public BooleanCharPair shallowCopy();
|
||||||
|
}
|
||||||
@ -0,0 +1,132 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanDoubleImmutablePair;
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanDoubleMutablePair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key Value Pair Interface that allows to reduce boxing/unboxing.
|
||||||
|
*/
|
||||||
|
public interface BooleanDoublePair
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Empty Reference for Immutable Pairs
|
||||||
|
*/
|
||||||
|
public static final BooleanDoublePair EMPTY = new BooleanDoubleImmutablePair();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return empty Immutable Pair
|
||||||
|
*/
|
||||||
|
public static BooleanDoublePair of() {
|
||||||
|
return EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @return Immutable Pair of Key
|
||||||
|
*/
|
||||||
|
public static BooleanDoublePair ofKey(boolean key) {
|
||||||
|
return new BooleanDoubleImmutablePair(key, 0D);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Immutable Pair of Value
|
||||||
|
*/
|
||||||
|
public static BooleanDoublePair ofValue(double value) {
|
||||||
|
return new BooleanDoubleImmutablePair(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Immutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static BooleanDoublePair of(boolean key, double value) {
|
||||||
|
return new BooleanDoubleImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be immutably copied
|
||||||
|
* @return a Immutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static BooleanDoublePair of(BooleanDoublePair pair) {
|
||||||
|
return new BooleanDoubleImmutablePair(pair.getBooleanKey(), pair.getDoubleValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return empty Mutable Pair
|
||||||
|
*/
|
||||||
|
public static BooleanDoublePair mutable() {
|
||||||
|
return new BooleanDoubleMutablePair();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @return Mutable Pair of key
|
||||||
|
*/
|
||||||
|
public static BooleanDoublePair mutableKey(boolean key) {
|
||||||
|
return new BooleanDoubleMutablePair(key, 0D);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Mutable Pair of value
|
||||||
|
*/
|
||||||
|
public static BooleanDoublePair mutableValue(double value) {
|
||||||
|
return new BooleanDoubleMutablePair(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Mutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static BooleanDoublePair mutable(boolean key, double value) {
|
||||||
|
return new BooleanDoubleMutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be copied
|
||||||
|
* @return a Mutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static BooleanDoublePair mutable(BooleanDoublePair pair) {
|
||||||
|
return new BooleanDoubleMutablePair(pair.getBooleanKey(), pair.getDoubleValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Key of the Pair.
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key.
|
||||||
|
*/
|
||||||
|
public BooleanDoublePair setBooleanKey(boolean key);
|
||||||
|
/**
|
||||||
|
* @return the Key of the Pair
|
||||||
|
*/
|
||||||
|
public boolean getBooleanKey();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Value of the Pair.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new value.
|
||||||
|
*/
|
||||||
|
public BooleanDoublePair setDoubleValue(double value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the Value of the Pair
|
||||||
|
*/
|
||||||
|
public double getDoubleValue();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets key and value of the Pair
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key and value.
|
||||||
|
*/
|
||||||
|
public BooleanDoublePair set(boolean key, double value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clones the Pair if it is mutable.
|
||||||
|
* @return a New Mutable Instance if it is mutable
|
||||||
|
*/
|
||||||
|
public BooleanDoublePair shallowCopy();
|
||||||
|
}
|
||||||
@ -0,0 +1,132 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanFloatImmutablePair;
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanFloatMutablePair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key Value Pair Interface that allows to reduce boxing/unboxing.
|
||||||
|
*/
|
||||||
|
public interface BooleanFloatPair
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Empty Reference for Immutable Pairs
|
||||||
|
*/
|
||||||
|
public static final BooleanFloatPair EMPTY = new BooleanFloatImmutablePair();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return empty Immutable Pair
|
||||||
|
*/
|
||||||
|
public static BooleanFloatPair of() {
|
||||||
|
return EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @return Immutable Pair of Key
|
||||||
|
*/
|
||||||
|
public static BooleanFloatPair ofKey(boolean key) {
|
||||||
|
return new BooleanFloatImmutablePair(key, 0F);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Immutable Pair of Value
|
||||||
|
*/
|
||||||
|
public static BooleanFloatPair ofValue(float value) {
|
||||||
|
return new BooleanFloatImmutablePair(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Immutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static BooleanFloatPair of(boolean key, float value) {
|
||||||
|
return new BooleanFloatImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be immutably copied
|
||||||
|
* @return a Immutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static BooleanFloatPair of(BooleanFloatPair pair) {
|
||||||
|
return new BooleanFloatImmutablePair(pair.getBooleanKey(), pair.getFloatValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return empty Mutable Pair
|
||||||
|
*/
|
||||||
|
public static BooleanFloatPair mutable() {
|
||||||
|
return new BooleanFloatMutablePair();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @return Mutable Pair of key
|
||||||
|
*/
|
||||||
|
public static BooleanFloatPair mutableKey(boolean key) {
|
||||||
|
return new BooleanFloatMutablePair(key, 0F);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Mutable Pair of value
|
||||||
|
*/
|
||||||
|
public static BooleanFloatPair mutableValue(float value) {
|
||||||
|
return new BooleanFloatMutablePair(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Mutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static BooleanFloatPair mutable(boolean key, float value) {
|
||||||
|
return new BooleanFloatMutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be copied
|
||||||
|
* @return a Mutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static BooleanFloatPair mutable(BooleanFloatPair pair) {
|
||||||
|
return new BooleanFloatMutablePair(pair.getBooleanKey(), pair.getFloatValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Key of the Pair.
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key.
|
||||||
|
*/
|
||||||
|
public BooleanFloatPair setBooleanKey(boolean key);
|
||||||
|
/**
|
||||||
|
* @return the Key of the Pair
|
||||||
|
*/
|
||||||
|
public boolean getBooleanKey();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Value of the Pair.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new value.
|
||||||
|
*/
|
||||||
|
public BooleanFloatPair setFloatValue(float value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the Value of the Pair
|
||||||
|
*/
|
||||||
|
public float getFloatValue();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets key and value of the Pair
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key and value.
|
||||||
|
*/
|
||||||
|
public BooleanFloatPair set(boolean key, float value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clones the Pair if it is mutable.
|
||||||
|
* @return a New Mutable Instance if it is mutable
|
||||||
|
*/
|
||||||
|
public BooleanFloatPair shallowCopy();
|
||||||
|
}
|
||||||
@ -0,0 +1,132 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanIntImmutablePair;
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanIntMutablePair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key Value Pair Interface that allows to reduce boxing/unboxing.
|
||||||
|
*/
|
||||||
|
public interface BooleanIntPair
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Empty Reference for Immutable Pairs
|
||||||
|
*/
|
||||||
|
public static final BooleanIntPair EMPTY = new BooleanIntImmutablePair();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return empty Immutable Pair
|
||||||
|
*/
|
||||||
|
public static BooleanIntPair of() {
|
||||||
|
return EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @return Immutable Pair of Key
|
||||||
|
*/
|
||||||
|
public static BooleanIntPair ofKey(boolean key) {
|
||||||
|
return new BooleanIntImmutablePair(key, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Immutable Pair of Value
|
||||||
|
*/
|
||||||
|
public static BooleanIntPair ofValue(int value) {
|
||||||
|
return new BooleanIntImmutablePair(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Immutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static BooleanIntPair of(boolean key, int value) {
|
||||||
|
return new BooleanIntImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be immutably copied
|
||||||
|
* @return a Immutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static BooleanIntPair of(BooleanIntPair pair) {
|
||||||
|
return new BooleanIntImmutablePair(pair.getBooleanKey(), pair.getIntValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return empty Mutable Pair
|
||||||
|
*/
|
||||||
|
public static BooleanIntPair mutable() {
|
||||||
|
return new BooleanIntMutablePair();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @return Mutable Pair of key
|
||||||
|
*/
|
||||||
|
public static BooleanIntPair mutableKey(boolean key) {
|
||||||
|
return new BooleanIntMutablePair(key, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Mutable Pair of value
|
||||||
|
*/
|
||||||
|
public static BooleanIntPair mutableValue(int value) {
|
||||||
|
return new BooleanIntMutablePair(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Mutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static BooleanIntPair mutable(boolean key, int value) {
|
||||||
|
return new BooleanIntMutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be copied
|
||||||
|
* @return a Mutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static BooleanIntPair mutable(BooleanIntPair pair) {
|
||||||
|
return new BooleanIntMutablePair(pair.getBooleanKey(), pair.getIntValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Key of the Pair.
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key.
|
||||||
|
*/
|
||||||
|
public BooleanIntPair setBooleanKey(boolean key);
|
||||||
|
/**
|
||||||
|
* @return the Key of the Pair
|
||||||
|
*/
|
||||||
|
public boolean getBooleanKey();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Value of the Pair.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new value.
|
||||||
|
*/
|
||||||
|
public BooleanIntPair setIntValue(int value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the Value of the Pair
|
||||||
|
*/
|
||||||
|
public int getIntValue();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets key and value of the Pair
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key and value.
|
||||||
|
*/
|
||||||
|
public BooleanIntPair set(boolean key, int value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clones the Pair if it is mutable.
|
||||||
|
* @return a New Mutable Instance if it is mutable
|
||||||
|
*/
|
||||||
|
public BooleanIntPair shallowCopy();
|
||||||
|
}
|
||||||
@ -0,0 +1,132 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanLongImmutablePair;
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanLongMutablePair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key Value Pair Interface that allows to reduce boxing/unboxing.
|
||||||
|
*/
|
||||||
|
public interface BooleanLongPair
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Empty Reference for Immutable Pairs
|
||||||
|
*/
|
||||||
|
public static final BooleanLongPair EMPTY = new BooleanLongImmutablePair();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return empty Immutable Pair
|
||||||
|
*/
|
||||||
|
public static BooleanLongPair of() {
|
||||||
|
return EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @return Immutable Pair of Key
|
||||||
|
*/
|
||||||
|
public static BooleanLongPair ofKey(boolean key) {
|
||||||
|
return new BooleanLongImmutablePair(key, 0L);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Immutable Pair of Value
|
||||||
|
*/
|
||||||
|
public static BooleanLongPair ofValue(long value) {
|
||||||
|
return new BooleanLongImmutablePair(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Immutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static BooleanLongPair of(boolean key, long value) {
|
||||||
|
return new BooleanLongImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be immutably copied
|
||||||
|
* @return a Immutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static BooleanLongPair of(BooleanLongPair pair) {
|
||||||
|
return new BooleanLongImmutablePair(pair.getBooleanKey(), pair.getLongValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return empty Mutable Pair
|
||||||
|
*/
|
||||||
|
public static BooleanLongPair mutable() {
|
||||||
|
return new BooleanLongMutablePair();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @return Mutable Pair of key
|
||||||
|
*/
|
||||||
|
public static BooleanLongPair mutableKey(boolean key) {
|
||||||
|
return new BooleanLongMutablePair(key, 0L);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Mutable Pair of value
|
||||||
|
*/
|
||||||
|
public static BooleanLongPair mutableValue(long value) {
|
||||||
|
return new BooleanLongMutablePair(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Mutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static BooleanLongPair mutable(boolean key, long value) {
|
||||||
|
return new BooleanLongMutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be copied
|
||||||
|
* @return a Mutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static BooleanLongPair mutable(BooleanLongPair pair) {
|
||||||
|
return new BooleanLongMutablePair(pair.getBooleanKey(), pair.getLongValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Key of the Pair.
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key.
|
||||||
|
*/
|
||||||
|
public BooleanLongPair setBooleanKey(boolean key);
|
||||||
|
/**
|
||||||
|
* @return the Key of the Pair
|
||||||
|
*/
|
||||||
|
public boolean getBooleanKey();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Value of the Pair.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new value.
|
||||||
|
*/
|
||||||
|
public BooleanLongPair setLongValue(long value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the Value of the Pair
|
||||||
|
*/
|
||||||
|
public long getLongValue();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets key and value of the Pair
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key and value.
|
||||||
|
*/
|
||||||
|
public BooleanLongPair set(boolean key, long value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clones the Pair if it is mutable.
|
||||||
|
* @return a New Mutable Instance if it is mutable
|
||||||
|
*/
|
||||||
|
public BooleanLongPair shallowCopy();
|
||||||
|
}
|
||||||
@ -0,0 +1,143 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanObjectImmutablePair;
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanObjectMutablePair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key Value Pair Interface that allows to reduce boxing/unboxing.
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
*/
|
||||||
|
public interface BooleanObjectPair<V>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Empty Reference for Immutable Pairs
|
||||||
|
*/
|
||||||
|
public static final BooleanObjectPair<?> EMPTY = new BooleanObjectImmutablePair<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
* @return empty Immutable Pair
|
||||||
|
*/
|
||||||
|
public static <V> BooleanObjectPair<V> of() {
|
||||||
|
return (BooleanObjectPair<V>)EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
* @return Immutable Pair of Key
|
||||||
|
*/
|
||||||
|
public static <V> BooleanObjectPair<V> ofKey(boolean key) {
|
||||||
|
return new BooleanObjectImmutablePair<>(key, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
* @return Immutable Pair of Value
|
||||||
|
*/
|
||||||
|
public static <V> BooleanObjectPair<V> ofValue(V value) {
|
||||||
|
return new BooleanObjectImmutablePair<>(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
* @return Immutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static <V> BooleanObjectPair<V> of(boolean key, V value) {
|
||||||
|
return new BooleanObjectImmutablePair<>(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be immutably copied
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
* @return a Immutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static <V> BooleanObjectPair<V> of(BooleanObjectPair<V> pair) {
|
||||||
|
return new BooleanObjectImmutablePair<>(pair.getBooleanKey(), pair.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
* @return empty Mutable Pair
|
||||||
|
*/
|
||||||
|
public static <V> BooleanObjectPair<V> mutable() {
|
||||||
|
return new BooleanObjectMutablePair<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
* @return Mutable Pair of key
|
||||||
|
*/
|
||||||
|
public static <V> BooleanObjectPair<V> mutableKey(boolean key) {
|
||||||
|
return new BooleanObjectMutablePair<>(key, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
* @return Mutable Pair of value
|
||||||
|
*/
|
||||||
|
public static <V> BooleanObjectPair<V> mutableValue(V value) {
|
||||||
|
return new BooleanObjectMutablePair<>(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
* @return Mutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static <V> BooleanObjectPair<V> mutable(boolean key, V value) {
|
||||||
|
return new BooleanObjectMutablePair<>(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be copied
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
* @return a Mutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static <V> BooleanObjectPair<V> mutable(BooleanObjectPair<V> pair) {
|
||||||
|
return new BooleanObjectMutablePair<>(pair.getBooleanKey(), pair.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Key of the Pair.
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key.
|
||||||
|
*/
|
||||||
|
public BooleanObjectPair<V> setBooleanKey(boolean key);
|
||||||
|
/**
|
||||||
|
* @return the Key of the Pair
|
||||||
|
*/
|
||||||
|
public boolean getBooleanKey();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Value of the Pair.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new value.
|
||||||
|
*/
|
||||||
|
public BooleanObjectPair<V> setValue(V value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the Value of the Pair
|
||||||
|
*/
|
||||||
|
public V getValue();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets key and value of the Pair
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key and value.
|
||||||
|
*/
|
||||||
|
public BooleanObjectPair<V> set(boolean key, V value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clones the Pair if it is mutable.
|
||||||
|
* @return a New Mutable Instance if it is mutable
|
||||||
|
*/
|
||||||
|
public BooleanObjectPair<V> shallowCopy();
|
||||||
|
}
|
||||||
@ -0,0 +1,132 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanShortImmutablePair;
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.impl.BooleanShortMutablePair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key Value Pair Interface that allows to reduce boxing/unboxing.
|
||||||
|
*/
|
||||||
|
public interface BooleanShortPair
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Empty Reference for Immutable Pairs
|
||||||
|
*/
|
||||||
|
public static final BooleanShortPair EMPTY = new BooleanShortImmutablePair();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return empty Immutable Pair
|
||||||
|
*/
|
||||||
|
public static BooleanShortPair of() {
|
||||||
|
return EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @return Immutable Pair of Key
|
||||||
|
*/
|
||||||
|
public static BooleanShortPair ofKey(boolean key) {
|
||||||
|
return new BooleanShortImmutablePair(key, (short)0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Immutable Pair of Value
|
||||||
|
*/
|
||||||
|
public static BooleanShortPair ofValue(short value) {
|
||||||
|
return new BooleanShortImmutablePair(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Immutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static BooleanShortPair of(boolean key, short value) {
|
||||||
|
return new BooleanShortImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be immutably copied
|
||||||
|
* @return a Immutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static BooleanShortPair of(BooleanShortPair pair) {
|
||||||
|
return new BooleanShortImmutablePair(pair.getBooleanKey(), pair.getShortValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return empty Mutable Pair
|
||||||
|
*/
|
||||||
|
public static BooleanShortPair mutable() {
|
||||||
|
return new BooleanShortMutablePair();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @return Mutable Pair of key
|
||||||
|
*/
|
||||||
|
public static BooleanShortPair mutableKey(boolean key) {
|
||||||
|
return new BooleanShortMutablePair(key, (short)0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Mutable Pair of value
|
||||||
|
*/
|
||||||
|
public static BooleanShortPair mutableValue(short value) {
|
||||||
|
return new BooleanShortMutablePair(false, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key the key that should be in the pair
|
||||||
|
* @param value the value that should be in the pair
|
||||||
|
* @return Mutable Pair of key and value
|
||||||
|
*/
|
||||||
|
public static BooleanShortPair mutable(boolean key, short value) {
|
||||||
|
return new BooleanShortMutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param pair the Pair that should be copied
|
||||||
|
* @return a Mutable Copy of the Provided Pair
|
||||||
|
*/
|
||||||
|
public static BooleanShortPair mutable(BooleanShortPair pair) {
|
||||||
|
return new BooleanShortMutablePair(pair.getBooleanKey(), pair.getShortValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Key of the Pair.
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key.
|
||||||
|
*/
|
||||||
|
public BooleanShortPair setBooleanKey(boolean key);
|
||||||
|
/**
|
||||||
|
* @return the Key of the Pair
|
||||||
|
*/
|
||||||
|
public boolean getBooleanKey();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the Value of the Pair.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new value.
|
||||||
|
*/
|
||||||
|
public BooleanShortPair setShortValue(short value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the Value of the Pair
|
||||||
|
*/
|
||||||
|
public short getShortValue();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets key and value of the Pair
|
||||||
|
* @param key the key that should be set.
|
||||||
|
* @param value the value that should be set.
|
||||||
|
* @return self or a new Pair instance with the new key and value.
|
||||||
|
*/
|
||||||
|
public BooleanShortPair set(boolean key, short value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clones the Pair if it is mutable.
|
||||||
|
* @return a New Mutable Instance if it is mutable
|
||||||
|
*/
|
||||||
|
public BooleanShortPair shallowCopy();
|
||||||
|
}
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanBooleanPair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
*/
|
||||||
|
public class BooleanBooleanImmutablePair implements BooleanBooleanPair
|
||||||
|
{
|
||||||
|
protected final boolean key;
|
||||||
|
protected final boolean value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanBooleanImmutablePair() {
|
||||||
|
this(false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanBooleanImmutablePair(boolean key, boolean value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanBooleanPair setBooleanKey(boolean key) {
|
||||||
|
return new BooleanBooleanImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanBooleanPair setBooleanValue(boolean value) {
|
||||||
|
return new BooleanBooleanImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanBooleanPair set(boolean key, boolean value) {
|
||||||
|
return new BooleanBooleanImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanBooleanPair shallowCopy() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanBooleanPair) {
|
||||||
|
BooleanBooleanPair entry = (BooleanBooleanPair)obj;
|
||||||
|
return key == entry.getBooleanKey() && value == entry.getBooleanValue();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Boolean.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Boolean.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,83 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanBooleanPair;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
*/
|
||||||
|
public class BooleanBooleanMutablePair implements BooleanBooleanPair
|
||||||
|
{
|
||||||
|
protected boolean key;
|
||||||
|
protected boolean value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanBooleanMutablePair() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanBooleanMutablePair(boolean key, boolean value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanBooleanPair setBooleanKey(boolean key) {
|
||||||
|
this.key = key;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanBooleanPair setBooleanValue(boolean value) {
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanBooleanPair set(boolean key, boolean value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanBooleanPair shallowCopy() {
|
||||||
|
return BooleanBooleanPair.mutable(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanBooleanPair) {
|
||||||
|
BooleanBooleanPair entry = (BooleanBooleanPair)obj;
|
||||||
|
return key == entry.getBooleanKey() && value == entry.getBooleanValue();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Boolean.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Boolean.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanBytePair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
*/
|
||||||
|
public class BooleanByteImmutablePair implements BooleanBytePair
|
||||||
|
{
|
||||||
|
protected final boolean key;
|
||||||
|
protected final byte value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanByteImmutablePair() {
|
||||||
|
this(false, (byte)0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanByteImmutablePair(boolean key, byte value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanBytePair setBooleanKey(boolean key) {
|
||||||
|
return new BooleanByteImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanBytePair setByteValue(byte value) {
|
||||||
|
return new BooleanByteImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte getByteValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanBytePair set(boolean key, byte value) {
|
||||||
|
return new BooleanByteImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanBytePair shallowCopy() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanBytePair) {
|
||||||
|
BooleanBytePair entry = (BooleanBytePair)obj;
|
||||||
|
return key == entry.getBooleanKey() && value == entry.getByteValue();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Byte.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Byte.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,83 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanBytePair;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
*/
|
||||||
|
public class BooleanByteMutablePair implements BooleanBytePair
|
||||||
|
{
|
||||||
|
protected boolean key;
|
||||||
|
protected byte value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanByteMutablePair() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanByteMutablePair(boolean key, byte value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanBytePair setBooleanKey(boolean key) {
|
||||||
|
this.key = key;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanBytePair setByteValue(byte value) {
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte getByteValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanBytePair set(boolean key, byte value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanBytePair shallowCopy() {
|
||||||
|
return BooleanBytePair.mutable(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanBytePair) {
|
||||||
|
BooleanBytePair entry = (BooleanBytePair)obj;
|
||||||
|
return key == entry.getBooleanKey() && value == entry.getByteValue();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Byte.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Byte.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanCharPair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
*/
|
||||||
|
public class BooleanCharImmutablePair implements BooleanCharPair
|
||||||
|
{
|
||||||
|
protected final boolean key;
|
||||||
|
protected final char value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanCharImmutablePair() {
|
||||||
|
this(false, (char)0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanCharImmutablePair(boolean key, char value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanCharPair setBooleanKey(boolean key) {
|
||||||
|
return new BooleanCharImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanCharPair setCharValue(char value) {
|
||||||
|
return new BooleanCharImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char getCharValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanCharPair set(boolean key, char value) {
|
||||||
|
return new BooleanCharImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanCharPair shallowCopy() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanCharPair) {
|
||||||
|
BooleanCharPair entry = (BooleanCharPair)obj;
|
||||||
|
return key == entry.getBooleanKey() && value == entry.getCharValue();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Character.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Character.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,83 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanCharPair;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
*/
|
||||||
|
public class BooleanCharMutablePair implements BooleanCharPair
|
||||||
|
{
|
||||||
|
protected boolean key;
|
||||||
|
protected char value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanCharMutablePair() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanCharMutablePair(boolean key, char value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanCharPair setBooleanKey(boolean key) {
|
||||||
|
this.key = key;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanCharPair setCharValue(char value) {
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public char getCharValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanCharPair set(boolean key, char value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanCharPair shallowCopy() {
|
||||||
|
return BooleanCharPair.mutable(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanCharPair) {
|
||||||
|
BooleanCharPair entry = (BooleanCharPair)obj;
|
||||||
|
return key == entry.getBooleanKey() && value == entry.getCharValue();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Character.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Character.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanDoublePair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
*/
|
||||||
|
public class BooleanDoubleImmutablePair implements BooleanDoublePair
|
||||||
|
{
|
||||||
|
protected final boolean key;
|
||||||
|
protected final double value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanDoubleImmutablePair() {
|
||||||
|
this(false, 0D);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanDoubleImmutablePair(boolean key, double value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanDoublePair setBooleanKey(boolean key) {
|
||||||
|
return new BooleanDoubleImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanDoublePair setDoubleValue(double value) {
|
||||||
|
return new BooleanDoubleImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getDoubleValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanDoublePair set(boolean key, double value) {
|
||||||
|
return new BooleanDoubleImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanDoublePair shallowCopy() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanDoublePair) {
|
||||||
|
BooleanDoublePair entry = (BooleanDoublePair)obj;
|
||||||
|
return key == entry.getBooleanKey() && Double.doubleToLongBits(value) == Double.doubleToLongBits(entry.getDoubleValue());
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Double.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Double.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,83 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanDoublePair;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
*/
|
||||||
|
public class BooleanDoubleMutablePair implements BooleanDoublePair
|
||||||
|
{
|
||||||
|
protected boolean key;
|
||||||
|
protected double value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanDoubleMutablePair() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanDoubleMutablePair(boolean key, double value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanDoublePair setBooleanKey(boolean key) {
|
||||||
|
this.key = key;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanDoublePair setDoubleValue(double value) {
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getDoubleValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanDoublePair set(boolean key, double value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanDoublePair shallowCopy() {
|
||||||
|
return BooleanDoublePair.mutable(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanDoublePair) {
|
||||||
|
BooleanDoublePair entry = (BooleanDoublePair)obj;
|
||||||
|
return key == entry.getBooleanKey() && Double.doubleToLongBits(value) == Double.doubleToLongBits(entry.getDoubleValue());
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Double.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Double.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanFloatPair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
*/
|
||||||
|
public class BooleanFloatImmutablePair implements BooleanFloatPair
|
||||||
|
{
|
||||||
|
protected final boolean key;
|
||||||
|
protected final float value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanFloatImmutablePair() {
|
||||||
|
this(false, 0F);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanFloatImmutablePair(boolean key, float value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanFloatPair setBooleanKey(boolean key) {
|
||||||
|
return new BooleanFloatImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanFloatPair setFloatValue(float value) {
|
||||||
|
return new BooleanFloatImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getFloatValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanFloatPair set(boolean key, float value) {
|
||||||
|
return new BooleanFloatImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanFloatPair shallowCopy() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanFloatPair) {
|
||||||
|
BooleanFloatPair entry = (BooleanFloatPair)obj;
|
||||||
|
return key == entry.getBooleanKey() && Float.floatToIntBits(value) == Float.floatToIntBits(entry.getFloatValue());
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Float.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Float.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,83 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanFloatPair;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
*/
|
||||||
|
public class BooleanFloatMutablePair implements BooleanFloatPair
|
||||||
|
{
|
||||||
|
protected boolean key;
|
||||||
|
protected float value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanFloatMutablePair() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanFloatMutablePair(boolean key, float value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanFloatPair setBooleanKey(boolean key) {
|
||||||
|
this.key = key;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanFloatPair setFloatValue(float value) {
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public float getFloatValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanFloatPair set(boolean key, float value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanFloatPair shallowCopy() {
|
||||||
|
return BooleanFloatPair.mutable(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanFloatPair) {
|
||||||
|
BooleanFloatPair entry = (BooleanFloatPair)obj;
|
||||||
|
return key == entry.getBooleanKey() && Float.floatToIntBits(value) == Float.floatToIntBits(entry.getFloatValue());
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Float.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Float.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanIntPair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
*/
|
||||||
|
public class BooleanIntImmutablePair implements BooleanIntPair
|
||||||
|
{
|
||||||
|
protected final boolean key;
|
||||||
|
protected final int value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanIntImmutablePair() {
|
||||||
|
this(false, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanIntImmutablePair(boolean key, int value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIntPair setBooleanKey(boolean key) {
|
||||||
|
return new BooleanIntImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIntPair setIntValue(int value) {
|
||||||
|
return new BooleanIntImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getIntValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIntPair set(boolean key, int value) {
|
||||||
|
return new BooleanIntImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIntPair shallowCopy() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanIntPair) {
|
||||||
|
BooleanIntPair entry = (BooleanIntPair)obj;
|
||||||
|
return key == entry.getBooleanKey() && value == entry.getIntValue();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Integer.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Integer.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,83 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanIntPair;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
*/
|
||||||
|
public class BooleanIntMutablePair implements BooleanIntPair
|
||||||
|
{
|
||||||
|
protected boolean key;
|
||||||
|
protected int value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanIntMutablePair() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanIntMutablePair(boolean key, int value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIntPair setBooleanKey(boolean key) {
|
||||||
|
this.key = key;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIntPair setIntValue(int value) {
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getIntValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIntPair set(boolean key, int value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIntPair shallowCopy() {
|
||||||
|
return BooleanIntPair.mutable(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanIntPair) {
|
||||||
|
BooleanIntPair entry = (BooleanIntPair)obj;
|
||||||
|
return key == entry.getBooleanKey() && value == entry.getIntValue();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Integer.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Integer.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanLongPair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
*/
|
||||||
|
public class BooleanLongImmutablePair implements BooleanLongPair
|
||||||
|
{
|
||||||
|
protected final boolean key;
|
||||||
|
protected final long value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanLongImmutablePair() {
|
||||||
|
this(false, 0L);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanLongImmutablePair(boolean key, long value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanLongPair setBooleanKey(boolean key) {
|
||||||
|
return new BooleanLongImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanLongPair setLongValue(long value) {
|
||||||
|
return new BooleanLongImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getLongValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanLongPair set(boolean key, long value) {
|
||||||
|
return new BooleanLongImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanLongPair shallowCopy() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanLongPair) {
|
||||||
|
BooleanLongPair entry = (BooleanLongPair)obj;
|
||||||
|
return key == entry.getBooleanKey() && value == entry.getLongValue();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Long.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Long.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,83 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanLongPair;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
*/
|
||||||
|
public class BooleanLongMutablePair implements BooleanLongPair
|
||||||
|
{
|
||||||
|
protected boolean key;
|
||||||
|
protected long value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanLongMutablePair() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanLongMutablePair(boolean key, long value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanLongPair setBooleanKey(boolean key) {
|
||||||
|
this.key = key;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanLongPair setLongValue(long value) {
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getLongValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanLongPair set(boolean key, long value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanLongPair shallowCopy() {
|
||||||
|
return BooleanLongPair.mutable(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanLongPair) {
|
||||||
|
BooleanLongPair entry = (BooleanLongPair)obj;
|
||||||
|
return key == entry.getBooleanKey() && value == entry.getLongValue();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Long.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Long.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,81 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanObjectPair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
*/
|
||||||
|
public class BooleanObjectImmutablePair<V> implements BooleanObjectPair<V>
|
||||||
|
{
|
||||||
|
protected final boolean key;
|
||||||
|
protected final V value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanObjectImmutablePair() {
|
||||||
|
this(false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanObjectImmutablePair(boolean key, V value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanObjectPair<V> setBooleanKey(boolean key) {
|
||||||
|
return new BooleanObjectImmutablePair<>(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanObjectPair<V> setValue(V value) {
|
||||||
|
return new BooleanObjectImmutablePair<>(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanObjectPair<V> set(boolean key, V value) {
|
||||||
|
return new BooleanObjectImmutablePair<>(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanObjectPair<V> shallowCopy() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanObjectPair) {
|
||||||
|
BooleanObjectPair<V> entry = (BooleanObjectPair<V>)obj;
|
||||||
|
return key == entry.getBooleanKey() && Objects.equals(value, entry.getValue());
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Objects.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Objects.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,85 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanObjectPair;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
* @param <V> the keyType of elements maintained by this Collection
|
||||||
|
*/
|
||||||
|
public class BooleanObjectMutablePair<V> implements BooleanObjectPair<V>
|
||||||
|
{
|
||||||
|
protected boolean key;
|
||||||
|
protected V value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanObjectMutablePair() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanObjectMutablePair(boolean key, V value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanObjectPair<V> setBooleanKey(boolean key) {
|
||||||
|
this.key = key;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanObjectPair<V> setValue(V value) {
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanObjectPair<V> set(boolean key, V value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanObjectPair<V> shallowCopy() {
|
||||||
|
return BooleanObjectPair.mutable(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanObjectPair) {
|
||||||
|
BooleanObjectPair<V> entry = (BooleanObjectPair<V>)obj;
|
||||||
|
return key == entry.getBooleanKey() && Objects.equals(value, entry.getValue());
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Objects.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Objects.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,79 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanShortPair;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
*/
|
||||||
|
public class BooleanShortImmutablePair implements BooleanShortPair
|
||||||
|
{
|
||||||
|
protected final boolean key;
|
||||||
|
protected final short value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanShortImmutablePair() {
|
||||||
|
this(false, (short)0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanShortImmutablePair(boolean key, short value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanShortPair setBooleanKey(boolean key) {
|
||||||
|
return new BooleanShortImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanShortPair setShortValue(short value) {
|
||||||
|
return new BooleanShortImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public short getShortValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanShortPair set(boolean key, short value) {
|
||||||
|
return new BooleanShortImmutablePair(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanShortPair shallowCopy() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanShortPair) {
|
||||||
|
BooleanShortPair entry = (BooleanShortPair)obj;
|
||||||
|
return key == entry.getBooleanKey() && value == entry.getShortValue();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Short.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Short.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,83 @@
|
|||||||
|
package speiger.src.collections.booleans.misc.pairs.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.misc.pairs.BooleanShortPair;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutable Pair Implementation that
|
||||||
|
*/
|
||||||
|
public class BooleanShortMutablePair implements BooleanShortPair
|
||||||
|
{
|
||||||
|
protected boolean key;
|
||||||
|
protected short value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanShortMutablePair() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Key/Value Constructur
|
||||||
|
* @param key the key of the Pair
|
||||||
|
* @param value the value of the Pair
|
||||||
|
*/
|
||||||
|
public BooleanShortMutablePair(boolean key, short value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanShortPair setBooleanKey(boolean key) {
|
||||||
|
this.key = key;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBooleanKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanShortPair setShortValue(short value) {
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public short getShortValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanShortPair set(boolean key, short value) {
|
||||||
|
this.key = key;
|
||||||
|
this.value = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanShortPair shallowCopy() {
|
||||||
|
return BooleanShortPair.mutable(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanShortPair) {
|
||||||
|
BooleanShortPair entry = (BooleanShortPair)obj;
|
||||||
|
return key == entry.getBooleanKey() && value == entry.getShortValue();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(key) ^ Short.hashCode(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return Boolean.toString(key) + "->" + Short.toString(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,42 @@
|
|||||||
|
package speiger.src.collections.booleans.queues;
|
||||||
|
|
||||||
|
import java.util.StringJoiner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper class that implements all the essential methods for the PriorityQueues
|
||||||
|
*/
|
||||||
|
public abstract class AbstractBooleanPriorityQueue implements BooleanPriorityQueue
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj instanceof BooleanPriorityQueue) {
|
||||||
|
BooleanPriorityQueue queue = (BooleanPriorityQueue)obj;
|
||||||
|
if(queue.size() != size()) return false;
|
||||||
|
for(int i = 0,m=size();i<m;i++) {
|
||||||
|
if(queue.peek(i) != peek(i)) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = 1;
|
||||||
|
for (int i = 0,m=size();i<m;i++) {
|
||||||
|
result = 31 * result + Boolean.hashCode(peek(i));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString()
|
||||||
|
{
|
||||||
|
if(isEmpty()) return "[]";
|
||||||
|
StringJoiner joiner = new StringJoiner(", ", "[", "]");
|
||||||
|
for (int i = 0,m=size();i<m;i++) {
|
||||||
|
joiner.add(Boolean.toString(peek(i)));
|
||||||
|
}
|
||||||
|
return joiner.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,406 @@
|
|||||||
|
package speiger.src.collections.booleans.queues;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanIterator;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanComparator;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanConsumer;
|
||||||
|
import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer;
|
||||||
|
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanPredicate;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanBooleanUnaryOperator;
|
||||||
|
import speiger.src.collections.utils.ITrimmable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Simple First In First Out Priority Queue that is a Good Replacement for a linked list (or ArrayDequeue)
|
||||||
|
* Its specific implementation uses a backing array that grows and shrinks as it is needed.
|
||||||
|
*/
|
||||||
|
public class BooleanArrayFIFOQueue extends AbstractBooleanPriorityQueue implements BooleanPriorityDequeue, ITrimmable
|
||||||
|
{
|
||||||
|
/** Max Possible ArraySize without the JVM Crashing */
|
||||||
|
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
|
||||||
|
/** The Minimum Capacity that is allowed */
|
||||||
|
public static final int MIN_CAPACITY = 4;
|
||||||
|
/** The Backing array */
|
||||||
|
protected transient boolean[] array;
|
||||||
|
/** The First Index pointer */
|
||||||
|
protected int first;
|
||||||
|
/** The Last Index pointer */
|
||||||
|
protected int last;
|
||||||
|
/** The Minimum Capacity of the Queue **/
|
||||||
|
protected int minCapacity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using a initial array
|
||||||
|
* @param values the Array that should be used
|
||||||
|
*/
|
||||||
|
public BooleanArrayFIFOQueue(boolean[] values) {
|
||||||
|
this(values, 0, values.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using a initial array
|
||||||
|
* @param values the Array that should be used
|
||||||
|
* @param size the amount of elements that are in the initial array
|
||||||
|
* @throws IllegalStateException if values is smaller then size
|
||||||
|
*/
|
||||||
|
public BooleanArrayFIFOQueue(boolean[] values, int size) {
|
||||||
|
this(values, 0, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using a initial array
|
||||||
|
* @param values the Array that should be used
|
||||||
|
* @param offset where to begin in the initial array
|
||||||
|
* @param size the amount of elements that are in the initial array
|
||||||
|
* @throws IllegalStateException if values is smaller then size
|
||||||
|
*/
|
||||||
|
public BooleanArrayFIFOQueue(boolean[] values, int offset, int size) {
|
||||||
|
if (values.length < size) throw new IllegalArgumentException("Initial array (" + values.length + ") is smaller then the expected size (" + size + ")");
|
||||||
|
if(values.length <= 0) values = new boolean[MIN_CAPACITY];
|
||||||
|
else if(values.length < MIN_CAPACITY) values = Arrays.copyOf(values, MIN_CAPACITY);
|
||||||
|
minCapacity = MIN_CAPACITY;
|
||||||
|
array = values;
|
||||||
|
first = offset;
|
||||||
|
last = (offset + size) % array.length;
|
||||||
|
if(array.length == size) expand();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor with a Min Capacity
|
||||||
|
* @param capacity the initial capacity of the backing array
|
||||||
|
* @throws IllegalStateException if the initial size is smaller 0
|
||||||
|
*/
|
||||||
|
public BooleanArrayFIFOQueue(int capacity) {
|
||||||
|
if (capacity < 0) throw new IllegalArgumentException("Initial capacity (" + capacity + ") is negative");
|
||||||
|
array = new boolean[Math.max(MIN_CAPACITY, capacity+1)];
|
||||||
|
minCapacity = array.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Construtor
|
||||||
|
*/
|
||||||
|
public BooleanArrayFIFOQueue() {
|
||||||
|
this(MIN_CAPACITY);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator() {
|
||||||
|
return new Iter();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
final int apparentLength = last - first;
|
||||||
|
return apparentLength >= 0 ? apparentLength : array.length + apparentLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
if(first != last) {
|
||||||
|
first = last = 0;
|
||||||
|
}
|
||||||
|
else if(first != 0) {
|
||||||
|
first = last = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enqueue(boolean e) {
|
||||||
|
array[last++] = e;
|
||||||
|
if(last == array.length) last = 0;
|
||||||
|
if(last == first) expand();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enqueueFirst(boolean e) {
|
||||||
|
if(first == 0) first = array.length;
|
||||||
|
array[--first] = e;
|
||||||
|
if(first == last) expand();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean dequeue() {
|
||||||
|
if(first == last) throw new NoSuchElementException();
|
||||||
|
boolean data = array[first];
|
||||||
|
if(++first == array.length) first = 0;
|
||||||
|
reduce();
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean dequeueLast() {
|
||||||
|
if(first == last) throw new NoSuchElementException();
|
||||||
|
if(last == 0) last = array.length;
|
||||||
|
boolean data = array[--last];
|
||||||
|
reduce();
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean peek(int index) {
|
||||||
|
if(first == last || index < 0 || index >= size()) throw new NoSuchElementException();
|
||||||
|
index += first;
|
||||||
|
return index >= array.length ? array[index-array.length] : array[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeFirst(boolean e) {
|
||||||
|
if(first == last) return false;
|
||||||
|
for(int i = 0,m=size();i<m;i++) {
|
||||||
|
int index = (first + i) % array.length;
|
||||||
|
if(e == array[index])
|
||||||
|
return removeIndex(index);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeLast(boolean e) {
|
||||||
|
if(first == last) return false;
|
||||||
|
for(int i = size()-1;i>=0;i--) {
|
||||||
|
int index = (first + i) % array.length;
|
||||||
|
if(e == array[index])
|
||||||
|
return removeIndex(index);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean removeIndex(int index) {
|
||||||
|
if(first >= last ? index < first && index > last : index < first || index > last) return false;
|
||||||
|
if(index == first) {
|
||||||
|
first++;
|
||||||
|
}
|
||||||
|
else if(index == last) {
|
||||||
|
last--;
|
||||||
|
}
|
||||||
|
else if(index > last) {
|
||||||
|
System.arraycopy(array, first, array, first+1, (index - first));
|
||||||
|
first = ++first % array.length;
|
||||||
|
}
|
||||||
|
else if(index < first) {
|
||||||
|
System.arraycopy(array, index+1, array, index, (last - index) - 1);
|
||||||
|
if(--last < 0) last += array.length;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if(index - first < last - index) {
|
||||||
|
System.arraycopy(array, first, array, first+1, (index - first));
|
||||||
|
first = ++first % array.length;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.arraycopy(array, index+1, array, index, (last - index) - 1);
|
||||||
|
if(--last < 0) last += array.length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
reduce();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChanged() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanArrayFIFOQueue copy() {
|
||||||
|
BooleanArrayFIFOQueue queue = new BooleanArrayFIFOQueue();
|
||||||
|
queue.first = first;
|
||||||
|
queue.last = last;
|
||||||
|
queue.minCapacity = minCapacity;
|
||||||
|
queue.array = Arrays.copyOf(array, array.length);
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanComparator comparator() { return null; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
if(first == last) return;
|
||||||
|
for(int i = 0,m=size();i<m;i++)
|
||||||
|
action.accept(array[(first + i) % array.length]);
|
||||||
|
clearAndTrim(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEachIndexed(IntBooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
if(first == last) return;
|
||||||
|
for(int i = 0,m=size();i<m;i++)
|
||||||
|
action.accept(i, array[(first + i) % array.length]);
|
||||||
|
clearAndTrim(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E> void forEach(E input, ObjectBooleanConsumer<E> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
if(first == last) return;
|
||||||
|
for(int i = 0,m=size();i<m;i++)
|
||||||
|
action.accept(input, array[(first + i) % array.length]);
|
||||||
|
clearAndTrim(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matchesAny(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0,m=size();i<m;i++) {
|
||||||
|
if(filter.test(array[(first + i) % array.length])) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matchesNone(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0,m=size();i<m;i++) {
|
||||||
|
if(filter.test(array[(first + i) % array.length])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matchesAll(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0,m=size();i<m;i++) {
|
||||||
|
if(!filter.test(array[(first + i) % array.length])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean findFirst(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0,m=size();i<m;i++) {
|
||||||
|
int index = (first + i) % array.length;
|
||||||
|
if(filter.test(array[index])) {
|
||||||
|
boolean data = array[index];
|
||||||
|
removeIndex(index);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean reduce(boolean identity, BooleanBooleanUnaryOperator operator) {
|
||||||
|
Objects.requireNonNull(operator);
|
||||||
|
boolean state = identity;
|
||||||
|
for(int i = 0,m=size();i<m;i++) {
|
||||||
|
state = operator.applyAsBoolean(state, array[(first + i) % array.length]);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean reduce(BooleanBooleanUnaryOperator operator) {
|
||||||
|
Objects.requireNonNull(operator);
|
||||||
|
boolean state = false;
|
||||||
|
boolean empty = true;
|
||||||
|
for(int i = 0,m=size();i<m;i++) {
|
||||||
|
if(empty) {
|
||||||
|
empty = false;
|
||||||
|
state = array[(first + i) % array.length];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
state = operator.applyAsBoolean(state, array[(first + i) % array.length]);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int count(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
int result = 0;
|
||||||
|
for(int i = 0,m=size();i<m;i++) {
|
||||||
|
if(filter.test(array[(first + i) % array.length])) result++;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean trim(int size) {
|
||||||
|
int newSize = Math.max(Math.max(size, size()), minCapacity);
|
||||||
|
if(newSize >= array.length) return false;
|
||||||
|
boolean[] newArray = new boolean[newSize];
|
||||||
|
if(first <= last) System.arraycopy(array, first, newArray, 0, last - first);
|
||||||
|
else {
|
||||||
|
System.arraycopy(array, first, newArray, 0, array.length - first);
|
||||||
|
System.arraycopy(array, 0, newArray, array.length - first, last);
|
||||||
|
}
|
||||||
|
first = 0;
|
||||||
|
last = size();
|
||||||
|
array = newArray;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trims the collection down to the requested size and clears all elements while doing so
|
||||||
|
* @param size the amount of elements that should be allowed
|
||||||
|
* @note this will enforce minimum size of the collection itself
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void clearAndTrim(int size) {
|
||||||
|
int newSize = Math.max(minCapacity, size);
|
||||||
|
if(array.length <= newSize) {
|
||||||
|
clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
first = last = 0;
|
||||||
|
array = new boolean[newSize];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] toBooleanArray(boolean[] input) {
|
||||||
|
if(input == null || input.length < size()) input = new boolean[size()];
|
||||||
|
if (first <= last) System.arraycopy(array, first, input, 0, last - first);
|
||||||
|
else {
|
||||||
|
System.arraycopy(array, first, input, 0, array.length - first);
|
||||||
|
System.arraycopy(array, 0, input, array.length - first, last);
|
||||||
|
}
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void reduce() {
|
||||||
|
final int size = size();
|
||||||
|
if (array.length > minCapacity && size <= array.length / 4) resize(size, Math.max(array.length / 2, minCapacity));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void expand() {
|
||||||
|
resize(array.length, (int)Math.min(MAX_ARRAY_SIZE, 2L * array.length));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final void resize(int oldSize, int newSize) {
|
||||||
|
boolean[] newArray = new boolean[newSize];
|
||||||
|
if(first >= last) {
|
||||||
|
if(oldSize != 0)
|
||||||
|
{
|
||||||
|
System.arraycopy(array, first, newArray, 0, array.length - first);
|
||||||
|
System.arraycopy(array, 0, newArray, array.length - first, last);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else System.arraycopy(array, first, newArray, 0, last-first);
|
||||||
|
first = 0;
|
||||||
|
last = oldSize;
|
||||||
|
array = newArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Iter implements BooleanIterator
|
||||||
|
{
|
||||||
|
int index = first;
|
||||||
|
@Override
|
||||||
|
public boolean hasNext()
|
||||||
|
{
|
||||||
|
return index != last;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean nextBoolean() {
|
||||||
|
if(!hasNext()) throw new NoSuchElementException();
|
||||||
|
boolean value = array[index];
|
||||||
|
removeIndex(index);
|
||||||
|
index = ++index % array.length;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,401 @@
|
|||||||
|
package speiger.src.collections.booleans.queues;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanCollection;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanIterator;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanComparator;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanConsumer;
|
||||||
|
import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer;
|
||||||
|
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanPredicate;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanBooleanUnaryOperator;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanArrays;
|
||||||
|
import speiger.src.collections.utils.SanityChecks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Array Priority Queue, this is a very unoptimized implementation of the PriorityQueue for very specific usecases.
|
||||||
|
* It allows for duplicated entries and works like {@link java.util.List#indexOf(Object)} search.
|
||||||
|
* It is highly suggested to use HeapPriorityQueue otherwise, unless you know why you need this specific implementation
|
||||||
|
*/
|
||||||
|
public class BooleanArrayPriorityQueue extends AbstractBooleanPriorityQueue
|
||||||
|
{
|
||||||
|
/** The Backing Array */
|
||||||
|
protected transient boolean[] array = BooleanArrays.EMPTY_ARRAY;
|
||||||
|
/** The Amount of elements stored within the array */
|
||||||
|
protected int size;
|
||||||
|
/** The Last known first index pointer */
|
||||||
|
protected int firstIndex = -1;
|
||||||
|
/** The Sorter of the Array */
|
||||||
|
protected BooleanComparator comparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanArrayPriorityQueue() {
|
||||||
|
this(0, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using custom sorter
|
||||||
|
* @param comp Comparator to sort the Array. Can be null
|
||||||
|
*/
|
||||||
|
public BooleanArrayPriorityQueue(BooleanComparator comp) {
|
||||||
|
this(0, comp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor with a Min Capacity
|
||||||
|
* @param size the initial capacity of the backing array
|
||||||
|
* @throws IllegalStateException if the initial size is smaller 0
|
||||||
|
*/
|
||||||
|
public BooleanArrayPriorityQueue(int size) {
|
||||||
|
this(size, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor with a Min Capacity and custom Sorter
|
||||||
|
* @param size the initial capacity of the backing array
|
||||||
|
* @param comp Comparator to sort the Array. Can be null
|
||||||
|
* @throws IllegalStateException if the initial size is smaller 0
|
||||||
|
*/
|
||||||
|
public BooleanArrayPriorityQueue(int size, BooleanComparator comp) {
|
||||||
|
if(size < 0) throw new IllegalAccessError("Size has to be 0 or positive");
|
||||||
|
if(size > 0) array = new boolean[size];
|
||||||
|
comparator = comp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using a initial array
|
||||||
|
* @param array the Array that should be used
|
||||||
|
*/
|
||||||
|
public BooleanArrayPriorityQueue(boolean[] array) {
|
||||||
|
this(array, array.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using a initial array
|
||||||
|
* @param array the Array that should be used
|
||||||
|
* @param size the amount of elements found within the array
|
||||||
|
* @throws NegativeArraySizeException if size is smaller then 0
|
||||||
|
*/
|
||||||
|
public BooleanArrayPriorityQueue(boolean[] array, int size) {
|
||||||
|
this.array = Arrays.copyOf(array, size);
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using a initial array and a custom sorter
|
||||||
|
* @param array the Array that should be used
|
||||||
|
* @param comp Comparator to sort the Array. Can be null
|
||||||
|
*/
|
||||||
|
public BooleanArrayPriorityQueue(boolean[] array, BooleanComparator comp) {
|
||||||
|
this(array, array.length, comp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using a initial array and a custom sorter
|
||||||
|
* @param array the Array that should be used
|
||||||
|
* @param size the amount of elements found within the array
|
||||||
|
* @param comp Comparator to sort the Array. Can be null
|
||||||
|
* @throws NegativeArraySizeException if size is smaller then 0
|
||||||
|
*/
|
||||||
|
public BooleanArrayPriorityQueue(boolean[] array, int size, BooleanComparator comp) {
|
||||||
|
this.array = Arrays.copyOf(array, size);
|
||||||
|
this.size = size;
|
||||||
|
this.comparator = comp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using a Collection
|
||||||
|
* @param c the Collection that should be used
|
||||||
|
*/
|
||||||
|
public BooleanArrayPriorityQueue(BooleanCollection c) {
|
||||||
|
array = c.toBooleanArray();
|
||||||
|
size = c.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using a Collection and a custom sorter
|
||||||
|
* @param c the Collection that should be used
|
||||||
|
* @param comp Comparator to sort the Array. Can be null
|
||||||
|
*/
|
||||||
|
public BooleanArrayPriorityQueue(BooleanCollection c, BooleanComparator comp) {
|
||||||
|
array = c.toBooleanArray();
|
||||||
|
size = c.size();
|
||||||
|
comparator = comp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapping method to help serialization
|
||||||
|
* @param array the array that should be used
|
||||||
|
* @return a ArrayPriorityQueue containing the original input array
|
||||||
|
*/
|
||||||
|
public static BooleanArrayPriorityQueue wrap(boolean[] array) {
|
||||||
|
return wrap(array, array.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapping method to help serialization
|
||||||
|
* @param array the array that should be used
|
||||||
|
* @param size the amount of elements within the array
|
||||||
|
* @return a ArrayPriorityQueue containing the original input array
|
||||||
|
*/
|
||||||
|
public static BooleanArrayPriorityQueue wrap(boolean[] array, int size) {
|
||||||
|
BooleanArrayPriorityQueue queue = new BooleanArrayPriorityQueue();
|
||||||
|
queue.array = array;
|
||||||
|
queue.size = size;
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapping method to help serialization, using a custom sorter
|
||||||
|
* @param array the array that should be used
|
||||||
|
* @param comp Comparator to sort the Array. Can be null
|
||||||
|
* @return a ArrayPriorityQueue containing the original input array
|
||||||
|
*/
|
||||||
|
public static BooleanArrayPriorityQueue wrap(boolean[] array, BooleanComparator comp) {
|
||||||
|
return wrap(array, array.length, comp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapping method to help serialization, using a custom sorter
|
||||||
|
* @param array the array that should be used
|
||||||
|
* @param size the amount of elements within the array
|
||||||
|
* @param comp Comparator to sort the Array. Can be null
|
||||||
|
* @return a ArrayPriorityQueue containing the original input array
|
||||||
|
*/
|
||||||
|
public static BooleanArrayPriorityQueue wrap(boolean[] array, int size, BooleanComparator comp) {
|
||||||
|
BooleanArrayPriorityQueue queue = new BooleanArrayPriorityQueue(comp);
|
||||||
|
queue.array = array;
|
||||||
|
queue.size = size;
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enqueue(boolean e) {
|
||||||
|
if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (long)(array.length >> 1), (long)SanityChecks.MAX_ARRAY_SIZE), size+1));
|
||||||
|
if(firstIndex != -1){
|
||||||
|
int compare = comparator == null ? Boolean.compare(e, array[firstIndex]) : comparator.compare(e, array[firstIndex]);
|
||||||
|
if(compare < 0) firstIndex = size;
|
||||||
|
else if(compare > 0) firstIndex = -1;
|
||||||
|
}
|
||||||
|
array[size++] = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean dequeue() {
|
||||||
|
if(size <= 0) throw new NoSuchElementException();
|
||||||
|
int index = findFirstIndex();
|
||||||
|
boolean value = array[index];
|
||||||
|
if(index != --size) System.arraycopy(array, index+1, array, index, size - index);
|
||||||
|
firstIndex = -1;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean first() {
|
||||||
|
if(isEmpty()) throw new NoSuchElementException();
|
||||||
|
if(firstIndex == -1) findFirstIndex();
|
||||||
|
return array[firstIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean peek(int index) {
|
||||||
|
if(index < 0 || index >= size) throw new NoSuchElementException();
|
||||||
|
return array[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeFirst(boolean e) {
|
||||||
|
for(int i = 0;i<size;i++)
|
||||||
|
if(e == array[i]) return removeIndex(i);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeLast(boolean e) {
|
||||||
|
for(int i = size-1;i>=0;i--)
|
||||||
|
if(e == array[i]) return removeIndex(i);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean removeIndex(int index) {
|
||||||
|
if(index != --size) System.arraycopy(array, index+1, array, index, size - index);
|
||||||
|
if(index == firstIndex) firstIndex = -1;
|
||||||
|
else if(firstIndex != -1 && index >= firstIndex) firstIndex--;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChanged() {
|
||||||
|
firstIndex = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
for(int i = 0,m=size;i<m;i++) action.accept(dequeue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEachIndexed(IntBooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
for(int i = 0,m=size;i<m;i++) action.accept(i, dequeue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E> void forEach(E input, ObjectBooleanConsumer<E> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
for(int i = 0,m=size;i<m;i++) action.accept(input, dequeue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matchesAny(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(filter.test(array[i])) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matchesNone(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(filter.test(array[i])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matchesAll(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(!filter.test(array[i])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean reduce(boolean identity, BooleanBooleanUnaryOperator operator) {
|
||||||
|
Objects.requireNonNull(operator);
|
||||||
|
boolean state = identity;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
state = operator.applyAsBoolean(state, array[i]);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean reduce(BooleanBooleanUnaryOperator operator) {
|
||||||
|
Objects.requireNonNull(operator);
|
||||||
|
boolean state = false;
|
||||||
|
boolean empty = true;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(empty) {
|
||||||
|
empty = false;
|
||||||
|
state = array[i];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
state = operator.applyAsBoolean(state, array[i]);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean findFirst(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(filter.test(array[i])) {
|
||||||
|
boolean data = array[i];
|
||||||
|
removeIndex(i);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int count(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
int result = 0;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(filter.test(array[i])) result++;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator() {
|
||||||
|
return new Iter();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanArrayPriorityQueue copy() {
|
||||||
|
BooleanArrayPriorityQueue queue = new BooleanArrayPriorityQueue();
|
||||||
|
queue.firstIndex = firstIndex;
|
||||||
|
queue.size = size;
|
||||||
|
queue.comparator = comparator;
|
||||||
|
queue.array = Arrays.copyOf(array, array.length);
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanComparator comparator() {
|
||||||
|
return comparator;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] toBooleanArray(boolean[] input) {
|
||||||
|
if(input == null || input.length < size()) input = new boolean[size()];
|
||||||
|
System.arraycopy(array, 0, input, 0, size());
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int findFirstIndex() {
|
||||||
|
if(firstIndex == -1) {
|
||||||
|
int index = size-1;
|
||||||
|
boolean value = array[index];
|
||||||
|
if(comparator == null) {
|
||||||
|
for(int i = index;i>=0;i--) {
|
||||||
|
if(Boolean.compare(array[i], value) < 0)
|
||||||
|
value = array[index = i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
for(int i = index;i>=0;i--) {
|
||||||
|
if(comparator.compare(array[i], value) < 0)
|
||||||
|
value = array[index = i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
firstIndex = index;
|
||||||
|
}
|
||||||
|
return firstIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Iter implements BooleanIterator {
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return !isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean nextBoolean() {
|
||||||
|
if(!hasNext()) throw new NoSuchElementException();
|
||||||
|
return dequeue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,369 @@
|
|||||||
|
package speiger.src.collections.booleans.queues;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanCollection;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanIterator;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanComparator;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanConsumer;
|
||||||
|
import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer;
|
||||||
|
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanPredicate;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanBooleanUnaryOperator;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanArrays;
|
||||||
|
import speiger.src.collections.utils.SanityChecks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Simple Heap base Priority Queue implementation
|
||||||
|
* It is a ArrayBased Alternative to TreeSets that has less object allocations
|
||||||
|
*/
|
||||||
|
public class BooleanHeapPriorityQueue extends AbstractBooleanPriorityQueue
|
||||||
|
{
|
||||||
|
/** The Backing Array */
|
||||||
|
protected transient boolean[] array = BooleanArrays.EMPTY_ARRAY;
|
||||||
|
/** The Amount of elements stored within the array */
|
||||||
|
protected int size;
|
||||||
|
/** The Sorter of the Array */
|
||||||
|
protected BooleanComparator comparator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default Constructor
|
||||||
|
*/
|
||||||
|
public BooleanHeapPriorityQueue() {
|
||||||
|
this(0, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using custom sorter
|
||||||
|
* @param comp Comparator to sort the Array. Can be null
|
||||||
|
*/
|
||||||
|
public BooleanHeapPriorityQueue(BooleanComparator comp) {
|
||||||
|
this(0, comp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor with a Min Capacity
|
||||||
|
* @param size the initial capacity of the backing array
|
||||||
|
* @throws IllegalStateException if the initial size is smaller 0
|
||||||
|
*/
|
||||||
|
public BooleanHeapPriorityQueue(int size) {
|
||||||
|
this(size, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor with a Min Capacity and custom Sorter
|
||||||
|
* @param size the initial capacity of the backing array
|
||||||
|
* @param comp Comparator to sort the Array. Can be null
|
||||||
|
* @throws IllegalStateException if the initial size is smaller 0
|
||||||
|
*/
|
||||||
|
public BooleanHeapPriorityQueue(int size, BooleanComparator comp) {
|
||||||
|
if(size > 0) array = new boolean[size];
|
||||||
|
comparator = comp;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using a initial array
|
||||||
|
* @param array the Array that should be used
|
||||||
|
*/
|
||||||
|
public BooleanHeapPriorityQueue(boolean[] array) {
|
||||||
|
this(array, array.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using a initial array
|
||||||
|
* @param array the Array that should be used
|
||||||
|
* @param size the amount of elements found within the array
|
||||||
|
* @throws NegativeArraySizeException if size is smaller then 0
|
||||||
|
*/
|
||||||
|
public BooleanHeapPriorityQueue(boolean[] array, int size) {
|
||||||
|
this.array = Arrays.copyOf(array, size);
|
||||||
|
this.size = size;
|
||||||
|
BooleanArrays.heapify(array, size, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using a initial array and a custom sorter
|
||||||
|
* @param array the Array that should be used
|
||||||
|
* @param comp Comparator to sort the Array. Can be null
|
||||||
|
*/
|
||||||
|
public BooleanHeapPriorityQueue(boolean[] array, BooleanComparator comp) {
|
||||||
|
this(array, array.length, comp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using a initial array and a custom sorter
|
||||||
|
* @param array the Array that should be used
|
||||||
|
* @param size the amount of elements found within the array
|
||||||
|
* @param comp Comparator to sort the Array. Can be null
|
||||||
|
* @throws NegativeArraySizeException if size is smaller then 0
|
||||||
|
*/
|
||||||
|
public BooleanHeapPriorityQueue(boolean[] array, int size, BooleanComparator comp) {
|
||||||
|
this.array = Arrays.copyOf(array, size);
|
||||||
|
this.size = size;
|
||||||
|
comparator = comp;
|
||||||
|
BooleanArrays.heapify(array, size, comp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using a Collection
|
||||||
|
* @param c the Collection that should be used
|
||||||
|
*/
|
||||||
|
public BooleanHeapPriorityQueue(BooleanCollection c) {
|
||||||
|
array = c.toBooleanArray();
|
||||||
|
size = c.size();
|
||||||
|
BooleanArrays.heapify(array, size, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor using a Collection and a custom sorter
|
||||||
|
* @param c the Collection that should be used
|
||||||
|
* @param comp Comparator to sort the Array. Can be null
|
||||||
|
*/
|
||||||
|
public BooleanHeapPriorityQueue(BooleanCollection c, BooleanComparator comp) {
|
||||||
|
array = c.toBooleanArray();
|
||||||
|
size = c.size();
|
||||||
|
comparator = comp;
|
||||||
|
BooleanArrays.heapify(array, size, comp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapping method to help serialization
|
||||||
|
* @param array the array that should be used
|
||||||
|
* @return a HeapPriorityQueue containing the original input array
|
||||||
|
*/
|
||||||
|
public static BooleanHeapPriorityQueue wrap(boolean[] array) {
|
||||||
|
return wrap(array, array.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapping method to help serialization
|
||||||
|
* @param array the array that should be used
|
||||||
|
* @param size the amount of elements within the array
|
||||||
|
* @return a HeapPriorityQueue containing the original input array
|
||||||
|
*/
|
||||||
|
public static BooleanHeapPriorityQueue wrap(boolean[] array, int size) {
|
||||||
|
BooleanHeapPriorityQueue queue = new BooleanHeapPriorityQueue();
|
||||||
|
queue.array = array;
|
||||||
|
queue.size = size;
|
||||||
|
BooleanArrays.heapify(array, size, null);
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapping method to help serialization, using a custom sorter
|
||||||
|
* @param array the array that should be used
|
||||||
|
* @param comp Comparator to sort the Array. Can be null
|
||||||
|
* @return a HeapPriorityQueue containing the original input array
|
||||||
|
*/
|
||||||
|
public static BooleanHeapPriorityQueue wrap(boolean[] array, BooleanComparator comp) {
|
||||||
|
return wrap(array, array.length, comp);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapping method to help serialization, using a custom sorter
|
||||||
|
* @param array the array that should be used
|
||||||
|
* @param size the amount of elements within the array
|
||||||
|
* @param comp Comparator to sort the Array. Can be null
|
||||||
|
* @return a HeapPriorityQueue containing the original input array
|
||||||
|
*/
|
||||||
|
public static BooleanHeapPriorityQueue wrap(boolean[] array, int size, BooleanComparator comp) {
|
||||||
|
BooleanHeapPriorityQueue queue = new BooleanHeapPriorityQueue(comp);
|
||||||
|
queue.array = array;
|
||||||
|
queue.size = size;
|
||||||
|
BooleanArrays.heapify(array, size, comp);
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator() {
|
||||||
|
return new Iter();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enqueue(boolean e) {
|
||||||
|
if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (long)(array.length >> 1), (long)SanityChecks.MAX_ARRAY_SIZE), size+1));
|
||||||
|
array[size++] = e;
|
||||||
|
BooleanArrays.shiftUp(array, size-1, comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean dequeue() {
|
||||||
|
if(size <= 0) throw new NoSuchElementException();
|
||||||
|
boolean value = array[0];
|
||||||
|
array[0] = array[--size];
|
||||||
|
if(size != 0) BooleanArrays.shiftDown(array, size, 0, comparator);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean peek(int index) {
|
||||||
|
if(index < 0 || index >= size) throw new NoSuchElementException();
|
||||||
|
return array[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeFirst(boolean e) {
|
||||||
|
for(int i = 0;i<size;i++)
|
||||||
|
if(e == array[i]) return removeIndex(i);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeLast(boolean e) {
|
||||||
|
for(int i = size-1;i>=0;i--)
|
||||||
|
if(e == array[i]) return removeIndex(i);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
for(int i = 0,m=size;i<m;i++) action.accept(dequeue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEachIndexed(IntBooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
for(int i = 0,m=size;i<m;i++) action.accept(i, dequeue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E> void forEach(E input, ObjectBooleanConsumer<E> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
for(int i = 0,m=size;i<m;i++) action.accept(input, dequeue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matchesAny(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(filter.test(array[i])) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matchesNone(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(filter.test(array[i])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matchesAll(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(!filter.test(array[i])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean reduce(boolean identity, BooleanBooleanUnaryOperator operator) {
|
||||||
|
Objects.requireNonNull(operator);
|
||||||
|
boolean state = identity;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
state = operator.applyAsBoolean(state, array[i]);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean reduce(BooleanBooleanUnaryOperator operator) {
|
||||||
|
Objects.requireNonNull(operator);
|
||||||
|
boolean state = false;
|
||||||
|
boolean empty = true;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(empty) {
|
||||||
|
empty = false;
|
||||||
|
state = array[i];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
state = operator.applyAsBoolean(state, array[i]);
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean findFirst(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(filter.test(array[i])) {
|
||||||
|
boolean data = array[i];
|
||||||
|
removeIndex(i);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int count(BooleanPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
int result = 0;
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(filter.test(array[i])) result++;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean removeIndex(int index) {
|
||||||
|
array[index] = array[--size];
|
||||||
|
if(size != index) BooleanArrays.shiftDown(array, size, index, comparator);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChanged() {
|
||||||
|
if(size <= 0) return;
|
||||||
|
BooleanArrays.shiftDown(array, size, 0, comparator);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanHeapPriorityQueue copy() {
|
||||||
|
BooleanHeapPriorityQueue queue = new BooleanHeapPriorityQueue();
|
||||||
|
queue.size = size;
|
||||||
|
queue.comparator = comparator;
|
||||||
|
queue.array = Arrays.copyOf(array, array.length);
|
||||||
|
return queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanComparator comparator() {
|
||||||
|
return comparator;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] toBooleanArray(boolean[] input) {
|
||||||
|
if(input == null || input.length < size()) input = new boolean[size()];
|
||||||
|
System.arraycopy(array, 0, input, 0, size());
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Iter implements BooleanIterator {
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return !isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean nextBoolean() {
|
||||||
|
if(!hasNext()) throw new NoSuchElementException();
|
||||||
|
return dequeue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,84 @@
|
|||||||
|
package speiger.src.collections.booleans.queues;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanCollection;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanIterator;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanPriorityQueues;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Speciifc PriorityDeque or Dequeue interface to allow implementations like FIFO queues.
|
||||||
|
*/
|
||||||
|
public interface BooleanPriorityDequeue extends BooleanPriorityQueue
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Method to insert a element into the first Index instead of the last.
|
||||||
|
* @param e the element that should be inserted into the first place
|
||||||
|
*/
|
||||||
|
public void enqueueFirst(boolean e);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to mass insert a elements into the first Index of the PriorityDequeue.
|
||||||
|
* @param e the elements that should be inserted
|
||||||
|
*/
|
||||||
|
public default void enqueueAllFirst(boolean... e) {
|
||||||
|
enqueueAllFirst(e, 0, e.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to mass insert a elements into the first Index of the PriorityDequeue.
|
||||||
|
* @param e the elements that should be inserted
|
||||||
|
* @param length the amount of elements that should be inserted
|
||||||
|
*/
|
||||||
|
public default void enqueueAllFirst(boolean[] e, int length) {
|
||||||
|
enqueueAllFirst(e, 0, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to mass insert a elements into the first Index of the PriorityDequeue.
|
||||||
|
* @param e the elements that should be inserted
|
||||||
|
* @param offset the offset where in the array should be started
|
||||||
|
* @param length the amount of elements that should be inserted
|
||||||
|
*/
|
||||||
|
public default void enqueueAllFirst(boolean[] e, int offset, int length) {
|
||||||
|
for(int i = 0;i<length;i++)
|
||||||
|
enqueueFirst(e[i+offset]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to mass insert elements into first Index of the PriorityDequeue.
|
||||||
|
* @param c the elements that should be inserted from the Collection
|
||||||
|
*/
|
||||||
|
public default void enqueueAllFirst(BooleanCollection c) {
|
||||||
|
for(BooleanIterator iter = c.iterator();iter.hasNext();)
|
||||||
|
enqueueFirst(iter.nextBoolean());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Method to remove a element from the last place instead of the first
|
||||||
|
* @return the last element inserted
|
||||||
|
* @throws java.util.NoSuchElementException if no element is in the deque
|
||||||
|
*/
|
||||||
|
public boolean dequeueLast();
|
||||||
|
/**
|
||||||
|
* Peeking function for the last element
|
||||||
|
* @return the Last Element within the dequeue without deleting it
|
||||||
|
*/
|
||||||
|
public default boolean last() { return peek(size()-1); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Wrapped PriorityDequeue that is Synchronized
|
||||||
|
* @return a new PriorityDequeue that is synchronized
|
||||||
|
* @see BooleanPriorityQueues#synchronize
|
||||||
|
*/
|
||||||
|
public default BooleanPriorityDequeue synchronizeQueue() { return BooleanPriorityQueues.synchronize(this); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Wrapped PriorityDequeue that is Synchronized
|
||||||
|
* @param mutex is the controller of the synchronization block
|
||||||
|
* @return a new PriorityDequeue Wrapper that is synchronized
|
||||||
|
* @see BooleanPriorityQueues#synchronize
|
||||||
|
*/
|
||||||
|
public default BooleanPriorityDequeue synchronizeQueue(Object mutex) { return BooleanPriorityQueues.synchronize(this, mutex); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanPriorityDequeue copy();
|
||||||
|
}
|
||||||
@ -0,0 +1,151 @@
|
|||||||
|
package speiger.src.collections.booleans.queues;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanComparator;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanCollection;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanIterable;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanIterator;
|
||||||
|
import speiger.src.collections.booleans.utils.BooleanPriorityQueues;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Simple PriorityQueue (or Queue) interface that provides with the nessesary functions to interact with it, without cluttering with the Collection interface.
|
||||||
|
*/
|
||||||
|
public interface BooleanPriorityQueue extends BooleanIterable
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return true if the PriorityQueue is empty
|
||||||
|
*/
|
||||||
|
public default boolean isEmpty() { return size() <= 0; }
|
||||||
|
/**
|
||||||
|
* @return the amount of elements that are stored in the PriorityQueue
|
||||||
|
*/
|
||||||
|
public int size();
|
||||||
|
/**
|
||||||
|
* clears all elements within the PriorityQueue,
|
||||||
|
* this does not resize the backing arrays
|
||||||
|
*/
|
||||||
|
public void clear();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to insert a element into the PriorityQueue
|
||||||
|
* @param e the element that should be inserted
|
||||||
|
*/
|
||||||
|
public void enqueue(boolean e);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to mass insert elements into the PriorityQueue
|
||||||
|
* @param e the elements that should be inserted
|
||||||
|
*/
|
||||||
|
public default void enqueueAll(boolean... e) {
|
||||||
|
enqueueAll(e, 0, e.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to mass insert elements into the PriorityQueue
|
||||||
|
* @param e the elements that should be inserted
|
||||||
|
* @param length the amount of elements that should be inserted
|
||||||
|
*/
|
||||||
|
public default void enqueueAll(boolean[] e, int length) {
|
||||||
|
enqueueAll(e, 0, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to mass insert elements into the PriorityQueue
|
||||||
|
* @param e the elements that should be inserted
|
||||||
|
* @param offset the offset where in the array should be started
|
||||||
|
* @param length the amount of elements that should be inserted
|
||||||
|
*/
|
||||||
|
public default void enqueueAll(boolean[] e, int offset, int length) {
|
||||||
|
for(int i = 0;i<length;i++)
|
||||||
|
enqueue(e[i+offset]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to mass insert elements into the PriorityQueue
|
||||||
|
* @param c the elements that should be inserted from the Collection
|
||||||
|
*/
|
||||||
|
public default void enqueueAll(BooleanCollection c) {
|
||||||
|
for(BooleanIterator iter = c.iterator();iter.hasNext();)
|
||||||
|
enqueue(iter.nextBoolean());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to extract a element from the PriorityQueue
|
||||||
|
* @return a element from the Queue
|
||||||
|
* @throws java.util.NoSuchElementException if no element is present
|
||||||
|
*/
|
||||||
|
public boolean dequeue();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Peeking function to see whats inside the queue.
|
||||||
|
* @param index of the element that is requested to be viewed.
|
||||||
|
* @return the element that is requested
|
||||||
|
*/
|
||||||
|
public boolean peek(int index);
|
||||||
|
/**
|
||||||
|
* Shows the element that is to be returned next
|
||||||
|
* @return the first element in the Queue
|
||||||
|
*/
|
||||||
|
public default boolean first() { return peek(0); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the first found element in the queue
|
||||||
|
* @param e the element that should be removed
|
||||||
|
* @return if a searched element was removed
|
||||||
|
*/
|
||||||
|
public boolean removeFirst(boolean e);
|
||||||
|
/**
|
||||||
|
* Removes the last found element in the queue
|
||||||
|
* @param e the element that should be removed
|
||||||
|
* @return if a searched element was removed
|
||||||
|
*/
|
||||||
|
public boolean removeLast(boolean e);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allows to notify the Queue to be revalidate its data
|
||||||
|
*/
|
||||||
|
public void onChanged();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Function that does a shallow clone of the PriorityQueue itself.
|
||||||
|
* This function is more optimized then a copy constructor since the PriorityQueue does not have to be unsorted/resorted.
|
||||||
|
* It can be compared to Cloneable but with less exception risk
|
||||||
|
* @return a Shallow Copy of the PriorityQueue
|
||||||
|
* @note Wrappers and view PriorityQueues will not support this feature
|
||||||
|
*/
|
||||||
|
public BooleanPriorityQueue copy();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the sorter of the Queue, can be null
|
||||||
|
*/
|
||||||
|
public BooleanComparator comparator();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Wrapped PriorityQueue that is Synchronized
|
||||||
|
* @return a new PriorityQueue that is synchronized
|
||||||
|
* @see BooleanPriorityQueues#synchronize
|
||||||
|
*/
|
||||||
|
public default BooleanPriorityQueue synchronizeQueue() { return BooleanPriorityQueues.synchronize(this); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Wrapped PriorityQueue that is Synchronized
|
||||||
|
* @param mutex is the controller of the synchronization block
|
||||||
|
* @return a new PriorityQueue Wrapper that is synchronized
|
||||||
|
* @see BooleanPriorityQueues#synchronize
|
||||||
|
*/
|
||||||
|
public default BooleanPriorityQueue synchronizeQueue(Object mutex) { return BooleanPriorityQueues.synchronize(this, mutex); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A method to drop the contents of the Queue without clearing the queue
|
||||||
|
* @return the contents of the queue into a seperate array.
|
||||||
|
*/
|
||||||
|
public default boolean[] toBooleanArray() { return toBooleanArray(new boolean[size()]); }
|
||||||
|
/**
|
||||||
|
* A method to drop the contents of the Queue without clearing the queue
|
||||||
|
* @param input where the elements should be inserted to. If it does not fit then it creates a new appropiatly created array
|
||||||
|
* @return the contents of the queue into a seperate array.
|
||||||
|
* @note if the Type is generic then a Object Array is created instead of a Type Array
|
||||||
|
*/
|
||||||
|
public boolean[] toBooleanArray(boolean[] input);
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,858 @@
|
|||||||
|
package speiger.src.collections.booleans.utils;
|
||||||
|
|
||||||
|
import java.util.concurrent.CancellationException;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
import java.util.concurrent.locks.LockSupport;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanConsumer;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanComparator;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanIterable;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanCollection;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanIterator;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanTask;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanPredicate;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanFunction;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanBooleanUnaryOperator;
|
||||||
|
import speiger.src.collections.booleans.lists.BooleanList;
|
||||||
|
import speiger.src.collections.booleans.lists.BooleanArrayList;
|
||||||
|
import speiger.src.collections.objects.utils.ObjectAsyncBuilder;
|
||||||
|
import speiger.src.collections.objects.utils.ObjectAsyncBuilder.BaseObjectTask;
|
||||||
|
import speiger.src.collections.ints.utils.IntAsyncBuilder;
|
||||||
|
import speiger.src.collections.ints.utils.IntAsyncBuilder.BaseIntTask;
|
||||||
|
import speiger.src.collections.utils.ISizeProvider;
|
||||||
|
import speiger.src.collections.utils.SanityChecks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* The Async API allows you to process collections on a different thread without having to deal with the Multithreading complexity. <br>
|
||||||
|
* It uses the Lightweight Stream Replace API to do its work, which can make it faster then sequential streams. <br>
|
||||||
|
* This feature isn't designed to do Multithreading work, but to allow moving work off the main thread into a worker thread. <br>
|
||||||
|
* So anything executed on this is still Singlethreaded. <br>
|
||||||
|
* <br>
|
||||||
|
* How it works is you create the AsyncBuilder using a Iterable of the Type. <br>
|
||||||
|
* Then select things you want to use on the Iterable. <br>
|
||||||
|
* - For example: map/filter/peek/limit/etc <br>
|
||||||
|
* <br>
|
||||||
|
* After that you select your action the Iterable should be applied on. <br>
|
||||||
|
* - For Example: forEach/findFirst/matchAny/etc <br>
|
||||||
|
* <br>
|
||||||
|
* Then optionally a custom Executor or callback can be applied <br>
|
||||||
|
* At the end either execute or join can be called to start the task. <br>
|
||||||
|
* - execute will return the Task reference so that the task can be traced back. <br>
|
||||||
|
* - join will await the completion of the task and return the return value <br>
|
||||||
|
* <br>
|
||||||
|
* During Construction a couple Disposable Builder Objects will be created. <br>
|
||||||
|
* These will be only used during construction. <br>
|
||||||
|
* <br>
|
||||||
|
* The Task Object is also pause-able/Interruptable at any moment during the Processing. <br>
|
||||||
|
*
|
||||||
|
* A small example
|
||||||
|
* <pre><code>
|
||||||
|
* public void processFiles(ObjectCollection<String> potentialFiles) {
|
||||||
|
* potentialFiles.asAsync()
|
||||||
|
* .map(Paths::get).filter(Files::exists) //Modifies the collection (Optional)
|
||||||
|
* .forEach(Files::delete) //Creates the action (Required)
|
||||||
|
* .onCompletion(T -> {}} //Callback on completion (Optional)
|
||||||
|
* .execute() //Starts the task. (Required)
|
||||||
|
* }
|
||||||
|
* </code></pre>
|
||||||
|
* @author Speiger
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class BooleanAsyncBuilder
|
||||||
|
{
|
||||||
|
BooleanIterable iterable;
|
||||||
|
BaseBooleanTask task;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main Constructor that uses a Iterable to build a Offthread Task.
|
||||||
|
* @param iterable that should be processed
|
||||||
|
*/
|
||||||
|
public BooleanAsyncBuilder(BooleanIterable iterable) {
|
||||||
|
this.iterable = iterable;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper constructor.
|
||||||
|
* @param task that had been build
|
||||||
|
*/
|
||||||
|
public BooleanAsyncBuilder(BaseBooleanTask task) {
|
||||||
|
this.task = task;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function that automatically wraps a Iterable into a AsyncBuilder since it forces this collections Iterable.
|
||||||
|
* @param iterable that should be wrapped
|
||||||
|
* @return a AsyncBuilder with the iterable wrapped
|
||||||
|
*/
|
||||||
|
public static BooleanAsyncBuilder of(Iterable<Boolean> iterable) {
|
||||||
|
return new BooleanAsyncBuilder(BooleanIterables.wrap(iterable));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function that automatically wraps a array into a AsyncBuilder since it forces this collections Iterable.
|
||||||
|
* @param values that should be wrapped
|
||||||
|
* @return a AsyncBuilder with the values wrapped
|
||||||
|
*/
|
||||||
|
public static BooleanAsyncBuilder of(boolean...values) {
|
||||||
|
return new BooleanAsyncBuilder(BooleanArrayList.wrap(values));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps the elements to something else
|
||||||
|
* @param mapper the mapping function
|
||||||
|
* @param <E> The return type.
|
||||||
|
* @return a new Builder Object with the mapped Iterable
|
||||||
|
*/
|
||||||
|
public <E> ObjectAsyncBuilder<E> map(BooleanFunction<E> mapper) {
|
||||||
|
return new ObjectAsyncBuilder<>(BooleanIterables.map(iterable, mapper));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps the elements to something else
|
||||||
|
* @param mapper the flatMapping function
|
||||||
|
* @param <V> The return type supplier.
|
||||||
|
* @param <E> The return type.
|
||||||
|
* @return a new Builder Object with the mapped Iterable
|
||||||
|
*/
|
||||||
|
public <E, V extends Iterable<E>> ObjectAsyncBuilder<E> flatMap(BooleanFunction<V> mapper) {
|
||||||
|
return new ObjectAsyncBuilder<>(BooleanIterables.flatMap(iterable, mapper));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps the elements to something else
|
||||||
|
* @param mapper the flatMapping function
|
||||||
|
* @param <E> The return type.
|
||||||
|
* @return a new Builder Object with the mapped Iterable
|
||||||
|
*/
|
||||||
|
public <E> ObjectAsyncBuilder<E> arrayflatMap(BooleanFunction<E[]> mapper) {
|
||||||
|
return new ObjectAsyncBuilder<>(BooleanIterables.arrayFlatMap(iterable, mapper));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters out the unwanted elements out of the Iterable
|
||||||
|
* @param filter the elements that should be kept
|
||||||
|
* @return Self with a filter applied
|
||||||
|
*/
|
||||||
|
public BooleanAsyncBuilder filter(BooleanPredicate filter) {
|
||||||
|
iterable = BooleanIterables.filter(iterable, filter);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes duplicated elements out of the Iterable
|
||||||
|
* @return Self with a deduplicator applied
|
||||||
|
*/
|
||||||
|
public BooleanAsyncBuilder distinct() {
|
||||||
|
iterable = BooleanIterables.distinct(iterable);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Repeats the elements inside of the Iterable
|
||||||
|
* @param repeats the amount of times the elements should be repeated
|
||||||
|
* @return self with a repeater applied
|
||||||
|
*/
|
||||||
|
public BooleanAsyncBuilder repeat(int repeats) {
|
||||||
|
iterable = BooleanIterables.repeat(iterable, repeats);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Limits how many elements are inside of the Iterable
|
||||||
|
* @param limit how many elements should max be iterated through
|
||||||
|
* @return self with a limiter applied
|
||||||
|
*/
|
||||||
|
public BooleanAsyncBuilder limit(long limit) {
|
||||||
|
iterable = BooleanIterables.limit(iterable, limit);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts the elements inside of the Iterable.
|
||||||
|
* This operation is heavily hurting performance because it rebuilds the entire iterator and then sorts it, and this will affect the pausing feature.
|
||||||
|
* @param sorter that sorts the elements.
|
||||||
|
* @return self with a sorter applied
|
||||||
|
*/
|
||||||
|
public BooleanAsyncBuilder sorted(BooleanComparator sorter) {
|
||||||
|
iterable = BooleanIterables.sorted(iterable, sorter);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allows to preview elements before they are processed
|
||||||
|
* @param action the action that should be applied
|
||||||
|
* @return self with a preview applied
|
||||||
|
*/
|
||||||
|
public BooleanAsyncBuilder peek(BooleanConsumer action) {
|
||||||
|
iterable = BooleanIterables.peek(iterable, action);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Iterates over the Iterable with a desired action
|
||||||
|
* @param action that should be applied
|
||||||
|
* @return a new Builder with the forEach action applied.
|
||||||
|
*/
|
||||||
|
public ObjectAsyncBuilder<Void> forEach(BooleanConsumer action) {
|
||||||
|
return new ObjectAsyncBuilder<>(new ForEachTask<>(iterable.iterator(), action));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reduces the elements inside of the Iterable down to one element
|
||||||
|
* @param operator that reduces the elements.
|
||||||
|
* @return self with the reduce action applied
|
||||||
|
*/
|
||||||
|
public BooleanAsyncBuilder reduce(BooleanBooleanUnaryOperator operator) {
|
||||||
|
task = new SimpleReduceTask(iterable.iterator(), operator);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reduces the elements inside of the Iterable down to one element using a identity element.
|
||||||
|
* @param identity the element the reduce function should start with
|
||||||
|
* @param operator that reduces the elements.
|
||||||
|
* @return a new Builder with the reduce function applied
|
||||||
|
*/
|
||||||
|
public BooleanAsyncBuilder reduce(boolean identity, BooleanBooleanUnaryOperator operator) {
|
||||||
|
return new BooleanAsyncBuilder(new ReduceTask(iterable.iterator(), operator, identity));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pours all elements of the Iterable down into a Array.
|
||||||
|
* @return a new Builder with the ToArray function applied
|
||||||
|
*/
|
||||||
|
public ObjectAsyncBuilder<boolean[]> toBooleanArray() {
|
||||||
|
return new ObjectAsyncBuilder<>(new ArrayTask(iterable));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pours all elements into a List that can be later
|
||||||
|
* @return a new Builder with the pour function applied
|
||||||
|
*/
|
||||||
|
public ObjectAsyncBuilder<BooleanList> pourAsList() {
|
||||||
|
return pour(new BooleanArrayList());
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Pours all elements into a collection that can be later
|
||||||
|
* @param <E> the return type
|
||||||
|
* @param collection the collection the elements
|
||||||
|
* @return a new Builder with the pour function applied
|
||||||
|
*/
|
||||||
|
public <E extends BooleanCollection> ObjectAsyncBuilder<E> pour(E collection) {
|
||||||
|
return new ObjectAsyncBuilder<>(new CollectTask<>(iterable.iterator(), collection));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches through the elements of the Iterable to find if the desired element is present.
|
||||||
|
* @param filter that decides the desired elements
|
||||||
|
* @return a new Builder with the matchAny function applied
|
||||||
|
*/
|
||||||
|
public BooleanAsyncBuilder matchAny(BooleanPredicate filter) {
|
||||||
|
return new BooleanAsyncBuilder(new MatchTask(iterable.iterator(), filter, 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches through the elements of the Iterable to find if unwanted elements are present.
|
||||||
|
* @param filter that decides the unwanted elements
|
||||||
|
* @return a new Builder with the matchNone function applied
|
||||||
|
*/
|
||||||
|
public BooleanAsyncBuilder matchNone(BooleanPredicate filter) {
|
||||||
|
return new BooleanAsyncBuilder(new MatchTask(iterable.iterator(), filter, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches through the elements of the Iterable to find if all the desired elements are present.
|
||||||
|
* @param filter that decides the desired elements
|
||||||
|
* @return a new Builder with the matchAll function applied
|
||||||
|
*/
|
||||||
|
public BooleanAsyncBuilder matchAll(BooleanPredicate filter) {
|
||||||
|
return new BooleanAsyncBuilder(new MatchTask(iterable.iterator(), filter, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches through the elements of the Iterable to find if the desired element.
|
||||||
|
* If not present it will return the default value of the type
|
||||||
|
* @param filter that decides the desired elements
|
||||||
|
* @return self with the findFirst function applied
|
||||||
|
*/
|
||||||
|
public BooleanAsyncBuilder findFirst(BooleanPredicate filter) {
|
||||||
|
task = new FindFirstTask(iterable.iterator(), filter);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Counts all desired elements inside the Iterable
|
||||||
|
* @param filter that decides the desired elements
|
||||||
|
* @return a new Builder with the count function applied
|
||||||
|
*/
|
||||||
|
public IntAsyncBuilder count(BooleanPredicate filter) {
|
||||||
|
return new IntAsyncBuilder(new CountTask(iterable.iterator(), filter));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optional way to add a custom executor that runs this offthread task.
|
||||||
|
* Can only be set after the action was decided on.
|
||||||
|
* @param executor that executes the task, defaults to {@link SanityChecks#invokeAsyncTask(Runnable) }
|
||||||
|
* @return self with the executor set
|
||||||
|
* @note has to be NonNull
|
||||||
|
*/
|
||||||
|
public BooleanAsyncBuilder executor(Executor executor) {
|
||||||
|
if(task == null) throw new IllegalStateException("Action is missing");
|
||||||
|
task.withExecutor(executor);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Optional way to set a callback that allows to compute actions after the task was completed.
|
||||||
|
* The state of the task has to be validated by the callback.
|
||||||
|
* @param callback that should be notified after completion of the task
|
||||||
|
* @return self with the callback set
|
||||||
|
* @note can be null
|
||||||
|
*/
|
||||||
|
public BooleanAsyncBuilder onCompletion(Consumer<BooleanTask> callback) {
|
||||||
|
if(task == null) throw new IllegalStateException("Action is missing");
|
||||||
|
task.withCallback(callback);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the Execution of the task without awaiting its result
|
||||||
|
* @return the task object that allow to trace it.
|
||||||
|
*/
|
||||||
|
public BooleanTask execute() {
|
||||||
|
BaseBooleanTask toRun = task;
|
||||||
|
toRun.begin();
|
||||||
|
task = null;
|
||||||
|
return toRun;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the Execution of the task and will await its completion, returning the result.
|
||||||
|
* @return the result of the task provided.
|
||||||
|
* @throws ExecutionException if the task threw a exception
|
||||||
|
* @throws InterruptedException if the caller thread was interrupted
|
||||||
|
*/
|
||||||
|
public boolean join() throws ExecutionException, InterruptedException {
|
||||||
|
BaseBooleanTask toRun = task;
|
||||||
|
task = null;
|
||||||
|
toRun.begin();
|
||||||
|
return toRun.getBoolean();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Starts the Execution of the task and will await its completion with a timeout, returning the result.
|
||||||
|
* @param timeout of how long the thread should wait the task to be completed
|
||||||
|
* @param unit of the desired waiting time.
|
||||||
|
* @return the result of the provided task
|
||||||
|
* @throws InterruptedException if the caller thread was interrupted
|
||||||
|
* @throws ExecutionException if the task threw a exception
|
||||||
|
* @throws TimeoutException if the timeout was reached before the task was finished
|
||||||
|
*/
|
||||||
|
public boolean join(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
|
||||||
|
BaseBooleanTask toRun = task;
|
||||||
|
task = null;
|
||||||
|
toRun.begin();
|
||||||
|
return toRun.getBoolean(timeout, unit);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ReduceTask extends BaseBooleanTask
|
||||||
|
{
|
||||||
|
BooleanIterator iter;
|
||||||
|
BooleanBooleanUnaryOperator operator;
|
||||||
|
boolean value;
|
||||||
|
|
||||||
|
public ReduceTask(BooleanIterator iter, BooleanBooleanUnaryOperator operator, boolean value) {
|
||||||
|
this.iter = iter;
|
||||||
|
this.operator = operator;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean execute() throws Exception {
|
||||||
|
while(shouldRun() && iter.hasNext()) {
|
||||||
|
value = operator.apply(value, iter.nextBoolean());
|
||||||
|
}
|
||||||
|
if(!iter.hasNext()) {
|
||||||
|
setResult(value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCompletion() {
|
||||||
|
super.onCompletion();
|
||||||
|
iter = null;
|
||||||
|
operator = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class SimpleReduceTask extends BaseBooleanTask
|
||||||
|
{
|
||||||
|
BooleanIterator iter;
|
||||||
|
BooleanBooleanUnaryOperator operator;
|
||||||
|
boolean first = true;
|
||||||
|
boolean value;
|
||||||
|
|
||||||
|
public SimpleReduceTask(BooleanIterator iter, BooleanBooleanUnaryOperator operator) {
|
||||||
|
this.iter = iter;
|
||||||
|
this.operator = operator;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean execute() throws Exception {
|
||||||
|
while(shouldRun() && iter.hasNext()) {
|
||||||
|
if(first) {
|
||||||
|
first = false;
|
||||||
|
value = iter.nextBoolean();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
value = operator.applyAsBoolean(value, iter.nextBoolean());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!iter.hasNext()) {
|
||||||
|
setResult(value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCompletion() {
|
||||||
|
super.onCompletion();
|
||||||
|
iter = null;
|
||||||
|
operator = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class MatchTask extends BaseBooleanTask
|
||||||
|
{
|
||||||
|
BooleanIterator iter;
|
||||||
|
BooleanPredicate filter;
|
||||||
|
int type;
|
||||||
|
|
||||||
|
public MatchTask(BooleanIterator iter, BooleanPredicate filter, int type) {
|
||||||
|
this.iter = iter;
|
||||||
|
this.filter = filter;
|
||||||
|
this.type = type;
|
||||||
|
if(type < 0 || type > 2) throw new IllegalArgumentException("Type is not allowed has to be between 0-2");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean execute() throws Exception {
|
||||||
|
switch(type) {
|
||||||
|
case 0:
|
||||||
|
while(shouldRun() && iter.hasNext()) {
|
||||||
|
if(filter.test(iter.nextBoolean())) {
|
||||||
|
setResult(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
while(shouldRun() && iter.hasNext()) {
|
||||||
|
if(filter.test(iter.nextBoolean())) {
|
||||||
|
setResult(false);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
while(shouldRun() && iter.hasNext()) {
|
||||||
|
if(!filter.test(iter.nextBoolean())) {
|
||||||
|
setResult(false);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(!iter.hasNext()) {
|
||||||
|
setResult(type >= 1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCompletion() {
|
||||||
|
super.onCompletion();
|
||||||
|
iter = null;
|
||||||
|
filter = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class FindFirstTask extends BaseBooleanTask
|
||||||
|
{
|
||||||
|
BooleanIterator iter;
|
||||||
|
BooleanPredicate filter;
|
||||||
|
|
||||||
|
public FindFirstTask(BooleanIterator iter, BooleanPredicate filter) {
|
||||||
|
this.iter = iter;
|
||||||
|
this.filter = filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean execute() throws Exception {
|
||||||
|
while(shouldRun() && iter.hasNext()) {
|
||||||
|
boolean entry = iter.nextBoolean();
|
||||||
|
if(filter.test(iter.nextBoolean())) {
|
||||||
|
setResult(entry);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return !iter.hasNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCompletion() {
|
||||||
|
super.onCompletion();
|
||||||
|
iter = null;
|
||||||
|
filter = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class CountTask extends BaseIntTask
|
||||||
|
{
|
||||||
|
BooleanIterator iter;
|
||||||
|
BooleanPredicate filter;
|
||||||
|
int counted = 0;
|
||||||
|
|
||||||
|
public CountTask(BooleanIterator iter, BooleanPredicate filter) {
|
||||||
|
this.iter = iter;
|
||||||
|
this.filter = filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean execute() throws Exception {
|
||||||
|
while(shouldRun() && iter.hasNext()) {
|
||||||
|
if(filter.test(iter.nextBoolean())) {
|
||||||
|
counted++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!iter.hasNext())
|
||||||
|
{
|
||||||
|
setResult(counted);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCompletion() {
|
||||||
|
super.onCompletion();
|
||||||
|
iter = null;
|
||||||
|
filter = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class CollectTask<T extends BooleanCollection> extends BaseObjectTask<T>
|
||||||
|
{
|
||||||
|
BooleanIterator iter;
|
||||||
|
T collection;
|
||||||
|
|
||||||
|
public CollectTask(BooleanIterator iter, T collection) {
|
||||||
|
this.iter = iter;
|
||||||
|
this.collection = collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean execute() throws Exception {
|
||||||
|
while(shouldRun() && iter.hasNext()) {
|
||||||
|
collection.add(iter.nextBoolean());
|
||||||
|
}
|
||||||
|
if(!iter.hasNext()) {
|
||||||
|
setResult(collection);
|
||||||
|
collection = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCompletion() {
|
||||||
|
super.onCompletion();
|
||||||
|
iter = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ArrayTask extends BaseObjectTask<boolean[]>
|
||||||
|
{
|
||||||
|
BooleanIterator iter;
|
||||||
|
BooleanCollections.CollectionWrapper wrapper;
|
||||||
|
|
||||||
|
public ArrayTask(BooleanIterable iterable) {
|
||||||
|
iter = iterable.iterator();
|
||||||
|
ISizeProvider prov = ISizeProvider.of(iterable);
|
||||||
|
int size = prov == null ? -1 : prov.size();
|
||||||
|
wrapper = size < 0 ? BooleanCollections.wrapper() : BooleanCollections.wrapper(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean execute() throws Exception {
|
||||||
|
while(shouldRun() && iter.hasNext()) {
|
||||||
|
wrapper.add(iter.nextBoolean());
|
||||||
|
}
|
||||||
|
if(!iter.hasNext()) {
|
||||||
|
setResult(wrapper.toBooleanArray());
|
||||||
|
wrapper = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCompletion() {
|
||||||
|
super.onCompletion();
|
||||||
|
iter = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ForEachTask<T> extends BaseObjectTask<Void>
|
||||||
|
{
|
||||||
|
BooleanIterator iter;
|
||||||
|
BooleanConsumer listener;
|
||||||
|
|
||||||
|
public ForEachTask(BooleanIterator iter, BooleanConsumer listener) {
|
||||||
|
this.iter = iter;
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean execute() throws Exception {
|
||||||
|
while(shouldRun() && iter.hasNext()) {
|
||||||
|
listener.accept(iter.nextBoolean());
|
||||||
|
}
|
||||||
|
return !iter.hasNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCompletion() {
|
||||||
|
super.onCompletion();
|
||||||
|
iter = null;
|
||||||
|
listener = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base Task of the Actions that can be performed.
|
||||||
|
* Allows to simplify the actions that get executed.
|
||||||
|
*/
|
||||||
|
public abstract static class BaseBooleanTask implements BooleanTask
|
||||||
|
{
|
||||||
|
private static final int CREATED = 0;
|
||||||
|
private static final int RUNNING = 1;
|
||||||
|
private static final int PAUSING = 2;
|
||||||
|
private static final int PAUSED = 3;
|
||||||
|
private static final int FINISHING = 4;
|
||||||
|
private static final int FINISHED = 5;
|
||||||
|
private static final int EXCEPTIONALLY = 6;
|
||||||
|
private static final int CANCELLED = 7;
|
||||||
|
private volatile WaitNode waiter;
|
||||||
|
private volatile int state = CREATED;
|
||||||
|
Consumer<BooleanTask> callback;
|
||||||
|
Executor executor = SanityChecks::invokeAsyncTask;
|
||||||
|
boolean result;
|
||||||
|
Throwable excpetion;
|
||||||
|
|
||||||
|
void withCallback(Consumer<BooleanTask> callback) {
|
||||||
|
this.callback = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
void withExecutor(Executor executor) {
|
||||||
|
this.executor = executor;
|
||||||
|
}
|
||||||
|
|
||||||
|
void begin() {
|
||||||
|
executor.execute(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract boolean execute() throws Exception;
|
||||||
|
|
||||||
|
protected void onCompletion() {
|
||||||
|
if(callback != null) {
|
||||||
|
callback.accept(this);
|
||||||
|
callback = null;
|
||||||
|
}
|
||||||
|
executor = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setResult(boolean result) {
|
||||||
|
this.result = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
state = RUNNING;
|
||||||
|
try {
|
||||||
|
if(execute()) {
|
||||||
|
state = FINISHING;
|
||||||
|
finishCompletion(FINISHED);
|
||||||
|
}
|
||||||
|
else if(state == PAUSING) {
|
||||||
|
state = PAUSED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(Exception e) {
|
||||||
|
state = EXCEPTIONALLY;
|
||||||
|
this.excpetion = e;
|
||||||
|
finishCompletion(EXCEPTIONALLY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void finishCompletion(int nextState) {
|
||||||
|
WaitNode current = waiter;
|
||||||
|
waiter = null;
|
||||||
|
while(current != null) {
|
||||||
|
Thread t = current.thread;
|
||||||
|
if (t != null) {
|
||||||
|
current.thread = null;
|
||||||
|
LockSupport.unpark(t);
|
||||||
|
}
|
||||||
|
WaitNode next = current.next;
|
||||||
|
if (next == null)
|
||||||
|
break;
|
||||||
|
current.next = null;
|
||||||
|
current = next;
|
||||||
|
}
|
||||||
|
state = nextState;
|
||||||
|
onCompletion();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean cancel(boolean cancelIfRunnning) {
|
||||||
|
if(state == RUNNING && !cancelIfRunnning) return false;
|
||||||
|
state = CANCELLED;
|
||||||
|
finishCompletion(CANCELLED);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBoolean() throws InterruptedException, ExecutionException {
|
||||||
|
int s = state;
|
||||||
|
return report(s <= FINISHING ? awaitDone(false, 0L) : s);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBoolean(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
|
||||||
|
if (unit == null) throw new NullPointerException();
|
||||||
|
int s = state;
|
||||||
|
if (s <= FINISHING && (s = awaitDone(true, unit.toNanos(timeout))) <= FINISHING) throw new TimeoutException();
|
||||||
|
return report(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean report(int s) throws ExecutionException {
|
||||||
|
if (s == FINISHED) return result;
|
||||||
|
if (s >= CANCELLED) throw new CancellationException();
|
||||||
|
throw new ExecutionException(excpetion);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int awaitDone(boolean timed, long nanos) throws InterruptedException {
|
||||||
|
final long deadline = timed ? System.nanoTime() + nanos : 0L;
|
||||||
|
WaitNode q = null;
|
||||||
|
boolean queued = false;
|
||||||
|
while(true) {
|
||||||
|
if(Thread.interrupted()) {
|
||||||
|
removeWaiter(q);
|
||||||
|
throw new InterruptedException();
|
||||||
|
}
|
||||||
|
int s = state;
|
||||||
|
if(s > FINISHING) {
|
||||||
|
if(q != null) q.thread = null;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
else if(s == FINISHING) Thread.yield();
|
||||||
|
else if(q == null) q = new WaitNode();
|
||||||
|
else if(!queued) {
|
||||||
|
q.next = waiter;
|
||||||
|
waiter = q;
|
||||||
|
queued = true;
|
||||||
|
}
|
||||||
|
else if(timed) {
|
||||||
|
nanos = deadline - System.nanoTime();
|
||||||
|
if(nanos <= 0L) {
|
||||||
|
removeWaiter(q);
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
LockSupport.parkNanos(this, nanos);
|
||||||
|
}
|
||||||
|
else LockSupport.park(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeWaiter(WaitNode node) {
|
||||||
|
if(node == null) return;
|
||||||
|
node.thread = null;
|
||||||
|
retry:
|
||||||
|
while(true) {
|
||||||
|
for(WaitNode prev = null, current = waiter, next = null; current != null; current = next) {
|
||||||
|
next = current.next;
|
||||||
|
if(current.thread != null) prev = current;
|
||||||
|
else if(prev != null) {
|
||||||
|
prev.next = next;
|
||||||
|
if(prev.thread == null) continue retry; //Previous element got removed which means another thread was editing this while we were editing.
|
||||||
|
}
|
||||||
|
else if(waiter == current) {
|
||||||
|
waiter = next;
|
||||||
|
continue retry;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCancelled() { return state >= CANCELLED; }
|
||||||
|
@Override
|
||||||
|
public boolean isDone() { return state >= FINISHING; }
|
||||||
|
@Override
|
||||||
|
public boolean isPaused() { return state == PAUSED; }
|
||||||
|
@Override
|
||||||
|
public boolean isSuccessful() { return state == FINISHED; }
|
||||||
|
protected boolean shouldRun() { return state == RUNNING; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void pause() {
|
||||||
|
if(state == PAUSED || state == PAUSING || state >= FINISHING) return;
|
||||||
|
state = PAUSING;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void awaitPausing() {
|
||||||
|
if(state == PAUSED) return;
|
||||||
|
pause();
|
||||||
|
if(state == PAUSING) {
|
||||||
|
while(state == PAUSING) {
|
||||||
|
Thread.yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void resume() {
|
||||||
|
if(state != PAUSED && state != PAUSING) return;
|
||||||
|
if(state == PAUSING) {
|
||||||
|
while(state == PAUSING) {
|
||||||
|
Thread.yield();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
state = RUNNING;
|
||||||
|
executor.execute(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
static final class WaitNode {
|
||||||
|
volatile Thread thread;
|
||||||
|
volatile WaitNode next;
|
||||||
|
|
||||||
|
WaitNode() {
|
||||||
|
thread = Thread.currentThread();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,590 @@
|
|||||||
|
package speiger.src.collections.booleans.utils;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.collections.AbstractBooleanCollection;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanCollection;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanIterator;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanComparator;
|
||||||
|
import speiger.src.collections.objects.utils.ObjectArrays;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanConsumer;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanPredicate;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanBooleanUnaryOperator;
|
||||||
|
import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer;
|
||||||
|
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
|
||||||
|
import speiger.src.collections.utils.ITrimmable;
|
||||||
|
import speiger.src.collections.utils.SanityChecks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper class for Collections
|
||||||
|
*/
|
||||||
|
public class BooleanCollections
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Empty Collection Reference
|
||||||
|
*/
|
||||||
|
public static final BooleanCollection EMPTY = new EmptyCollection();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a Immutable EmptyCollection instance that is automatically casted.
|
||||||
|
* @return an empty collection
|
||||||
|
*/
|
||||||
|
public static BooleanCollection empty() {
|
||||||
|
return EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a Immutable Collection instance based on the instance given.
|
||||||
|
* @param c that should be made immutable/unmodifiable
|
||||||
|
* @return a unmodifiable collection wrapper. If the Collection already a unmodifiable wrapper then it just returns itself.
|
||||||
|
*/
|
||||||
|
public static BooleanCollection unmodifiable(BooleanCollection c) {
|
||||||
|
return c instanceof UnmodifiableCollection ? c : new UnmodifiableCollection(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a synchronized Collection instance based on the instance given.
|
||||||
|
* @param c that should be synchronized
|
||||||
|
* @return a synchronized collection wrapper. If the Collection already a synchronized wrapper then it just returns itself.
|
||||||
|
*/
|
||||||
|
public static BooleanCollection synchronize(BooleanCollection c) {
|
||||||
|
return c instanceof SynchronizedCollection ? c : new SynchronizedCollection(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a synchronized Collection instance based on the instance given.
|
||||||
|
* @param c that should be synchronized
|
||||||
|
* @param mutex is the controller of the synchronization block.
|
||||||
|
* @return a synchronized collection wrapper. If the Collection already a synchronized wrapper then it just returns itself.
|
||||||
|
*/
|
||||||
|
public static BooleanCollection synchronize(BooleanCollection c, Object mutex) {
|
||||||
|
return c instanceof SynchronizedCollection ? c : new SynchronizedCollection(c, mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Singleton Collection of a given element
|
||||||
|
* @param element the element that should be converted into a singleton collection
|
||||||
|
* @return a singletoncollection of the given element
|
||||||
|
*/
|
||||||
|
public static BooleanCollection singleton(boolean element) {
|
||||||
|
return new SingletonCollection(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static CollectionWrapper wrapper() {
|
||||||
|
return new CollectionWrapper();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static CollectionWrapper wrapper(int size) {
|
||||||
|
return new CollectionWrapper(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static class CollectionWrapper extends AbstractBooleanCollection implements ITrimmable {
|
||||||
|
boolean[] elements;
|
||||||
|
int size = 0;
|
||||||
|
|
||||||
|
public CollectionWrapper() {
|
||||||
|
this(10);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CollectionWrapper(int size) {
|
||||||
|
if(size < 0) throw new IllegalStateException("Size has to be 0 or greater");
|
||||||
|
elements = new boolean[size];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean add(boolean o) {
|
||||||
|
if(size >= elements.length) elements = Arrays.copyOf(elements, (int)Math.min((long)elements.length + (elements.length >> 1), SanityChecks.MAX_ARRAY_SIZE));
|
||||||
|
elements[size++] = o;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getBoolean(int index) {
|
||||||
|
if(index < 0 || index >= size) throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
|
||||||
|
return elements[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean remBoolean(boolean e) {
|
||||||
|
for(int i = 0;i<size;i++) {
|
||||||
|
if(elements[i] == e) {
|
||||||
|
removeIndex(i);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void removeIndex(int index) {
|
||||||
|
if(index < 0 || index >= size) throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
|
||||||
|
size--;
|
||||||
|
if(index != size) System.arraycopy(elements, index+1, elements, index, size - index);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator() {
|
||||||
|
return new BooleanIterator() {
|
||||||
|
int index = 0;
|
||||||
|
int lastReturned = -1;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() {
|
||||||
|
return index < size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean nextBoolean() {
|
||||||
|
int i = index++;
|
||||||
|
return elements[(lastReturned = i)];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void remove() {
|
||||||
|
if(lastReturned == -1) throw new IllegalStateException();
|
||||||
|
removeIndex(lastReturned);
|
||||||
|
index = lastReturned;
|
||||||
|
lastReturned = -1;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clear() {
|
||||||
|
size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sort(BooleanComparator c) {
|
||||||
|
if(c != null) BooleanArrays.stableSort(elements, size, c);
|
||||||
|
else BooleanArrays.stableSort(elements, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unstableSort(BooleanComparator c) {
|
||||||
|
if(c != null) BooleanArrays.unstableSort(elements, size, c);
|
||||||
|
else BooleanArrays.unstableSort(elements, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
for(int i = 0;i<size;i++)
|
||||||
|
action.accept(elements[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <E> void forEach(E input, ObjectBooleanConsumer<E> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
for(int i = 0;i<size;i++)
|
||||||
|
action.accept(input, elements[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean trim(int size) {
|
||||||
|
if(size > size() || size() == elements.length) return false;
|
||||||
|
int value = Math.max(size, size());
|
||||||
|
elements = value == 0 ? BooleanArrays.EMPTY_ARRAY : Arrays.copyOf(elements, value);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clearAndTrim(int size) {
|
||||||
|
if(elements.length <= size) {
|
||||||
|
clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
elements = size == 0 ? BooleanArrays.EMPTY_ARRAY : new boolean[size];
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public Object[] toArray() {
|
||||||
|
Object[] obj = new Object[size];
|
||||||
|
for(int i = 0;i<size;i++)
|
||||||
|
obj[i] = Boolean.valueOf(elements[i]);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public <E> E[] toArray(E[] a) {
|
||||||
|
if(a == null) a = (E[])new Object[size];
|
||||||
|
else if(a.length < size) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size);
|
||||||
|
for(int i = 0;i<size;i++)
|
||||||
|
a[i] = (E)Boolean.valueOf(elements[i]);
|
||||||
|
if (a.length > size) a[size] = null;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] toBooleanArray(boolean[] a) {
|
||||||
|
if(a.length < size) a = new boolean[size];
|
||||||
|
System.arraycopy(elements, 0, a, 0, size);
|
||||||
|
if (a.length > size) a[size] = false;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class SingletonCollection extends AbstractBooleanCollection
|
||||||
|
{
|
||||||
|
boolean element;
|
||||||
|
|
||||||
|
SingletonCollection(boolean element) {
|
||||||
|
this.element = element;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean remBoolean(boolean o) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean add(boolean o) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator()
|
||||||
|
{
|
||||||
|
return new BooleanIterator() {
|
||||||
|
boolean next = true;
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() { return next; }
|
||||||
|
@Override
|
||||||
|
public boolean nextBoolean() {
|
||||||
|
if(!hasNext()) throw new NoSuchElementException();
|
||||||
|
next = false;
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (o == this)
|
||||||
|
return true;
|
||||||
|
if (!(o instanceof Collection))
|
||||||
|
return false;
|
||||||
|
Collection<?> l = (Collection<?>)o;
|
||||||
|
if(l.size() != size()) return false;
|
||||||
|
Iterator<?> iter = l.iterator();
|
||||||
|
if (iter.hasNext() && !Objects.equals(element, iter.next())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return !iter.hasNext();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Boolean.hashCode(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() { return 1; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SingletonCollection copy() { return new SingletonCollection(element); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Synchronized Collection Wrapper for the synchronizedCollection function
|
||||||
|
*/
|
||||||
|
public static class SynchronizedCollection implements BooleanCollection {
|
||||||
|
BooleanCollection c;
|
||||||
|
protected Object mutex;
|
||||||
|
|
||||||
|
SynchronizedCollection(BooleanCollection c) {
|
||||||
|
this.c = c;
|
||||||
|
mutex = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
SynchronizedCollection(BooleanCollection c, Object mutex) {
|
||||||
|
this.c = c;
|
||||||
|
this.mutex = mutex;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean add(boolean o) { synchronized(mutex) { return c.add(o); } }
|
||||||
|
@Override
|
||||||
|
public boolean addAll(Collection<? extends Boolean> c) { synchronized(mutex) { return this.c.addAll(c); } }
|
||||||
|
@Override
|
||||||
|
public boolean addAll(BooleanCollection c) { synchronized(mutex) { return this.c.addAll(c); } }
|
||||||
|
@Override
|
||||||
|
public boolean addAll(boolean[] e, int offset, int length) { synchronized(mutex) { return c.addAll(e, offset, length); } }
|
||||||
|
@Override
|
||||||
|
public boolean contains(boolean o) { synchronized(mutex) { return c.contains(o); } }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean containsAll(Collection<?> c) { synchronized(mutex) { return this.c.containsAll(c); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean containsAny(Collection<?> c) { synchronized(mutex) { return this.c.containsAny(c); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean containsAll(BooleanCollection c) { synchronized(mutex) { return this.c.containsAll(c); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean containsAny(BooleanCollection c) { synchronized(mutex) { return this.c.containsAny(c); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() { synchronized(mutex) { return c.size(); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty() { synchronized(mutex) { return c.isEmpty(); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator() {
|
||||||
|
return c.iterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanCollection copy() { synchronized(mutex) { return c.copy(); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean remove(Object o) { synchronized(mutex) { return c.remove(o); } }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean removeAll(Collection<?> c) { synchronized(mutex) { return this.c.removeAll(c); } }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean retainAll(Collection<?> c) { synchronized(mutex) { return this.c.retainAll(c); } }
|
||||||
|
@Override
|
||||||
|
public boolean remBoolean(boolean o) { synchronized(mutex) { return c.remBoolean(o); } }
|
||||||
|
@Override
|
||||||
|
public boolean removeAll(BooleanCollection c) { synchronized(mutex) { return this.c.removeAll(c); } }
|
||||||
|
@Override
|
||||||
|
public boolean removeAll(BooleanCollection c, BooleanConsumer r) { synchronized(mutex) { return this.c.removeAll(c, r); } }
|
||||||
|
@Override
|
||||||
|
public boolean retainAll(BooleanCollection c) { synchronized(mutex) { return this.c.retainAll(c); } }
|
||||||
|
@Override
|
||||||
|
public boolean retainAll(BooleanCollection c, BooleanConsumer r) { synchronized(mutex) { return this.c.retainAll(c, r); } }
|
||||||
|
@Override
|
||||||
|
public void clear() { synchronized(mutex) { c.clear(); } }
|
||||||
|
@Override
|
||||||
|
public Object[] toArray() { synchronized(mutex) { return c.toArray(); } }
|
||||||
|
@Override
|
||||||
|
public <T> T[] toArray(T[] a) { synchronized(mutex) { return c.toArray(a); } }
|
||||||
|
@Override
|
||||||
|
public boolean[] toBooleanArray() { synchronized(mutex) { return c.toBooleanArray(); } }
|
||||||
|
@Override
|
||||||
|
public boolean[] toBooleanArray(boolean[] a) { synchronized(mutex) { return c.toBooleanArray(a); } }
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) { synchronized(mutex) { c.forEach(action); } }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public void forEach(Consumer<? super Boolean> action) { synchronized(mutex) { c.forEach(action); } }
|
||||||
|
@Override
|
||||||
|
public void forEachIndexed(IntBooleanConsumer action) { synchronized(mutex) { c.forEachIndexed(action); } }
|
||||||
|
@Override
|
||||||
|
public int hashCode() { synchronized(mutex) { return c.hashCode(); } }
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if(obj == this) return true;
|
||||||
|
synchronized(mutex) { return c.equals(obj); }
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public String toString() { synchronized(mutex) { return c.toString(); } }
|
||||||
|
@Override
|
||||||
|
public <E> void forEach(E input, ObjectBooleanConsumer<E> action) { synchronized(mutex) { c.forEach(input, action); } }
|
||||||
|
@Override
|
||||||
|
public boolean matchesAny(BooleanPredicate filter) { synchronized(mutex) { return c.matchesAny(filter); } }
|
||||||
|
@Override
|
||||||
|
public boolean matchesNone(BooleanPredicate filter) { synchronized(mutex) { return c.matchesNone(filter); } }
|
||||||
|
@Override
|
||||||
|
public boolean matchesAll(BooleanPredicate filter) { synchronized(mutex) { return c.matchesAll(filter); } }
|
||||||
|
@Override
|
||||||
|
public boolean reduce(boolean identity, BooleanBooleanUnaryOperator operator) { synchronized(mutex) { return c.reduce(identity, operator); } }
|
||||||
|
@Override
|
||||||
|
public boolean reduce(BooleanBooleanUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } }
|
||||||
|
@Override
|
||||||
|
public boolean findFirst(BooleanPredicate filter) { synchronized(mutex) { return c.findFirst(filter); } }
|
||||||
|
@Override
|
||||||
|
public int count(BooleanPredicate filter) { synchronized(mutex) { return c.count(filter); } }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unmodifyable Collection Wrapper for the unmodifyableCollection method
|
||||||
|
*/
|
||||||
|
public static class UnmodifiableCollection implements BooleanCollection {
|
||||||
|
BooleanCollection c;
|
||||||
|
|
||||||
|
UnmodifiableCollection(BooleanCollection c) {
|
||||||
|
this.c = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean add(boolean o) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean addAll(Collection<? extends Boolean> c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean addAll(BooleanCollection c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean addAll(boolean[] e, int offset, int length) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean contains(boolean o) { return c.contains(o); }
|
||||||
|
@Override
|
||||||
|
public boolean containsAll(BooleanCollection c) { return this.c.containsAll(c); }
|
||||||
|
@Override
|
||||||
|
public boolean containsAny(BooleanCollection c) { return this.c.containsAny(c); }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean containsAny(Collection<?> c) { return this.c.containsAny(c); }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean containsAll(Collection<?> c) { return this.c.containsAll(c); }
|
||||||
|
@Override
|
||||||
|
public int size() { return c.size(); }
|
||||||
|
@Override
|
||||||
|
public boolean isEmpty() { return c.isEmpty(); }
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator() { return BooleanIterators.unmodifiable(c.iterator()); }
|
||||||
|
@Override
|
||||||
|
public BooleanCollection copy() { return c.copy(); }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean remove(Object o) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean removeAll(Collection<?> c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean retainAll(Collection<?> c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean removeIf(Predicate<? super Boolean> filter) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean remBoolean(boolean o) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean removeAll(BooleanCollection c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean removeAll(BooleanCollection c, BooleanConsumer r) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean retainAll(BooleanCollection c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean retainAll(BooleanCollection c, BooleanConsumer r) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public void clear() { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public Object[] toArray() { return c.toArray(); }
|
||||||
|
@Override
|
||||||
|
public <T> T[] toArray(T[] a) { return c.toArray(a); }
|
||||||
|
@Override
|
||||||
|
public boolean[] toBooleanArray() { return c.toBooleanArray(); }
|
||||||
|
@Override
|
||||||
|
public boolean[] toBooleanArray(boolean[] a) { return c.toBooleanArray(a); }
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) { c.forEach(action); }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public void forEach(Consumer<? super Boolean> action) { c.forEach(action); }
|
||||||
|
@Override
|
||||||
|
public void forEachIndexed(IntBooleanConsumer action) { c.forEachIndexed(action); }
|
||||||
|
@Override
|
||||||
|
public int hashCode() { return c.hashCode(); }
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) { return obj == this || c.equals(obj); }
|
||||||
|
@Override
|
||||||
|
public String toString() { return c.toString(); }
|
||||||
|
@Override
|
||||||
|
public <E> void forEach(E input, ObjectBooleanConsumer<E> action) { c.forEach(input, action); }
|
||||||
|
@Override
|
||||||
|
public boolean matchesAny(BooleanPredicate filter) { return c.matchesAny(filter); }
|
||||||
|
@Override
|
||||||
|
public boolean matchesNone(BooleanPredicate filter) { return c.matchesNone(filter); }
|
||||||
|
@Override
|
||||||
|
public boolean matchesAll(BooleanPredicate filter) { return c.matchesAll(filter); }
|
||||||
|
@Override
|
||||||
|
public boolean reduce(boolean identity, BooleanBooleanUnaryOperator operator) { return c.reduce(identity, operator); }
|
||||||
|
@Override
|
||||||
|
public boolean reduce(BooleanBooleanUnaryOperator operator) { return c.reduce(operator); }
|
||||||
|
@Override
|
||||||
|
public boolean findFirst(BooleanPredicate filter) { return c.findFirst(filter); }
|
||||||
|
@Override
|
||||||
|
public int count(BooleanPredicate filter) { return c.count(filter); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Empty Collection implementation for the empty collection function
|
||||||
|
*/
|
||||||
|
public static class EmptyCollection extends AbstractBooleanCollection {
|
||||||
|
@Override
|
||||||
|
public boolean add(boolean o) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(BooleanCollection c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean addAll(boolean[] e, int offset, int length) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean contains(boolean o) { return false; }
|
||||||
|
@Override
|
||||||
|
public boolean containsAll(BooleanCollection c) { return c.isEmpty(); }
|
||||||
|
@Override
|
||||||
|
public boolean containsAny(BooleanCollection c) { return false; }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean containsAny(Collection<?> c) { return false; }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean containsAll(Collection<?> c) { return c.isEmpty(); }
|
||||||
|
@Override
|
||||||
|
public int hashCode() { return 0; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if(o == this) return true;
|
||||||
|
if(!(o instanceof Collection)) return false;
|
||||||
|
return ((Collection<?>)o).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean remove(Object o) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean removeAll(Collection<?> c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean retainAll(Collection<?> c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean removeIf(Predicate<? super Boolean> filter) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean remBoolean(boolean o) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean removeAll(BooleanCollection c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean retainAll(BooleanCollection c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public Object[] toArray() { return ObjectArrays.EMPTY_ARRAY; }
|
||||||
|
@Override
|
||||||
|
public <T> T[] toArray(T[] a) {
|
||||||
|
if(a != null && a.length > 0)
|
||||||
|
a[0] = null;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] toBooleanArray() { return BooleanArrays.EMPTY_ARRAY; }
|
||||||
|
@Override
|
||||||
|
public boolean[] toBooleanArray(boolean[] a) {
|
||||||
|
if(a != null && a.length > 0)
|
||||||
|
a[0] = false;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator() { return BooleanIterators.empty(); }
|
||||||
|
@Override
|
||||||
|
public void clear() {}
|
||||||
|
@Override
|
||||||
|
public int size() { return 0; }
|
||||||
|
@Override
|
||||||
|
public EmptyCollection copy() { return this; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,490 @@
|
|||||||
|
package speiger.src.collections.booleans.utils;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanIterable;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanCollection;
|
||||||
|
import speiger.src.collections.objects.collections.ObjectIterable;
|
||||||
|
import speiger.src.collections.objects.collections.ObjectIterator;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanConsumer;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanComparator;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanIterator;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanFunction;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanPredicate;
|
||||||
|
import speiger.src.collections.utils.ISizeProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper class for Iterables
|
||||||
|
*/
|
||||||
|
public class BooleanIterables
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Helper function that maps a Java-Iterable into a new Type.
|
||||||
|
* @param iterable the iterable that should be mapped
|
||||||
|
* @param mapper the function that decides what the result turns into.
|
||||||
|
* @param <E> The return type.
|
||||||
|
* @return a iterable that is mapped to a new result
|
||||||
|
*/
|
||||||
|
public static <E> ObjectIterable<E> map(Iterable<? extends Boolean> iterable, BooleanFunction<E> mapper) {
|
||||||
|
return new MappedIterable<>(wrap(iterable), mapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that maps a Iterable into a new Type.
|
||||||
|
* @param iterable the iterable that should be mapped
|
||||||
|
* @param mapper the function that decides what the result turns into.
|
||||||
|
* @param <E> The return type.
|
||||||
|
* @return a iterable that is mapped to a new result
|
||||||
|
*/
|
||||||
|
public static <E> ObjectIterable<E> map(BooleanIterable iterable, BooleanFunction<E> mapper) {
|
||||||
|
return new MappedIterable<>(iterable, mapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that flatMaps a Java-Iterable into a new Type.
|
||||||
|
* @param iterable the iterable that should be flatMapped
|
||||||
|
* @param mapper the function that decides what the result turns into.
|
||||||
|
* @param <V> The return type supplier.
|
||||||
|
* @param <E> The return type.
|
||||||
|
* @return a iterable that is flatMapped to a new result
|
||||||
|
*/
|
||||||
|
public static <E, V extends Iterable<E>> ObjectIterable<E> flatMap(Iterable<? extends Boolean> iterable, BooleanFunction<V> mapper) {
|
||||||
|
return new FlatMappedIterable<>(wrap(iterable), mapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that flatMaps a Iterable into a new Type.
|
||||||
|
* @param iterable the iterable that should be flatMapped
|
||||||
|
* @param mapper the function that decides what the result turns into.
|
||||||
|
* @param <V> The return type supplier.
|
||||||
|
* @param <E> The return type.
|
||||||
|
* @return a iterable that is flatMapped to a new result
|
||||||
|
*/
|
||||||
|
public static <E, V extends Iterable<E>> ObjectIterable<E> flatMap(BooleanIterable iterable, BooleanFunction<V> mapper) {
|
||||||
|
return new FlatMappedIterable<>(iterable, mapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that flatMaps a Java-Iterable into a new Type.
|
||||||
|
* @param iterable the iterable that should be flatMapped
|
||||||
|
* @param mapper the function that decides what the result turns into.
|
||||||
|
* @param <E> The return type.
|
||||||
|
* @return a iterable that is flatMapped to a new result
|
||||||
|
*/
|
||||||
|
public static <E> ObjectIterable<E> arrayFlatMap(Iterable<? extends Boolean> iterable, BooleanFunction<E[]> mapper) {
|
||||||
|
return new FlatMappedArrayIterable<>(wrap(iterable), mapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that flatMaps a Iterable into a new Type.
|
||||||
|
* @param iterable the iterable that should be flatMapped
|
||||||
|
* @param mapper the function that decides what the result turns into.
|
||||||
|
* @param <E> The return type.
|
||||||
|
* @return a iterable that is flatMapped to a new result
|
||||||
|
*/
|
||||||
|
public static <E> ObjectIterable<E> arrayFlatMap(BooleanIterable iterable, BooleanFunction<E[]> mapper) {
|
||||||
|
return new FlatMappedArrayIterable<>(iterable, mapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that filters out all desired elements from a Java-Iterable
|
||||||
|
* @param iterable that should be filtered.
|
||||||
|
* @param filter the filter that decides that should be let through
|
||||||
|
* @return a filtered iterable
|
||||||
|
*/
|
||||||
|
public static BooleanIterable filter(Iterable<? extends Boolean> iterable, BooleanPredicate filter) {
|
||||||
|
return new FilteredIterable(wrap(iterable), filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that filters out all desired elements
|
||||||
|
* @param iterable that should be filtered.
|
||||||
|
* @param filter the filter that decides that should be let through
|
||||||
|
* @return a filtered iterable
|
||||||
|
*/
|
||||||
|
public static BooleanIterable filter(BooleanIterable iterable, BooleanPredicate filter) {
|
||||||
|
return new FilteredIterable(iterable, filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that filters out all duplicated elements.
|
||||||
|
* @param iterable that should be distinct
|
||||||
|
* @return a distinct iterable
|
||||||
|
*/
|
||||||
|
public static BooleanIterable distinct(BooleanIterable iterable) {
|
||||||
|
return new DistinctIterable(iterable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that filters out all duplicated elements from a Java Iterable.
|
||||||
|
* @param iterable that should be distinct
|
||||||
|
* @return a distinct iterable
|
||||||
|
*/
|
||||||
|
public static BooleanIterable distinct(Iterable<? extends Boolean> iterable) {
|
||||||
|
return new DistinctIterable(wrap(iterable));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that repeats the Iterable a specific amount of times
|
||||||
|
* @param iterable that should be repeated
|
||||||
|
* @param repeats the amount of times the iterable should be repeated
|
||||||
|
* @return a repeating iterable
|
||||||
|
*/
|
||||||
|
public static BooleanIterable repeat(BooleanIterable iterable, int repeats) {
|
||||||
|
return new RepeatingIterable(iterable, repeats);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that repeats the Iterable a specific amount of times from a Java Iterable
|
||||||
|
* @param iterable that should be repeated
|
||||||
|
* @param repeats the amount of times the iterable should be repeated
|
||||||
|
* @return a repeating iterable
|
||||||
|
*/
|
||||||
|
public static BooleanIterable repeat(Iterable<? extends Boolean> iterable, int repeats) {
|
||||||
|
return new RepeatingIterable(wrap(iterable), repeats);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that hard limits the Iterable to a specific size
|
||||||
|
* @param iterable that should be limited
|
||||||
|
* @param limit the amount of elements it should be limited to
|
||||||
|
* @return a limited iterable
|
||||||
|
*/
|
||||||
|
public static BooleanIterable limit(BooleanIterable iterable, long limit) {
|
||||||
|
return new LimitedIterable(iterable, limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that hard limits the Iterable to a specific size from a Java Iterable
|
||||||
|
* @param iterable that should be limited
|
||||||
|
* @param limit the amount of elements it should be limited to
|
||||||
|
* @return a limited iterable
|
||||||
|
*/
|
||||||
|
public static BooleanIterable limit(Iterable<? extends Boolean> iterable, long limit) {
|
||||||
|
return new LimitedIterable(wrap(iterable), limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that sorts the Iterable.
|
||||||
|
* This operation is heavily hurting performance because it rebuilds the entire iterator and then sorts it.
|
||||||
|
* @param iterable that should be sorted
|
||||||
|
* @param sorter that sorts the iterable. Can be null.
|
||||||
|
* @return a sorted iterable.
|
||||||
|
*/
|
||||||
|
public static BooleanIterable sorted(BooleanIterable iterable, BooleanComparator sorter) {
|
||||||
|
return new SortedIterable(iterable, sorter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that sorts the Iterable from a Java Iterable
|
||||||
|
* This operation is heavily hurting performance because it rebuilds the entire iterator and then sorts it.
|
||||||
|
* @param iterable that should be sorted
|
||||||
|
* @param sorter that sorts the iterable. Can be null.
|
||||||
|
* @return a sorted iterable.
|
||||||
|
*/
|
||||||
|
public static BooleanIterable sorted(Iterable<? extends Boolean> iterable, BooleanComparator sorter) {
|
||||||
|
return new SortedIterable(wrap(iterable), sorter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that allows to preview the result of a Iterable.
|
||||||
|
* @param iterable that should be peeked at
|
||||||
|
* @param action callback that receives the value before the iterable returns it
|
||||||
|
* @return a peeked iterable
|
||||||
|
*/
|
||||||
|
public static BooleanIterable peek(BooleanIterable iterable, BooleanConsumer action) {
|
||||||
|
return new PeekIterable(iterable, action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that allows to preview the result of a Iterable from a Java Iterable
|
||||||
|
* @param iterable that should be peeked at
|
||||||
|
* @param action callback that receives the value before the iterable returns it
|
||||||
|
* @return a peeked iterable
|
||||||
|
*/
|
||||||
|
public static BooleanIterable peek(Iterable<? extends Boolean> iterable, BooleanConsumer action) {
|
||||||
|
return new PeekIterable(wrap(iterable), action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Wrapper function that wraps a Java-Iterable into a Type Specific Iterable
|
||||||
|
* @param iterable that should be wrapped
|
||||||
|
* @return a type specific iterable
|
||||||
|
*/
|
||||||
|
public static BooleanIterable wrap(Iterable<? extends Boolean> iterable) {
|
||||||
|
return new WrappedIterable(iterable);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class WrappedIterable implements BooleanIterable, ISizeProvider
|
||||||
|
{
|
||||||
|
Iterable<? extends Boolean> iterable;
|
||||||
|
|
||||||
|
public WrappedIterable(Iterable<? extends Boolean> iterable) {
|
||||||
|
this.iterable = iterable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BooleanIterator iterator() {
|
||||||
|
return BooleanIterators.wrap(iterable.iterator());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
ISizeProvider prov = ISizeProvider.of(iterable);
|
||||||
|
return prov == null ? -1 : prov.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
iterable.forEach(action);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class MappedIterable<T> implements ObjectIterable<T>, ISizeProvider
|
||||||
|
{
|
||||||
|
BooleanIterable iterable;
|
||||||
|
BooleanFunction<T> mapper;
|
||||||
|
|
||||||
|
MappedIterable(BooleanIterable iterable, BooleanFunction<T> mapper) {
|
||||||
|
this.iterable = iterable;
|
||||||
|
this.mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ObjectIterator<T> iterator() {
|
||||||
|
return BooleanIterators.map(iterable.iterator(), mapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
ISizeProvider prov = ISizeProvider.of(this);
|
||||||
|
return prov == null ? -1 : prov.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(Consumer<? super T> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
iterable.forEach(E -> action.accept(mapper.apply(E)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class FlatMappedIterable<T, V extends Iterable<T>> implements ObjectIterable<T>
|
||||||
|
{
|
||||||
|
BooleanIterable iterable;
|
||||||
|
BooleanFunction<V> mapper;
|
||||||
|
|
||||||
|
FlatMappedIterable(BooleanIterable iterable, BooleanFunction<V> mapper) {
|
||||||
|
this.iterable = iterable;
|
||||||
|
this.mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ObjectIterator<T> iterator() {
|
||||||
|
return BooleanIterators.flatMap(iterable.iterator(), mapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(Consumer<? super T> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
iterable.forEach(E -> mapper.apply(E).forEach(action));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class FlatMappedArrayIterable<T> implements ObjectIterable<T>
|
||||||
|
{
|
||||||
|
BooleanIterable iterable;
|
||||||
|
BooleanFunction<T[]> mapper;
|
||||||
|
|
||||||
|
FlatMappedArrayIterable(BooleanIterable iterable, BooleanFunction<T[]> mapper) {
|
||||||
|
this.iterable = iterable;
|
||||||
|
this.mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ObjectIterator<T> iterator() {
|
||||||
|
return BooleanIterators.arrayFlatMap(iterable.iterator(), mapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(Consumer<? super T> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
iterable.forEach(E -> {
|
||||||
|
T[] array = mapper.apply(E);
|
||||||
|
for(int i = 0,m=array.length;i<m;action.accept(array[i++]));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class RepeatingIterable implements BooleanIterable, ISizeProvider
|
||||||
|
{
|
||||||
|
BooleanIterable iterable;
|
||||||
|
int repeats;
|
||||||
|
|
||||||
|
public RepeatingIterable(BooleanIterable iterable, int repeats) {
|
||||||
|
this.iterable = iterable;
|
||||||
|
this.repeats = repeats;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator() {
|
||||||
|
return BooleanIterators.repeat(iterable.iterator(), repeats);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
ISizeProvider prov = ISizeProvider.of(iterable);
|
||||||
|
return prov == null ? -1 : prov.size() * (repeats+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
BooleanCollection repeater = BooleanCollections.wrapper();
|
||||||
|
iterable.forEach(action.andThen(repeater::add));
|
||||||
|
for(int i = 0;i<repeats;i++)
|
||||||
|
repeater.forEach(action);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class FilteredIterable implements BooleanIterable
|
||||||
|
{
|
||||||
|
BooleanIterable iterable;
|
||||||
|
BooleanPredicate filter;
|
||||||
|
|
||||||
|
public FilteredIterable(BooleanIterable iterable, BooleanPredicate filter) {
|
||||||
|
this.iterable = iterable;
|
||||||
|
this.filter = filter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator() {
|
||||||
|
return BooleanIterators.filter(iterable.iterator(), filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
iterable.forEach(T -> { if(!filter.test(T)) action.accept(T); } );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class LimitedIterable implements BooleanIterable, ISizeProvider
|
||||||
|
{
|
||||||
|
BooleanIterable iterable;
|
||||||
|
long limit;
|
||||||
|
|
||||||
|
public LimitedIterable(BooleanIterable iterable, long limit) {
|
||||||
|
this.iterable = iterable;
|
||||||
|
this.limit = limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator() {
|
||||||
|
return BooleanIterators.limit(iterable.iterator(), limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
ISizeProvider prov = ISizeProvider.of(iterable);
|
||||||
|
return prov == null ? -1 : (int)Math.min(prov.size(), limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
AtomicLong counter = new AtomicLong();
|
||||||
|
iterable.forEach(T -> {
|
||||||
|
if(counter.get() >= limit) return;
|
||||||
|
counter.incrementAndGet();
|
||||||
|
action.accept(T);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class SortedIterable implements BooleanIterable, ISizeProvider
|
||||||
|
{
|
||||||
|
BooleanIterable iterable;
|
||||||
|
BooleanComparator sorter;
|
||||||
|
|
||||||
|
public SortedIterable(BooleanIterable iterable, BooleanComparator sorter) {
|
||||||
|
this.iterable = iterable;
|
||||||
|
this.sorter = sorter;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator() {
|
||||||
|
return BooleanIterators.sorted(iterable.iterator(), sorter);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
ISizeProvider prov = ISizeProvider.of(iterable);
|
||||||
|
return prov == null ? -1 : prov.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
BooleanCollections.CollectionWrapper wrapper = BooleanCollections.wrapper();
|
||||||
|
iterable.forEach(wrapper::add);
|
||||||
|
wrapper.unstableSort(sorter);
|
||||||
|
wrapper.forEach(action);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class DistinctIterable implements BooleanIterable
|
||||||
|
{
|
||||||
|
BooleanIterable iterable;
|
||||||
|
|
||||||
|
public DistinctIterable(BooleanIterable iterable) {
|
||||||
|
this.iterable = iterable;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator() {
|
||||||
|
return BooleanIterators.distinct(iterable.iterator());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
AtomicInteger result = new AtomicInteger();
|
||||||
|
iterable.forEach(T -> {
|
||||||
|
if(((result.get() & (T ? 2 : 1)) != 0)) return;
|
||||||
|
result.getAndAdd(T ? 2 : 1);
|
||||||
|
action.accept(T);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class PeekIterable implements BooleanIterable, ISizeProvider
|
||||||
|
{
|
||||||
|
BooleanIterable iterable;
|
||||||
|
BooleanConsumer action;
|
||||||
|
|
||||||
|
public PeekIterable(BooleanIterable iterable, BooleanConsumer action) {
|
||||||
|
this.iterable = iterable;
|
||||||
|
this.action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator() {
|
||||||
|
return BooleanIterators.peek(iterable.iterator(), action);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int size() {
|
||||||
|
ISizeProvider prov = ISizeProvider.of(iterable);
|
||||||
|
return prov == null ? -1 : prov.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
iterable.forEach(this.action.andThen(action));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,533 @@
|
|||||||
|
package speiger.src.collections.booleans.utils;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.RandomAccess;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanCollection;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanConsumer;
|
||||||
|
import speiger.src.collections.booleans.lists.AbstractBooleanList;
|
||||||
|
import speiger.src.collections.booleans.lists.BooleanList;
|
||||||
|
import speiger.src.collections.booleans.lists.BooleanListIterator;
|
||||||
|
import speiger.src.collections.utils.SanityChecks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper class for Lists
|
||||||
|
*/
|
||||||
|
public class BooleanLists
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Empty List reference
|
||||||
|
*/
|
||||||
|
private static final EmptyList EMPTY = new EmptyList();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a Immutable EmptyList instance that is automatically casted.
|
||||||
|
* @return an empty list
|
||||||
|
*/
|
||||||
|
public static EmptyList empty() {
|
||||||
|
return EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a Immutable List instance based on the instance given.
|
||||||
|
* @param l that should be made immutable/unmodifiable
|
||||||
|
* @return a unmodifiable list wrapper. If the list is implementing RandomAccess that is also transferred. If the List already a unmodifiable wrapper then it just returns itself.
|
||||||
|
*/
|
||||||
|
public static BooleanList unmodifiable(BooleanList l) {
|
||||||
|
return l instanceof UnmodifiableList ? l : l instanceof RandomAccess ? new UnmodifiableRandomList(l) : new UnmodifiableList(l);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a synchronized List instance based on the instance given.
|
||||||
|
* @param l that should be synchronized
|
||||||
|
* @return a synchronized list wrapper. If the list is implementing RandomAccess or IBooleanArray that is also transferred. If the List already a synchronized wrapper then it just returns itself.
|
||||||
|
*/
|
||||||
|
public static BooleanList synchronize(BooleanList l) {
|
||||||
|
return l instanceof SynchronizedList ? l : (l instanceof IBooleanArray ? new SynchronizedArrayList(l) : (l instanceof RandomAccess ? new SynchronizedRandomAccessList(l) : new SynchronizedList(l)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a synchronized List instance based on the instance given.
|
||||||
|
* @param l that should be synchronized
|
||||||
|
* @param mutex is the controller of the synchronization block.
|
||||||
|
* @return a synchronized list wrapper. If the list is implementing RandomAccess or IBooleanArray that is also transferred. If the List already a synchronized wrapper then it just returns itself.
|
||||||
|
*/
|
||||||
|
public static BooleanList synchronize(BooleanList l, Object mutex) {
|
||||||
|
return l instanceof SynchronizedList ? l : (l instanceof IBooleanArray ? new SynchronizedArrayList(l, mutex) : (l instanceof RandomAccess ? new SynchronizedRandomAccessList(l, mutex) : new SynchronizedList(l, mutex)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Unmodifiable Singleton list
|
||||||
|
* @param element that should be used in the Singleton
|
||||||
|
* @return a singleton list that is unmodifiable
|
||||||
|
*/
|
||||||
|
public static BooleanList singleton(boolean element) {
|
||||||
|
return new SingletonList(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverses the list
|
||||||
|
* @param list that should be reversed.
|
||||||
|
* @return the input list
|
||||||
|
*/
|
||||||
|
public static BooleanList reverse(BooleanList list) {
|
||||||
|
int size = list.size();
|
||||||
|
if(list instanceof IBooleanArray) {
|
||||||
|
IBooleanArray array = (IBooleanArray)list;
|
||||||
|
if(list instanceof SynchronizedArrayList) array.elements(T -> BooleanArrays.reverse(T, size));
|
||||||
|
else BooleanArrays.reverse(array.elements(), size);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
if(list instanceof RandomAccess) {
|
||||||
|
for (int i = 0, mid = size >> 1, j = size - 1; i < mid; i++, j--) {
|
||||||
|
boolean t = list.getBoolean(i);
|
||||||
|
list.set(i, list.getBoolean(j));
|
||||||
|
list.set(j, t);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
BooleanListIterator fwd = list.listIterator();
|
||||||
|
BooleanListIterator rev = list.listIterator(size);
|
||||||
|
for(int i = 0, mid = size >> 1; i < mid; i++) {
|
||||||
|
boolean tmp = fwd.nextBoolean();
|
||||||
|
fwd.set(rev.previousBoolean());
|
||||||
|
rev.set(tmp);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shuffles the list
|
||||||
|
* @param list that should be Shuffled.
|
||||||
|
* @return the input list
|
||||||
|
*/
|
||||||
|
public static BooleanList shuffle(BooleanList list) {
|
||||||
|
return shuffle(list, SanityChecks.getRandom());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shuffles the list
|
||||||
|
* @param list that should be Shuffled.
|
||||||
|
* @param random the random that should be used
|
||||||
|
* @return the input list
|
||||||
|
*/
|
||||||
|
public static BooleanList shuffle(BooleanList list, Random random) {
|
||||||
|
int size = list.size();
|
||||||
|
if(list instanceof IBooleanArray) {
|
||||||
|
IBooleanArray array = (IBooleanArray)list;
|
||||||
|
if(list instanceof SynchronizedArrayList) array.elements(T -> BooleanArrays.shuffle(T, size, random));
|
||||||
|
else BooleanArrays.shuffle(array.elements(), size, random);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
for(int i = size; i-- != 0;) {
|
||||||
|
int p = random.nextInt(i + 1);
|
||||||
|
boolean t = list.getBoolean(i);
|
||||||
|
list.set(i, list.getBoolean(p));
|
||||||
|
list.set(p, t);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class SingletonList extends AbstractBooleanList
|
||||||
|
{
|
||||||
|
boolean element;
|
||||||
|
|
||||||
|
SingletonList(boolean element)
|
||||||
|
{
|
||||||
|
this.element = element;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void add(int index, boolean e) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, Collection<? extends Boolean> c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, BooleanCollection c) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, BooleanList c) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBoolean(int index) {
|
||||||
|
if(index == 0) return element;
|
||||||
|
throw new IndexOutOfBoundsException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean set(int index, boolean e) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean removeBoolean(int index) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean swapRemove(int index) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public void addElements(int from, boolean[] a, int offset, int length) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean[] getElements(int from, boolean[] a, int offset, int length) {
|
||||||
|
if(from != 0 || length != 1) throw new IndexOutOfBoundsException();
|
||||||
|
a[offset] = element;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeElements(int from, int to) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public boolean[] extractElements(int from, int to) { throw new UnsupportedOperationException(); }
|
||||||
|
@Override
|
||||||
|
public int size() { return 1; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SingletonList copy() { return new SingletonList(element); }
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class SynchronizedArrayList extends SynchronizedList implements IBooleanArray
|
||||||
|
{
|
||||||
|
IBooleanArray l;
|
||||||
|
|
||||||
|
SynchronizedArrayList(BooleanList l) {
|
||||||
|
super(l);
|
||||||
|
this.l = (IBooleanArray)l;
|
||||||
|
}
|
||||||
|
|
||||||
|
SynchronizedArrayList(BooleanList l, Object mutex) {
|
||||||
|
super(l, mutex);
|
||||||
|
this.l = (IBooleanArray)l;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void ensureCapacity(int size) { synchronized(mutex) { l.ensureCapacity(size); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean trim(int size) { synchronized(mutex) { return l.trim(size); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void clearAndTrim(int size) { synchronized(mutex) { l.clearAndTrim(size); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] elements() { synchronized(mutex) { return l.elements(); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void elements(Consumer<boolean[]> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
synchronized(mutex) {
|
||||||
|
l.elements(action);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class SynchronizedRandomAccessList extends SynchronizedList implements RandomAccess
|
||||||
|
{
|
||||||
|
SynchronizedRandomAccessList(BooleanList l) {
|
||||||
|
super(l);
|
||||||
|
}
|
||||||
|
|
||||||
|
SynchronizedRandomAccessList(BooleanList l, Object mutex) {
|
||||||
|
super(l, mutex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class SynchronizedList extends BooleanCollections.SynchronizedCollection implements BooleanList
|
||||||
|
{
|
||||||
|
BooleanList l;
|
||||||
|
|
||||||
|
SynchronizedList(BooleanList l) {
|
||||||
|
super(l);
|
||||||
|
this.l = l;
|
||||||
|
}
|
||||||
|
|
||||||
|
SynchronizedList(BooleanList l, Object mutex) {
|
||||||
|
super(l, mutex);
|
||||||
|
this.l = l;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, Collection<? extends Boolean> c) { synchronized(mutex) { return l.addAll(index, c); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(int index, Boolean element) { synchronized(mutex) { l.add(index, element); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(int index, boolean e) { synchronized(mutex) { l.add(index, e); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, BooleanCollection c) { synchronized(mutex) { return l.addAll(index, c); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(BooleanList c) { synchronized(mutex) { return l.addAll(c); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, BooleanList c) { synchronized(mutex) { return l.addAll(index, c); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBoolean(int index) { synchronized(mutex) { return l.getBoolean(index); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) { synchronized(mutex) { l.forEach(action); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean set(int index, boolean e) { synchronized(mutex) { return l.set(index, e); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeBoolean(int index) { synchronized(mutex) { return l.removeBoolean(index); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean swapRemove(int index) { synchronized(mutex) { return l.swapRemove(index); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean swapRemoveBoolean(boolean e) { synchronized(mutex) { return l.swapRemoveBoolean(e); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public int indexOf(Object e) { synchronized(mutex) { return l.indexOf(e); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public int lastIndexOf(Object e) { synchronized(mutex) { return l.lastIndexOf(e); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int indexOf(boolean e) { synchronized(mutex) { return l.indexOf(e); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int lastIndexOf(boolean e) { synchronized(mutex) { return l.lastIndexOf(e); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addElements(int from, boolean[] a, int offset, int length) { synchronized(mutex) { l.addElements(from, a, offset, length); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] getElements(int from, boolean[] a, int offset, int length) { synchronized(mutex) { return l.getElements(from, a, offset, length); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeElements(int from, int to) { synchronized(mutex) { l.removeElements(from, to); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] extractElements(int from, int to) { synchronized(mutex) { return l.extractElements(from, to); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanListIterator listIterator() {
|
||||||
|
return l.listIterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanListIterator listIterator(int index) {
|
||||||
|
return l.listIterator(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanList subList(int from, int to) {
|
||||||
|
return BooleanLists.synchronize(l.subList(from, to));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanList reversed() {
|
||||||
|
return BooleanLists.synchronize(l.reversed());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void size(int size) { synchronized(mutex) { l.size(size); } }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanList copy() { synchronized(mutex) { return l.copy(); } }
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class UnmodifiableRandomList extends UnmodifiableList implements RandomAccess
|
||||||
|
{
|
||||||
|
UnmodifiableRandomList(BooleanList l) {
|
||||||
|
super(l);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class UnmodifiableList extends BooleanCollections.UnmodifiableCollection implements BooleanList
|
||||||
|
{
|
||||||
|
final BooleanList l;
|
||||||
|
|
||||||
|
UnmodifiableList(BooleanList l) {
|
||||||
|
super(l);
|
||||||
|
this.l = l;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, Collection<? extends Boolean> c) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(int index, Boolean element) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(int index, boolean e) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, BooleanCollection c) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(BooleanList c) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, BooleanList c) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBoolean(int index) { return l.getBoolean(index); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) { l.forEach(action); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean set(int index, boolean e) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeBoolean(int index) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean swapRemove(int index) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean swapRemoveBoolean(boolean e) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public int indexOf(Object e) { return l.indexOf(e); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public int lastIndexOf(Object e) { return l.lastIndexOf(e); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int indexOf(boolean e) { return l.indexOf(e); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int lastIndexOf(boolean e) { return l.lastIndexOf(e); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addElements(int from, boolean[] a, int offset, int length) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] getElements(int from, boolean[] a, int offset, int length) {
|
||||||
|
return l.getElements(from, a, offset, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeElements(int from, int to) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] extractElements(int from, int to) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanListIterator listIterator() {
|
||||||
|
return BooleanIterators.unmodifiable(l.listIterator());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanListIterator listIterator(int index) {
|
||||||
|
return BooleanIterators.unmodifiable(l.listIterator(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanList subList(int from, int to) {
|
||||||
|
return BooleanLists.unmodifiable(l.subList(from, to));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanList reversed() {
|
||||||
|
return BooleanLists.unmodifiable(l.reversed());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void size(int size) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanList copy() { return l.copy(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class EmptyList extends BooleanCollections.EmptyCollection implements BooleanList
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, Collection<? extends Boolean> c) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(int index, Boolean element) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void add(int index, boolean e) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, BooleanCollection c) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(BooleanList c) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(int index, BooleanList c) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean getBoolean(int index) { throw new IndexOutOfBoundsException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean set(int index, boolean e) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeBoolean(int index) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean swapRemove(int index) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean swapRemoveBoolean(boolean e) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int indexOf(Object e) { return -1; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int lastIndexOf(Object e) { return -1; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int indexOf(boolean e) { return -1; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int lastIndexOf(boolean e) { return -1; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addElements(int from, boolean[] a, int offset, int length){ throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] getElements(int from, boolean[] a, int offset, int length) { throw new IndexOutOfBoundsException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeElements(int from, int to) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean[] extractElements(int from, int to) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanListIterator listIterator() {
|
||||||
|
return BooleanIterators.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanListIterator listIterator(int index) {
|
||||||
|
if(index != 0)
|
||||||
|
throw new IndexOutOfBoundsException();
|
||||||
|
return BooleanIterators.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() { return 1; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if(o == this) return true;
|
||||||
|
if(!(o instanceof List)) return false;
|
||||||
|
return ((List<?>)o).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanList subList(int from, int to) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanList reversed() { return this; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void size(int size) { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public EmptyList copy() { return this; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,146 @@
|
|||||||
|
package speiger.src.collections.booleans.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanIterator;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanCollection;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanComparator;
|
||||||
|
import speiger.src.collections.booleans.queues.BooleanPriorityDequeue;
|
||||||
|
import speiger.src.collections.booleans.queues.BooleanPriorityQueue;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanConsumer;
|
||||||
|
import speiger.src.collections.booleans.functions.function.BooleanPredicate;
|
||||||
|
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper class for PriorityQueues
|
||||||
|
*/
|
||||||
|
public class BooleanPriorityQueues
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a synchronized PriorityQueue instance based on the instance given.
|
||||||
|
* @param queue that should be synchronized
|
||||||
|
* @return a synchronized PriorityQueue wrapper.
|
||||||
|
*/
|
||||||
|
public static BooleanPriorityQueue synchronize(BooleanPriorityQueue queue) {
|
||||||
|
return queue instanceof SynchronizedPriorityQueue ? (SynchronizedPriorityQueue)queue : new SynchronizedPriorityQueue(queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a synchronized PriorityQueue instance based on the instance given.
|
||||||
|
* @param queue that should be synchronized
|
||||||
|
* @param mutex is the controller of the synchronization block.
|
||||||
|
* @return a synchronized PriorityQueue wrapper.
|
||||||
|
*/
|
||||||
|
public static BooleanPriorityQueue synchronize(BooleanPriorityQueue queue, Object mutex) {
|
||||||
|
return queue instanceof SynchronizedPriorityQueue ? (SynchronizedPriorityQueue)queue : new SynchronizedPriorityQueue(queue, mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a synchronized PriorityDequeue instance based on the instance given.
|
||||||
|
* @param dequeue that should be synchronized
|
||||||
|
* @return a synchronized PriorityDequeue wrapper.
|
||||||
|
*/
|
||||||
|
public static BooleanPriorityDequeue synchronize(BooleanPriorityDequeue dequeue) {
|
||||||
|
return dequeue instanceof SynchronizedPriorityDequeue ? (SynchronizedPriorityDequeue)dequeue : new SynchronizedPriorityDequeue(dequeue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a synchronized PriorityDequeue instance based on the instance given.
|
||||||
|
* @param dequeue that should be synchronized
|
||||||
|
* @param mutex is the controller of the synchronization block.
|
||||||
|
* @return a synchronized PriorityDequeue wrapper.
|
||||||
|
*/
|
||||||
|
public static BooleanPriorityDequeue synchronize(BooleanPriorityDequeue dequeue, Object mutex) {
|
||||||
|
return dequeue instanceof SynchronizedPriorityDequeue ? (SynchronizedPriorityDequeue)dequeue : new SynchronizedPriorityDequeue(dequeue, mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper class for synchronization
|
||||||
|
*/
|
||||||
|
public static class SynchronizedPriorityQueue implements BooleanPriorityQueue
|
||||||
|
{
|
||||||
|
BooleanPriorityQueue queue;
|
||||||
|
protected Object mutex;
|
||||||
|
|
||||||
|
protected SynchronizedPriorityQueue(BooleanPriorityQueue queue) {
|
||||||
|
this.queue = queue;
|
||||||
|
mutex = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected SynchronizedPriorityQueue(BooleanPriorityQueue queue, Object mutex) {
|
||||||
|
this.queue = queue;
|
||||||
|
this.mutex = mutex;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanIterator iterator() { return queue.iterator(); }
|
||||||
|
@Override
|
||||||
|
public int size() { synchronized(mutex) { return queue.size(); } }
|
||||||
|
@Override
|
||||||
|
public void clear() { synchronized(mutex) { queue.clear(); } }
|
||||||
|
@Override
|
||||||
|
public void enqueue(boolean e) { synchronized(mutex) { queue.enqueue(e); } }
|
||||||
|
@Override
|
||||||
|
public void enqueueAll(boolean[] e, int offset, int length) { synchronized(mutex) { queue.enqueueAll(e, offset, length); } }
|
||||||
|
@Override
|
||||||
|
public void enqueueAll(BooleanCollection c) { synchronized(mutex) { queue.enqueueAll(c); } }
|
||||||
|
@Override
|
||||||
|
public boolean dequeue() { synchronized(mutex) { return queue.dequeue(); } }
|
||||||
|
@Override
|
||||||
|
public boolean peek(int index) { synchronized(mutex) { return queue.peek(index); } }
|
||||||
|
@Override
|
||||||
|
public boolean removeFirst(boolean e) { synchronized(mutex) { return queue.removeFirst(e); } }
|
||||||
|
@Override
|
||||||
|
public boolean removeLast(boolean e) { synchronized(mutex) { return queue.removeLast(e); } }
|
||||||
|
@Override
|
||||||
|
public void onChanged() { synchronized(mutex) { queue.onChanged(); } }
|
||||||
|
@Override
|
||||||
|
public BooleanComparator comparator() { synchronized(mutex) { return queue.comparator(); } }
|
||||||
|
@Override
|
||||||
|
public boolean[] toBooleanArray(boolean[] input) { synchronized(mutex) { return queue.toBooleanArray(input); } }
|
||||||
|
@Override
|
||||||
|
public BooleanPriorityQueue copy() { synchronized(mutex) { return queue.copy(); } }
|
||||||
|
@Override
|
||||||
|
public void forEach(BooleanConsumer action) { synchronized(mutex) { queue.forEach(action); } }
|
||||||
|
@Override
|
||||||
|
public <E> void forEach(E input, ObjectBooleanConsumer<E> action) { synchronized(mutex) { queue.forEach(input, action); } }
|
||||||
|
@Override
|
||||||
|
public boolean matchesAny(BooleanPredicate filter) { synchronized(mutex) { return queue.matchesAny(filter); } }
|
||||||
|
@Override
|
||||||
|
public boolean matchesNone(BooleanPredicate filter) { synchronized(mutex) { return queue.matchesNone(filter); } }
|
||||||
|
@Override
|
||||||
|
public boolean matchesAll(BooleanPredicate filter) { synchronized(mutex) { return queue.matchesAll(filter); } }
|
||||||
|
@Override
|
||||||
|
public boolean findFirst(BooleanPredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } }
|
||||||
|
@Override
|
||||||
|
public int count(BooleanPredicate filter) { synchronized(mutex) { return queue.count(filter); } }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper class for synchronization
|
||||||
|
*/
|
||||||
|
public static class SynchronizedPriorityDequeue extends SynchronizedPriorityQueue implements BooleanPriorityDequeue
|
||||||
|
{
|
||||||
|
BooleanPriorityDequeue dequeue;
|
||||||
|
protected SynchronizedPriorityDequeue(BooleanPriorityDequeue queue) {
|
||||||
|
super(queue);
|
||||||
|
dequeue = queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected SynchronizedPriorityDequeue(BooleanPriorityDequeue queue, Object mutex) {
|
||||||
|
super(queue, mutex);
|
||||||
|
dequeue = queue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enqueueFirst(boolean e) { synchronized(mutex) { dequeue.enqueueFirst(e); } }
|
||||||
|
@Override
|
||||||
|
public void enqueueAllFirst(boolean[] e, int offset, int length) { synchronized(mutex) { dequeue.enqueueAllFirst(e, offset, length); } }
|
||||||
|
@Override
|
||||||
|
public void enqueueAllFirst(BooleanCollection c) { synchronized(mutex) { dequeue.enqueueAllFirst(c); } }
|
||||||
|
@Override
|
||||||
|
public boolean dequeueLast() { synchronized(mutex) { return dequeue.dequeueLast(); } }
|
||||||
|
@Override
|
||||||
|
public BooleanPriorityDequeue copy() { synchronized(mutex) { return dequeue.copy(); } }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,258 @@
|
|||||||
|
package speiger.src.collections.booleans.utils;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Spliterator;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanCollection;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanIterator;
|
||||||
|
import speiger.src.collections.booleans.collections.BooleanSplititerator;
|
||||||
|
import speiger.src.collections.booleans.functions.BooleanConsumer;
|
||||||
|
import speiger.src.collections.utils.SanityChecks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper class that provides SplitIterators for normal and stream usage
|
||||||
|
*/
|
||||||
|
public class BooleanSplititerators
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Creates a Type Specific SplitIterator to reduce boxing/unboxing
|
||||||
|
* @param array that should be wrapped into a split iterator
|
||||||
|
* @param characteristics characteristics properties of this spliterator's source or elements.
|
||||||
|
* @return a Type Specific SplitIterator
|
||||||
|
*/
|
||||||
|
public static BooleanSplititerator createArraySplititerator(boolean[] array, int characteristics) { return createArraySplititerator(array, 0, array.length, characteristics);}
|
||||||
|
/**
|
||||||
|
* Creates a Type Specific SplitIterator to reduce boxing/unboxing
|
||||||
|
* @param array that should be wrapped into a split iterator
|
||||||
|
* @param size the maximum index within the array
|
||||||
|
* @param characteristics characteristics properties of this spliterator's source or elements.
|
||||||
|
* @return a Type Specific SplitIterator
|
||||||
|
* @throws IllegalStateException if the size is outside of the array size
|
||||||
|
*/
|
||||||
|
public static BooleanSplititerator createArraySplititerator(boolean[] array, int size, int characteristics) { return createArraySplititerator(array, 0, size, characteristics);}
|
||||||
|
/**
|
||||||
|
* Creates a Type Specific SplitIterator to reduce boxing/unboxing
|
||||||
|
* @param array that should be wrapped into a split iterator
|
||||||
|
* @param offset the starting index of the array
|
||||||
|
* @param size the maximum index within the array
|
||||||
|
* @param characteristics characteristics properties of this spliterator's source or elements.
|
||||||
|
* @return a Type Specific SplitIterator
|
||||||
|
* @throws IllegalStateException the offset and size are outside of the arrays range
|
||||||
|
*/
|
||||||
|
public static BooleanSplititerator createArraySplititerator(boolean[] array, int offset, int size, int characteristics) {
|
||||||
|
SanityChecks.checkArrayCapacity(array.length, offset, size);
|
||||||
|
return new TypeArraySplitIterator(array, offset, size, characteristics);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Type Specific SplitIterator to reduce boxing/unboxing
|
||||||
|
* @param collection the collection that should be wrapped in a split iterator
|
||||||
|
* @param characteristics characteristics properties of this spliterator's source or elements.
|
||||||
|
* @return a Type Specific SplitIterator
|
||||||
|
*/
|
||||||
|
public static BooleanSplititerator createSplititerator(BooleanCollection collection, int characteristics) {
|
||||||
|
return new TypeIteratorSpliterator(collection, characteristics);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Type Specific SplitIterator to reduce boxing/unboxing
|
||||||
|
* @param iterator the Iterator that should be wrapped in a split iterator
|
||||||
|
* @param characteristics characteristics properties of this spliterator's source or elements.
|
||||||
|
* @return a Type Specific SplitIterator
|
||||||
|
*/
|
||||||
|
public static BooleanSplititerator createUnknownSplititerator(BooleanIterator iterator, int characteristics) {
|
||||||
|
return new TypeIteratorSpliterator(iterator, characteristics);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Type Specific SplitIterator to reduce boxing/unboxing
|
||||||
|
* @param iterator the collection that should be wrapped in a split iterator
|
||||||
|
* @param size the amount of elements in the iterator
|
||||||
|
* @param characteristics characteristics properties of this spliterator's source or elements.
|
||||||
|
* @return a Type Specific SplitIterator
|
||||||
|
*/
|
||||||
|
public static BooleanSplititerator createSizedSplititerator(BooleanIterator iterator, long size, int characteristics) {
|
||||||
|
return new TypeIteratorSpliterator(iterator, size, characteristics);
|
||||||
|
}
|
||||||
|
|
||||||
|
static class TypeIteratorSpliterator implements BooleanSplititerator {
|
||||||
|
static final int BATCH_UNIT = 1 << 10;
|
||||||
|
static final int MAX_BATCH = 1 << 25;
|
||||||
|
private final BooleanCollection collection;
|
||||||
|
private BooleanIterator it;
|
||||||
|
private final int characteristics;
|
||||||
|
private long est;
|
||||||
|
private int batch;
|
||||||
|
|
||||||
|
TypeIteratorSpliterator(BooleanCollection collection, int characteristics) {
|
||||||
|
this.collection = collection;
|
||||||
|
it = null;
|
||||||
|
this.characteristics = (characteristics & Spliterator.CONCURRENT) == 0
|
||||||
|
? characteristics | Spliterator.SIZED | Spliterator.SUBSIZED
|
||||||
|
: characteristics;
|
||||||
|
}
|
||||||
|
|
||||||
|
TypeIteratorSpliterator(BooleanIterator iterator, long size, int characteristics) {
|
||||||
|
collection = null;
|
||||||
|
it = iterator;
|
||||||
|
est = size;
|
||||||
|
this.characteristics = (characteristics & Spliterator.CONCURRENT) == 0
|
||||||
|
? characteristics | Spliterator.SIZED | Spliterator.SUBSIZED
|
||||||
|
: characteristics;
|
||||||
|
}
|
||||||
|
|
||||||
|
TypeIteratorSpliterator(BooleanIterator iterator, int characteristics) {
|
||||||
|
collection = null;
|
||||||
|
it = iterator;
|
||||||
|
est = Long.MAX_VALUE;
|
||||||
|
this.characteristics = characteristics & ~(Spliterator.SIZED | Spliterator.SUBSIZED);
|
||||||
|
}
|
||||||
|
|
||||||
|
private BooleanIterator iterator()
|
||||||
|
{
|
||||||
|
if (it == null) {
|
||||||
|
it = collection.iterator();
|
||||||
|
est = collection.size();
|
||||||
|
}
|
||||||
|
return it;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanSplititerator trySplit() {
|
||||||
|
BooleanIterator i = iterator();
|
||||||
|
if (est > 1 && i.hasNext()) {
|
||||||
|
int n = Math.min(batch + BATCH_UNIT, Math.min((int)est, MAX_BATCH));
|
||||||
|
boolean[] a = new boolean[n];
|
||||||
|
int j = 0;
|
||||||
|
do { a[j] = i.nextBoolean(); } while (++j < n && i.hasNext());
|
||||||
|
batch = j;
|
||||||
|
if (est != Long.MAX_VALUE)
|
||||||
|
est -= j;
|
||||||
|
return new TypeArraySplitIterator(a, 0, j, characteristics);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEachRemaining(BooleanConsumer action) {
|
||||||
|
if (action == null) throw new NullPointerException();
|
||||||
|
iterator().forEachRemaining(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean tryAdvance(BooleanConsumer action) {
|
||||||
|
if (action == null) throw new NullPointerException();
|
||||||
|
BooleanIterator iter = iterator();
|
||||||
|
if (iter.hasNext()) {
|
||||||
|
action.accept(iter.nextBoolean());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean tryAdvance(Consumer<? super Boolean> action) {
|
||||||
|
if (action == null) throw new NullPointerException();
|
||||||
|
BooleanIterator iter = iterator();
|
||||||
|
if (iter.hasNext()) {
|
||||||
|
action.accept(Boolean.valueOf(iter.nextBoolean()));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long estimateSize() {
|
||||||
|
iterator();
|
||||||
|
return est;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int characteristics() { return characteristics; }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Comparator<? super Boolean> getComparator() {
|
||||||
|
if (hasCharacteristics(4)) //Sorted
|
||||||
|
return null;
|
||||||
|
throw new IllegalStateException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean nextBoolean() { return iterator().nextBoolean(); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() { return iterator().hasNext(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
static final class TypeArraySplitIterator implements BooleanSplititerator {
|
||||||
|
private final boolean[] array;
|
||||||
|
private int index;
|
||||||
|
private final int fence;
|
||||||
|
private final int characteristics;
|
||||||
|
|
||||||
|
public TypeArraySplitIterator(boolean[] array, int origin, int fence, int additionalCharacteristics) {
|
||||||
|
this.array = array;
|
||||||
|
index = origin;
|
||||||
|
this.fence = fence;
|
||||||
|
characteristics = additionalCharacteristics | Spliterator.SIZED | Spliterator.SUBSIZED;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public BooleanSplititerator trySplit() {
|
||||||
|
int lo = index, mid = (lo + fence) >>> 1;
|
||||||
|
return (lo >= mid) ? null : new TypeArraySplitIterator(array, lo, index = mid, characteristics);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void forEachRemaining(BooleanConsumer action) {
|
||||||
|
if (action == null) throw new NullPointerException();
|
||||||
|
boolean[] a; int i, hi;
|
||||||
|
if ((a = array).length >= (hi = fence) && (i = index) >= 0 && i < (index = hi)) {
|
||||||
|
do { action.accept(a[i]); } while (++i < hi);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean tryAdvance(BooleanConsumer action) {
|
||||||
|
if (action == null) throw new NullPointerException();
|
||||||
|
if (index >= 0 && index < fence) {
|
||||||
|
action.accept(array[index++]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean tryAdvance(Consumer<? super Boolean> action) {
|
||||||
|
if (action == null) throw new NullPointerException();
|
||||||
|
if (index >= 0 && index < fence) {
|
||||||
|
action.accept(Boolean.valueOf(array[index++]));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long estimateSize() { return fence - index; }
|
||||||
|
@Override
|
||||||
|
public int characteristics() { return characteristics; }
|
||||||
|
@Override
|
||||||
|
public Comparator<? super Boolean> getComparator() {
|
||||||
|
if (hasCharacteristics(4)) //Sorted
|
||||||
|
return null;
|
||||||
|
throw new IllegalStateException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean nextBoolean() {
|
||||||
|
if(!hasNext()) throw new NoSuchElementException();
|
||||||
|
return array[index++];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasNext() { return index < fence; }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,46 @@
|
|||||||
|
package speiger.src.collections.booleans.utils;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Strategy class that allows to give control hashcode generation and equals comparason for maps
|
||||||
|
*/
|
||||||
|
public interface BooleanStrategy
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Normal Strategy
|
||||||
|
*/
|
||||||
|
public static final BooleanStrategy NORMAL = new NormalStrategy();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return a Normal Strategy that is behaving exactly like the normal Hash Strategy in the Hash Collections
|
||||||
|
*/
|
||||||
|
public static BooleanStrategy normalStrategy() { return (BooleanStrategy)NORMAL; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type Specific HashCode function
|
||||||
|
* @param o the element that the hashcode is requested for (if object may be null)
|
||||||
|
* @return hashcode for the given entry
|
||||||
|
*/
|
||||||
|
public int hashCode(boolean o);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type Specific Equals function
|
||||||
|
* @param key the first element that should be compared with
|
||||||
|
* @param value the second element that should be compared with (if object may be null)
|
||||||
|
* @return if the elements match
|
||||||
|
*/
|
||||||
|
public boolean equals(boolean key, boolean value);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Strategy that simulates the normal Hash Collection Behavior if you want to use Hash Control Collections to replace normal ones.
|
||||||
|
* Only real reason to do that is you have to use this and want to get rid of implementations.
|
||||||
|
*/
|
||||||
|
public static class NormalStrategy implements BooleanStrategy
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public int hashCode(boolean o) { return Boolean.hashCode(o); }
|
||||||
|
@Override
|
||||||
|
public boolean equals(boolean key, boolean value) { return Objects.equals(value, Boolean.valueOf(key)); }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,30 @@
|
|||||||
|
package speiger.src.collections.booleans.utils;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import speiger.src.collections.utils.IArray;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type-Specific Helper class to get the underlying array of array implementations.
|
||||||
|
* @see speiger.src.collections.booleans.lists.BooleanArrayList
|
||||||
|
*/
|
||||||
|
public interface IBooleanArray extends IArray
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Provides the Underlying Array in the Implementation
|
||||||
|
* @return underlying Array
|
||||||
|
* @throws ClassCastException if the return type does not match the underlying array. (Only for Object Implementations)
|
||||||
|
*/
|
||||||
|
public boolean[] elements();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides the Underlying Array in the Implementation. This function exists purely because of Synchronization wrappers to help run Synchronized Code
|
||||||
|
* @param action the action that handles the array
|
||||||
|
* @throws NullPointerException if action is null
|
||||||
|
*/
|
||||||
|
public default void elements(Consumer<boolean[]> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
action.accept(elements());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,252 @@
|
|||||||
|
package speiger.src.collections.bytes.collections;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.AbstractCollection;
|
||||||
|
|
||||||
|
import speiger.src.collections.bytes.functions.ByteConsumer;
|
||||||
|
import speiger.src.collections.bytes.utils.ByteIterators;
|
||||||
|
import speiger.src.collections.bytes.utils.ByteArrays;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract Type Specific Collection that reduces boxing/unboxing
|
||||||
|
*/
|
||||||
|
public abstract class AbstractByteCollection extends AbstractCollection<Byte> implements ByteCollection
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public abstract ByteIterator iterator();
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean add(Byte e) { return ByteCollection.super.add(e); }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean addAll(ByteCollection c) {
|
||||||
|
boolean modified = false;
|
||||||
|
for(ByteIterator iter = c.iterator();iter.hasNext();modified |= add(iter.nextByte()));
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ByteCollection copy() { throw new UnsupportedOperationException(); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean contains(Object e) { return ByteCollection.super.contains(e); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation of contains. This implementation iterates over the elements and returns true if the value match.
|
||||||
|
* @param e the element that should be searched for.
|
||||||
|
* @return true if the value was found.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean contains(byte e) {
|
||||||
|
for(ByteIterator iter = iterator();iter.hasNext();) { if(iter.nextByte() == e) return true; }
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean addAll(Collection<? extends Byte> c)
|
||||||
|
{
|
||||||
|
return c instanceof ByteCollection ? addAll((ByteCollection)c) : super.addAll(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation of containsAll. This implementation iterates over all elements and checks all elements are present in the other collection.
|
||||||
|
* @param c the collection that should be checked if it contains all elements.
|
||||||
|
* @return true if all elements were found in the collection
|
||||||
|
* @throws java.lang.NullPointerException if the collection is null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean containsAll(ByteCollection c) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
if(c.isEmpty()) return true;
|
||||||
|
for(ByteIterator iter = c.iterator();iter.hasNext();)
|
||||||
|
if(!contains(iter.nextByte()))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean containsAll(Collection<?> c) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
return c instanceof ByteCollection ? containsAll((ByteCollection)c) : super.containsAll(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This implementation iterates over the elements of the collection and checks if they are stored in this collection
|
||||||
|
* @param c the elements that should be checked for
|
||||||
|
* @return true if any element is in this collection
|
||||||
|
* @throws java.lang.NullPointerException if the collection is null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean containsAny(Collection<?> c) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
if(c.isEmpty()) return false;
|
||||||
|
for(Object e : c)
|
||||||
|
if(contains(e))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This implementation iterates over the elements of the collection and checks if they are stored in this collection.
|
||||||
|
* @param c the elements that should be checked for
|
||||||
|
* @return true if any element is in this collection
|
||||||
|
* @throws java.lang.NullPointerException if the collection is null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean containsAny(ByteCollection c) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
if(c.isEmpty()) return false;
|
||||||
|
for(ByteIterator iter = c.iterator();iter.hasNext();)
|
||||||
|
if(contains(iter.nextByte()))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public boolean remove(Object e) { return ByteCollection.super.remove(e); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation of remove. This implementation iterates over the elements until it finds the element that is searched for or it runs out of elements.
|
||||||
|
* It stops after finding the first element
|
||||||
|
* @param e the element that is searched for
|
||||||
|
* @return true if the element was found and removed.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean remByte(byte e) {
|
||||||
|
for(ByteIterator iter = iterator();iter.hasNext();) {
|
||||||
|
if(iter.nextByte() == e) {
|
||||||
|
iter.remove();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation of removeAll. This Implementation iterates over all elements and removes them as they were found in the other collection.
|
||||||
|
* @param c the elements that should be deleted
|
||||||
|
* @return true if the collection was modified.
|
||||||
|
* @throws java.lang.NullPointerException if the collection is null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean removeAll(ByteCollection c) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
if(c.isEmpty()) return false;
|
||||||
|
boolean modified = false;
|
||||||
|
for(ByteIterator iter = iterator();iter.hasNext();) {
|
||||||
|
if(c.contains(iter.nextByte())) {
|
||||||
|
iter.remove();
|
||||||
|
modified = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean removeAll(ByteCollection c, ByteConsumer r) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
if(c.isEmpty()) return false;
|
||||||
|
Objects.requireNonNull(r);
|
||||||
|
boolean modified = false;
|
||||||
|
for(ByteIterator iter = iterator();iter.hasNext();) {
|
||||||
|
byte e = iter.nextByte();
|
||||||
|
if(c.contains(e)) {
|
||||||
|
r.accept(e);
|
||||||
|
iter.remove();
|
||||||
|
modified = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation of retainAll. This Implementation iterates over all elements and removes them as they were not found in the other collection.
|
||||||
|
* @param c the elements that should be kept
|
||||||
|
* @return true if the collection was modified.
|
||||||
|
* @throws java.lang.NullPointerException if the collection is null
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean retainAll(ByteCollection c) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
if(c.isEmpty()) {
|
||||||
|
boolean modified = !isEmpty();
|
||||||
|
clear();
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
boolean modified = false;
|
||||||
|
for(ByteIterator iter = iterator();iter.hasNext();) {
|
||||||
|
if(!c.contains(iter.nextByte())) {
|
||||||
|
iter.remove();
|
||||||
|
modified = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean retainAll(ByteCollection c, ByteConsumer r) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
Objects.requireNonNull(r);
|
||||||
|
if(c.isEmpty()) {
|
||||||
|
boolean modified = !isEmpty();
|
||||||
|
forEach(r);
|
||||||
|
clear();
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
boolean modified = false;
|
||||||
|
for(ByteIterator iter = iterator();iter.hasNext();) {
|
||||||
|
byte e = iter.nextByte();
|
||||||
|
if(!c.contains(e)) {
|
||||||
|
r.accept(e);
|
||||||
|
iter.remove();
|
||||||
|
modified = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation of toArray that links to {@link #toByteArray(byte[])} with a newly created array.
|
||||||
|
* @return an array containing all of the elements in this collection
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public byte[] toByteArray() {
|
||||||
|
if(isEmpty()) return ByteArrays.EMPTY_ARRAY;
|
||||||
|
return toByteArray(new byte[size()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific implementation of toArray. This implementation iterates over all elements and unwraps them into primitive type.
|
||||||
|
* @param a array that the elements should be injected to. If null or to small a new array with the right size is created
|
||||||
|
* @return an array containing all of the elements in this collection
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public byte[] toByteArray(byte[] a) {
|
||||||
|
if(a == null || a.length < size()) a = new byte[size()];
|
||||||
|
ByteIterators.unwrap(a, iterator());
|
||||||
|
if (a.length > size()) a[size()] = (byte)0;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,52 @@
|
|||||||
|
package speiger.src.collections.bytes.collections;
|
||||||
|
|
||||||
|
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
|
||||||
|
/**
|
||||||
|
* A Type-Specific {@link ObjectBidirectionalIterator} to reduce (un)boxing
|
||||||
|
*/
|
||||||
|
public interface ByteBidirectionalIterator extends ByteIterator, ObjectBidirectionalIterator<Byte>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns true if the Iterator has a Previous element
|
||||||
|
* @return true if the Iterator has a Previous element
|
||||||
|
*/
|
||||||
|
public boolean hasPrevious();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the Previous element of the iterator.
|
||||||
|
* @return the Previous element of the iterator.
|
||||||
|
* @throws java.util.NoSuchElementException if the iteration has no more elements
|
||||||
|
*/
|
||||||
|
public byte previousByte();
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default Byte previous() {
|
||||||
|
return Byte.valueOf(previousByte());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
default int skip(int amount)
|
||||||
|
{
|
||||||
|
return ByteIterator.super.skip(amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverses the Given amount of elements if possible. A Optimization function to reverse elements faster if the implementation allows it.
|
||||||
|
* @param amount the amount of elements that should be reversed
|
||||||
|
* @return the amount of elements that were reversed
|
||||||
|
*/
|
||||||
|
public default int back(int amount) {
|
||||||
|
if(amount < 0) throw new IllegalStateException("Can't go forward");
|
||||||
|
int i = 0;
|
||||||
|
for(;i<amount && hasPrevious();previousByte(),i++);
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,279 @@
|
|||||||
|
package speiger.src.collections.bytes.collections;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.IntPredicate;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
import speiger.src.collections.bytes.functions.ByteConsumer;
|
||||||
|
import speiger.src.collections.bytes.utils.ByteSplititerators;
|
||||||
|
import speiger.src.collections.bytes.utils.ByteCollections;
|
||||||
|
import speiger.src.collections.utils.ISizeProvider;
|
||||||
|
import speiger.src.collections.utils.SanityChecks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific {@link Collection} that reduces (un)boxing
|
||||||
|
*/
|
||||||
|
public interface ByteCollection extends Collection<Byte>, ByteIterable, ISizeProvider
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* A Type-Specific add function to reduce (un)boxing
|
||||||
|
* @param o the element that should be added
|
||||||
|
* @return true if the element was added to the collection
|
||||||
|
*/
|
||||||
|
public boolean add(byte o);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific addAll function to reduce (un)boxing
|
||||||
|
* @param c the collection of elements that should be added
|
||||||
|
* @return true if elements were added into the collection
|
||||||
|
*/
|
||||||
|
public boolean addAll(ByteCollection c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific Array based addAll method to reduce the amount of Wrapping
|
||||||
|
* @param e the elements that should be added
|
||||||
|
* @return if the collection was modified
|
||||||
|
*/
|
||||||
|
public default boolean addAll(byte... e) { return addAll(e, 0, e.length); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific Array based addAll method to reduce the amount of Wrapping
|
||||||
|
* @param e the elements that should be added
|
||||||
|
* @param length how many elements of the array should be added
|
||||||
|
* @return if the collection was modified
|
||||||
|
*/
|
||||||
|
public default boolean addAll(byte[] e, int length) { return addAll(e, 0, length); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific Array based addAll method to reduce the amount of Wrapping
|
||||||
|
* @param e the elements that should be added
|
||||||
|
* @param offset where to start within the array
|
||||||
|
* @param length how many elements of the array should be added
|
||||||
|
* @return if the collection was modified
|
||||||
|
*/
|
||||||
|
public default boolean addAll(byte[] e, int offset, int length) {
|
||||||
|
if(length <= 0) return false;
|
||||||
|
SanityChecks.checkArrayCapacity(e.length, offset, length);
|
||||||
|
boolean added = false;
|
||||||
|
for(int i = 0;i<length;i++) {
|
||||||
|
if(add(e[offset+i])) added = true;
|
||||||
|
}
|
||||||
|
return added;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific contains function to reduce (un)boxing
|
||||||
|
* @param o the element that is checked for
|
||||||
|
* @return true if the element is found in the collection
|
||||||
|
*/
|
||||||
|
public boolean contains(byte o);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific containsAll function to reduce (un)boxing
|
||||||
|
* @param c the collection of elements that should be tested for
|
||||||
|
* @return true if all the element is found in the collection
|
||||||
|
*/
|
||||||
|
public boolean containsAll(ByteCollection c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific containsAny function to reduce (un)boxing
|
||||||
|
* @param c the collection of elements that should be tested for
|
||||||
|
* @return true if any element was found
|
||||||
|
*/
|
||||||
|
public boolean containsAny(ByteCollection c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if any element of the Collection is found in the provided collection.
|
||||||
|
* A Small Optimization function to find out of any element is present when comparing collections and not all of them.
|
||||||
|
* @param c the collection of elements that should be tested for
|
||||||
|
* @return true if any element was found.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public boolean containsAny(Collection<?> c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific remove function that reduces (un)boxing.
|
||||||
|
* @param o the element that should be removed
|
||||||
|
* @return true if the element was removed
|
||||||
|
* @see Collection#remove(Object)
|
||||||
|
*/
|
||||||
|
public boolean remByte(byte o);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific removeAll function that reduces (un)boxing.
|
||||||
|
* @param c the collection of elements that should be removed
|
||||||
|
* @return true if any element was removed
|
||||||
|
* @see Collection#removeAll(Collection)
|
||||||
|
*/
|
||||||
|
public boolean removeAll(ByteCollection c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific removeAll function that reduces (un)boxing.
|
||||||
|
* It also notifies the remover of which exact element is going to be removed.
|
||||||
|
* @param c the collection of elements that should be removed
|
||||||
|
* @param r elements that got removed
|
||||||
|
* @return true if any element was removed
|
||||||
|
* @see Collection#removeAll(Collection)
|
||||||
|
*/
|
||||||
|
public boolean removeAll(ByteCollection c, ByteConsumer r);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific retainAll function that reduces (un)boxing.
|
||||||
|
* @param c the collection of elements that should be kept
|
||||||
|
* @return true if any element was removed
|
||||||
|
* @see Collection#retainAll(Collection)
|
||||||
|
*/
|
||||||
|
public boolean retainAll(ByteCollection c);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific retainAll function that reduces (un)boxing.
|
||||||
|
* It also notifies the remover of which exact element is going to be removed.
|
||||||
|
* @param c the collection of elements that should be kept
|
||||||
|
* @param r elements that got removed
|
||||||
|
* @return true if any element was removed
|
||||||
|
* @see Collection#retainAll(Collection)
|
||||||
|
*/
|
||||||
|
public boolean retainAll(ByteCollection c, ByteConsumer r);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to collect all elements
|
||||||
|
* @param collection that the elements should be inserted to
|
||||||
|
* @param <E> the collection type
|
||||||
|
* @return the input with the desired elements
|
||||||
|
*/
|
||||||
|
default <E extends ByteCollection> E pour(E collection) {
|
||||||
|
collection.addAll(this);
|
||||||
|
return collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Function that does a shallow clone of the Collection itself.
|
||||||
|
* This function is more optimized then a copy constructor since the Collection does not have to be unsorted/resorted.
|
||||||
|
* It can be compared to Cloneable but with less exception risk
|
||||||
|
* @return a Shallow Copy of the collection
|
||||||
|
* @note Wrappers and view collections will not support this feature
|
||||||
|
*/
|
||||||
|
public ByteCollection copy();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific toArray function that delegates to {@link #toByteArray(byte[])} with a newly created array.
|
||||||
|
* @return an array containing all of the elements in this collection
|
||||||
|
* @see Collection#toArray()
|
||||||
|
*/
|
||||||
|
public byte[] toByteArray();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific toArray function that reduces (un)boxing.
|
||||||
|
* @param a array that the elements should be injected to. If null or to small a new array with the right size is created
|
||||||
|
* @return an array containing all of the elements in this collection
|
||||||
|
* @see Collection#toArray(Object[])
|
||||||
|
*/
|
||||||
|
public byte[] toByteArray(byte[] a);
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default boolean removeIf(Predicate<? super Byte> filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
return remIf(v -> filter.test(Byte.valueOf(SanityChecks.castToByte(v))));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific removeIf function to reduce (un)boxing.
|
||||||
|
* <p>Removes elements that were selected by the filter
|
||||||
|
* @see Collection#removeIf(Predicate)
|
||||||
|
* @param filter Filters the elements that should be removed
|
||||||
|
* @return true if the collection was modified
|
||||||
|
* @throws java.lang.NullPointerException if filter is null
|
||||||
|
*/
|
||||||
|
public default boolean remIf(IntPredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
boolean removed = false;
|
||||||
|
final ByteIterator each = iterator();
|
||||||
|
while (each.hasNext()) {
|
||||||
|
if (filter.test(each.nextByte())) {
|
||||||
|
each.remove();
|
||||||
|
removed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return removed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default boolean add(Byte o) { return add(o.byteValue()); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default boolean contains(Object o) { return o != null && contains(((Byte)o).byteValue()); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default boolean remove(Object o) { return o != null && remByte(((Byte)o).byteValue()); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a Type-Specific Iterator to reduce (un)boxing
|
||||||
|
* @return a iterator of the collection
|
||||||
|
* @see Collection#iterator()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public ByteIterator iterator();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Wrapped Collection that is Synchronized
|
||||||
|
* @return a new Collection that is synchronized
|
||||||
|
* @see ByteCollections#synchronize
|
||||||
|
*/
|
||||||
|
public default ByteCollection synchronize() { return ByteCollections.synchronize(this); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Wrapped Collection that is Synchronized
|
||||||
|
* @param mutex is the controller of the synchronization block
|
||||||
|
* @return a new Collection Wrapper that is synchronized
|
||||||
|
* @see ByteCollections#synchronize
|
||||||
|
*/
|
||||||
|
public default ByteCollection synchronize(Object mutex) { return ByteCollections.synchronize(this, mutex); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Wrapped Collection that is unmodifiable
|
||||||
|
* @return a new Collection Wrapper that is unmodifiable
|
||||||
|
* @see ByteCollections#unmodifiable
|
||||||
|
*/
|
||||||
|
public default ByteCollection unmodifiable() { return ByteCollections.unmodifiable(this); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a Java-Type-Specific Stream to reduce boxing/unboxing.
|
||||||
|
* @return a Stream of the closest java type
|
||||||
|
*/
|
||||||
|
default IntStream primitiveStream() { return StreamSupport.intStream(ByteSplititerators.createJavaSplititerator(this, 0), false); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a Java-Type-Specific Parallel Stream to reduce boxing/unboxing.
|
||||||
|
* @return a Stream of the closest java type
|
||||||
|
*/
|
||||||
|
default IntStream parallelPrimitiveStream() { return StreamSupport.intStream(ByteSplititerators.createJavaSplititerator(this, 0), true); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Type Splititerator to reduce boxing/unboxing
|
||||||
|
* @return type specific splititerator
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
default ByteSplititerator spliterator() { return ByteSplititerators.createSplititerator(this, 0); }
|
||||||
|
}
|
||||||
@ -0,0 +1,343 @@
|
|||||||
|
package speiger.src.collections.bytes.collections;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
|
||||||
|
import speiger.src.collections.bytes.functions.ByteConsumer;
|
||||||
|
import speiger.src.collections.bytes.functions.ByteComparator;
|
||||||
|
import speiger.src.collections.objects.collections.ObjectIterable;
|
||||||
|
import speiger.src.collections.bytes.functions.function.ByteFunction;
|
||||||
|
import speiger.src.collections.ints.functions.consumer.IntByteConsumer;
|
||||||
|
import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer;
|
||||||
|
import speiger.src.collections.bytes.functions.function.BytePredicate;
|
||||||
|
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
|
||||||
|
import speiger.src.collections.bytes.lists.ByteList;
|
||||||
|
import speiger.src.collections.bytes.lists.ByteArrayList;
|
||||||
|
import speiger.src.collections.bytes.sets.ByteSet;
|
||||||
|
import speiger.src.collections.bytes.sets.ByteLinkedOpenHashSet;
|
||||||
|
import speiger.src.collections.bytes.utils.ByteArrays;
|
||||||
|
import speiger.src.collections.bytes.utils.ByteAsyncBuilder;
|
||||||
|
import speiger.src.collections.bytes.utils.ByteSplititerators;
|
||||||
|
import speiger.src.collections.bytes.utils.ByteIterables;
|
||||||
|
import speiger.src.collections.bytes.utils.ByteIterators;
|
||||||
|
import speiger.src.collections.utils.ISizeProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific {@link Iterable} that reduces (un)boxing
|
||||||
|
*/
|
||||||
|
public interface ByteIterable extends Iterable<Byte>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns an iterator over elements of type {@code T}.
|
||||||
|
*
|
||||||
|
* @return an Iterator.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
ByteIterator iterator();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific foreach function that reduces (un)boxing
|
||||||
|
*
|
||||||
|
* @implSpec
|
||||||
|
* <p>The default implementation behaves as if:
|
||||||
|
* <pre>{@code
|
||||||
|
* iterator().forEachRemaining(action);
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param action The action to be performed for each element
|
||||||
|
* @throws NullPointerException if the specified action is null
|
||||||
|
* @see Iterable#forEach(Consumer)
|
||||||
|
*/
|
||||||
|
default void forEach(ByteConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
iterator().forEachRemaining(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
@Override
|
||||||
|
default void forEach(Consumer<? super Byte> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
iterator().forEachRemaining(action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Indexed forEach implementation that allows you to keep track of how many elements were already iterated over.
|
||||||
|
* @param action The action to be performed for each element
|
||||||
|
* @throws java.lang.NullPointerException if the specified action is null
|
||||||
|
*/
|
||||||
|
public default void forEachIndexed(IntByteConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
int index = 0;
|
||||||
|
for(ByteIterator iter = iterator();iter.hasNext();action.accept(index++, iter.nextByte()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to reduce Lambda usage and allow for more method references, since these are faster/cleaner.
|
||||||
|
* @param input the object that should be included
|
||||||
|
* @param action The action to be performed for each element
|
||||||
|
* @param <E> the generic type of the Object
|
||||||
|
* @throws java.lang.NullPointerException if the specified action is null
|
||||||
|
*/
|
||||||
|
default <E> void forEach(E input, ObjectByteConsumer<E> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
iterator().forEachRemaining(input, action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Type Splititerator to reduce boxing/unboxing
|
||||||
|
* @return type specific splititerator
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
default ByteSplititerator spliterator() { return ByteSplititerators.createUnknownSplititerator(iterator(), 0); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Async Builder for moving work of the thread.
|
||||||
|
* It is not designed to split the work to multithreaded work, so using this keep it singlethreaded, but it allows to be moved to another thread.
|
||||||
|
* @see ByteAsyncBuilder
|
||||||
|
* @return a AsyncBuilder
|
||||||
|
*/
|
||||||
|
default ByteAsyncBuilder asAsync() {
|
||||||
|
return new ByteAsyncBuilder(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to convert a Iterable to something else.
|
||||||
|
* @param mapper the mapping function
|
||||||
|
* @param <E> The return type.
|
||||||
|
* @return a new Iterable that returns the desired result
|
||||||
|
*/
|
||||||
|
default <E> ObjectIterable<E> map(ByteFunction<E> mapper) {
|
||||||
|
return ByteIterables.map(this, mapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to convert a Iterable to something else.
|
||||||
|
* @param mapper the flatMapping function
|
||||||
|
* @param <V> The return type supplier.
|
||||||
|
* @param <E> The return type.
|
||||||
|
* @return a new Iterable that returns the desired result
|
||||||
|
* @note does not support toByteArray optimizations.
|
||||||
|
*/
|
||||||
|
default <E, V extends Iterable<E>> ObjectIterable<E> flatMap(ByteFunction<V> mapper) {
|
||||||
|
return ByteIterables.flatMap(this, mapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to convert a Iterable to something else.
|
||||||
|
* @param mapper the flatMapping function
|
||||||
|
* @param <E> The return type.
|
||||||
|
* @return a new Iterable that returns the desired result
|
||||||
|
* @note does not support toByteArray optimizations.
|
||||||
|
*/
|
||||||
|
default <E> ObjectIterable<E> arrayflatMap(ByteFunction<E[]> mapper) {
|
||||||
|
return ByteIterables.arrayFlatMap(this, mapper);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to filter out unwanted elements
|
||||||
|
* @param filter the elements that should be kept.
|
||||||
|
* @return a Iterable that filtered out all unwanted elements
|
||||||
|
* @note does not support toByteArray optimizations.
|
||||||
|
*/
|
||||||
|
default ByteIterable filter(BytePredicate filter) {
|
||||||
|
return ByteIterables.filter(this, filter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to filter out duplicated elements
|
||||||
|
* @return a Iterable that filtered out all duplicated elements
|
||||||
|
* @note does not support toByteArray optimizations.
|
||||||
|
*/
|
||||||
|
default ByteIterable distinct() {
|
||||||
|
return ByteIterables.distinct(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to repeat elements a desired amount of times
|
||||||
|
* @param repeats how many times the elements should be repeated
|
||||||
|
* @return a Iterable that is repeating multiple times
|
||||||
|
*/
|
||||||
|
default ByteIterable repeat(int repeats) {
|
||||||
|
return ByteIterables.repeat(this, repeats);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to limit the amount of elements
|
||||||
|
* @param limit the amount of elements it should be limited to
|
||||||
|
* @return a Iterable that is limited in length
|
||||||
|
*/
|
||||||
|
default ByteIterable limit(long limit) {
|
||||||
|
return ByteIterables.limit(this, limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to sort the elements
|
||||||
|
* @param sorter that sorts the elements.
|
||||||
|
* @return a Iterable that is sorted
|
||||||
|
*/
|
||||||
|
default ByteIterable sorted(ByteComparator sorter) {
|
||||||
|
return ByteIterables.sorted(this, sorter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to preview elements before they are iterated through
|
||||||
|
* @param action the action that should be applied
|
||||||
|
* @return a Peeked Iterable
|
||||||
|
*/
|
||||||
|
default ByteIterable peek(ByteConsumer action) {
|
||||||
|
return ByteIterables.peek(this, action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function to reduce the usage of Streams and allows to collect all elements
|
||||||
|
* @param collection that the elements should be inserted to
|
||||||
|
* @param <E> the collection type
|
||||||
|
* @return the input with the desired elements
|
||||||
|
*/
|
||||||
|
default <E extends ByteCollection> E pour(E collection) {
|
||||||
|
ByteIterators.pour(iterator(), collection);
|
||||||
|
return collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that reduces the usage of streams and allows to collect all elements as a ArrayList
|
||||||
|
* @return a new ArrayList of all elements
|
||||||
|
*/
|
||||||
|
default ByteList pourAsList() {
|
||||||
|
return pour(new ByteArrayList());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that reduces the usage of streams and allows to collect all elements as a LinkedHashSet
|
||||||
|
* @return a new LinkedHashSet of all elements
|
||||||
|
*/
|
||||||
|
default ByteSet pourAsSet() {
|
||||||
|
return pour(new ByteLinkedOpenHashSet());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Helper function that reduces the usage of streams and allows to collect all elements as a Array
|
||||||
|
* @return a new Array of all elements
|
||||||
|
*/
|
||||||
|
default byte[] toByteArray() {
|
||||||
|
ISizeProvider prov = ISizeProvider.of(this);
|
||||||
|
if(prov != null) {
|
||||||
|
int size = prov.size();
|
||||||
|
if(size >= 0) {
|
||||||
|
byte[] array = new byte[size];
|
||||||
|
ByteIterators.unwrap(array, iterator());
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ByteArrays.pour(iterator());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to reduce stream usage that allows to filter for any matches.
|
||||||
|
* @param filter that should be applied
|
||||||
|
* @return true if any matches were found
|
||||||
|
*/
|
||||||
|
default boolean matchesAny(BytePredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(ByteIterator iter = iterator();iter.hasNext();) {
|
||||||
|
if(filter.test(iter.nextByte())) return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to reduce stream usage that allows to filter for no matches.
|
||||||
|
* @param filter that should be applied
|
||||||
|
* @return true if no matches were found
|
||||||
|
*/
|
||||||
|
default boolean matchesNone(BytePredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(ByteIterator iter = iterator();iter.hasNext();) {
|
||||||
|
if(filter.test(iter.nextByte())) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to reduce stream usage that allows to filter for all matches.
|
||||||
|
* @param filter that should be applied
|
||||||
|
* @return true if all matches.
|
||||||
|
*/
|
||||||
|
default boolean matchesAll(BytePredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(ByteIterator iter = iterator();iter.hasNext();) {
|
||||||
|
if(!filter.test(iter.nextByte())) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to reduce stream usage that allows to filter for the first match.
|
||||||
|
* @param filter that should be applied
|
||||||
|
* @return the found value or the null equivalent variant.
|
||||||
|
*/
|
||||||
|
default byte findFirst(BytePredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
for(ByteIterator iter = iterator();iter.hasNext();) {
|
||||||
|
byte entry = iter.nextByte();
|
||||||
|
if(filter.test(entry)) return entry;
|
||||||
|
}
|
||||||
|
return (byte)0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the
|
||||||
|
* elements of this Iterable
|
||||||
|
* @param operator the operation that should be applied
|
||||||
|
* @param identity the start value
|
||||||
|
* @return the reduction result, returns identity if nothing was found
|
||||||
|
*/
|
||||||
|
default byte reduce(byte identity, ByteByteUnaryOperator operator) {
|
||||||
|
Objects.requireNonNull(operator);
|
||||||
|
byte state = identity;
|
||||||
|
for(ByteIterator iter = iterator();iter.hasNext();) {
|
||||||
|
state = operator.applyAsByte(state, iter.nextByte());
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the
|
||||||
|
* elements of this Iterable
|
||||||
|
* @param operator the operation that should be applied
|
||||||
|
* @return the reduction result, returns null value if nothing was found
|
||||||
|
*/
|
||||||
|
default byte reduce(ByteByteUnaryOperator operator) {
|
||||||
|
Objects.requireNonNull(operator);
|
||||||
|
byte state = (byte)0;
|
||||||
|
boolean empty = true;
|
||||||
|
for(ByteIterator iter = iterator();iter.hasNext();) {
|
||||||
|
if(empty) {
|
||||||
|
empty = false;
|
||||||
|
state = iter.nextByte();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
state = operator.applyAsByte(state, iter.nextByte());
|
||||||
|
}
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to reduce stream usage that allows to count the valid elements.
|
||||||
|
* @param filter that should be applied
|
||||||
|
* @return the amount of Valid Elements
|
||||||
|
*/
|
||||||
|
default int count(BytePredicate filter) {
|
||||||
|
Objects.requireNonNull(filter);
|
||||||
|
int result = 0;
|
||||||
|
for(ByteIterator iter = iterator();iter.hasNext();) {
|
||||||
|
if(filter.test(iter.nextByte())) result++;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,88 @@
|
|||||||
|
package speiger.src.collections.bytes.collections;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import speiger.src.collections.bytes.functions.ByteConsumer;
|
||||||
|
|
||||||
|
import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific {@link Iterator} that reduces (un)boxing
|
||||||
|
*/
|
||||||
|
public interface ByteIterator extends Iterator<Byte>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Returns the next element in the iteration.
|
||||||
|
*
|
||||||
|
* @return the next element in the iteration
|
||||||
|
* @throws java.util.NoSuchElementException if the iteration has no more elements
|
||||||
|
* @see Iterator#next()
|
||||||
|
*/
|
||||||
|
public byte nextByte();
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public default Byte next() { return Byte.valueOf(nextByte()); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Performs the given action for each remaining element until all elements
|
||||||
|
* have been processed or the action throws an exception. Actions are
|
||||||
|
* performed in the order of iteration, if that order is specified.
|
||||||
|
* Exceptions thrown by the action are relayed to the caller.
|
||||||
|
*
|
||||||
|
* @implSpec
|
||||||
|
* <p>The default implementation behaves as if:
|
||||||
|
* <pre>{@code
|
||||||
|
* while (hasNext()) action.accept(nextByte());
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param action The action to be performed for each element
|
||||||
|
* @throws java.lang.NullPointerException if the specified action is null
|
||||||
|
* @see Iterator#forEachRemaining(Consumer)
|
||||||
|
*/
|
||||||
|
public default void forEachRemaining(ByteConsumer action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
while(hasNext()) { action.accept(nextByte()); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
@Override
|
||||||
|
default void forEachRemaining(Consumer<? super Byte> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
forEachRemaining(action::accept);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function to reduce Lambda usage and allow for more method references, since these are faster/cleaner.
|
||||||
|
* @param input the object that should be included
|
||||||
|
* @param action The action to be performed for each element
|
||||||
|
* @param <E> the generic type of the Object
|
||||||
|
* @throws java.lang.NullPointerException if the specified action is null
|
||||||
|
*/
|
||||||
|
default <E> void forEachRemaining(E input, ObjectByteConsumer<E> action) {
|
||||||
|
Objects.requireNonNull(action);
|
||||||
|
while(hasNext()) { action.accept(input, nextByte()); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Skips the Given amount of elements if possible. A Optimization function to skip elements faster if the implementation allows it.
|
||||||
|
* @param amount the amount of elements that should be skipped
|
||||||
|
* @return the amount of elements that were skipped
|
||||||
|
*/
|
||||||
|
default int skip(int amount) {
|
||||||
|
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
|
||||||
|
int i = 0;
|
||||||
|
for(;i<amount && hasNext();nextByte(), i++);
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
package speiger.src.collections.bytes.collections;
|
||||||
|
|
||||||
|
import java.util.Spliterator.OfPrimitive;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
import speiger.src.collections.bytes.functions.ByteConsumer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Split-Iterator that reduces boxing/unboxing
|
||||||
|
* It fills the gaps of the java and uses this collection interfaces
|
||||||
|
*/
|
||||||
|
public interface ByteSplititerator extends OfPrimitive<Byte, ByteConsumer, ByteSplititerator>, ByteIterator
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
default void forEachRemaining(ByteConsumer action) { ByteIterator.super.forEachRemaining(action); }
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default void forEachRemaining(Consumer<? super Byte> action) { ByteIterator.super.forEachRemaining(action); }
|
||||||
|
}
|
||||||
@ -0,0 +1,85 @@
|
|||||||
|
package speiger.src.collections.bytes.collections;
|
||||||
|
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
|
import speiger.src.collections.utils.Stack;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type-Specific {@link Stack} that reduces (un)boxing
|
||||||
|
*/
|
||||||
|
public interface ByteStack
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Inserts a given Object on top of the stack
|
||||||
|
* @param e the Object to insert
|
||||||
|
* @see Stack#push(Object)
|
||||||
|
*/
|
||||||
|
public void push(byte e);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function that pushes the top element on top of the stack again.
|
||||||
|
* @throws NoSuchElementException if the stack is empty
|
||||||
|
*/
|
||||||
|
public default void pushTop() {
|
||||||
|
push(top());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the Object on top of the stack.
|
||||||
|
* @return the element that is on top of the stack
|
||||||
|
* @throws ArrayIndexOutOfBoundsException if the stack is empty
|
||||||
|
* @see Stack#pop()
|
||||||
|
*/
|
||||||
|
public byte pop();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides the Object on top of the stack
|
||||||
|
* @return the element that is on top of the stack
|
||||||
|
* @throws ArrayIndexOutOfBoundsException if the stack is empty
|
||||||
|
* @see Stack#top()
|
||||||
|
*/
|
||||||
|
public default byte top() {
|
||||||
|
return peek(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides the Selected Object from the stack.
|
||||||
|
* Top to bottom
|
||||||
|
* @param index of the element that should be provided
|
||||||
|
* @return the element that was requested
|
||||||
|
* @throws ArrayIndexOutOfBoundsException if the index is out of bounds
|
||||||
|
* @see Stack#peek(int)
|
||||||
|
*/
|
||||||
|
public byte peek(int index);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clears the stack
|
||||||
|
*/
|
||||||
|
public void clear();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides the amount of elements currently in the stack
|
||||||
|
* @return amount of elements in the list
|
||||||
|
*/
|
||||||
|
public int size();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return if the stack is empty
|
||||||
|
*/
|
||||||
|
public default boolean isEmpty() {
|
||||||
|
return size() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A method to drop the contents of the Stack without clearing the stack
|
||||||
|
* @return the contents of the stack into a seperate array.
|
||||||
|
*/
|
||||||
|
public default byte[] toByteArray() { return toByteArray(new byte[size()]); }
|
||||||
|
/**
|
||||||
|
* A method to drop the contents of the Stack without clearing the stack
|
||||||
|
* @param input where the elements should be inserted to. If it does not fit then it creates a new appropiatly created array
|
||||||
|
* @return the contents of the stack into a seperate array.
|
||||||
|
* @note if the Type is generic then a Object Array is created instead of a Type Array
|
||||||
|
*/
|
||||||
|
public byte[] toByteArray(byte[] input);
|
||||||
|
}
|
||||||
@ -0,0 +1,70 @@
|
|||||||
|
package speiger.src.collections.bytes.functions;
|
||||||
|
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type-Specific Class for Comparator to reduce (un)boxing
|
||||||
|
*/
|
||||||
|
public interface ByteComparator extends Comparator<Byte>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Type-Specific compare function to reduce (un)boxing
|
||||||
|
* @param o1 the first object to be compared.
|
||||||
|
* @param o2 the second object to be compared.
|
||||||
|
* @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
|
||||||
|
* @see Comparator#compare(Object, Object)
|
||||||
|
*/
|
||||||
|
int compare(byte o1, byte o2);
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default int compare(Byte o1, Byte o2) {
|
||||||
|
return compare(o1.byteValue(), o2.byteValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Wrapper function to convert a Non-Type-Specific Comparator to a Type-Specific-Comparator
|
||||||
|
* @param c comparator to convert
|
||||||
|
* @return the wrapper of the comparator
|
||||||
|
* @throws NullPointerException if the comparator is null
|
||||||
|
*/
|
||||||
|
public static ByteComparator of(Comparator<Byte> c) {
|
||||||
|
Objects.requireNonNull(c);
|
||||||
|
return (K, V) -> c.compare(Byte.valueOf(K), Byte.valueOf(V));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public default ByteComparator reversed() {
|
||||||
|
return new Reversed(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Type Specific Reversed Comparator to reduce boxing/unboxing
|
||||||
|
*/
|
||||||
|
static class Reversed implements ByteComparator
|
||||||
|
{
|
||||||
|
ByteComparator original;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* default constructor
|
||||||
|
* @param original that is going to be reversed
|
||||||
|
*/
|
||||||
|
public Reversed(ByteComparator original) {
|
||||||
|
this.original = original;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int compare(byte o1, byte o2) {
|
||||||
|
return original.compare(o2, o1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ByteComparator reversed() {
|
||||||
|
return original;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
package speiger.src.collections.bytes.functions;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
/**
|
||||||
|
* Type-Specific Consumer interface that reduces (un)boxing and allows to merge other consumer types into this interface
|
||||||
|
*/
|
||||||
|
@FunctionalInterface
|
||||||
|
public interface ByteConsumer extends Consumer<Byte>
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Type-Specific function to reduce (un)boxing.
|
||||||
|
* Performs this operation on the given argument.
|
||||||
|
*
|
||||||
|
* @param t the input argument
|
||||||
|
*/
|
||||||
|
void accept(byte t);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type Specific sequencing method to reduce boxing/unboxing.
|
||||||
|
* @param after a operation that should be performed afterwards
|
||||||
|
* @return a sequenced consumer that does 2 operations
|
||||||
|
* @throws NullPointerException if after is null
|
||||||
|
*/
|
||||||
|
public default ByteConsumer andThen(ByteConsumer after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return T -> {accept(T); after.accept(T);};
|
||||||
|
}
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default void accept(Byte t) { accept(t.byteValue()); }
|
||||||
|
|
||||||
|
/** {@inheritDoc}
|
||||||
|
* <p>This default implementation delegates to the corresponding type-specific function.
|
||||||
|
* @deprecated Please use the corresponding type-specific function instead.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
default ByteConsumer andThen(Consumer<? super Byte> after) {
|
||||||
|
Objects.requireNonNull(after);
|
||||||
|
return T -> {accept(T); after.accept(Byte.valueOf(T));};
|
||||||
|
}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user