Finished first loop of JavaDoc generation.

-Fixed: A couple bugs that were found during javadoc generation.

Next loop of javadoc comes later right now i want to add splititerators
and streams
This commit is contained in:
2021-04-26 22:25:09 +02:00
parent f7d311fd09
commit a9a38f7853
22 changed files with 1173 additions and 212 deletions
@@ -18,74 +18,141 @@ import speiger.src.collections.PACKAGE.utils.ITERATORS;
#endif
import speiger.src.collections.utils.SanityChecks;
/**
* A Simple Type Specific AVL TreeSet implementation that reduces boxing/unboxing.
* It is using a bit more memory then <a href="https://github.com/vigna/fastutil">FastUtil</a>,
* but it saves a lot of Performance on the Optimized removal and iteration logic.
* Which makes the implementation actually useable and does not get outperformed by Javas default implementation.
* @Type(T)
*/
public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE
{
/** The center of the Tree */
protected transient Entry KEY_GENERIC_TYPE tree;
/** The Lowest possible Node */
protected transient Entry KEY_GENERIC_TYPE first;
/** The Highest possible Node */
protected transient Entry KEY_GENERIC_TYPE last;
/** The amount of elements stored in the Set */
protected int size = 0;
/** The Sorter of the Tree */
protected transient COMPARATOR KEY_GENERIC_TYPE comparator;
#if !TYPE_OBJECT
#if TYPE_BOOLEAN
protected KEY_TYPE defaultMaxNotFound = CLASS_TYPE.FALSE;
protected KEY_TYPE defaultMinNotFound = CLASS_TYPE.TRUE;
#else
/** the default return value for max searches */
protected KEY_TYPE defaultMaxNotFound = CLASS_TYPE.MIN_VALUE;
/** the default return value for min searches */
protected KEY_TYPE defaultMinNotFound = CLASS_TYPE.MAX_VALUE;
#endif
#endif
/**
* Default Constructor
*/
public AVL_TREE_SET() {
}
/**
* Constructor that allows to define the sorter
* @param comp the function that decides how the tree is sorted, can be null
*/
public AVL_TREE_SET(COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
}
/**
* Helper constructor that allow to create a set from an array
* @param array the elements that should be used
*/
public AVL_TREE_SET(KEY_TYPE[] array) {
this(array, 0, array.length);
}
/**
* Helper constructor that allow to create a set from an array
* @param array the elements that should be used
* @param offset the starting index within the array
* @param length the amount of elements that are within the array
* @throws IllegalStateException if offset and length causes to step outside of the arrays range
*/
public AVL_TREE_SET(KEY_TYPE[] array, int offset, int length) {
SanityChecks.checkArrayCapacity(array.length, offset, length);
for(int i = 0;i<length;i++) add(array[offset+i]);
}
/**
* Helper constructor that allow to create a set from an array
* @param array the elements that should be used
* @param comp the sorter of the tree, can be null
*/
public AVL_TREE_SET(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
this(array, 0, array.length, comp);
}
/**
* Helper constructor that allow to create a set from an array
* @param array the elements that should be used
* @param offset the starting index within the array
* @param length the amount of elements that are within the array
* @param comp the sorter of the tree, can be null
* @throws IllegalStateException if offset and length causes to step outside of the arrays range
*/
public AVL_TREE_SET(KEY_TYPE[] array, int offset, int length, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
SanityChecks.checkArrayCapacity(array.length, offset, length);
for(int i = 0;i<length;i++) add(array[offset+i]);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided SortedSet.
* @param sortedSet the set the elements should be added to the TreeSet
* @note this also includes the Comparator if present
*/
public AVL_TREE_SET(SORTED_SET KEY_GENERIC_TYPE sortedSet) {
comparator = sortedSet.comparator();
addAll(sortedSet);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the TreeSet
*/
@Deprecated
public AVL_TREE_SET(Collection<? extends CLASS_TYPE> collection) {
addAll(collection);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the TreeSet
* @param comp the sorter of the tree, can be null
*/
@Deprecated
public AVL_TREE_SET(Collection<? extends CLASS_TYPE> collection, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
addAll(collection);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the TreeSet
*/
public AVL_TREE_SET(COLLECTION KEY_GENERIC_TYPE collection) {
addAll(collection);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the TreeSet
* @param comp the sorter of the tree, can be null
*/
public AVL_TREE_SET(COLLECTION KEY_GENERIC_TYPE collection, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
addAll(collection);
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
*/
public AVL_TREE_SET(Iterator<CLASS_TYPE> iterator) {
#if !TYPE_OBJECT
this(ITERATORS.wrap(iterator));
@@ -94,6 +161,11 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
#endif
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
* @param comp the sorter of the tree, can be null
*/
public AVL_TREE_SET(Iterator<CLASS_TYPE> iterator, COMPARATOR KEY_GENERIC_TYPE comp) {
#if !TYPE_OBJECT
this(ITERATORS.wrap(iterator), comp);
@@ -103,10 +175,19 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
#endif
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
*/
public AVL_TREE_SET(ITERATOR KEY_GENERIC_TYPE iterator) {
while(iterator.hasNext()) add(iterator.NEXT());
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
* @param comp the sorter of the tree, can be null
*/
public AVL_TREE_SET(ITERATOR KEY_GENERIC_TYPE iterator, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
while(iterator.hasNext()) add(iterator.NEXT());
@@ -15,6 +15,7 @@ import java.util.function.JAVA_PREDICATE;
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
@@ -22,39 +23,81 @@ import speiger.src.collections.PACKAGE.functions.CONSUMER;
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
import speiger.src.collections.PACKAGE.utils.ARRAYS;
/**
* A Type Specific ArraySet implementation.
* That is based around the idea of {@link java.util.List#indexOf(Object)} for no duplication.
* Unless a array constructor is used the ArraySet does not allow for duplication.
* This implementation does not shrink the backing array
* @Type(T)
*/
public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE
{
/** The Backing Array */
protected transient KEY_TYPE[] data;
/** The amount of elements stored in the array*/
protected int size = 0;
/**
* Default Constructor
*/
public ARRAY_SET() {
data = EMPTY_KEY_ARRAY;
}
/**
* Minimum Capacity Constructor
* @param capacity the minimum capacity of the internal array
* @throws NegativeArraySizeException if the capacity is negative
*/
public ARRAY_SET(int capacity) {
data = NEW_KEY_ARRAY(capacity);
}
/**
* Constructur using initial Array
* @param array the array that should be used for set.
*/
public ARRAY_SET(KEY_TYPE[] array) {
this(array, array.length);
}
/**
* Constructur using initial Array
* @param array the array that should be used for set.
* @param length the amount of elements present within the array
* @throws NegativeArraySizeException if the length is negative
*/
public ARRAY_SET(KEY_TYPE[] array, int length) {
data = Arrays.copyOf(array, length);
size = length;
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param c the elements that should be added to the set.
* @note this slowly checks every element to remove duplicates
*/
@Deprecated
public ARRAY_SET(Collection<? extends CLASS_TYPE> c) {
this(c.size());
addAll(c);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param c the elements that should be added to the set.
* @note this slowly checks every element to remove duplicates
*/
public ARRAY_SET(COLLECTION KEY_GENERIC_TYPE c) {
this(c.size());
addAll(c);
}
/**
* A Helper constructor that fast copies the element out of a set into the ArraySet.
* Since it is assumed that there is no duplication in the first place
* @param s the set the element should be taken from
*/
@Deprecated
public ARRAY_SET(Set<? extends CLASS_TYPE> s) {
this(s.size());
@@ -62,10 +105,14 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
data[size++] = OBJ_TO_KEY(e);
}
/**
* A Helper constructor that fast copies the element out of a set into the ArraySet.
* Since it is assumed that there is no duplication in the first place
* @param s the set the element should be taken from
*/
public ARRAY_SET(SET KEY_GENERIC_TYPE s) {
this(s.size());
for(KEY_TYPE e : s)
data[size++] = e;
for(ITERATOR KEY_GENERIC_TYPE iter = s.iterator();iter.hasNext();data[size++] = iter.NEXT());
}
@Override
@@ -23,67 +23,173 @@ import speiger.src.collections.PACKAGE.utils.STRATEGY;
import speiger.src.collections.utils.HashUtil;
import speiger.src.collections.utils.SanityChecks;
/**
* A Type Specific LinkedHashSet that allows for custom HashControl. That uses arrays to create links between nodes.
* For cases where Objects/primitive do not allow hashcoding this can be really useful and provide a lot of control.
* This implementation of SortedSet does not support SubSet of any kind. It implements the interface due to sortability and first/last access
* @Type(T)
*/
public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE
{
protected long[] links;
/** The Backing array for links between nodes. Left 32 Bits => Previous Entry, Right 32 Bits => Next Entry */
protected transient long[] links;
/** The First Index in the Map */
protected int firstIndex = -1;
/** The Last Index in the Map */
protected int lastIndex = -1;
/**
* Default Contstructor
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
*/
public LINKED_CUSTOM_HASH_SET(STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(HashUtil.DEFAULT_MIN_CAPACITY, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
/**
* Constructor that defines the minimum capacity
* @param minCapacity the minimum capacity the HashSet is allowed to be.
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if the minimum capacity is negative
*/
public LINKED_CUSTOM_HASH_SET(int minCapacity, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(minCapacity, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
/**
* Constructor that defines the minimum capacity and load factor
* @param minCapacity the minimum capacity the HashSet is allowed to be.
* @param loadFactor the percentage of how full the backing array can be before they resize
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if the minimum capacity is negative
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public LINKED_CUSTOM_HASH_SET(int minCapacity, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
super(minCapacity, loadFactor, strategy);
links = new long[nullIndex + 1];
}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
*/
public LINKED_CUSTOM_HASH_SET(KEY_TYPE[] array, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(array, 0, array.length, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public LINKED_CUSTOM_HASH_SET(KEY_TYPE[] array, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(array, 0, array.length, loadFactor, strategy);
}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
* @param offset the starting index within the array that should be used
* @param length the amount of elements used from the array
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if offset and length causes to step outside of the arrays range
*/
public LINKED_CUSTOM_HASH_SET(KEY_TYPE[] array, int offset, int length, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(array, offset, length, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
* @param offset the starting index within the array that should be used
* @param length the amount of elements used from the array
* @param loadFactor the percentage of how full the backing array can be before they resize
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
* @throws IllegalStateException if offset and length causes to step outside of the arrays range
*/
public LINKED_CUSTOM_HASH_SET(KEY_TYPE[] array, int offset, int length, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(length < 0 ? 0 : length, strategy);
SanityChecks.checkArrayCapacity(array.length, offset, length);
for(int i = 0;i<length;i++) add(array[offset+i]);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the Set
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
*/
@Deprecated
public LINKED_CUSTOM_HASH_SET(Collection<? extends CLASS_TYPE> collection, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(collection, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the Set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
@Deprecated
public LINKED_CUSTOM_HASH_SET(Collection<? extends CLASS_TYPE> collection, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(collection.size(), loadFactor, strategy);
addAll(collection);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the Set
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
*/
public LINKED_CUSTOM_HASH_SET(COLLECTION KEY_GENERIC_TYPE collection, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(collection, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the Set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public LINKED_CUSTOM_HASH_SET(COLLECTION KEY_GENERIC_TYPE collection, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(collection.size(), strategy);
addAll(collection);
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
*/
public LINKED_CUSTOM_HASH_SET(Iterator<CLASS_TYPE> iterator, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(iterator, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public LINKED_CUSTOM_HASH_SET(Iterator<CLASS_TYPE> iterator, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
#if !TYPE_OBJECT
this(ITERATORS.wrap(iterator), loadFactor, strategy);
@@ -94,10 +200,24 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
}
#if !TYPE_OBJECT
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
*/
public LINKED_CUSTOM_HASH_SET(ITERATOR KEY_GENERIC_TYPE iterator, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(iterator, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public LINKED_CUSTOM_HASH_SET(ITERATOR KEY_GENERIC_TYPE iterator, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(HashUtil.DEFAULT_MIN_CAPACITY, loadFactor, strategy);
while(iterator.hasNext()) add(iterator.NEXT());
@@ -25,67 +25,147 @@ import speiger.src.collections.PACKAGE.utils.ITERATORS;
import speiger.src.collections.utils.HashUtil;
import speiger.src.collections.utils.SanityChecks;
/**
* A Type Specific LinkedHashMap implementation that uses specific arrays to create links between nodes to remove the wrapping of elements
* to greatly reduce memory usage. In Addition adding some helper methods to move around elements.
* This implementation of SortedSet does not support SubSet of any kind. It implements the interface due to sortability and first/last access
* @Type(T)
*/
public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE
{
protected long[] links;
/** The Backing array for links between nodes. Left 32 Bits => Previous Entry, Right 32 Bits => Next Entry */
protected transient long[] links;
/** The First Index in the Map */
protected int firstIndex = -1;
/** The Last Index in the Map */
protected int lastIndex = -1;
/**
* Default Constructor
*/
public LINKED_HASH_SET() {
this(HashUtil.DEFAULT_MIN_CAPACITY, HashUtil.DEFAULT_LOAD_FACTOR);
}
/**
* Constructor that defines the minimum capacity
* @param minCapacity the minimum capacity the HashSet is allowed to be.
* @throws IllegalStateException if the minimum capacity is negative
*/
public LINKED_HASH_SET(int minCapacity) {
this(minCapacity, HashUtil.DEFAULT_LOAD_FACTOR);
}
/**
* Constructor that defines the minimum capacity and load factor
* @param minCapacity the minimum capacity the HashSet is allowed to be.
* @param loadFactor the percentage of how full the backing array can be before they resize
* @throws IllegalStateException if the minimum capacity is negative
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public LINKED_HASH_SET(int minCapacity, float loadFactor) {
super(minCapacity, loadFactor);
links = new long[nullIndex + 1];
}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
*/
public LINKED_HASH_SET(KEY_TYPE[] array) {
this(array, 0, array.length, HashUtil.DEFAULT_LOAD_FACTOR);
}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public LINKED_HASH_SET(KEY_TYPE[] array, float loadFactor) {
this(array, 0, array.length, loadFactor);
}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
* @param offset the starting index within the array that should be used
* @param length the amount of elements used from the array
* @throws IllegalStateException if offset and length causes to step outside of the arrays range
*/
public LINKED_HASH_SET(KEY_TYPE[] array, int offset, int length) {
this(array, offset, length, HashUtil.DEFAULT_LOAD_FACTOR);
}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
* @param offset the starting index within the array that should be used
* @param length the amount of elements used from the array
* @param loadFactor the percentage of how full the backing array can be before they resize
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
* @throws IllegalStateException if offset and length causes to step outside of the arrays range
*/
public LINKED_HASH_SET(KEY_TYPE[] array, int offset, int length, float loadFactor) {
this(length < 0 ? 0 : length);
SanityChecks.checkArrayCapacity(array.length, offset, length);
for(int i = 0;i<length;i++) add(array[offset+i]);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the Set
*/
@Deprecated
public LINKED_HASH_SET(Collection<? extends CLASS_TYPE> collection) {
this(collection, HashUtil.DEFAULT_LOAD_FACTOR);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the Set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
@Deprecated
public LINKED_HASH_SET(Collection<? extends CLASS_TYPE> collection, float loadFactor) {
this(collection.size(), loadFactor);
addAll(collection);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the Set
*/
public LINKED_HASH_SET(COLLECTION KEY_GENERIC_TYPE collection) {
this(collection, HashUtil.DEFAULT_LOAD_FACTOR);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the Set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public LINKED_HASH_SET(COLLECTION KEY_GENERIC_TYPE collection, float loadFactor) {
this(collection.size());
addAll(collection);
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
*/
public LINKED_HASH_SET(Iterator<CLASS_TYPE> iterator) {
this(iterator, HashUtil.DEFAULT_LOAD_FACTOR);
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public LINKED_HASH_SET(Iterator<CLASS_TYPE> iterator, float loadFactor) {
#if !TYPE_OBJECT
this(ITERATORS.wrap(iterator), loadFactor);
@@ -96,10 +176,20 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
}
#if !TYPE_OBJECT
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
*/
public LINKED_HASH_SET(ITERATOR KEY_GENERIC_TYPE iterator) {
this(iterator, HashUtil.DEFAULT_LOAD_FACTOR);
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public LINKED_HASH_SET(ITERATOR KEY_GENERIC_TYPE iterator, float loadFactor) {
this(HashUtil.DEFAULT_MIN_CAPACITY, loadFactor);
while(iterator.hasNext()) add(iterator.NEXT());
@@ -4,66 +4,113 @@ import java.util.NavigableSet;
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
/**
* A Type Specific Navigable Set interface with a couple helper methods
* @Type(T)
*/
public interface NAVIGABLE_SET KEY_GENERIC_TYPE extends NavigableSet<CLASS_TYPE>, SORTED_SET KEY_GENERIC_TYPE
{
#if !TYPE_OBJECT
public KEY_TYPE lower(KEY_TYPE e);
public KEY_TYPE floor(KEY_TYPE e);
public KEY_TYPE ceiling(KEY_TYPE e);
public KEY_TYPE higher(KEY_TYPE e);
/**
* A Type Specific lower method to reduce boxing/unboxing.
* @param key that should be compared with.
* @return the greatest lower key that can be found
*/
public KEY_TYPE lower(KEY_TYPE key);
/**
* A Type Specific higher method to reduce boxing/unboxing.
* @param key that should be compared with.
* @return the lowest higher key that can be found
*/
public KEY_TYPE higher(KEY_TYPE key);
/**
* A Type Specific floor method to reduce boxing/unboxing.
* @param key that should be compared with.
* @return the greatest lower or equal key that can be found
*/
public KEY_TYPE floor(KEY_TYPE key);
/**
* A Type Specific ceiling method to reduce boxing/unboxing.
* @param key that should be compared with.
* @return the lowest higher or equal key that can be found
*/
public KEY_TYPE ceiling(KEY_TYPE key);
/**
* A Helper method to set the max value for SubSets. (Default: KEY_TYPE.MIN_VALUE)
* @param e the new max value
*/
public void setDefaultMaxValue(KEY_TYPE e);
/**
* A Helper method to get the max value for SubSets.
* @return the default max value.
*/
public KEY_TYPE getDefaultMaxValue();
/**
* A Helper method to set the min value for SubSets. (Default: KEY_TYPE.MAX_VALUE)
* @param e the new min value
*/
public void setDefaultMinValue(KEY_TYPE e);
/**
* A Helper method to get the min value for SubSets.
* @return the default min value.
*/
public KEY_TYPE getDefaultMinValue();
@Override
public default NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { return subSet(fromElement, true, toElement, false); }
@Override
public default NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { return headSet(toElement, false); }
@Override
public default NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { return tailSet(fromElement, true); }
/**
* A Type Specific SubSet method to reduce boxing/unboxing
* @param fromElement where the SubSet should start
* @param fromInclusive if the fromElement is inclusive or not
* @param toElement where the SubSet should end
* @param toInclusive if the toElement is inclusive or not
* @return a SubSet that is within the range of the desired range
*/
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive);
/**
* A Type Specific HeadSet method to reduce boxing/unboxing
* @param toElement where the HeadSet should end
* @param inclusive if the toElement is inclusive or not
* @return a HeadSet that is within the range of the desired range
*/
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive);
/**
* A Type Specific TailSet method to reduce boxing/unboxing
* @param fromElement where the TailSet should start
* @param inclusive if the fromElement is inclusive or not
* @return a TailSet that is within the range of the desired range
*/
public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive);
#else
@Override
public default NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { return subSet(fromElement, true, toElement, false); }
@Override
public default NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { return headSet(toElement, false); }
@Override
public default NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { return tailSet(fromElement, true); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive);
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive);
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive);
#endif
/** @return a Type Specific iterator */
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator();
/** @return a Type Specific desendingIterator */
@Override
public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator();
/** @return a Type Specific desendingSet */
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet();
@@ -71,31 +118,24 @@ public interface NAVIGABLE_SET KEY_GENERIC_TYPE extends NavigableSet<CLASS_TYPE>
@Override
@Deprecated
public default CLASS_TYPE lower(CLASS_TYPE e) { return KEY_TO_OBJ(lower(OBJ_TO_KEY(e))); }
@Override
@Deprecated
public default CLASS_TYPE floor(CLASS_TYPE e) { return KEY_TO_OBJ(floor(OBJ_TO_KEY(e))); }
@Override
@Deprecated
public default CLASS_TYPE ceiling(CLASS_TYPE e) { return KEY_TO_OBJ(ceiling(OBJ_TO_KEY(e))); }
@Override
@Deprecated
public default CLASS_TYPE higher(CLASS_TYPE e) { return KEY_TO_OBJ(higher(OBJ_TO_KEY(e))); }
@Override
@Deprecated
default CLASS_TYPE first() { return SORTED_SET.super.first(); }
@Override
@Deprecated
default CLASS_TYPE last() { return SORTED_SET.super.last(); }
@Override
@Deprecated
public default CLASS_TYPE pollFirst() { return KEY_TO_OBJ(POLL_FIRST_KEY()); }
@Override
@Deprecated
public default CLASS_TYPE pollLast() { return KEY_TO_OBJ(POLL_LAST_KEY()); }
@@ -103,23 +143,18 @@ public interface NAVIGABLE_SET KEY_GENERIC_TYPE extends NavigableSet<CLASS_TYPE>
@Override
@Deprecated
public default NAVIGABLE_SET KEY_GENERIC_TYPE subSet(CLASS_TYPE fromElement, boolean fromInclusive, CLASS_TYPE toElement, boolean toInclusive) { return subSet(OBJ_TO_KEY(fromElement), fromInclusive, OBJ_TO_KEY(toElement), toInclusive); }
@Override
@Deprecated
public default NAVIGABLE_SET KEY_GENERIC_TYPE headSet(CLASS_TYPE toElement, boolean inclusive) { return headSet(OBJ_TO_KEY(toElement), inclusive); }
@Override
@Deprecated
public default NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(CLASS_TYPE fromElement, boolean inclusive) { return tailSet(OBJ_TO_KEY(fromElement), inclusive); }
@Override
@Deprecated
public default SORTED_SET KEY_GENERIC_TYPE subSet(CLASS_TYPE fromElement, CLASS_TYPE toElement) { return SORTED_SET.super.subSet(fromElement, toElement); }
@Override
@Deprecated
public default SORTED_SET KEY_GENERIC_TYPE headSet(CLASS_TYPE toElement) { return SORTED_SET.super.headSet(toElement); }
@Override
@Deprecated
public default SORTED_SET KEY_GENERIC_TYPE tailSet(CLASS_TYPE fromElement) { return SORTED_SET.super.tailSet(fromElement); }
@@ -18,27 +18,62 @@ import speiger.src.collections.utils.HashUtil;
import speiger.src.collections.utils.ITrimmable;
import speiger.src.collections.utils.SanityChecks;
/**
* A Type Specific HashSet that allows for custom HashControl.
* For cases where Objects/primitive do not allow hashcoding this can be really useful and provide a lot of control.
* @Type(T)
*/
public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements ITrimmable
{
/** The Backing keys array */
protected transient KEY_TYPE[] keys;
/** If a null value is present */
protected transient boolean containsNull;
/** Minimum array size the HashSet will be */
protected transient int minCapacity;
/** Index of the Null Value */
protected transient int nullIndex;
/** Maximum amount of Values that can be stored before the array gets expanded usually 75% */
protected transient int maxFill;
/** Max Index that is allowed to be searched through nullIndex - 1 */
protected transient int mask;
/** Amount of Elements stored in the HashSet */
protected int size;
/** How full the Array is allowed to get before resize */
protected final float loadFactor;
/** Strategy that allows to control the Hash Generation and equals comparason */
protected final STRATEGY KEY_SUPER_GENERIC_TYPE strategy;
/**
* Default Contstructor
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
*/
public CUSTOM_HASH_SET(STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(HashUtil.DEFAULT_MIN_CAPACITY, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
/**
* Constructor that defines the minimum capacity
* @param minCapacity the minimum capacity the HashSet is allowed to be.
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if the minimum capacity is negative
*/
public CUSTOM_HASH_SET(int minCapacity, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(minCapacity, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
/**
* Constructor that defines the minimum capacity and load factor
* @param minCapacity the minimum capacity the HashSet is allowed to be.
* @param loadFactor the percentage of how full the backing array can be before they resize
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if the minimum capacity is negative
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public CUSTOM_HASH_SET(int minCapacity, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
if(minCapacity < 0) throw new IllegalStateException("Minimum Capacity is negative. This is not allowed");
if(loadFactor <= 0 || loadFactor >= 1F) throw new IllegalStateException("Load Factor is not between 0 and 1");
@@ -50,48 +85,124 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
this.strategy = strategy;
}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
*/
public CUSTOM_HASH_SET(KEY_TYPE[] array, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(array, 0, array.length, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public CUSTOM_HASH_SET(KEY_TYPE[] array, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(array, 0, array.length, loadFactor, strategy);
}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
* @param offset the starting index within the array that should be used
* @param length the amount of elements used from the array
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if offset and length causes to step outside of the arrays range
*/
public CUSTOM_HASH_SET(KEY_TYPE[] array, int offset, int length, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(array, offset, length, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
* @param offset the starting index within the array that should be used
* @param length the amount of elements used from the array
* @param loadFactor the percentage of how full the backing array can be before they resize
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
* @throws IllegalStateException if offset and length causes to step outside of the arrays range
*/
public CUSTOM_HASH_SET(KEY_TYPE[] array, int offset, int length, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(length < 0 ? 0 : length, strategy);
SanityChecks.checkArrayCapacity(array.length, offset, length);
for(int i = 0;i<length;i++) add(array[offset+i]);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the Set
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
*/
@Deprecated
public CUSTOM_HASH_SET(Collection<? extends CLASS_TYPE> collection, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(collection, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the Set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
@Deprecated
public CUSTOM_HASH_SET(Collection<? extends CLASS_TYPE> collection, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(collection.size(), loadFactor, strategy);
addAll(collection);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the Set
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
*/
public CUSTOM_HASH_SET(COLLECTION KEY_GENERIC_TYPE collection, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(collection, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the Set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public CUSTOM_HASH_SET(COLLECTION KEY_GENERIC_TYPE collection, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(collection.size(), strategy);
addAll(collection);
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
*/
public CUSTOM_HASH_SET(Iterator<CLASS_TYPE> iterator, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(iterator, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public CUSTOM_HASH_SET(Iterator<CLASS_TYPE> iterator, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
#if !TYPE_OBJECT
this(ITERATORS.wrap(iterator), loadFactor, strategy);
@@ -102,16 +213,34 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
}
#if !TYPE_OBJECT
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
*/
public CUSTOM_HASH_SET(ITERATOR KEY_GENERIC_TYPE iterator, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(iterator, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @param strategy the strategy that allows hash control.
* @throws NullPointerException if Strategy is null
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public CUSTOM_HASH_SET(ITERATOR KEY_GENERIC_TYPE iterator, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(HashUtil.DEFAULT_MIN_CAPACITY, loadFactor, strategy);
while(iterator.hasNext()) add(iterator.NEXT());
}
#endif
/**
* Helper getter function to get the current strategy
* @return the current strategy
*/
public STRATEGY KEY_SUPER_GENERIC_TYPE getStrategy() {
return strategy;
}
@@ -17,26 +17,55 @@ import speiger.src.collections.utils.HashUtil;
import speiger.src.collections.utils.ITrimmable;
import speiger.src.collections.utils.SanityChecks;
/**
* A Type Specific Custom implementation of the HashSet
* Instead of using Wrapper Object Arrays for storing keys and values there is dedicated arrays for storing keys.
* Extra to that there is a couple quality of life functions provided
* @Type(T)
*/
public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements ITrimmable
{
/** The Backing keys array */
protected transient KEY_TYPE[] keys;
/** If a null value is present */
protected transient boolean containsNull;
/** Minimum array size the HashSet will be */
protected transient int minCapacity;
/** Index of the Null Value */
protected transient int nullIndex;
/** Maximum amount of Values that can be stored before the array gets expanded usually 75% */
protected transient int maxFill;
/** Max Index that is allowed to be searched through nullIndex - 1 */
protected transient int mask;
/** Amount of Elements stored in the HashSet */
protected int size;
/** How full the Array is allowed to get before resize */
protected final float loadFactor;
/**
* Default Constructor
*/
public HASH_SET() {
this(HashUtil.DEFAULT_MIN_CAPACITY, HashUtil.DEFAULT_LOAD_FACTOR);
}
/**
* Constructor that defines the minimum capacity
* @param minCapacity the minimum capacity the HashSet is allowed to be.
* @throws IllegalStateException if the minimum capacity is negative
*/
public HASH_SET(int minCapacity) {
this(minCapacity, HashUtil.DEFAULT_LOAD_FACTOR);
}
/**
* Constructor that defines the minimum capacity and load factor
* @param minCapacity the minimum capacity the HashSet is allowed to be.
* @param loadFactor the percentage of how full the backing array can be before they resize
* @throws IllegalStateException if the minimum capacity is negative
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public HASH_SET(int minCapacity, float loadFactor) {
if(minCapacity < 0) throw new IllegalStateException("Minimum Capacity is negative. This is not allowed");
if(loadFactor <= 0 || loadFactor >= 1F) throw new IllegalStateException("Load Factor is not between 0 and 1");
@@ -47,48 +76,104 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
keys = NEW_KEY_ARRAY(nullIndex + 1);
}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
*/
public HASH_SET(KEY_TYPE[] array) {
this(array, 0, array.length, HashUtil.DEFAULT_LOAD_FACTOR);
}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public HASH_SET(KEY_TYPE[] array, float loadFactor) {
this(array, 0, array.length, loadFactor);
}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
* @param offset the starting index within the array that should be used
* @param length the amount of elements used from the array
* @throws IllegalStateException if offset and length causes to step outside of the arrays range
*/
public HASH_SET(KEY_TYPE[] array, int offset, int length) {
this(array, offset, length, HashUtil.DEFAULT_LOAD_FACTOR);
}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
* @param offset the starting index within the array that should be used
* @param length the amount of elements used from the array
* @param loadFactor the percentage of how full the backing array can be before they resize
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
* @throws IllegalStateException if offset and length causes to step outside of the arrays range
*/
public HASH_SET(KEY_TYPE[] array, int offset, int length, float loadFactor) {
this(length < 0 ? 0 : length);
SanityChecks.checkArrayCapacity(array.length, offset, length);
for(int i = 0;i<length;i++) add(array[offset+i]);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the Set
*/
@Deprecated
public HASH_SET(Collection<? extends CLASS_TYPE> collection) {
this(collection, HashUtil.DEFAULT_LOAD_FACTOR);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the Set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
@Deprecated
public HASH_SET(Collection<? extends CLASS_TYPE> collection, float loadFactor) {
this(collection.size(), loadFactor);
addAll(collection);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the Set
*/
public HASH_SET(COLLECTION KEY_GENERIC_TYPE collection) {
this(collection, HashUtil.DEFAULT_LOAD_FACTOR);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the Set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public HASH_SET(COLLECTION KEY_GENERIC_TYPE collection, float loadFactor) {
this(collection.size());
addAll(collection);
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
*/
public HASH_SET(Iterator<CLASS_TYPE> iterator) {
this(iterator, HashUtil.DEFAULT_LOAD_FACTOR);
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public HASH_SET(Iterator<CLASS_TYPE> iterator, float loadFactor) {
#if !TYPE_OBJECT
this(ITERATORS.wrap(iterator), loadFactor);
@@ -99,10 +184,20 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
}
#if !TYPE_OBJECT
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
*/
public HASH_SET(ITERATOR KEY_GENERIC_TYPE iterator) {
this(iterator, HashUtil.DEFAULT_LOAD_FACTOR);
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
* @param loadFactor the percentage of how full the backing array can be before they resize
* @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
*/
public HASH_SET(ITERATOR KEY_GENERIC_TYPE iterator, float loadFactor) {
this(HashUtil.DEFAULT_MIN_CAPACITY, loadFactor);
while(iterator.hasNext()) add(iterator.NEXT());
@@ -18,74 +18,141 @@ import speiger.src.collections.PACKAGE.utils.ITERATORS;
#endif
import speiger.src.collections.utils.SanityChecks;
/**
* A Simple Type Specific RB TreeSet implementation that reduces boxing/unboxing.
* It is using a bit more memory then <a href="https://github.com/vigna/fastutil">FastUtil</a>,
* but it saves a lot of Performance on the Optimized removal and iteration logic.
* Which makes the implementation actually useable and does not get outperformed by Javas default implementation.
* @Type(T)
*/
public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE
{
/** The center of the Tree */
protected transient Entry KEY_GENERIC_TYPE tree;
/** The Lowest possible Node */
protected transient Entry KEY_GENERIC_TYPE first;
/** The Highest possible Node */
protected transient Entry KEY_GENERIC_TYPE last;
/** The amount of elements stored in the Set */
protected int size = 0;
/** The Sorter of the Tree */
protected transient COMPARATOR KEY_GENERIC_TYPE comparator;
#if !TYPE_OBJECT
#if TYPE_BOOLEAN
protected KEY_TYPE defaultMaxNotFound = CLASS_TYPE.FALSE;
protected KEY_TYPE defaultMinNotFound = CLASS_TYPE.TRUE;
#else
/** the default return value for max searches */
protected KEY_TYPE defaultMaxNotFound = CLASS_TYPE.MIN_VALUE;
/** the default return value for min searches */
protected KEY_TYPE defaultMinNotFound = CLASS_TYPE.MAX_VALUE;
#endif
#endif
/**
* Default Constructor
*/
public RB_TREE_SET() {
}
/**
* Constructor that allows to define the sorter
* @param comp the function that decides how the tree is sorted, can be null
*/
public RB_TREE_SET(COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
}
/**
* Helper constructor that allow to create a set from an array
* @param array the elements that should be used
*/
public RB_TREE_SET(KEY_TYPE[] array) {
this(array, 0, array.length);
}
/**
* Helper constructor that allow to create a set from an array
* @param array the elements that should be used
* @param offset the starting index within the array
* @param length the amount of elements that are within the array
* @throws IllegalStateException if offset and length causes to step outside of the arrays range
*/
public RB_TREE_SET(KEY_TYPE[] array, int offset, int length) {
SanityChecks.checkArrayCapacity(array.length, offset, length);
for(int i = 0;i<length;i++) add(array[offset+i]);
}
/**
* Helper constructor that allow to create a set from an array
* @param array the elements that should be used
* @param comp the sorter of the tree, can be null
*/
public RB_TREE_SET(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
this(array, 0, array.length, comp);
}
/**
* Helper constructor that allow to create a set from an array
* @param array the elements that should be used
* @param offset the starting index within the array
* @param length the amount of elements that are within the array
* @param comp the sorter of the tree, can be null
* @throws IllegalStateException if offset and length causes to step outside of the arrays range
*/
public RB_TREE_SET(KEY_TYPE[] array, int offset, int length, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
SanityChecks.checkArrayCapacity(array.length, offset, length);
for(int i = 0;i<length;i++) add(array[offset+i]);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided SortedSet.
* @param sortedSet the set the elements should be added to the TreeSet
* @note this also includes the Comparator if present
*/
public RB_TREE_SET(SORTED_SET KEY_GENERIC_TYPE sortedSet) {
comparator = sortedSet.comparator();
addAll(sortedSet);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the TreeSet
*/
@Deprecated
public RB_TREE_SET(Collection<? extends CLASS_TYPE> collection) {
addAll(collection);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the TreeSet
* @param comp the sorter of the tree, can be null
*/
@Deprecated
public RB_TREE_SET(Collection<? extends CLASS_TYPE> collection, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
addAll(collection);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the TreeSet
*/
public RB_TREE_SET(COLLECTION KEY_GENERIC_TYPE collection) {
addAll(collection);
}
/**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param collection the set the elements should be added to the TreeSet
* @param comp the sorter of the tree, can be null
*/
public RB_TREE_SET(COLLECTION KEY_GENERIC_TYPE collection, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
addAll(collection);
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
*/
public RB_TREE_SET(Iterator<CLASS_TYPE> iterator) {
#if !TYPE_OBJECT
this(ITERATORS.wrap(iterator));
@@ -93,7 +160,12 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
while(iterator.hasNext()) add(iterator.next());
#endif
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
* @param comp the sorter of the tree, can be null
*/
public RB_TREE_SET(Iterator<CLASS_TYPE> iterator, COMPARATOR KEY_GENERIC_TYPE comp) {
#if !TYPE_OBJECT
this(ITERATORS.wrap(iterator), comp);
@@ -103,10 +175,19 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
#endif
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
*/
public RB_TREE_SET(ITERATOR KEY_GENERIC_TYPE iterator) {
while(iterator.hasNext()) add(iterator.NEXT());
}
/**
* A Helper constructor that allows to create a set from a iterator of an unknown size
* @param iterator the elements that should be added to the set
* @param comp the sorter of the tree, can be null
*/
public RB_TREE_SET(ITERATOR KEY_GENERIC_TYPE iterator, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
while(iterator.hasNext()) add(iterator.NEXT());
@@ -5,12 +5,21 @@ import java.util.Set;
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
/**
* A Type Specific Set class to reduce boxing/unboxing
* @Type(T)
*/
public interface SET KEY_GENERIC_TYPE extends Set<CLASS_TYPE>, COLLECTION KEY_GENERIC_TYPE
{
@Override
public ITERATOR KEY_GENERIC_TYPE iterator();
#if !TYPE_OBJECT
/**
* A Type Specific remove function to reduce boxing/unboxing
* @param o the element that should be removed
* @return true if the element was removed
*/
public boolean remove(KEY_TYPE o);
@Override
@@ -9,29 +9,111 @@ import java.util.Comparator;
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif
/**
* A Type Specific SortedSet implementation to reduce boxing/unboxing
* with a couple extra methods that allow greater control over sets.
* @Type(T)
*/
public interface SORTED_SET KEY_GENERIC_TYPE extends SET KEY_GENERIC_TYPE, SortedSet<CLASS_TYPE>
{
/**
* A customized add method that allows you to insert into the first index.
* @param o the element that should be inserted
* @return true if it was added
* @see java.util.Set#add(Object)
* @note some implementations do not support this method
*/
public boolean addAndMoveToFirst(KEY_TYPE o);
/**
* A customized add method that allows you to insert into the last index.
* @param o the element that should be inserted
* @return true if it was added
* @see java.util.Set#add(Object)
* @note some implementations do not support this method
*/
public boolean addAndMoveToLast(KEY_TYPE o);
/**
* A specific move method to move a given key to the first index.
* @param o that should be moved to the first index
* @return true if the value was moved.
* @note returns false if the value was not present in the first place
* @note some implementations do not support this method
*/
public boolean moveToFirst(KEY_TYPE o);
/**
* A specific move method to move a given key to the last index.
* @param o that should be moved to the first last
* @return true if the value was moved.
* @note returns false if the value was not present in the first place
* @note some implementations do not support this method
*/
public boolean moveToLast(KEY_TYPE o);
/**
* A Type Specific Comparator method
* @return the type specific comparator
*/
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator();
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator();
/**
* A type Specific Iterator starting from a given key
* @param fromElement the element the iterator should start from
* @return a iterator starting from the given element
* @throws java.util.NoSuchElementException if fromElement isn't found
*/
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement);
#if !TYPE_OBJECT
/**
* A Type Specific SubSet method to reduce boxing/unboxing
* @param fromElement where the SubSet should start
* @param toElement where the SubSet should end
* @return a SubSet that is within the range of the desired range
* @note Some implementations may not support this method.
* @note Some implementations may not keep the desired range when the original is changed.
*/
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement);
/**
* A Type Specific HeadSet method to reduce boxing/unboxing
* @param toElement where the HeadSet should end
* @return a HeadSet that is within the range of the desired range
* @note Some implementations may not support this method.
* @note Some implementations may not keep the desired range when the original is changed.
*/
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement);
/**
* A Type Specific TailSet method to reduce boxing/unboxing
* @param fromElement where the TailSet should start
* @return a TailSet that is within the range of the desired range
* @note Some implementations may not support this method.
* @note Some implementations may not keep the desired range when the original is changed.
*/
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement);
/**
* A method to get the first element in the set
* @return first element in the set
*/
public KEY_TYPE FIRST_KEY();
/**
* A method to get and remove the first element in the set
* @return first element in the set
*/
public KEY_TYPE POLL_FIRST_KEY();
/**
* A method to get the last element in the set
* @return last element in the set
*/
public KEY_TYPE LAST_KEY();
/**
* A method to get and remove the last element in the set
* @return last element in the set
*/
public KEY_TYPE POLL_LAST_KEY();
@Override
@@ -51,7 +133,15 @@ public interface SORTED_SET KEY_GENERIC_TYPE extends SET KEY_GENERIC_TYPE, Sorte
@Deprecated
default CLASS_TYPE last() { return KEY_TO_OBJ(LAST_KEY()); }
#else
/**
* A method to get and remove the first element in the set
* @return first element in the set
*/
public CLASS_TYPE pollFirst();
/**
* A method to get and remove the last element in the set
* @return last element in the set
*/
public CLASS_TYPE pollLast();
@Override