Map Tests & BugFixes.

-Added: Tests for all map implementations.
-Added: Missing Map Constructors.
-Fixed: Bugs with Maps & Sets.
-Fixed: Gradle Java Container.
-Fixed: Some javadoc stuff.
-Note: SubMap/List implementation are not really well tested and most likely buggy
-Changed: set JavaDoc to be quiet for now. Later goal.
This commit is contained in:
Speiger 2021-04-22 23:02:04 +02:00
parent aaee550ea9
commit 0b11c3929a
43 changed files with 1206 additions and 149 deletions

View File

@ -26,6 +26,18 @@
<attribute name="test" value="true"/> <attribute name="test" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="src" output="bin/builder" path="src/builder/java">
<attributes>
<attribute name="gradle_scope" value="builder"/>
<attribute name="gradle_used_by_scope" value="builder"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="bin/builder" path="src/builder/resources">
<attributes>
<attribute name="gradle_scope" value="builder"/>
<attribute name="gradle_used_by_scope" value="builder"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/> <classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/> <classpathentry kind="output" path="bin/default"/>

View File

@ -1,23 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<projectDescription> <projectDescription>
<name>Primitive-Collections</name> <name>Primitive-Collections</name>
<comment>Project Primitive-Collections created by Buildship.</comment> <comment></comment>
<projects> <projects/>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures> <natures>
<nature>org.eclipse.jdt.core.javanature</nature> <nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature> <nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures> </natures>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments/>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments/>
</buildCommand>
</buildSpec>
<linkedResources/>
<filteredResources/>
</projectDescription> </projectDescription>

View File

@ -18,6 +18,10 @@ repositories {
sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8' sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8'
javadoc {
options.tags = [ "implSpec", "note" ]
}
eclipse { eclipse {
classpath { classpath {
downloadJavadoc = true downloadJavadoc = true
@ -31,6 +35,12 @@ eclipse {
} }
} }
compileJava {
options.compilerArgs << '-XDignore.symbol.file'
options.fork = true // may not needed on 1.8
options.forkOptions.executable = 'javac' // may not needed on 1.8
}
sourceSets { sourceSets {
builder builder
} }
@ -62,9 +72,11 @@ task srcJar(type: Jar) {
} }
javadoc.failOnError = false javadoc.failOnError = false
//javadoc.options.showAll()
javadoc.options.quiet()
artifacts { artifacts {
archives javadocJar // archives javadocJar
archives srcJar archives srcJar
} }

View File

@ -42,7 +42,7 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle
/** /**
* A Type-Specific implementation of contains. This implementation iterates over the elements and returns true if the value match. * A Type-Specific implementation of contains. This implementation iterates over the elements and returns true if the value match.
* @param value that should be searched for. * @param e the element that should be searched for.
* @return true if the value was found. * @return true if the value was found.
*/ */
@Override @Override
@ -66,9 +66,9 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle
/** /**
* A Type-Specific implementation of containsAll. This implementation iterates over all elements and checks all elements are present in the other collection. * A Type-Specific implementation of containsAll. This implementation iterates over all elements and checks all elements are present in the other collection.
* @param the collection that should be checked if it contains all elements. * @param c the collection that should be checked if it contains all elements.
* @return true if all elements were found in the collection * @return true if all elements were found in the collection
* @throws NullPointerException if the collection is null * @throws java.lang.NullPointerException if the collection is null
*/ */
@Override @Override
public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c) { public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c) {
@ -84,7 +84,7 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle
* @param c the elements that should be checked for * @param c the elements that should be checked for
* @return true if any element is in this collection * @return true if any element is in this collection
* @deprecated if this is a primitive collection * @deprecated if this is a primitive collection
* @throws NullPointerException if the collection is null * @throws java.lang.NullPointerException if the collection is null
*/ */
@Override @Override
@Primitive @Primitive
@ -100,7 +100,7 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle
* This implementation iterates over the elements of the collection and checks if they are stored in this collection. * 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 * @param c the elements that should be checked for
* @return true if any element is in this collection * @return true if any element is in this collection
* @throws NullPointerException if the collection is null * @throws java.lang.NullPointerException if the collection is null
*/ */
@Override @Override
public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c) { public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c) {
@ -142,7 +142,7 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle
* A Type-Specific implementation of removeAll. This Implementation iterates over all elements and removes them as they were found in the other collection. * 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 * @param c the elements that should be deleted
* @return true if the collection was modified. * @return true if the collection was modified.
* @throws NullPointerException if the collection is null * @throws java.lang.NullPointerException if the collection is null
*/ */
@Override @Override
public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) { public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) {
@ -161,7 +161,7 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle
* A Type-Specific implementation of retainAll. This Implementation iterates over all elements and removes them as they were not found in the other collection. * 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 * @param c the elements that should be kept
* @return true if the collection was modified. * @return true if the collection was modified.
* @throws NullPointerException if the collection is null * @throws java.lang.NullPointerException if the collection is null
*/ */
@Override @Override
public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c) { public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c) {

View File

@ -23,7 +23,7 @@ public interface BI_ITERATOR KEY_GENERIC_TYPE extends ITERATOR KEY_GENERIC_TYPE
/** /**
* Returns the Previous element of the iterator. * Returns the Previous element of the iterator.
* @return the Previous element of the iterator. * @return the Previous element of the iterator.
* @throws NoSuchElementException if the iteration has no more elements * @throws java.util.NoSuchElementException if the iteration has no more elements
*/ */
public KEY_TYPE PREVIOUS(); public KEY_TYPE PREVIOUS();

View File

@ -19,7 +19,7 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITE
#if !TYPE_OBJECT #if !TYPE_OBJECT
/** /**
* A Type-Specific add function to reduce (un)boxing * A Type-Specific add function to reduce (un)boxing
* @see Collection#add(Object) * @param o the element that should be added
* @return true if the element was added to the collection * @return true if the element was added to the collection
*/ */
public boolean add(KEY_TYPE o); public boolean add(KEY_TYPE o);
@ -27,7 +27,7 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITE
#endif #endif
/** /**
* A Type-Specific addAll function to reduce (un)boxing * A Type-Specific addAll function to reduce (un)boxing
* @see Collection#addAll(Collection) * @param c the collection of elements that should be added
* @return true if elements were added into the collection * @return true if elements were added into the collection
*/ */
public boolean addAll(COLLECTION KEY_GENERIC_TYPE c); public boolean addAll(COLLECTION KEY_GENERIC_TYPE c);
@ -35,7 +35,7 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITE
#if !TYPE_OBJECT #if !TYPE_OBJECT
/** /**
* A Type-Specific contains function to reduce (un)boxing * A Type-Specific contains function to reduce (un)boxing
* @see Collection#contains(Object) * @param o the element that is checked for
* @return true if the element is found in the collection * @return true if the element is found in the collection
*/ */
public boolean contains(KEY_TYPE o); public boolean contains(KEY_TYPE o);
@ -43,14 +43,14 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITE
#endif #endif
/** /**
* A Type-Specific containsAll function to reduce (un)boxing * A Type-Specific containsAll function to reduce (un)boxing
* @see Collection#containsAll(Object) * @param c the collection of elements that should be tested for
* @return true if all the element is found in the collection * @return true if all the element is found in the collection
*/ */
public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c); public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c);
/** /**
* A Type-Specific containsAny function to reduce (un)boxing * A Type-Specific containsAny function to reduce (un)boxing
* @see #containsAny(Collection) * @param c the collection of elements that should be tested for
* @return true if any element was found * @return true if any element was found
*/ */
public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c); public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c);
@ -58,6 +58,7 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITE
/** /**
* Returns true if any element of the Collection is found in the provided collection. * 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. * 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. * @return true if any element was found.
*/ */
@Primitive @Primitive
@ -66,6 +67,7 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITE
#if !TYPE_OBJECT #if !TYPE_OBJECT
/** /**
* A Type-Specific remove function that reduces (un)boxing. * A Type-Specific remove function that reduces (un)boxing.
* @param o the element that should be removed
* @return true if the element was removed * @return true if the element was removed
* @see Collection#remove(Object) * @see Collection#remove(Object)
*/ */
@ -74,6 +76,7 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITE
#endif #endif
/** /**
* A Type-Specific removeAll function that reduces (un)boxing. * 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 * @return true if any element was removed
* @see Collection#removeAll(Collection) * @see Collection#removeAll(Collection)
*/ */
@ -81,6 +84,7 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITE
/** /**
* A Type-Specific retainAll function that reduces (un)boxing. * 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 * @return true if any element was removed
* @see Collection#retainAll(Collection) * @see Collection#retainAll(Collection)
*/ */
@ -123,7 +127,8 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITE
* <p>Removes elements that were selected by the filter * <p>Removes elements that were selected by the filter
* @see Collection#removeIf(Predicate) * @see Collection#removeIf(Predicate)
* @param filter Filters the elements that should be removed * @param filter Filters the elements that should be removed
* @throws NullPointerException if filter is null * @return true if the collection was modified
* @throws java.lang.NullPointerException if filter is null
*/ */
public default boolean remIf(JAVA_PREDICATE filter) { public default boolean remIf(JAVA_PREDICATE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);

View File

@ -19,7 +19,7 @@ public interface ITERATOR KEY_GENERIC_TYPE extends Iterator<CLASS_TYPE>
* Returns the next element in the iteration. * Returns the next element in the iteration.
* *
* @return the next element in the iteration * @return the next element in the iteration
* @throws NoSuchElementException if the iteration has no more elements * @throws java.util.NoSuchElementException if the iteration has no more elements
* @see Iterator#next() * @see Iterator#next()
*/ */
public KEY_TYPE NEXT(); public KEY_TYPE NEXT();
@ -45,7 +45,7 @@ public interface ITERATOR KEY_GENERIC_TYPE extends Iterator<CLASS_TYPE>
* }</pre> * }</pre>
* *
* @param action The action to be performed for each element * @param action The action to be performed for each element
* @throws NullPointerException if the specified action is null * @throws java.lang.NullPointerException if the specified action is null
* @see Iterator#forEachRemaining(Consumer) * @see Iterator#forEachRemaining(Consumer)
*/ */
public default void forEachRemaining(CONSUMER action) { public default void forEachRemaining(CONSUMER action) {

View File

@ -61,7 +61,7 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
/** /**
* The IndexOf implementation iterates over all elements and compares them to the search value. * The IndexOf implementation iterates over all elements and compares them to the search value.
* @param e the value that the index is searched for. * @param o the value that the index is searched for.
* @return index of the value that was searched for. -1 if not found * @return index of the value that was searched for. -1 if not found
* @deprecated 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. * @deprecated 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.
*/ */
@ -89,7 +89,7 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
/** /**
* The lastIndexOf implementation iterates over all elements and compares them to the search value. * The lastIndexOf implementation iterates over all elements and compares them to the search value.
* @param e the value that the index is searched for. * @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 * @return the last index of the value that was searched for. -1 if not found
* @deprecated 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. * @deprecated 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.
*/ */

View File

@ -126,13 +126,18 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
/** /**
* Creates a wrapped arraylist that uses the array as backing array * 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 GENERIC_KEY_BRACES ARRAY_LIST KEY_GENERIC_TYPE wrap(KEY_TYPE[] a) { public static GENERIC_KEY_BRACES ARRAY_LIST KEY_GENERIC_TYPE wrap(KEY_TYPE... a) {
return wrap(a, a.length); return wrap(a, a.length);
} }
/** /**
* Creates a wrapped arraylist that uses the array as backing array and a custom fillsize * Creates a wrapped arraylist that uses the array as backing array and a custom fillsize
* @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 GENERIC_KEY_BRACES ARRAY_LIST KEY_GENERIC_TYPE wrap(KEY_TYPE[] a, int length) { public static GENERIC_KEY_BRACES ARRAY_LIST KEY_GENERIC_TYPE wrap(KEY_TYPE[] a, int length) {
SanityChecks.checkArrayCapacity(a.length, 0, length); SanityChecks.checkArrayCapacity(a.length, 0, length);
@ -145,6 +150,8 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
#if TYPE_OBJECT #if TYPE_OBJECT
/** /**
* Creates a new ArrayList with a EmptyObject array of the Type requested * Creates a new ArrayList with a EmptyObject array of the Type requested
* @param c the type of the array
* @return a typed List
*/ */
public static GENERIC_KEY_BRACES ARRAY_LIST KEY_GENERIC_TYPE of(Class<KEY_TYPE> c) { public static GENERIC_KEY_BRACES ARRAY_LIST KEY_GENERIC_TYPE of(Class<KEY_TYPE> c) {
ARRAY_LIST KEY_GENERIC_TYPE list = new ARRAY_LISTBRACES(); ARRAY_LIST KEY_GENERIC_TYPE list = new ARRAY_LISTBRACES();
@ -194,7 +201,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
* Appends the specified elements to the index of the list. * Appends the specified elements to the index of the list.
* This function may delegate to more appropiate function if nessesary * This function may delegate to more appropiate function if nessesary
* @param index the index where to append the elements to * @param index the index where to append the elements to
* @param e the elements to append to the list * @param c the elements to append to the list
* @throws IndexOutOfBoundsException if index is outside of the lists range * @throws IndexOutOfBoundsException if index is outside of the lists range
* @deprecated if type is primitive * @deprecated if type is primitive
*/ */
@ -216,7 +223,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
* Appends the specified elements to the index of the list. * Appends the specified elements to the index of the list.
* This function may delegate to more appropiate function if nessesary * This function may delegate to more appropiate function if nessesary
* @param index the index where to append the elements to * @param index the index where to append the elements to
* @param e the elements to append to the list * @param c the elements to append to the list
* @throws IndexOutOfBoundsException if index is outside of the lists range * @throws IndexOutOfBoundsException if index is outside of the lists range
* @deprecated if type is primitive * @deprecated if type is primitive
*/ */
@ -236,7 +243,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
/** /**
* Appends the specified elements to the index of the list. * Appends the specified elements to the index of the list.
* @param index the index where to append the elements to * @param index the index where to append the elements to
* @param e the elements to append to the list * @param c the elements to append to the list
* @throws IndexOutOfBoundsException if index is outside of the lists range * @throws IndexOutOfBoundsException if index is outside of the lists range
* @deprecated if type is primitive * @deprecated if type is primitive
*/ */
@ -277,7 +284,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
* @param a the array where the values should be inserted to * @param a the array where the values should be inserted to
* @param offset the startIndex of where the array should be written 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 * @param length the number of elements the values should be fetched from
* @returns the inputArray * @return the inputArray
* @throws NullPointerException if the array is null * @throws NullPointerException if the array is null
* @throws IndexOutOfBoundsException if from is outside of the lists range * @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 * @throws IllegalStateException if offset or length are smaller then 0 or exceed the array length
@ -353,7 +360,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
/** /**
* A function to find if the Element is present in this list. * A function to find if the Element is present in this list.
* @param e the element that is searched for * @param o the element that is searched for
* @return if the element was found. * @return if the element was found.
* @deprecated if type-specific but still supported because of special edgecase Object-Comparason features * @deprecated if type-specific but still supported because of special edgecase Object-Comparason features
*/ */
@ -365,7 +372,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
/** /**
* A function to find the index of a given element * A function to find the index of a given element
* @param e the element that is searched for * @param o the element that is searched for
* @return the index of the element if found. (if not found then -1) * @return the index of the element if found. (if not found then -1)
* @deprecated if type-specific but still supported because of special edgecase Object-Comparason features * @deprecated if type-specific but still supported because of special edgecase Object-Comparason features
*/ */
@ -389,7 +396,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
/** /**
* A function to find the last index of a given element * A function to find the last index of a given element
* @param e the element that is searched for * @param o the element that is searched for
* @return the last index of the element if found. (if not found then -1) * @return the last index of the element if found. (if not found then -1)
* @deprecated if type-specific but still supported because of special edgecase Object-Comparason features * @deprecated if type-specific but still supported because of special edgecase Object-Comparason features
*/ */
@ -414,6 +421,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
#if TYPE_OBJECT #if TYPE_OBJECT
/** /**
* Sorts the elements specified by the Natural order either by using the Comparator or the elements * 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 List#sort(Comparator) * @see List#sort(Comparator)
* @see ARRAYS#stableSort(KEY_TYPE[], Comparator) * @see ARRAYS#stableSort(KEY_TYPE[], Comparator)
*/ */
@ -425,6 +433,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
/** /**
* Sorts the elements specified by the Natural order either by using the Comparator or the elements using a unstable sort * 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 List#sort(Comparator) * @see List#sort(Comparator)
* @see ARRAYS#unstableSort(KEY_TYPE[], Comparator) * @see ARRAYS#unstableSort(KEY_TYPE[], Comparator)
*/ */
@ -437,8 +446,8 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
#else #else
/** /**
* A Type Specific implementation of the Collection#contains function. * A Type Specific implementation of the Collection#contains function.
* @param the element that is searched for. * @param e the element that is searched for.
* @returns if the element was found * @return if the element was found
*/ */
@Override @Override
public boolean contains(KEY_TYPE e) { public boolean contains(KEY_TYPE e) {
@ -473,8 +482,9 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
/** /**
* Sorts the elements specified by the Natural order either by using the Comparator or the elements * 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 List#sort(Comparator) * @see List#sort(Comparator)
* @see ARRAYS#stableSort(KEY_TYPE[], Comparator) * @see ARRAYS#stableSort(KEY_TYPE[], COMPARATOR)
*/ */
@Override @Override
public void sort(COMPARATOR c) { public void sort(COMPARATOR c) {
@ -484,8 +494,9 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
/** /**
* Sorts the elements specified by the Natural order either by using the Comparator or the elements using a unstable sort * 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 List#sort(Comparator) * @see List#sort(Comparator)
* @see ARRAYS#unstableSort(KEY_TYPE[], Comparator) * @see ARRAYS#unstableSort(KEY_TYPE[], COMPARATOR)
*/ */
@Override @Override
public void unstableSort(COMPARATOR c) { public void unstableSort(COMPARATOR c) {
@ -541,7 +552,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
* }</pre> * }</pre>
* *
* @param action The action to be performed for each element * @param action The action to be performed for each element
* @throws NullPointerException if the specified action is null * @throws java.util.NullPointerException if the specified action is null
* @see Iterable#forEach(Consumer) * @see Iterable#forEach(Consumer)
*/ */
@Override @Override

View File

@ -46,7 +46,7 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
* @param c the elements that need to be added * @param c the elements that need to be added
* @param index index at which the specified elements is to be inserted * @param index index at which the specified elements is to be inserted
* @return true if the list was modified * @return true if the list was modified
* @see List#addAll(int, Object) * @see java.util.List#addAll(int, Collection)
*/ */
public boolean addAll(int index, COLLECTION KEY_GENERIC_TYPE c); public boolean addAll(int index, COLLECTION KEY_GENERIC_TYPE c);
@ -78,7 +78,7 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
/** /**
* A Type-Specific set function to reduce (un)boxing * A Type-Specific set function to reduce (un)boxing
* @param index index of the element to replace * @param index index of the element to replace
* @param element element to be stored at the specified position * @param e element to be stored at the specified position
* @return the element previously at the specified position * @return the element previously at the specified position
* @throws IndexOutOfBoundsException if the index is not within the list range * @throws IndexOutOfBoundsException if the index is not within the list range
* @see List#set(int, Object) * @see List#set(int, Object)
@ -141,13 +141,20 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
} }
#endif #endif
/**
* 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(KEY_TYPE... a) { addElements(0, a, 0, a.length); }
/** /**
* A function to fast add elements to the list * A function to fast add elements to the list
* @param from the index where the elements should be added into the list * @param from the index where the elements should be added into the list
* @param a the elements that should be added * @param a the elements that should be added
* @throws IndexOutOfBoundsException if from is outside of the lists range * @throws IndexOutOfBoundsException if from is outside of the lists range
*/ */
public default void addElements(int from, KEY_TYPE[] a) { addElements(from, a, 0, a.length); } public default void addElements(int from, KEY_TYPE... a) { addElements(from, a, 0, a.length); }
/** /**
* A function to fast add elements to the list * A function to fast add elements to the list
@ -164,7 +171,7 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
* A function to fast fetch elements from the list * A function to fast fetch elements from the list
* @param from index where the list should be fetching elements from * @param from index where the list should be fetching elements from
* @param a the array where the values should be inserted to * @param a the array where the values should be inserted to
* @returns the inputArray * @return the inputArray
* @throws NullPointerException if the array is null * @throws NullPointerException if the array is null
* @throws IndexOutOfBoundsException if from is outside of the lists range * @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 * @throws IllegalStateException if offset or length are smaller then 0 or exceed the array length
@ -177,7 +184,7 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
* @param a the array where the values should be inserted to * @param a the array where the values should be inserted to
* @param offset the startIndex of where the array should be written 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 * @param length the number of elements the values should be fetched from
* @returns the inputArray * @return the inputArray
* @throws NullPointerException if the array is null * @throws NullPointerException if the array is null
* @throws IndexOutOfBoundsException if from is outside of the lists range * @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 * @throws IllegalStateException if offset or length are smaller then 0 or exceed the array length
@ -255,8 +262,9 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
/** /**
* Sorts the elements specified by the Natural order either by using the Comparator or the elements * 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 List#sort(Comparator) * @see List#sort(Comparator)
* @see ARRAYS#stableSort(KEY_TYPE[], Comparator) * @see ARRAYS#stableSort(KEY_TYPE[], COMPARATOR)
*/ */
public default void sort(COMPARATOR c) { public default void sort(COMPARATOR c) {
KEY_TYPE[] array = TO_ARRAY(); KEY_TYPE[] array = TO_ARRAY();
@ -280,8 +288,9 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List
/** /**
* Sorts the elements specified by the Natural order either by using the Comparator or the elements using a unstable sort * 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 List#sort(Comparator) * @see List#sort(Comparator)
* @see ARRAYS#unstableSort(KEY_TYPE[], Comparator) * @see ARRAYS#unstableSort(KEY_TYPE[], COMPARATOR)
*/ */
public default void unstableSort(COMPARATOR c) { public default void unstableSort(COMPARATOR c) {
KEY_TYPE[] array = TO_ARRAY(); KEY_TYPE[] array = TO_ARRAY();

View File

@ -92,7 +92,7 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
@Override @Override
public boolean replace(KEY_TYPE key, VALUE_TYPE oldValue, VALUE_TYPE newValue) { public boolean replace(KEY_TYPE key, VALUE_TYPE oldValue, VALUE_TYPE newValue) {
VALUE_TYPE curValue = get(key); VALUE_TYPE curValue = GET_VALUE(key);
if (VALUE_EQUALS_NOT(curValue, oldValue) || (VALUE_EQUALS(curValue, getDefaultReturnValue()) && !containsKey(key))) { if (VALUE_EQUALS_NOT(curValue, oldValue) || (VALUE_EQUALS(curValue, getDefaultReturnValue()) && !containsKey(key))) {
return false; return false;
} }
@ -103,7 +103,7 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
@Override @Override
public VALUE_TYPE replace(KEY_TYPE key, VALUE_TYPE value) { public VALUE_TYPE replace(KEY_TYPE key, VALUE_TYPE value) {
VALUE_TYPE curValue; VALUE_TYPE curValue;
if (VALUE_EQUALS_NOT((curValue = get(key)), getDefaultReturnValue()) || containsKey(key)) { if (VALUE_EQUALS_NOT((curValue = GET_VALUE(key)), getDefaultReturnValue()) || containsKey(key)) {
curValue = put(key, value); curValue = put(key, value);
} }
return curValue; return curValue;
@ -121,7 +121,7 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
@Override @Override
public VALUE_TYPE COMPUTE(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { public VALUE_TYPE COMPUTE(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
Objects.requireNonNull(mappingFunction); Objects.requireNonNull(mappingFunction);
VALUE_TYPE value = get(key); VALUE_TYPE value = GET_VALUE(key);
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, value); VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, value);
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
if(VALUE_EQUALS_NOT(value, getDefaultReturnValue()) || containsKey(key)) { if(VALUE_EQUALS_NOT(value, getDefaultReturnValue()) || containsKey(key)) {
@ -138,7 +138,7 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) {
Objects.requireNonNull(mappingFunction); Objects.requireNonNull(mappingFunction);
VALUE_TYPE value; VALUE_TYPE value;
if((value = get(key)) == getDefaultReturnValue()) { if((value = GET_VALUE(key)) == getDefaultReturnValue() || !containsKey(key)) {
VALUE_TYPE newValue = mappingFunction.GET_VALUE(key); VALUE_TYPE newValue = mappingFunction.GET_VALUE(key);
if(VALUE_EQUALS_NOT(newValue, getDefaultReturnValue())) { if(VALUE_EQUALS_NOT(newValue, getDefaultReturnValue())) {
put(key, newValue); put(key, newValue);
@ -152,7 +152,7 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
Objects.requireNonNull(mappingFunction); Objects.requireNonNull(mappingFunction);
VALUE_TYPE value; VALUE_TYPE value;
if(VALUE_EQUALS_NOT((value = get(key)), getDefaultReturnValue())) { if(VALUE_EQUALS_NOT((value = GET_VALUE(key)), getDefaultReturnValue()) || containsKey(key)) {
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, value); VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, value);
if(VALUE_EQUALS_NOT(newValue, getDefaultReturnValue())) { if(VALUE_EQUALS_NOT(newValue, getDefaultReturnValue())) {
put(key, newValue); put(key, newValue);
@ -166,7 +166,7 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
@Override @Override
public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) {
Objects.requireNonNull(mappingFunction); Objects.requireNonNull(mappingFunction);
VALUE_TYPE oldValue = get(key); VALUE_TYPE oldValue = GET_VALUE(key);
VALUE_TYPE newValue = VALUE_EQUALS(oldValue, getDefaultReturnValue()) ? value : mappingFunction.APPLY_VALUE(oldValue, value); VALUE_TYPE newValue = VALUE_EQUALS(oldValue, getDefaultReturnValue()) ? value : mappingFunction.APPLY_VALUE(oldValue, value);
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) remove(key); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) remove(key);
else put(key, newValue); else put(key, newValue);

View File

@ -105,6 +105,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@ -120,6 +121,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@ -137,6 +139,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@ -152,6 +155,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@ -305,7 +309,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
@Override @Override
public VALUE_COLLECTION VALUE_GENERIC_TYPE values() { public VALUE_COLLECTION VALUE_GENERIC_TYPE values() {
if(values == null) valuesC = new Values(); if(valuesC == null) valuesC = new Values();
return valuesC; return valuesC;
} }
@ -425,8 +429,9 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
KEY_TYPE[] newKeys = NEW_KEY_ARRAY(newSize + 1); KEY_TYPE[] newKeys = NEW_KEY_ARRAY(newSize + 1);
VALUE_TYPE[] newValues = NEW_VALUE_ARRAY(newSize + 1); VALUE_TYPE[] newValues = NEW_VALUE_ARRAY(newSize + 1);
long[] newLinks = new long[newSize + 1]; long[] newLinks = new long[newSize + 1];
int newPrev = -1; int i = firstIndex, prev = -1, newPrev = -1, pos;
for(int j = size, i = firstIndex, pos = 0, prev = -1;j != 0;) { firstIndex = -1;
for(int j = size; j-- != 0;) {
if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) pos = newSize; if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) pos = newSize;
else { else {
pos = HashUtil.mix(strategy.hashCode(keys[i])) & newMask; pos = HashUtil.mix(strategy.hashCode(keys[i])) & newMask;
@ -452,6 +457,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
mask = newMask; mask = newMask;
maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1); maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
keys = newKeys; keys = newKeys;
values = newValues;
} }
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements SORTED_MAP.FastSortedSet KEY_VALUE_GENERIC_TYPE { private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements SORTED_MAP.FastSortedSet KEY_VALUE_GENERIC_TYPE {
@ -761,7 +767,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
} }
private class FastEntryIterator extends MapIterator implements ObjectListIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> { private class FastEntryIterator extends MapIterator implements ObjectListIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> {
MapEntry entry = new MapEntry(nextEntry()); MapEntry entry = new MapEntry();
public FastEntryIterator() {} public FastEntryIterator() {}
public FastEntryIterator(KEY_TYPE from) { public FastEntryIterator(KEY_TYPE from) {

View File

@ -154,7 +154,11 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
@Override @Override
@Deprecated @Deprecated
public boolean containsKey(Object key) { public boolean containsKey(Object key) {
return findIndex(key) >= 0; #if !TYPE_OBJECT
return key instanceof CLASS_TYPE && findIndex(CLASS_TO_KEY(key)) >= 0;
#else
return findIndex(CLASS_TO_KEY(key)) >= 0;
#endif
} }
#if !VALUE_OBJECT #if !VALUE_OBJECT
@ -186,7 +190,10 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
@Override @Override
@Deprecated @Deprecated
public CLASS_VALUE_TYPE remove(Object key) { public CLASS_VALUE_TYPE remove(Object key) {
int slot = findIndex(key); #if !TYPE_OBJECT
if(!(key instanceof CLASS_TYPE)) return getDefaultReturnValue();
#endif
int slot = findIndex(CLASS_TO_KEY(key));
if(slot < 0) return VALUE_TO_OBJ(getDefaultReturnValue()); if(slot < 0) return VALUE_TO_OBJ(getDefaultReturnValue());
return removeIndex(slot); return removeIndex(slot);
} }
@ -228,6 +235,9 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
} }
return false; return false;
} }
#if !TYPE_OBJECT
if(!(key instanceof CLASS_TYPE)) return false;
#endif
KEY_TYPE keyType = CLASS_TO_KEY(key); KEY_TYPE keyType = CLASS_TO_KEY(key);
int pos = HashUtil.mix(strategy.hashCode(keyType)) & mask; int pos = HashUtil.mix(strategy.hashCode(keyType)) & mask;
KEY_TYPE current = keys[pos]; KEY_TYPE current = keys[pos];
@ -253,14 +263,17 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
@Override @Override
public CLASS_VALUE_TYPE get(Object key) { public CLASS_VALUE_TYPE get(Object key) {
int slot = findIndex(key); #if !TYPE_OBJECT
if(!(key instanceof CLASS_TYPE)) return getDefaultReturnValue();
#endif
int slot = findIndex(CLASS_TO_KEY(key));
return VALUE_TO_OBJ(slot < 0 ? getDefaultReturnValue() : values[slot]); return VALUE_TO_OBJ(slot < 0 ? getDefaultReturnValue() : values[slot]);
} }
#if TYPE_OBJECT && VALUE_OBJECT #if TYPE_OBJECT && VALUE_OBJECT
@Override @Override
public VALUE_TYPE getOrDefault(Object key, VALUE_TYPE defaultValue) { public VALUE_TYPE getOrDefault(Object key, VALUE_TYPE defaultValue) {
int slot = findIndex(key); int slot = findIndex(CLASS_TO_KEY(key));
return slot < 0 ? defaultValue : values[slot]; return slot < 0 ? defaultValue : values[slot];
} }
@ -299,6 +312,9 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
} }
} }
@Override
public int size() { return size; }
@Override @Override
public void clear() { public void clear() {
if(size == 0) return; if(size == 0) return;
@ -322,7 +338,7 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
} }
#endif #endif
protected int findIndex(Object key) { protected int findIndex(CLASS_TYPE key) {
if(key == null) return containsNull ? nullIndex : -(nullIndex + 1); if(key == null) return containsNull ? nullIndex : -(nullIndex + 1);
KEY_TYPE keyType = CLASS_TO_KEY(key); KEY_TYPE keyType = CLASS_TO_KEY(key);
int pos = HashUtil.mix(strategy.hashCode(keyType)) & mask; int pos = HashUtil.mix(strategy.hashCode(keyType)) & mask;
@ -645,14 +661,14 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
public void forEach(Consumer VALUE_SUPER_GENERIC_TYPE action) { public void forEach(Consumer VALUE_SUPER_GENERIC_TYPE action) {
if(containsNull) action.accept(values[nullIndex]); if(containsNull) action.accept(values[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) for(int i = nullIndex-1;i>=0;i--)
if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) action.accept(values[i]); if(!strategy.equals(keys[i], EMPTY_KEY_VALUE)) action.accept(values[i]);
} }
#else #else
@Override @Override
public void forEach(VALUE_CONSUMER VALUE_SUPER_GENERIC_TYPE action) { public void forEach(VALUE_CONSUMER VALUE_SUPER_GENERIC_TYPE action) {
if(containsNull) action.accept(values[nullIndex]); if(containsNull) action.accept(values[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) for(int i = nullIndex-1;i>=0;i--)
if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) action.accept(values[i]); if(!strategy.equals(keys[i], EMPTY_KEY_VALUE)) action.accept(values[i]);
} }
#endif #endif
} }

View File

@ -107,6 +107,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex);
} }
else { else {
int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask; int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask;
@ -122,6 +123,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@ -139,6 +141,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex);
} }
else { else {
int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask; int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask;
@ -154,6 +157,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@ -307,7 +311,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
@Override @Override
public VALUE_COLLECTION VALUE_GENERIC_TYPE values() { public VALUE_COLLECTION VALUE_GENERIC_TYPE values() {
if(values == null) valuesC = new Values(); if(valuesC == null) valuesC = new Values();
return valuesC; return valuesC;
} }
@ -427,8 +431,9 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
KEY_TYPE[] newKeys = NEW_KEY_ARRAY(newSize + 1); KEY_TYPE[] newKeys = NEW_KEY_ARRAY(newSize + 1);
VALUE_TYPE[] newValues = NEW_VALUE_ARRAY(newSize + 1); VALUE_TYPE[] newValues = NEW_VALUE_ARRAY(newSize + 1);
long[] newLinks = new long[newSize + 1]; long[] newLinks = new long[newSize + 1];
int newPrev = -1; int i = firstIndex, prev = -1, newPrev = -1, pos;
for(int j = size, i = firstIndex, pos = 0, prev = -1;j != 0;) { firstIndex = -1;
for(int j = size; j-- != 0;) {
if(KEY_EQUALS_NULL(keys[i])) pos = newSize; if(KEY_EQUALS_NULL(keys[i])) pos = newSize;
else { else {
pos = HashUtil.mix(KEY_TO_HASH(keys[i])) & newMask; pos = HashUtil.mix(KEY_TO_HASH(keys[i])) & newMask;
@ -454,6 +459,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
mask = newMask; mask = newMask;
maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1); maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
keys = newKeys; keys = newKeys;
values = newValues;
} }
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements SORTED_MAP.FastSortedSet KEY_VALUE_GENERIC_TYPE { private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements SORTED_MAP.FastSortedSet KEY_VALUE_GENERIC_TYPE {
@ -763,7 +769,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
} }
private class FastEntryIterator extends MapIterator implements ObjectListIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> { private class FastEntryIterator extends MapIterator implements ObjectListIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> {
MapEntry entry = new MapEntry(nextEntry()); MapEntry entry = new MapEntry();
public FastEntryIterator() {} public FastEntryIterator() {}
public FastEntryIterator(KEY_TYPE from) { public FastEntryIterator(KEY_TYPE from) {

View File

@ -295,6 +295,9 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE
} }
} }
@Override
public int size() { return size; }
@Override @Override
public void clear() { public void clear() {
if(size == 0) return; if(size == 0) return;

View File

@ -12,6 +12,7 @@ import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif #endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER;
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR; import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP; import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.MAP; import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
@ -19,6 +20,7 @@ import speiger.src.collections.PACKAGE.maps.interfaces.SORTED_MAP;
import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET; import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET;
import speiger.src.collections.PACKAGE.sets.SORTED_SET; import speiger.src.collections.PACKAGE.sets.SORTED_SET;
import speiger.src.collections.PACKAGE.sets.SET; import speiger.src.collections.PACKAGE.sets.SET;
import speiger.src.collections.PACKAGE.utils.maps.MAPS;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR;
@ -26,6 +28,9 @@ import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR;
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_CONSUMER; import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_CONSUMER;
import speiger.src.collections.VALUE_PACKAGE.lists.VALUE_LIST_ITERATOR; import speiger.src.collections.VALUE_PACKAGE.lists.VALUE_LIST_ITERATOR;
#endif #endif
#if !VALUE_OBJECT
import speiger.src.collections.objects.collections.ObjectIterator;
#endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
import speiger.src.collections.objects.lists.ObjectListIterator; import speiger.src.collections.objects.lists.ObjectListIterator;
@ -33,6 +38,7 @@ import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSortedSet; import speiger.src.collections.objects.sets.ObjectSortedSet;
import speiger.src.collections.objects.sets.ObjectSet; import speiger.src.collections.objects.sets.ObjectSet;
#endif #endif
import speiger.src.collections.utils.HashUtil;
public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE
{ {
@ -43,6 +49,57 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
protected VALUE_COLLECTION VALUE_GENERIC_TYPE valuesC; protected VALUE_COLLECTION VALUE_GENERIC_TYPE valuesC;
protected FastSortedSet KEY_VALUE_GENERIC_TYPE entrySet; protected FastSortedSet KEY_VALUE_GENERIC_TYPE entrySet;
public ARRAY_MAP() {
this(HashUtil.DEFAULT_MIN_CAPACITY);
}
public ARRAY_MAP(int minCapacity) {
if(minCapacity < 0) throw new IllegalStateException("Minimum Capacity is negative. This is not allowed");
keys = NEW_KEY_ARRAY(minCapacity);
values = NEW_VALUE_ARRAY(minCapacity);
}
#if !TYPE_OBJECT || !VALUE_OBJECT
public ARRAY_MAP(CLASS_TYPE[] keys, CLASS_VALUE_TYPE[] values) {
this(keys, values, keys.length);
}
public ARRAY_MAP(CLASS_TYPE[] keys, CLASS_VALUE_TYPE[] values, int length) {
this(length);
if(keys.length != values.length) throw new IllegalStateException("Input Arrays are not equal size");
size = length;
for(int i = 0,m=length;i<m;i++) {
this.keys[i] = OBJ_TO_KEY(keys[i]);
this.values[i] = OBJ_TO_VALUE(values[i]);
}
}
#endif
public ARRAY_MAP(KEY_TYPE[] keys, VALUE_TYPE[] values) {
this(keys, values, keys.length);
}
public ARRAY_MAP(KEY_TYPE[] keys, VALUE_TYPE[] values, int length) {
if(keys.length != values.length) throw new IllegalStateException("Input Arrays are not equal size");
this.keys = Arrays.copyOf(keys, length);
this.values = Arrays.copyOf(values, length);
this.size = length;
}
public ARRAY_MAP(Map<? extends CLASS_TYPE, ? extends CLASS_VALUE_TYPE> map) {
this(map.size());
putAll(map);
}
public ARRAY_MAP(MAP KEY_VALUE_GENERIC_TYPE map) {
this(map.size());
for(ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iter = MAPS.fastIterator(map);iter.hasNext();size++) {
MAP.Entry KEY_VALUE_GENERIC_TYPE entry = iter.next();
keys[size] = entry.ENTRY_KEY();
values[size] = entry.ENTRY_VALUE();
}
}
@Override @Override
public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) { public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) {
int index = findIndex(key); int index = findIndex(key);
@ -106,7 +163,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
@Override @Override
public boolean moveToFirst(KEY_TYPE key) { public boolean moveToFirst(KEY_TYPE key) {
int index = findIndex(key); int index = findIndex(key);
if(index >= 0) { if(index > 0) {
moveIndexToFirst(index); moveIndexToFirst(index);
return true; return true;
} }
@ -116,7 +173,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
@Override @Override
public boolean moveToLast(KEY_TYPE key) { public boolean moveToLast(KEY_TYPE key) {
int index = findIndex(key); int index = findIndex(key);
if(index >= 0) { if(index < size-1) {
moveIndexToLast(index); moveIndexToLast(index);
return true; return true;
} }
@ -205,13 +262,13 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
@Override @Override
public VALUE_TYPE FIRST_ENTRY_VALUE() { public VALUE_TYPE FIRST_ENTRY_VALUE() {
if(size <= 0) throw new NoSuchElementException(); if(size <= 0) throw new NoSuchElementException();
return values[size-1]; return values[0];
} }
@Override @Override
public VALUE_TYPE LAST_ENTRY_VALUE() { public VALUE_TYPE LAST_ENTRY_VALUE() {
if(size <= 0) throw new NoSuchElementException(); if(size <= 0) throw new NoSuchElementException();
return values[0]; return values[size-1];
} }
@Override @Override
@ -266,6 +323,13 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
return true; return true;
} }
@Override
public void forEach(BI_CONSUMER KEY_VALUE_GENERIC_TYPE action) {
if(size() <= 0) return;
for(int i = 0;i<size;i++)
action.accept(keys[i], values[i]);
}
@Override @Override
public SET KEY_GENERIC_TYPE keySet() { public SET KEY_GENERIC_TYPE keySet() {
if(keySet == null) keySet = new KeySet(); if(keySet == null) keySet = new KeySet();
@ -518,7 +582,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
@Override @Override
public boolean moveToFirst(KEY_TYPE key) { public boolean moveToFirst(KEY_TYPE key) {
int index = findIndex(key); int index = findIndex(key);
if(index >= 0) { if(index > 0) {
moveIndexToFirst(index); moveIndexToFirst(index);
return true; return true;
} }
@ -528,7 +592,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
@Override @Override
public boolean moveToLast(KEY_TYPE key) { public boolean moveToLast(KEY_TYPE key) {
int index = findIndex(key); int index = findIndex(key);
if(index >= 0) { if(index < size()-1) {
moveIndexToLast(index); moveIndexToLast(index);
return true; return true;
} }
@ -650,6 +714,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
VALUE_TYPE value = values[index]; VALUE_TYPE value = values[index];
removeIndex(index); removeIndex(index);
length--;
return value; return value;
} }
@ -659,6 +724,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
int index = findIndex(key, value); int index = findIndex(key, value);
if(index < 0) return false; if(index < 0) return false;
removeIndex(index); removeIndex(index);
length--;
return true; return true;
} }
@ -669,6 +735,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
if(index < 0) return VALUE_TO_OBJ(getDefaultReturnValue()); if(index < 0) return VALUE_TO_OBJ(getDefaultReturnValue());
VALUE_TYPE value = values[index]; VALUE_TYPE value = values[index];
removeIndex(index); removeIndex(index);
length--;
return VALUE_TO_OBJ(value); return VALUE_TO_OBJ(value);
} }
@ -677,6 +744,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
int index = findIndex(key, value); int index = findIndex(key, value);
if(index < 0) return false; if(index < 0) return false;
removeIndex(index); removeIndex(index);
length--;
return true; return true;
} }
@ -750,8 +818,8 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
if(index == length-1) return; if(index == length-1) return;
KEY_TYPE key = keys[index]; KEY_TYPE key = keys[index];
VALUE_TYPE value = values[index]; VALUE_TYPE value = values[index];
System.arraycopy(keys, offset+index+1, keys, offset+index, length-index-1); System.arraycopy(keys, index+1, keys, index, end()-index-1);
System.arraycopy(values, offset+index+1, values, offset+index, length-index-1); System.arraycopy(values, index+1, values, index, end()-index-1);
keys[end()-1] = key; keys[end()-1] = key;
values[end()-1] = value; values[end()-1] = value;
} }
@ -767,7 +835,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
#if !TYPE_OBJECT #if !TYPE_OBJECT
protected int findIndex(KEY_TYPE key) { protected int findIndex(KEY_TYPE key) {
for(int i = length-1;i>=0;i--) for(int i = length-1;i>=0;i--)
if(KEY_EQUALS(keys[offset+i], key)) return i; if(KEY_EQUALS(keys[offset+i], key)) return i+offset;
return -1; return -1;
} }
@ -775,7 +843,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
#if !VALUE_OBJECT #if !VALUE_OBJECT
protected int findValue(VALUE_TYPE value) { protected int findValue(VALUE_TYPE value) {
for(int i = length-1;i>=0;i--) for(int i = length-1;i>=0;i--)
if(VALUE_EQUALS(values[offset+i], value)) return i; if(VALUE_EQUALS(values[offset+i], value)) return i+offset;
return -1; return -1;
} }
@ -783,21 +851,21 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
protected int findIndex(Object key, Object value) { protected int findIndex(Object key, Object value) {
if(key == null || value == null) return -1; if(key == null || value == null) return -1;
for(int i = length-1;i>=0;i--) for(int i = length-1;i>=0;i--)
if(EQUALS_KEY_TYPE(keys[offset+i], key) && EQUALS_VALUE_TYPE(values[offset+i], value)) return i; if(EQUALS_KEY_TYPE(keys[offset+i], key) && EQUALS_VALUE_TYPE(values[offset+i], value)) return i+offset;
return -1; return -1;
} }
protected int findIndex(Object key) { protected int findIndex(Object key) {
if(key == null) return -1; if(key == null) return -1;
for(int i = length-1;i>=0;i--) for(int i = length-1;i>=0;i--)
if(EQUALS_KEY_TYPE(keys[offset+i], key)) return i; if(EQUALS_KEY_TYPE(keys[offset+i], key)) return i+offset;
return -1; return -1;
} }
protected int findValue(Object value) { protected int findValue(Object value) {
if(value == null) return -1; if(value == null) return -1;
for(int i = length-1;i>=0;i--) for(int i = length-1;i>=0;i--)
if(EQUALS_VALUE_TYPE(values[offset+i], value)) return i; if(EQUALS_VALUE_TYPE(values[offset+i], value)) return i+offset;
return -1; return -1;
} }
@ -1371,13 +1439,13 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
#if TYPE_OBJECT #if TYPE_OBJECT
@Override @Override
public void forEach(Consumer<? super CLASS_TYPE> action) { public void forEach(Consumer<? super CLASS_TYPE> action) {
for(int i = 0;i<size;action.accept(keys[i])); for(int i = 0;i<size;action.accept(keys[i++]));
} }
#else #else
@Override @Override
public void forEach(CONSUMER KEY_GENERIC_TYPE action) { public void forEach(CONSUMER KEY_GENERIC_TYPE action) {
for(int i = 0;i<size;action.accept(keys[i])); for(int i = 0;i<size;action.accept(keys[i++]));
} }
#endif #endif
@ -1428,12 +1496,12 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
#if VALUE_OBJECT #if VALUE_OBJECT
@Override @Override
public void forEach(Consumer<? super CLASS_VALUE_TYPE> action) { public void forEach(Consumer<? super CLASS_VALUE_TYPE> action) {
for(int i = 0;i<size;action.accept(values[i])); for(int i = 0;i<size;action.accept(values[i++]));
} }
#else #else
@Override @Override
public void forEach(VALUE_CONSUMER VALUE_GENERIC_TYPE action) { public void forEach(VALUE_CONSUMER VALUE_GENERIC_TYPE action) {
for(int i = 0;i<size;action.accept(values[i])); for(int i = 0;i<size;action.accept(values[i++]));
} }
#endif #endif
} }

View File

@ -16,7 +16,7 @@ import speiger.src.collections.objects.maps.abstracts.ABSTRACT_MAP;
import speiger.src.collections.objects.maps.interfaces.MAP; import speiger.src.collections.objects.maps.interfaces.MAP;
import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSet; import speiger.src.collections.objects.sets.ObjectSet;
//import sun.misc.SharedSecrets; import sun.misc.SharedSecrets;
public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE
{ {
@ -183,8 +183,8 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
} }
protected boolean isSet(int index) { return (present[index >> 6] & (1L << index)) != 0; } protected boolean isSet(int index) { return (present[index >> 6] & (1L << index)) != 0; }
private static <K extends Enum<K>> K[] getKeyUniverse(Class<K> keyType) { private static <K extends Enum<K>> K[] getKeyUniverse(Class<K> keyType) {
return keyType.getEnumConstants(); // return keyType.getEnumConstants();
// return SharedSecrets.getJavaLangAccess().getEnumConstantsShared(keyType); return SharedSecrets.getJavaLangAccess().getEnumConstantsShared(keyType);
} }
class EntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> { class EntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> {

View File

@ -58,6 +58,54 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
protected NAVIGABLE_SET KEY_GENERIC_TYPE keySet; protected NAVIGABLE_SET KEY_GENERIC_TYPE keySet;
protected VALUE_COLLECTION VALUE_GENERIC_TYPE values; protected VALUE_COLLECTION VALUE_GENERIC_TYPE values;
protected ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> entrySet; protected ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> entrySet;
public AVL_TREE_MAP() {
}
public AVL_TREE_MAP(COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
}
#if !TYPE_OBJECT || !VALUE_OBJECT
public AVL_TREE_MAP(CLASS_TYPE[] keys, CLASS_VALUE_TYPE[] values) {
this(keys, values, null);
}
public AVL_TREE_MAP(CLASS_TYPE[] keys, CLASS_VALUE_TYPE[] values, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
if(keys.length != values.length) throw new IllegalStateException("Input Arrays are not equal size");
for(int i = 0,m=keys.length;i<m;i++) put(OBJ_TO_KEY(keys[i]), OBJ_TO_VALUE(values[i]));
}
#endif
public AVL_TREE_MAP(KEY_TYPE[] keys, VALUE_TYPE[] values) {
this(keys, values, null);
}
public AVL_TREE_MAP(KEY_TYPE[] keys, VALUE_TYPE[] values, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
if(keys.length != values.length) throw new IllegalStateException("Input Arrays are not equal size");
for(int i = 0,m=keys.length;i<m;i++) put(keys[i], values[i]);
}
public AVL_TREE_MAP(Map<? extends CLASS_TYPE, ? extends CLASS_VALUE_TYPE> map) {
this(map, null);
}
public AVL_TREE_MAP(Map<? extends CLASS_TYPE, ? extends CLASS_VALUE_TYPE> map, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
putAll(map);
}
public AVL_TREE_MAP(MAP KEY_VALUE_GENERIC_TYPE map) {
this(map, null);
}
public AVL_TREE_MAP(MAP KEY_VALUE_GENERIC_TYPE map, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
putAll(map);
}
#if TYPE_OBJECT #if TYPE_OBJECT
public KEY_TYPE getDefaultMaxValue() { return defaultMaxNotFound; } public KEY_TYPE getDefaultMaxValue() { return defaultMaxNotFound; }
public KEY_TYPE getDefaultMinValue() { return defaultMinNotFound; } public KEY_TYPE getDefaultMinValue() { return defaultMinNotFound; }
@ -324,6 +372,17 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
} }
#endif #endif
@Override
public int size() { return size; }
@Override
public void clear() {
size = 0;
first = null;
last = null;
tree = null;
}
LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(boolean descending) { LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(boolean descending) {
LIST_ITERATOR KEY_GENERIC_TYPE iter = new KeyIterator(descending); LIST_ITERATOR KEY_GENERIC_TYPE iter = new KeyIterator(descending);
return descending ? ITERATORS.invert(iter) : iter; return descending ? ITERATORS.invert(iter) : iter;
@ -567,7 +626,6 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
protected int compare(KEY_TYPE k, KEY_TYPE v) { return comparator != null ? comparator.compare(k, v) : COMPAREABLE_TO_KEY(k, v);} protected int compare(KEY_TYPE k, KEY_TYPE v) { return comparator != null ? comparator.compare(k, v) : COMPAREABLE_TO_KEY(k, v);}
/** From CLR */
protected void rotateLeft(Entry KEY_VALUE_GENERIC_TYPE entry) { protected void rotateLeft(Entry KEY_VALUE_GENERIC_TYPE entry) {
if(entry != null) { if(entry != null) {
Entry KEY_VALUE_GENERIC_TYPE right = entry.right; Entry KEY_VALUE_GENERIC_TYPE right = entry.right;
@ -582,7 +640,6 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
} }
} }
/** From CLR */
protected void rotateRight(Entry KEY_VALUE_GENERIC_TYPE entry) { protected void rotateRight(Entry KEY_VALUE_GENERIC_TYPE entry) {
if(entry != null) { if(entry != null) {
Entry KEY_VALUE_GENERIC_TYPE left = entry.left; Entry KEY_VALUE_GENERIC_TYPE left = entry.left;
@ -597,7 +654,6 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
} }
} }
/** From CLR */
protected void fixAfterInsertion(Entry KEY_VALUE_GENERIC_TYPE entry) { protected void fixAfterInsertion(Entry KEY_VALUE_GENERIC_TYPE entry) {
while(entry != null) { while(entry != null) {
entry.updateHeight(); entry.updateHeight();
@ -622,7 +678,6 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
} }
} }
/** From CLR */
protected void fixAfterDeletion(Entry KEY_VALUE_GENERIC_TYPE entry) { protected void fixAfterDeletion(Entry KEY_VALUE_GENERIC_TYPE entry) {
if(entry != null) { if(entry != null) {
entry.updateHeight(); entry.updateHeight();
@ -698,6 +753,60 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
super(m, fromStart, low, loInclusive, toEnd, high, hiInclusive); super(m, fromStart, low, loInclusive, toEnd, high, hiInclusive);
} }
@Override
public KEY_TYPE FIRST_ENTRY_KEY() { return super.LAST_ENTRY_KEY(); }
@Override
public KEY_TYPE POLL_FIRST_ENTRY_KEY() { return super.POLL_LAST_ENTRY_KEY(); }
@Override
public KEY_TYPE LAST_ENTRY_KEY() { return super.FIRST_ENTRY_KEY(); }
@Override
public KEY_TYPE POLL_LAST_ENTRY_KEY() { return super.POLL_FIRST_ENTRY_KEY(); }
@Override
public VALUE_TYPE FIRST_ENTRY_VALUE() { return super.LAST_ENTRY_VALUE(); }
@Override
public VALUE_TYPE LAST_ENTRY_VALUE() { return super.FIRST_ENTRY_VALUE(); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE firstEntry() { return super.lastEntry(); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE lastEntry() { return super.firstEntry(); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE pollFirstEntry() { return super.pollLastEntry(); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLastEntry() { return super.pollFirstEntry(); }
@Override
public KEY_TYPE lowerKey(KEY_TYPE e) { return super.higherKey(e); }
@Override
public KEY_TYPE floorKey(KEY_TYPE e) { return super.ceilingKey(e); }
@Override
public KEY_TYPE ceilingKey(KEY_TYPE e) { return super.floorKey(e); }
@Override
public KEY_TYPE higherKey(KEY_TYPE e) { return super.lowerKey(e); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(KEY_TYPE e) { return super.higherEntry(e); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE e) { return super.ceilingEntry(e); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE e) { return super.floorEntry(e); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(KEY_TYPE e) { return super.lowerEntry(e); }
@Override @Override
LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(boolean descending) { LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(boolean descending) {
LIST_ITERATOR KEY_GENERIC_TYPE iter = new SubMapKeyIterator(!descending); LIST_ITERATOR KEY_GENERIC_TYPE iter = new SubMapKeyIterator(!descending);
@ -711,7 +820,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
} }
@Override @Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return m.comparator().reversed(); } public COMPARATOR KEY_GENERIC_TYPE comparator() { return m.comparator() == null ? null : m.comparator().reversed(); }
@Override @Override
VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE valueIterator() { return VALUE_ITERATORS.invert(new SubMapValueIterator(true)); } VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE valueIterator() { return VALUE_ITERATORS.invert(new SubMapValueIterator(true)); }
@ -933,7 +1042,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
#endif #endif
@Override @Override
public VALUE_TYPE REMOVE_KEY(KEY_TYPE key) { public VALUE_TYPE REMOVE_KEY(KEY_TYPE key) {
return inRange(key) ? m.remove(key) : getDefaultReturnValue(); return inRange(key) ? m.REMOVE_KEY(key) : getDefaultReturnValue();
} }
#if TYPE_OBJECT && VALUE_OBJECT #if TYPE_OBJECT && VALUE_OBJECT

View File

@ -58,6 +58,54 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
protected NAVIGABLE_SET KEY_GENERIC_TYPE keySet; protected NAVIGABLE_SET KEY_GENERIC_TYPE keySet;
protected VALUE_COLLECTION VALUE_GENERIC_TYPE values; protected VALUE_COLLECTION VALUE_GENERIC_TYPE values;
protected ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> entrySet; protected ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> entrySet;
public RB_TREE_MAP() {
}
public RB_TREE_MAP(COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
}
#if !TYPE_OBJECT || !VALUE_OBJECT
public RB_TREE_MAP(CLASS_TYPE[] keys, CLASS_VALUE_TYPE[] values) {
this(keys, values, null);
}
public RB_TREE_MAP(CLASS_TYPE[] keys, CLASS_VALUE_TYPE[] values, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
if(keys.length != values.length) throw new IllegalStateException("Input Arrays are not equal size");
for(int i = 0,m=keys.length;i<m;i++) put(OBJ_TO_KEY(keys[i]), OBJ_TO_VALUE(values[i]));
}
#endif
public RB_TREE_MAP(KEY_TYPE[] keys, VALUE_TYPE[] values) {
this(keys, values, null);
}
public RB_TREE_MAP(KEY_TYPE[] keys, VALUE_TYPE[] values, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
if(keys.length != values.length) throw new IllegalStateException("Input Arrays are not equal size");
for(int i = 0,m=keys.length;i<m;i++) put(keys[i], values[i]);
}
public RB_TREE_MAP(Map<? extends CLASS_TYPE, ? extends CLASS_VALUE_TYPE> map) {
this(map, null);
}
public RB_TREE_MAP(Map<? extends CLASS_TYPE, ? extends CLASS_VALUE_TYPE> map, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
putAll(map);
}
public RB_TREE_MAP(MAP KEY_VALUE_GENERIC_TYPE map) {
this(map, null);
}
public RB_TREE_MAP(MAP KEY_VALUE_GENERIC_TYPE map, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
putAll(map);
}
#if TYPE_OBJECT #if TYPE_OBJECT
public KEY_TYPE getDefaultMaxValue() { return defaultMaxNotFound; } public KEY_TYPE getDefaultMaxValue() { return defaultMaxNotFound; }
public KEY_TYPE getDefaultMinValue() { return defaultMinNotFound; } public KEY_TYPE getDefaultMinValue() { return defaultMinNotFound; }
@ -323,6 +371,17 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
} }
#endif #endif
@Override
public int size() { return size; }
@Override
public void clear() {
size = 0;
first = null;
last = null;
tree = null;
}
LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(boolean descending) { LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(boolean descending) {
LIST_ITERATOR KEY_GENERIC_TYPE iter = new KeyIterator(descending); LIST_ITERATOR KEY_GENERIC_TYPE iter = new KeyIterator(descending);
return descending ? ITERATORS.invert(iter) : iter; return descending ? ITERATORS.invert(iter) : iter;
@ -572,7 +631,6 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
protected static GENERIC_KEY_VALUE_BRACES Entry KEY_VALUE_GENERIC_TYPE leftOf(Entry KEY_VALUE_GENERIC_TYPE p) { return p == null ? null : p.left; } protected static GENERIC_KEY_VALUE_BRACES Entry KEY_VALUE_GENERIC_TYPE leftOf(Entry KEY_VALUE_GENERIC_TYPE p) { return p == null ? null : p.left; }
protected static GENERIC_KEY_VALUE_BRACES Entry KEY_VALUE_GENERIC_TYPE rightOf(Entry KEY_VALUE_GENERIC_TYPE p) { return (p == null) ? null : p.right; } protected static GENERIC_KEY_VALUE_BRACES Entry KEY_VALUE_GENERIC_TYPE rightOf(Entry KEY_VALUE_GENERIC_TYPE p) { return (p == null) ? null : p.right; }
/** From CLR */
protected void rotateLeft(Entry KEY_VALUE_GENERIC_TYPE entry) { protected void rotateLeft(Entry KEY_VALUE_GENERIC_TYPE entry) {
if(entry != null) { if(entry != null) {
Entry KEY_VALUE_GENERIC_TYPE right = entry.right; Entry KEY_VALUE_GENERIC_TYPE right = entry.right;
@ -587,7 +645,6 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
} }
} }
/** From CLR */
protected void rotateRight(Entry KEY_VALUE_GENERIC_TYPE entry) { protected void rotateRight(Entry KEY_VALUE_GENERIC_TYPE entry) {
if(entry != null) { if(entry != null) {
Entry KEY_VALUE_GENERIC_TYPE left = entry.left; Entry KEY_VALUE_GENERIC_TYPE left = entry.left;
@ -602,7 +659,6 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
} }
} }
/** From CLR */
protected void fixAfterInsertion(Entry KEY_VALUE_GENERIC_TYPE entry) { protected void fixAfterInsertion(Entry KEY_VALUE_GENERIC_TYPE entry) {
entry.setBlack(false); entry.setBlack(false);
while(entry != null && entry != tree && !entry.parent.isBlack()) { while(entry != null && entry != tree && !entry.parent.isBlack()) {
@ -646,7 +702,6 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
tree.setBlack(true); tree.setBlack(true);
} }
/** From CLR */
protected void fixAfterDeletion(Entry KEY_VALUE_GENERIC_TYPE entry) { protected void fixAfterDeletion(Entry KEY_VALUE_GENERIC_TYPE entry) {
while(entry != tree && isBlack(entry)) { while(entry != tree && isBlack(entry)) {
if(entry == leftOf(parentOf(entry))) { if(entry == leftOf(parentOf(entry))) {
@ -755,6 +810,60 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
super(m, fromStart, low, loInclusive, toEnd, high, hiInclusive); super(m, fromStart, low, loInclusive, toEnd, high, hiInclusive);
} }
@Override
public KEY_TYPE FIRST_ENTRY_KEY() { return super.LAST_ENTRY_KEY(); }
@Override
public KEY_TYPE POLL_FIRST_ENTRY_KEY() { return super.POLL_LAST_ENTRY_KEY(); }
@Override
public KEY_TYPE LAST_ENTRY_KEY() { return super.FIRST_ENTRY_KEY(); }
@Override
public KEY_TYPE POLL_LAST_ENTRY_KEY() { return super.POLL_FIRST_ENTRY_KEY(); }
@Override
public VALUE_TYPE FIRST_ENTRY_VALUE() { return super.LAST_ENTRY_VALUE(); }
@Override
public VALUE_TYPE LAST_ENTRY_VALUE() { return super.FIRST_ENTRY_VALUE(); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE firstEntry() { return super.lastEntry(); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE lastEntry() { return super.firstEntry(); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE pollFirstEntry() { return super.pollLastEntry(); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLastEntry() { return super.pollFirstEntry(); }
@Override
public KEY_TYPE lowerKey(KEY_TYPE e) { return super.higherKey(e); }
@Override
public KEY_TYPE floorKey(KEY_TYPE e) { return super.ceilingKey(e); }
@Override
public KEY_TYPE ceilingKey(KEY_TYPE e) { return super.floorKey(e); }
@Override
public KEY_TYPE higherKey(KEY_TYPE e) { return super.lowerKey(e); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(KEY_TYPE e) { return super.higherEntry(e); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE e) { return super.ceilingEntry(e); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE e) { return super.floorEntry(e); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(KEY_TYPE e) { return super.lowerEntry(e); }
@Override @Override
LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(boolean descending) { LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(boolean descending) {
LIST_ITERATOR KEY_GENERIC_TYPE iter = new SubMapKeyIterator(!descending); LIST_ITERATOR KEY_GENERIC_TYPE iter = new SubMapKeyIterator(!descending);
@ -768,7 +877,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
} }
@Override @Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return m.comparator().reversed(); } public COMPARATOR KEY_GENERIC_TYPE comparator() { return m.comparator() == null ? null : m.comparator().reversed(); }
@Override @Override
VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE valueIterator() { return VALUE_ITERATORS.invert(new SubMapValueIterator(true)); } VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE valueIterator() { return VALUE_ITERATORS.invert(new SubMapValueIterator(true)); }
@ -990,7 +1099,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
#endif #endif
@Override @Override
public VALUE_TYPE REMOVE_KEY(KEY_TYPE key) { public VALUE_TYPE REMOVE_KEY(KEY_TYPE key) {
return inRange(key) ? m.remove(key) : getDefaultReturnValue(); return inRange(key) ? m.REMOVE_KEY(key) : getDefaultReturnValue();
} }
#if TYPE_OBJECT && VALUE_OBJECT #if TYPE_OBJECT && VALUE_OBJECT

View File

@ -38,6 +38,10 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
public AVL_TREE_SET() { public AVL_TREE_SET() {
} }
public AVL_TREE_SET(COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
}
public AVL_TREE_SET(KEY_TYPE[] array) { public AVL_TREE_SET(KEY_TYPE[] array) {
this(array, 0, array.length); this(array, 0, array.length);
} }
@ -47,15 +51,41 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
for(int i = 0;i<length;i++) add(array[offset+i]); for(int i = 0;i<length;i++) add(array[offset+i]);
} }
public AVL_TREE_SET(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
this(array, 0, array.length, comp);
}
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]);
}
public AVL_TREE_SET(SORTED_SET KEY_GENERIC_TYPE sortedSet) {
comparator = sortedSet.comparator();
addAll(sortedSet);
}
@Deprecated @Deprecated
public AVL_TREE_SET(Collection<? extends CLASS_TYPE> collection) { public AVL_TREE_SET(Collection<? extends CLASS_TYPE> collection) {
addAll(collection); addAll(collection);
} }
@Deprecated
public AVL_TREE_SET(Collection<? extends CLASS_TYPE> collection, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
addAll(collection);
}
public AVL_TREE_SET(COLLECTION KEY_GENERIC_TYPE collection) { public AVL_TREE_SET(COLLECTION KEY_GENERIC_TYPE collection) {
addAll(collection); addAll(collection);
} }
public AVL_TREE_SET(COLLECTION KEY_GENERIC_TYPE collection, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
addAll(collection);
}
public AVL_TREE_SET(Iterator<CLASS_TYPE> iterator) { public AVL_TREE_SET(Iterator<CLASS_TYPE> iterator) {
#if !TYPE_OBJECT #if !TYPE_OBJECT
this(ITERATORS.wrap(iterator)); this(ITERATORS.wrap(iterator));
@ -64,10 +94,24 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
#endif #endif
} }
public AVL_TREE_SET(Iterator<CLASS_TYPE> iterator, COMPARATOR KEY_GENERIC_TYPE comp) {
#if !TYPE_OBJECT
this(ITERATORS.wrap(iterator), comp);
#else
comparator = comp;
while(iterator.hasNext()) add(iterator.next());
#endif
}
public AVL_TREE_SET(ITERATOR KEY_GENERIC_TYPE iterator) { public AVL_TREE_SET(ITERATOR KEY_GENERIC_TYPE iterator) {
while(iterator.hasNext()) add(iterator.NEXT()); while(iterator.hasNext()) add(iterator.NEXT());
} }
public AVL_TREE_SET(ITERATOR KEY_GENERIC_TYPE iterator, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
while(iterator.hasNext()) add(iterator.NEXT());
}
#if !TYPE_OBJECT #if !TYPE_OBJECT
@Override @Override
public void setDefaultMaxValue(KEY_TYPE value) { defaultMaxNotFound = value; } public void setDefaultMaxValue(KEY_TYPE value) { defaultMaxNotFound = value; }
@ -339,6 +383,14 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
@Override @Override
public int size() { return size; } public int size() { return size; }
@Override
public void clear() {
size = 0;
first = null;
last = null;
tree = null;
}
@Override @Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; } public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; }
@ -533,7 +585,8 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
COMPARATOR KEY_GENERIC_TYPE comparator; COMPARATOR KEY_GENERIC_TYPE comparator;
DescendingSubSet(AVL_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) { DescendingSubSet(AVL_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) {
super(set, fromStart, start, loInclusive, toEnd, end, hiInclusive); super(set, fromStart, start, loInclusive, toEnd, end, hiInclusive);
comparator = set.comparator().reversed(); comparator = set.comparator();
if(comparator != null) comparator = comparator.reversed();
} }
@Override @Override

View File

@ -346,8 +346,9 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
int newMask = newSize - 1; int newMask = newSize - 1;
KEY_TYPE[] newKeys = NEW_KEY_ARRAY(newSize + 1); KEY_TYPE[] newKeys = NEW_KEY_ARRAY(newSize + 1);
long[] newLinks = new long[newSize + 1]; long[] newLinks = new long[newSize + 1];
int newPrev = -1; int i = firstIndex, prev = -1, newPrev = -1, pos;
for(int j = size, i = firstIndex, pos = 0, prev = -1;j != 0;) { firstIndex = -1;
for(int j = size; j-- != 0;) {
if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) pos = newSize; if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) pos = newSize;
else { else {
pos = HashUtil.mix(strategy.hashCode(keys[i])) & newMask; pos = HashUtil.mix(strategy.hashCode(keys[i])) & newMask;

View File

@ -348,8 +348,9 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
int newMask = newSize - 1; int newMask = newSize - 1;
KEY_TYPE[] newKeys = NEW_KEY_ARRAY(newSize + 1); KEY_TYPE[] newKeys = NEW_KEY_ARRAY(newSize + 1);
long[] newLinks = new long[newSize + 1]; long[] newLinks = new long[newSize + 1];
int newPrev = -1; int i = firstIndex, prev = -1, newPrev = -1, pos;
for(int j = size, i = firstIndex, pos = 0, prev = -1;j != 0;) { firstIndex = -1;
for(int j = size; j-- != 0;) {
if(KEY_EQUALS_NULL(keys[i])) pos = newSize; if(KEY_EQUALS_NULL(keys[i])) pos = newSize;
else { else {
pos = HashUtil.mix(KEY_TO_HASH(keys[i])) & newMask; pos = HashUtil.mix(KEY_TO_HASH(keys[i])) & newMask;

View File

@ -38,6 +38,10 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
public RB_TREE_SET() { public RB_TREE_SET() {
} }
public RB_TREE_SET(COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
}
public RB_TREE_SET(KEY_TYPE[] array) { public RB_TREE_SET(KEY_TYPE[] array) {
this(array, 0, array.length); this(array, 0, array.length);
} }
@ -47,15 +51,41 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
for(int i = 0;i<length;i++) add(array[offset+i]); for(int i = 0;i<length;i++) add(array[offset+i]);
} }
public RB_TREE_SET(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
this(array, 0, array.length, comp);
}
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]);
}
public RB_TREE_SET(SORTED_SET KEY_GENERIC_TYPE sortedSet) {
comparator = sortedSet.comparator();
addAll(sortedSet);
}
@Deprecated @Deprecated
public RB_TREE_SET(Collection<? extends CLASS_TYPE> collection) { public RB_TREE_SET(Collection<? extends CLASS_TYPE> collection) {
addAll(collection); addAll(collection);
} }
@Deprecated
public RB_TREE_SET(Collection<? extends CLASS_TYPE> collection, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
addAll(collection);
}
public RB_TREE_SET(COLLECTION KEY_GENERIC_TYPE collection) { public RB_TREE_SET(COLLECTION KEY_GENERIC_TYPE collection) {
addAll(collection); addAll(collection);
} }
public RB_TREE_SET(COLLECTION KEY_GENERIC_TYPE collection, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
addAll(collection);
}
public RB_TREE_SET(Iterator<CLASS_TYPE> iterator) { public RB_TREE_SET(Iterator<CLASS_TYPE> iterator) {
#if !TYPE_OBJECT #if !TYPE_OBJECT
this(ITERATORS.wrap(iterator)); this(ITERATORS.wrap(iterator));
@ -64,10 +94,24 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
#endif #endif
} }
public RB_TREE_SET(Iterator<CLASS_TYPE> iterator, COMPARATOR KEY_GENERIC_TYPE comp) {
#if !TYPE_OBJECT
this(ITERATORS.wrap(iterator), comp);
#else
comparator = comp;
while(iterator.hasNext()) add(iterator.next());
#endif
}
public RB_TREE_SET(ITERATOR KEY_GENERIC_TYPE iterator) { public RB_TREE_SET(ITERATOR KEY_GENERIC_TYPE iterator) {
while(iterator.hasNext()) add(iterator.NEXT()); while(iterator.hasNext()) add(iterator.NEXT());
} }
public RB_TREE_SET(ITERATOR KEY_GENERIC_TYPE iterator, COMPARATOR KEY_GENERIC_TYPE comp) {
comparator = comp;
while(iterator.hasNext()) add(iterator.NEXT());
}
#if !TYPE_OBJECT #if !TYPE_OBJECT
@Override @Override
public void setDefaultMaxValue(KEY_TYPE value) { defaultMaxNotFound = value; } public void setDefaultMaxValue(KEY_TYPE value) { defaultMaxNotFound = value; }
@ -341,6 +385,14 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
@Override @Override
public int size() { return size; } public int size() { return size; }
@Override
public void clear() {
size = 0;
first = null;
last = null;
tree = null;
}
@Override @Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; } public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; }
@ -593,7 +645,8 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
COMPARATOR KEY_GENERIC_TYPE comparator; COMPARATOR KEY_GENERIC_TYPE comparator;
DescendingSubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) { DescendingSubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) {
super(set, fromStart, start, loInclusive, toEnd, end, hiInclusive); super(set, fromStart, start, loInclusive, toEnd, end, hiInclusive);
comparator = set.comparator().reversed(); comparator = set.comparator();
if(comparator != null) comparator = comparator.reversed();
} }
@Override @Override

View File

@ -566,7 +566,7 @@ public class ARRAYS
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations * This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order * @param comp the Comparator that decides the sorting order
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static GENERIC_KEY_BRACES void parallelMergeSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) { public static GENERIC_KEY_BRACES void parallelMergeSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
parallelMergeSort(array, null, 0, array.length, comp); parallelMergeSort(array, null, 0, array.length, comp);
@ -578,7 +578,7 @@ public class ARRAYS
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted * @param length the maxmium size of the array to be sorted
* @param comp the Comparator that decides the sorting order * @param comp the Comparator that decides the sorting order
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static GENERIC_KEY_BRACES void parallelMergeSort(KEY_TYPE[] array, int length, COMPARATOR KEY_GENERIC_TYPE comp) { public static GENERIC_KEY_BRACES void parallelMergeSort(KEY_TYPE[] array, int length, COMPARATOR KEY_GENERIC_TYPE comp) {
parallelMergeSort(array, null, 0, length, comp); parallelMergeSort(array, null, 0, length, comp);
@ -591,7 +591,7 @@ public class ARRAYS
* @param from where the array should be sorted from * @param from where the array should be sorted from
* @param to where the array should be sorted to * @param to where the array should be sorted to
* @param comp the Comparator that decides the sorting order * @param comp the Comparator that decides the sorting order
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static GENERIC_KEY_BRACES void parallelMergeSort(KEY_TYPE[] array, KEY_TYPE[] supp, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp) { public static GENERIC_KEY_BRACES void parallelMergeSort(KEY_TYPE[] array, KEY_TYPE[] supp, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp) {
if(SanityChecks.canParallelTask() && to - from >= PARALLEL_THRESHOLD) { if(SanityChecks.canParallelTask() && to - from >= PARALLEL_THRESHOLD) {
@ -605,7 +605,7 @@ public class ARRAYS
* Sorts an array according to the natural ascending order using Parallel Merge Sort, * Sorts an array according to the natural ascending order using Parallel Merge Sort,
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations * This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static COMPAREABLE_KEY_BRACES void parallelMergeSort(KEY_TYPE[] array) { public static COMPAREABLE_KEY_BRACES void parallelMergeSort(KEY_TYPE[] array) {
parallelMergeSort(array, null, 0, array.length); parallelMergeSort(array, null, 0, array.length);
@ -616,7 +616,7 @@ public class ARRAYS
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations * This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted * @param length the maxmium size of the array to be sorted
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static COMPAREABLE_KEY_BRACES void parallelMergeSort(KEY_TYPE[] array, int length) { public static COMPAREABLE_KEY_BRACES void parallelMergeSort(KEY_TYPE[] array, int length) {
parallelMergeSort(array, null, 0, length); parallelMergeSort(array, null, 0, length);
@ -628,7 +628,7 @@ public class ARRAYS
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param from where the array should be sorted from * @param from where the array should be sorted from
* @param to where the array should be sorted to * @param to where the array should be sorted to
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static COMPAREABLE_KEY_BRACES void parallelMergeSort(KEY_TYPE[] array, KEY_TYPE[] supp, int from, int to) { public static COMPAREABLE_KEY_BRACES void parallelMergeSort(KEY_TYPE[] array, KEY_TYPE[] supp, int from, int to) {
if(SanityChecks.canParallelTask() && to - from >= PARALLEL_THRESHOLD) { if(SanityChecks.canParallelTask() && to - from >= PARALLEL_THRESHOLD) {
@ -788,7 +788,7 @@ public class ARRAYS
* @author Speiger * @author Speiger
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order * @param comp the Comparator that decides the sorting order
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static GENERIC_KEY_BRACES void parallelMemFreeMergeSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) { public static GENERIC_KEY_BRACES void parallelMemFreeMergeSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
parallelMemFreeMergeSort(array, 0, array.length, comp); parallelMemFreeMergeSort(array, 0, array.length, comp);
@ -803,7 +803,7 @@ public class ARRAYS
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted * @param length the maxmium size of the array to be sorted
* @param comp the Comparator that decides the sorting order * @param comp the Comparator that decides the sorting order
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static GENERIC_KEY_BRACES void parallelMemFreeMergeSort(KEY_TYPE[] array, int length, COMPARATOR KEY_GENERIC_TYPE comp) { public static GENERIC_KEY_BRACES void parallelMemFreeMergeSort(KEY_TYPE[] array, int length, COMPARATOR KEY_GENERIC_TYPE comp) {
parallelMemFreeMergeSort(array, 0, length, comp); parallelMemFreeMergeSort(array, 0, length, comp);
@ -819,7 +819,7 @@ public class ARRAYS
* @param from where the array should be sorted from * @param from where the array should be sorted from
* @param to where the array should be sorted to * @param to where the array should be sorted to
* @param comp the Comparator that decides the sorting order * @param comp the Comparator that decides the sorting order
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static GENERIC_KEY_BRACES void parallelMemFreeMergeSort(KEY_TYPE[] array, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp) { public static GENERIC_KEY_BRACES void parallelMemFreeMergeSort(KEY_TYPE[] array, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp) {
if(SanityChecks.canParallelTask() && to - from >= PARALLEL_THRESHOLD) { if(SanityChecks.canParallelTask() && to - from >= PARALLEL_THRESHOLD) {
@ -836,7 +836,7 @@ public class ARRAYS
* It does stack allocate tiny amounts of data for shifting around elements. * It does stack allocate tiny amounts of data for shifting around elements.
* @author Speiger * @author Speiger
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static COMPAREABLE_KEY_BRACES void parallelMemFreeMergeSort(KEY_TYPE[] array) { public static COMPAREABLE_KEY_BRACES void parallelMemFreeMergeSort(KEY_TYPE[] array) {
parallelMemFreeMergeSort(array, 0, array.length); parallelMemFreeMergeSort(array, 0, array.length);
@ -850,7 +850,7 @@ public class ARRAYS
* @author Speiger * @author Speiger
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted * @param length the maxmium size of the array to be sorted
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static COMPAREABLE_KEY_BRACES void parallelMemFreeMergeSort(KEY_TYPE[] array, int length) { public static COMPAREABLE_KEY_BRACES void parallelMemFreeMergeSort(KEY_TYPE[] array, int length) {
parallelMemFreeMergeSort(array, 0, length); parallelMemFreeMergeSort(array, 0, length);
@ -865,7 +865,7 @@ public class ARRAYS
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param from where the array should be sorted from * @param from where the array should be sorted from
* @param to where the array should be sorted to * @param to where the array should be sorted to
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static COMPAREABLE_KEY_BRACES void parallelMemFreeMergeSort(KEY_TYPE[] array, int from, int to) { public static COMPAREABLE_KEY_BRACES void parallelMemFreeMergeSort(KEY_TYPE[] array, int from, int to) {
if(SanityChecks.canParallelTask() && to - from >= PARALLEL_THRESHOLD) { if(SanityChecks.canParallelTask() && to - from >= PARALLEL_THRESHOLD) {
@ -878,7 +878,7 @@ public class ARRAYS
/** /**
* Sorts the specified range of elements according to the order induced by the specified comparator using Quick Sort, * Sorts the specified range of elements according to the order induced by the specified comparator using Quick Sort,
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure, * This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages12491265, 1993. * and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order * @param comp the Comparator that decides the sorting order
*/ */
@ -889,7 +889,7 @@ public class ARRAYS
/** /**
* Sorts the specified range of elements according to the order induced by the specified comparator using Quick Sort, * Sorts the specified range of elements according to the order induced by the specified comparator using Quick Sort,
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure, * This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages12491265, 1993. * and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted * @param length the maxmium size of the array to be sorted
* @param comp the Comparator that decides the sorting order * @param comp the Comparator that decides the sorting order
@ -901,7 +901,7 @@ public class ARRAYS
/** /**
* Sorts the specified range of elements according to the order induced by the specified comparator using Quick Sort, * Sorts the specified range of elements according to the order induced by the specified comparator using Quick Sort,
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure, * This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages12491265, 1993. * and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param from where the array should be sorted from * @param from where the array should be sorted from
* @param to where the array should be sorted to * @param to where the array should be sorted to
@ -934,7 +934,7 @@ public class ARRAYS
/** /**
* Sorts an array according to the natural ascending order using Quick Sort, * Sorts an array according to the natural ascending order using Quick Sort,
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure, * This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages12491265, 1993. * and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
*/ */
public static COMPAREABLE_KEY_BRACES void quickSort(KEY_TYPE[] array) { public static COMPAREABLE_KEY_BRACES void quickSort(KEY_TYPE[] array) {
@ -944,7 +944,7 @@ public class ARRAYS
/** /**
* Sorts an array according to the natural ascending order using Quick Sort, * Sorts an array according to the natural ascending order using Quick Sort,
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure, * This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages12491265, 1993. * and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted * @param length the maxmium size of the array to be sorted
*/ */
@ -955,7 +955,7 @@ public class ARRAYS
/** /**
* Sorts an array according to the natural ascending order using Quick Sort, * Sorts an array according to the natural ascending order using Quick Sort,
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure, * This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages12491265, 1993. * and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param from where the array should be sorted from * @param from where the array should be sorted from
* @param to where the array should be sorted to * @param to where the array should be sorted to
@ -987,10 +987,10 @@ public class ARRAYS
/** /**
* Sorts the specified range of elements according to the order induced by the specified comparator using Parallel Quick Sort, * Sorts the specified range of elements according to the order induced by the specified comparator using Parallel Quick Sort,
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure, * This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages12491265, 1993. * and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order * @param comp the Comparator that decides the sorting order
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static GENERIC_KEY_BRACES void parallelQuickSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) { public static GENERIC_KEY_BRACES void parallelQuickSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp) {
parallelQuickSort(array, 0, array.length, comp); parallelQuickSort(array, 0, array.length, comp);
@ -999,11 +999,11 @@ public class ARRAYS
/** /**
* Sorts the specified range of elements according to the order induced by the specified comparator using Parallel Quick Sort, * Sorts the specified range of elements according to the order induced by the specified comparator using Parallel Quick Sort,
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure, * This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages12491265, 1993. * and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted * @param length the maxmium size of the array to be sorted
* @param comp the Comparator that decides the sorting order * @param comp the Comparator that decides the sorting order
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static GENERIC_KEY_BRACES void parallelQuickSort(KEY_TYPE[] array, int length, COMPARATOR KEY_GENERIC_TYPE comp) { public static GENERIC_KEY_BRACES void parallelQuickSort(KEY_TYPE[] array, int length, COMPARATOR KEY_GENERIC_TYPE comp) {
parallelQuickSort(array, 0, length, comp); parallelQuickSort(array, 0, length, comp);
@ -1012,12 +1012,12 @@ public class ARRAYS
/** /**
* Sorts the specified range of elements according to the order induced by the specified comparator using Parallel Quick Sort, * Sorts the specified range of elements according to the order induced by the specified comparator using Parallel Quick Sort,
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure, * This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages12491265, 1993. * and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param from where the array should be sorted from * @param from where the array should be sorted from
* @param to where the array should be sorted to * @param to where the array should be sorted to
* @param comp the Comparator that decides the sorting order * @param comp the Comparator that decides the sorting order
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static GENERIC_KEY_BRACES void parallelQuickSort(KEY_TYPE[] array, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp) { public static GENERIC_KEY_BRACES void parallelQuickSort(KEY_TYPE[] array, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp) {
if(SanityChecks.canParallelTask() && to - from >= PARALLEL_THRESHOLD) { if(SanityChecks.canParallelTask() && to - from >= PARALLEL_THRESHOLD) {
@ -1030,9 +1030,9 @@ public class ARRAYS
/** /**
* Sorts an array according to the natural ascending order using Parallel Quick Sort, * Sorts an array according to the natural ascending order using Parallel Quick Sort,
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure, * This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages12491265, 1993. * and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static COMPAREABLE_KEY_BRACES void parallelQuickSort(KEY_TYPE[] array) { public static COMPAREABLE_KEY_BRACES void parallelQuickSort(KEY_TYPE[] array) {
parallelQuickSort(array, 0, array.length); parallelQuickSort(array, 0, array.length);
@ -1041,10 +1041,10 @@ public class ARRAYS
/** /**
* Sorts an array according to the natural ascending order using Parallel Quick Sort, * Sorts an array according to the natural ascending order using Parallel Quick Sort,
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure, * This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages12491265, 1993. * and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted * @param length the maxmium size of the array to be sorted
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static COMPAREABLE_KEY_BRACES void parallelQuickSort(KEY_TYPE[] array, int length) { public static COMPAREABLE_KEY_BRACES void parallelQuickSort(KEY_TYPE[] array, int length) {
parallelQuickSort(array, 0, length); parallelQuickSort(array, 0, length);
@ -1053,11 +1053,11 @@ public class ARRAYS
/** /**
* Sorts an array according to the natural ascending order using Parallel Quick Sort, * Sorts an array according to the natural ascending order using Parallel Quick Sort,
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure, * This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages12491265, 1993. * and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
* @param from where the array should be sorted from * @param from where the array should be sorted from
* @param to where the array should be sorted to * @param to where the array should be sorted to
* @Note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed * @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/ */
public static COMPAREABLE_KEY_BRACES void parallelQuickSort(KEY_TYPE[] array, int from, int to) { public static COMPAREABLE_KEY_BRACES void parallelQuickSort(KEY_TYPE[] array, int from, int to) {
if(SanityChecks.canParallelTask() && to - from >= PARALLEL_THRESHOLD) { if(SanityChecks.canParallelTask() && to - from >= PARALLEL_THRESHOLD) {

View File

@ -64,7 +64,7 @@ public class ITERATORS
/** /**
* Returns a Immutable Iterator instance based on the instance given. * Returns a Immutable Iterator instance based on the instance given.
* @param l that should be made immutable/unmodifyable * @param iterator that should be made immutable/unmodifyable
* @return a unmodifiable iterator wrapper. If the Iterator already a unmodifyable wrapper then it just returns itself. * @return a unmodifiable iterator wrapper. If the Iterator already a unmodifyable wrapper then it just returns itself.
*/ */
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE unmodifiable(ITERATOR KEY_GENERIC_TYPE iterator) { public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE unmodifiable(ITERATOR KEY_GENERIC_TYPE iterator) {
@ -77,7 +77,7 @@ public class ITERATORS
/** /**
* Returns a Immutable ListIterator instance based on the instance given. * Returns a Immutable ListIterator instance based on the instance given.
* @param l that should be made immutable/unmodifyable * @param iterator that should be made immutable/unmodifyable
* @return a unmodifiable listiterator wrapper. If the ListIterator already a unmodifyable wrapper then it just returns itself. * @return a unmodifiable listiterator wrapper. If the ListIterator already a unmodifyable wrapper then it just returns itself.
*/ */
public static GENERIC_KEY_BRACES LIST_ITERATOR KEY_GENERIC_TYPE unmodifiable(LIST_ITERATOR KEY_GENERIC_TYPE iterator) { public static GENERIC_KEY_BRACES LIST_ITERATOR KEY_GENERIC_TYPE unmodifiable(LIST_ITERATOR KEY_GENERIC_TYPE iterator) {

View File

@ -8,6 +8,7 @@ public interface ITrimmable
{ {
/** /**
* Trims the original collection down to the size of the current elements * Trims the original collection down to the size of the current elements
* @return if the internal array has been trimmed.
*/ */
public default boolean trim() { public default boolean trim() {
return trim(0); return trim(0);
@ -16,6 +17,7 @@ public interface ITrimmable
/** /**
* Trims the original collection down to the size of the current elements or the requested size depending which is bigger * 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. * @param size the requested trim size.
* @return if the internal array has been trimmed.
*/ */
public boolean trim(int size); public boolean trim(int size);
} }

View File

@ -81,7 +81,7 @@ public class SanityChecks
* @return true if the threadcount is bigger the 1 * @return true if the threadcount is bigger the 1
*/ */
public static boolean canParallelTask() { public static boolean canParallelTask() {
return WORK_POOL.getParallelism() > 1 || FORCE_IGNORE_PARALLELISM; return getPool().getParallelism() > 1 || FORCE_IGNORE_PARALLELISM;
} }
/** /**

View File

@ -0,0 +1,152 @@
package speiger.src.collections.ints.base;
import java.util.EnumSet;
import java.util.stream.IntStream;
import org.junit.Assert;
import org.junit.Test;
import speiger.src.collections.ints.maps.interfaces.Int2IntMap;
import speiger.src.collections.ints.utils.IntStrategy;
import speiger.src.collections.tests.MapTests;
public abstract class BaseInt2IntMapTest
{
protected static final IntStrategy STRATEGY = new Strategy();
protected static final int[] TEST_ARRAY = IntStream.range(0, 100).toArray();
protected static final int[] PUT_ARRAY = IntStream.range(512, 1024).toArray();
protected static final int[] PUT_VALUE_ARRAY = IntStream.range(0, 512).toArray();
public abstract Int2IntMap createMap(int[] keys, int[] values);
public abstract Int2IntMap createEmptyMap();
public EnumSet<MapTests> getValidMapTests() { return EnumSet.allOf(MapTests.class); }
@Test
public void testPut()
{
if(!getValidMapTests().contains(MapTests.PUT)) return;
Int2IntMap putMap = createMap(PUT_ARRAY, PUT_VALUE_ARRAY);
Assert.assertEquals(PUT_ARRAY.length, putMap.size());
Assert.assertEquals(0, putMap.put(0, 512));
Assert.assertEquals(1, putMap.put(513, 2));
Assert.assertEquals(PUT_ARRAY.length + 1, putMap.size());
Assert.assertEquals(512, putMap.addTo(0, 1));
Assert.assertEquals(513, putMap.getInt(0));
}
@Test
public void testPutAll()
{
if(!getValidMapTests().contains(MapTests.PUT_ALL)) return;
Int2IntMap putMap = createMap(TEST_ARRAY, TEST_ARRAY);
Assert.assertEquals(TEST_ARRAY.length, putMap.size());
putMap.putAll(createMap(PUT_ARRAY, PUT_VALUE_ARRAY));
Assert.assertEquals(TEST_ARRAY.length + PUT_ARRAY.length, putMap.size());
putMap = createMap(TEST_ARRAY, TEST_ARRAY);
putMap.putAll(createMap(PUT_VALUE_ARRAY, PUT_ARRAY));
Assert.assertEquals(PUT_ARRAY.length, putMap.size());
}
@Test
public void testContains()
{
if(!getValidMapTests().contains(MapTests.CONTAINS)) return;
Int2IntMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Assert.assertTrue(map.containsKey(0));
Assert.assertFalse(map.containsKey(-1));
Assert.assertTrue(map.containsKey(Integer.valueOf(10)));
Assert.assertFalse(map.containsKey(Short.valueOf((short)10)));
Assert.assertTrue(map.containsValue(50));
Assert.assertFalse(map.containsValue(150));
Assert.assertTrue(map.containsValue(Integer.valueOf(10)));
Assert.assertFalse(map.containsValue(Short.valueOf((short)10)));
}
@Test
public void testReplace()
{
if(!getValidMapTests().contains(MapTests.REPLACE)) return;
Int2IntMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Assert.assertEquals(0, map.replace(0, 512));
Assert.assertEquals(512, map.getInt(0));
Assert.assertTrue(map.replace(0, 512, 0));
Assert.assertFalse(map.replace(0, 512, 0));
map = createMap(TEST_ARRAY, TEST_ARRAY);
map.replaceInts((K, V) -> 99 - V);
Assert.assertEquals(99, map.getInt(0));
Assert.assertEquals(0, map.getInt(99));
}
@Test
public void testCompute()
{
if(!getValidMapTests().contains(MapTests.COMPUTE)) return;
Int2IntMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Assert.assertEquals(512, map.computeInt(0, (K, V) -> 512));
Assert.assertEquals(512, map.getInt(0));
Assert.assertEquals(512, map.computeIntIfAbsent(0, T -> 0));
Assert.assertEquals(0, map.computeIntIfPresent(0, (T, V) -> 0));
Assert.assertEquals(0, map.computeIntIfAbsent(-10, T -> 0));
}
@Test
public void testMerge()
{
if(!getValidMapTests().contains(MapTests.MERGE)) return;
Int2IntMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Assert.assertEquals(50, map.mergeInt(1, 50, Integer::max));
Assert.assertEquals(2, map.mergeInt(2, 50, Integer::min));
}
@Test
public void testGet()
{
if(!getValidMapTests().contains(MapTests.GET)) return;
Int2IntMap map = createMap(TEST_ARRAY, TEST_ARRAY);
for(int i = 0;i<TEST_ARRAY.length;i++)
{
Assert.assertEquals(i, map.getInt(i));
}
}
@Test
public void testIterators()
{
if(!getValidMapTests().contains(MapTests.ITERATORS)) return;
Int2IntMap map = createMap(PUT_VALUE_ARRAY, PUT_ARRAY);
map.forEach((K, V) -> Assert.assertEquals(PUT_ARRAY[K], V));
map.int2IntEntrySet().forEach(T -> Assert.assertEquals(PUT_ARRAY[T.getIntKey()], T.getIntValue()));
map.keySet().forEach(T -> Assert.assertEquals(PUT_VALUE_ARRAY[T], T));
map.values().forEach(T -> Assert.assertTrue(T >= 512 && T <= 1024));
Assert.assertTrue(map.keySet().contains(50));
Assert.assertFalse(map.keySet().contains(-50));
Assert.assertTrue(map.values().contains(1000));
Assert.assertFalse(map.values().contains(-1000));
}
@Test
public void testRemove()
{
if(!getValidMapTests().contains(MapTests.REMOVE)) return;
Int2IntMap map = createMap(PUT_VALUE_ARRAY, PUT_ARRAY);
Assert.assertEquals(PUT_ARRAY[50], map.remInt(PUT_VALUE_ARRAY[50]));
Assert.assertTrue(map.remove(PUT_VALUE_ARRAY[51], PUT_ARRAY[51]));
}
public static class Strategy implements IntStrategy
{
@Override
public int hashCode(int o)
{
return o;
}
@Override
public boolean equals(int key, int value)
{
return key == value;
}
}
}

View File

@ -0,0 +1,92 @@
package speiger.src.collections.ints.base;
import java.util.EnumSet;
import org.junit.Assert;
import org.junit.Test;
import speiger.src.collections.ints.maps.interfaces.Int2IntNavigableMap;
import speiger.src.collections.tests.NavigableSetTest;
public abstract class BaseInt2IntNavigableMapTest extends BaseInt2IntSortedMapTest
{
@Override
public abstract Int2IntNavigableMap createMap(int[] keys, int[] values);
@Override
public abstract Int2IntNavigableMap createEmptyMap();
public EnumSet<NavigableSetTest> getValidNavigableMapTests() { return EnumSet.allOf(NavigableSetTest.class); }
@Test
public void desendingTest() {
if(getValidNavigableMapTests().contains(NavigableSetTest.DESENDING)) {
Int2IntNavigableMap set = createMap(TEST_ARRAY, TEST_ARRAY).descendingMap();
Assert.assertEquals(TEST_ARRAY[TEST_ARRAY.length - 1], set.firstIntKey());
Assert.assertEquals(TEST_ARRAY[0], set.lastIntKey());
}
}
@Test
public void lowerTest() {
if(getValidNavigableMapTests().contains(NavigableSetTest.LOWER)) {
Assert.assertTrue(createMap(TEST_ARRAY, TEST_ARRAY).lowerKey(50) < 50);
}
}
@Test
public void higherTest() {
if(getValidNavigableMapTests().contains(NavigableSetTest.HIGHER)) {
Assert.assertTrue(createMap(TEST_ARRAY, TEST_ARRAY).higherKey(50) > 50);
}
}
@Test
public void ceilTest() {
if(getValidNavigableMapTests().contains(NavigableSetTest.CEILING)) {
Assert.assertTrue(createMap(TEST_ARRAY, TEST_ARRAY).ceilingKey(50) >= 50);
}
}
@Test
public void floorTest() {
if(getValidNavigableMapTests().contains(NavigableSetTest.FLOOR)) {
Assert.assertTrue(createMap(TEST_ARRAY, TEST_ARRAY).floorKey(50) <= 50);
}
}
@Test
public void naviSubSetTest() {
if(getValidNavigableMapTests().contains(NavigableSetTest.SUB_SET)) {
Int2IntNavigableMap set = createMap(TEST_ARRAY, TEST_ARRAY);
Int2IntNavigableMap subSet = set.subMap(25, 75);
Assert.assertTrue(subSet.lowerKey(50) < 50);
Assert.assertTrue(subSet.higherKey(50) > 50);
Assert.assertTrue(subSet.ceilingKey(50) >= 50);
Assert.assertTrue(subSet.floorKey(50) <= 50);
}
}
@Test
public void naviHeadSetTest() {
if(getValidNavigableMapTests().contains(NavigableSetTest.HEAD_SET)) {
Int2IntNavigableMap set = createMap(TEST_ARRAY, TEST_ARRAY);
Int2IntNavigableMap subSet = set.headMap(75);
Assert.assertTrue(subSet.lowerKey(50) < 50);
Assert.assertTrue(subSet.higherKey(50) > 50);
Assert.assertTrue(subSet.ceilingKey(50) >= 50);
Assert.assertTrue(subSet.floorKey(50) <= 50);
}
}
@Test
public void naviTailSetTest() {
if(getValidNavigableMapTests().contains(NavigableSetTest.TAIL_SET)) {
Int2IntNavigableMap set = createMap(TEST_ARRAY, TEST_ARRAY);
Int2IntNavigableMap subSet = set.tailMap(25);
Assert.assertTrue(subSet.lowerKey(50) < 50);
Assert.assertTrue(subSet.higherKey(50) > 50);
Assert.assertTrue(subSet.ceilingKey(50) >= 50);
Assert.assertTrue(subSet.floorKey(50) <= 50);
}
}
}

View File

@ -0,0 +1,116 @@
package speiger.src.collections.ints.base;
import java.util.EnumSet;
import org.junit.Assert;
import org.junit.Test;
import speiger.src.collections.ints.maps.interfaces.Int2IntSortedMap;
import speiger.src.collections.tests.SortedMapTests;
public abstract class BaseInt2IntSortedMapTest extends BaseInt2IntMapTest
{
@Override
public abstract Int2IntSortedMap createMap(int[] keys, int[] values);
@Override
public abstract Int2IntSortedMap createEmptyMap();
public EnumSet<SortedMapTests> getValidSortedMapTests() { return EnumSet.allOf(SortedMapTests.class); }
@Test
public void testPutMove()
{
if(!getValidSortedMapTests().contains(SortedMapTests.PUT_MOVE)) return;
Int2IntSortedMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Assert.assertEquals(0, map.putAndMoveToFirst(120, -1));
Assert.assertEquals(120, map.firstIntKey());
Assert.assertEquals(-1, map.firstIntValue());
Assert.assertEquals(0, map.putAndMoveToLast(121, -2));
Assert.assertEquals(121, map.lastIntKey());
Assert.assertEquals(-2, map.lastIntValue());
}
@Test
public void testMove()
{
if(!getValidSortedMapTests().contains(SortedMapTests.MOVE)) return;
Int2IntSortedMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Assert.assertTrue(map.moveToFirst(99));
Assert.assertFalse(map.moveToFirst(99));
Assert.assertEquals(99, map.firstIntKey());
Assert.assertTrue(map.moveToLast(0));
Assert.assertFalse(map.moveToLast(0));
Assert.assertEquals(0, map.lastIntKey());
}
@Test
public void testGetMove()
{
if(!getValidSortedMapTests().contains(SortedMapTests.GET_MOVE)) return;
Int2IntSortedMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Assert.assertNotEquals(99, map.firstIntValue());
Assert.assertEquals(99, map.getAndMoveToFirst(99));
Assert.assertEquals(99, map.firstIntValue());
Assert.assertNotEquals(0, map.lastIntValue());
Assert.assertEquals(0, map.getAndMoveToLast(0));
Assert.assertEquals(0, map.lastIntValue());
}
@Test
public void testFirst()
{
if(!getValidSortedMapTests().contains(SortedMapTests.FIRST)) return;
Int2IntSortedMap map = createMap(TEST_ARRAY, TEST_ARRAY);
for(int i = 0;i<TEST_ARRAY.length;i++)
{
Assert.assertEquals(TEST_ARRAY[i], map.pollFirstIntKey());
}
Assert.assertEquals(0, map.size());
}
@Test
public void testLast()
{
if(!getValidSortedMapTests().contains(SortedMapTests.LAST)) return;
Int2IntSortedMap map = createMap(TEST_ARRAY, TEST_ARRAY);
for(int i = TEST_ARRAY.length-1;i>=0;i--)
{
Assert.assertEquals(TEST_ARRAY[i], map.pollLastIntKey());
}
Assert.assertEquals(0, map.size());
}
@Test
public void testSubMap()
{
if(!getValidSortedMapTests().contains(SortedMapTests.SUB_MAP)) return;
Int2IntSortedMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Int2IntSortedMap subMap = map.subMap(25, 75);
Assert.assertEquals(50, subMap.remInt(50));
Assert.assertNotEquals(50, subMap.remInt(50));
Assert.assertFalse(subMap.containsKey(20));
Assert.assertFalse(subMap.containsKey(80));
}
@Test
public void testHeadMap()
{
if(!getValidSortedMapTests().contains(SortedMapTests.HEAD_MAP)) return;
Int2IntSortedMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Int2IntSortedMap subMap = map.headMap(75);
Assert.assertEquals(50, subMap.remInt(50));
Assert.assertNotEquals(50, subMap.remInt(50));
Assert.assertFalse(subMap.containsKey(80));
}
@Test
public void testTailMap()
{
if(!getValidSortedMapTests().contains(SortedMapTests.TAIL_MAP)) return;
Int2IntSortedMap map = createMap(TEST_ARRAY, TEST_ARRAY);
Int2IntSortedMap subMap = map.tailMap(25);
Assert.assertEquals(50, subMap.remInt(50));
Assert.assertNotEquals(50, subMap.remInt(50));
Assert.assertFalse(subMap.containsKey(20));
}
}

View File

@ -2,6 +2,7 @@ package speiger.src.collections.ints.base;
import java.util.Arrays; import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.stream.IntStream;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -16,6 +17,7 @@ import speiger.src.collections.tests.CollectionTest;
public abstract class BaseIntCollectionTest extends BaseIntIterableTest public abstract class BaseIntCollectionTest extends BaseIntIterableTest
{ {
protected static final int[] ADD_ARRAY = new int[]{3212, -12, 423, -182, -4912}; protected static final int[] ADD_ARRAY = new int[]{3212, -12, 423, -182, -4912};
protected static final int[] BULK_ADD_ARRAY = IntStream.range(200, 500).toArray();
protected static final int[] CONTAINS_ARRAY = new int[]{23, 45, 63, 89, 32}; protected static final int[] CONTAINS_ARRAY = new int[]{23, 45, 63, 89, 32};
@Override @Override
@ -40,6 +42,10 @@ public abstract class BaseIntCollectionTest extends BaseIntIterableTest
Assert.assertEquals(TEST_ARRAY.length, collection.size()); Assert.assertEquals(TEST_ARRAY.length, collection.size());
collection.addAll(create(ADD_ARRAY)); collection.addAll(create(ADD_ARRAY));
Assert.assertEquals(TEST_ARRAY.length + ADD_ARRAY.length, collection.size()); Assert.assertEquals(TEST_ARRAY.length + ADD_ARRAY.length, collection.size());
collection = create(TEST_ARRAY);
Assert.assertEquals(TEST_ARRAY.length, collection.size());
collection.addAll(create(BULK_ADD_ARRAY));
Assert.assertEquals(TEST_ARRAY.length + BULK_ADD_ARRAY.length, collection.size());
} }
@Test @Test

View File

@ -15,6 +15,15 @@ public abstract class BaseIntNavigableSetTest extends BaseIntSortedSetTest
protected EnumSet<NavigableSetTest> getValidNavigableSetTests() { return EnumSet.allOf(NavigableSetTest.class); } protected EnumSet<NavigableSetTest> getValidNavigableSetTests() { return EnumSet.allOf(NavigableSetTest.class); }
@Test
public void desendingTest() {
if(getValidNavigableSetTests().contains(NavigableSetTest.DESENDING)) {
IntNavigableSet set = create(TEST_ARRAY).descendingSet();
Assert.assertEquals(TEST_ARRAY[TEST_ARRAY.length - 1], set.firstInt());
Assert.assertEquals(TEST_ARRAY[0], set.lastInt());
}
}
@Test @Test
public void lowerTest() { public void lowerTest() {
if(getValidNavigableSetTests().contains(NavigableSetTest.LOWER)) { if(getValidNavigableSetTests().contains(NavigableSetTest.LOWER)) {

View File

@ -0,0 +1,26 @@
package speiger.src.collections.ints.maps;
import java.util.EnumSet;
import speiger.src.collections.ints.base.BaseInt2IntNavigableMapTest;
import speiger.src.collections.ints.maps.impl.tree.Int2IntAVLTreeMap;
import speiger.src.collections.ints.maps.interfaces.Int2IntNavigableMap;
import speiger.src.collections.tests.SortedMapTests;
public class Int2IntAVLTreeMapTest extends BaseInt2IntNavigableMapTest
{
@Override
public EnumSet<SortedMapTests> getValidSortedMapTests() { return EnumSet.complementOf(EnumSet.of(SortedMapTests.GET_MOVE, SortedMapTests.MOVE, SortedMapTests.PUT_MOVE)); }
@Override
public Int2IntNavigableMap createMap(int[] keys, int[] values)
{
return new Int2IntAVLTreeMap(keys, values);
}
@Override
public Int2IntNavigableMap createEmptyMap()
{
return new Int2IntAVLTreeMap();
}
}

View File

@ -0,0 +1,20 @@
package speiger.src.collections.ints.maps;
import speiger.src.collections.ints.base.BaseInt2IntSortedMapTest;
import speiger.src.collections.ints.maps.impl.misc.Int2IntArrayMap;
import speiger.src.collections.ints.maps.interfaces.Int2IntSortedMap;
public class Int2IntArrayMapTest extends BaseInt2IntSortedMapTest
{
@Override
public Int2IntSortedMap createMap(int[] keys, int[] values)
{
return new Int2IntArrayMap(keys, values);
}
@Override
public Int2IntSortedMap createEmptyMap()
{
return new Int2IntArrayMap();
}
}

View File

@ -0,0 +1,20 @@
package speiger.src.collections.ints.maps;
import speiger.src.collections.ints.base.BaseInt2IntMapTest;
import speiger.src.collections.ints.maps.impl.customHash.Int2IntOpenCustomHashMap;
import speiger.src.collections.ints.maps.interfaces.Int2IntMap;
public class Int2IntCustomHashMapTest extends BaseInt2IntMapTest
{
@Override
public Int2IntMap createMap(int[] keys, int[] values)
{
return new Int2IntOpenCustomHashMap(keys, values, STRATEGY);
}
@Override
public Int2IntMap createEmptyMap()
{
return new Int2IntOpenCustomHashMap(STRATEGY);
}
}

View File

@ -0,0 +1,22 @@
package speiger.src.collections.ints.maps;
import speiger.src.collections.ints.base.BaseInt2IntMapTest;
import speiger.src.collections.ints.maps.impl.hash.Int2IntOpenHashMap;
import speiger.src.collections.ints.maps.interfaces.Int2IntMap;
public class Int2IntHashMapTest extends BaseInt2IntMapTest
{
@Override
public Int2IntMap createMap(int[] keys, int[] values)
{
return new Int2IntOpenHashMap(keys, values);
}
@Override
public Int2IntMap createEmptyMap()
{
return new Int2IntOpenHashMap();
}
}

View File

@ -0,0 +1,26 @@
package speiger.src.collections.ints.maps;
import java.util.EnumSet;
import speiger.src.collections.ints.base.BaseInt2IntSortedMapTest;
import speiger.src.collections.ints.maps.impl.customHash.Int2IntLinkedOpenCustomHashMap;
import speiger.src.collections.ints.maps.interfaces.Int2IntSortedMap;
import speiger.src.collections.tests.SortedMapTests;
public class Int2IntLinkedOpenCustomHashMapTest extends BaseInt2IntSortedMapTest
{
@Override
public EnumSet<SortedMapTests> getValidSortedMapTests() { return EnumSet.complementOf(EnumSet.of(SortedMapTests.SUB_MAP, SortedMapTests.TAIL_MAP, SortedMapTests.HEAD_MAP)); }
@Override
public Int2IntSortedMap createMap(int[] keys, int[] values)
{
return new Int2IntLinkedOpenCustomHashMap(keys, values, STRATEGY);
}
@Override
public Int2IntSortedMap createEmptyMap()
{
return new Int2IntLinkedOpenCustomHashMap(STRATEGY);
}
}

View File

@ -0,0 +1,26 @@
package speiger.src.collections.ints.maps;
import java.util.EnumSet;
import speiger.src.collections.ints.base.BaseInt2IntSortedMapTest;
import speiger.src.collections.ints.maps.impl.hash.Int2IntLinkedOpenHashMap;
import speiger.src.collections.ints.maps.interfaces.Int2IntSortedMap;
import speiger.src.collections.tests.SortedMapTests;
public class Int2IntLinkedOpenHashMapTest extends BaseInt2IntSortedMapTest
{
@Override
public EnumSet<SortedMapTests> getValidSortedMapTests() { return EnumSet.complementOf(EnumSet.of(SortedMapTests.SUB_MAP, SortedMapTests.TAIL_MAP, SortedMapTests.HEAD_MAP)); }
@Override
public Int2IntSortedMap createMap(int[] keys, int[] values)
{
return new Int2IntLinkedOpenHashMap(keys, values);
}
@Override
public Int2IntSortedMap createEmptyMap()
{
return new Int2IntLinkedOpenHashMap();
}
}

View File

@ -0,0 +1,26 @@
package speiger.src.collections.ints.maps;
import java.util.EnumSet;
import speiger.src.collections.ints.base.BaseInt2IntNavigableMapTest;
import speiger.src.collections.ints.maps.impl.tree.Int2IntRBTreeMap;
import speiger.src.collections.ints.maps.interfaces.Int2IntNavigableMap;
import speiger.src.collections.tests.SortedMapTests;
public class Int2IntRBTreeMapTest extends BaseInt2IntNavigableMapTest
{
@Override
public EnumSet<SortedMapTests> getValidSortedMapTests() { return EnumSet.complementOf(EnumSet.of(SortedMapTests.GET_MOVE, SortedMapTests.MOVE, SortedMapTests.PUT_MOVE)); }
@Override
public Int2IntNavigableMap createMap(int[] keys, int[] values)
{
return new Int2IntRBTreeMap(keys, values);
}
@Override
public Int2IntNavigableMap createEmptyMap()
{
return new Int2IntRBTreeMap();
}
}

View File

@ -0,0 +1,14 @@
package speiger.src.collections.tests;
public enum MapTests
{
PUT,
PUT_ALL,
CONTAINS,
REPLACE,
COMPUTE,
MERGE,
GET,
ITERATORS,
REMOVE;
}

View File

@ -0,0 +1,13 @@
package speiger.src.collections.tests;
public enum NavigableMapTests
{
DESENDING,
LOWER,
HIGHER,
CEILING,
FLOOR,
SUB_SET,
HEAD_SET,
TAIL_SET;
}

View File

@ -2,6 +2,7 @@ package speiger.src.collections.tests;
public enum NavigableSetTest public enum NavigableSetTest
{ {
DESENDING,
LOWER, LOWER,
HIGHER, HIGHER,
CEILING, CEILING,

View File

@ -0,0 +1,13 @@
package speiger.src.collections.tests;
public enum SortedMapTests
{
PUT_MOVE,
MOVE,
GET_MOVE,
FIRST,
LAST,
SUB_MAP,
TAIL_MAP,
HEAD_MAP;
}