From ddc58ee221104563a6613bc674c84d677f588d70 Mon Sep 17 00:00:00 2001 From: Speiger Date: Sun, 12 Jun 2022 14:31:45 +0200 Subject: [PATCH] Version 0.7.0 Release - Added: Coverage Badge - Updated: Changelog. - Changed: Changelog now has info how to obtain the sourcecode. - Added: Over 11 Million Unit Tests to this library to ensure quality. - Added: ArrayList size constructor now throws IllegalStateException if the size parameter is negative - Added: EnumMap specialized forEach implementation. - Added: AbstractMap.remove now delegates to its primitive counterpart. - Added: ConcurrentHashMap now implements ITrimmable - Refactor: Removed a lot of disabled code from ArraySet. - Removed: LinkedList.addAll(index, List) now delegates to LinkedList.addAll(index, Collection) due to no special optimization required. - Fixed: AbstractList.SubList.get/set/swapRemove didn't calculate their List index Properly - Fixed: AbstractList.SubList chains now properly if you create SubLists within SubLists. - Fixed: AbstractList.Iterator.add now respects Immutable/UnmodifiableLists. - Fixed: AbstractList.Iterator.skip/back now keep track of the last returned value for remove function to work properly. - Fixed: CopyOnWriteArrayList.extract/removeElements(int, int) does now proper range checks and remove elements properly. - Fixed: CopyOnWriteArrayList.SubList now works properly. (Reimplemented entirely) - Fixed: CopyOnWriteArrayList.Iterator.previous() was returning the wrong values. - Fixed: CopyOnWriteArrayList.Iterator.skip now skips the right amount of elements and stops where it should. - Fixed: LinkedList.first/last/dequeue/dequeueLast now throws NoSuchElementException when empty instead of IllegalStateException. - Fixed: LinkedList had an edge case where the entire reverse iterator would break if the wrong element was removed. - Fixed: LinkedList.extractElement now returns the correct values. - Fixed: AbstractMap.entrySet().remove(Object) now returns true if defaultReturnValue elements were removed. - Fixed: ConcurrentHashMap.remove(Object, Object) checks if the type matches before comparing against null Values. - Fixed: LinkedHashMap.clearAndTrim() was checking the wrong value for determining the full reset or clearing of a Map. - Fixed: HashMap.trim/clearToTrim() was using the wrong value to determin if something should be done. - Fixed: HashMap now compares empty values (0) against nullKeys when Object Variants of the type are used. - Fixed: ImmutableMap now compares empty values (0) against nullKeys when Object Variants of the type are used. - Fixed: ArrayMap.iterator(key) now throws NoSuchElementException when the element wasn't found. - Fixed: Linked/EnumMap array constructor was creating the wrong size values array. - Fixed: LinkedEnumMap.getAndMoveToFirst/Last was moving elements even if the element wasn't present. - Fixed: AVL/RBTreeMap.getFirst/LastKey was not throwing a NoSuchElementException if the map was empty. - Fixed: Map.Builder wasn't throwing a IllegalStateException when creating a negative size builder. - Fixed: AVL/RBTreeSet.DecendingSet.subSet(from, fromInclusive, to, toInclusive) was creating a corrupt asending subset. - Fixed: ArraySet throws now a IllegalStateException when trying to create it with a negative size. - Fixed: ArraySet.addMoveToLast(key) was crashing when a key was already present. - Fixed: Immutable/LinkedHashSet now keep track of their iteration index properly. - Fixed: LinkedHashSet.moveToFirst/Last(key) would crash if the Set was empty. - Fixed: LinkedHashSet.clearAndTrim() was checking the wrong value for determining the full reset or clearing of a Map. - Fixed: HashSet.trim/clearToTrim() was using the wrong value to determin if something should be done. --- Changelog.md | 39 ++ README.md | 41 +- build.gradle | 4 +- .../templates/lists/AbstractList.template | 23 +- .../templates/lists/ArrayList.template | 1 + .../templates/lists/CopyOnWriteList.template | 455 ++++++++++++++++-- .../templates/lists/LinkedList.template | 52 +- .../maps/abstracts/AbstractMap.template | 16 +- .../concurrent/ConcurrentOpenHashMap.template | 38 +- .../LinkedOpenCustomHashMap.template | 8 +- .../customHash/OpenCustomHashMap.template | 11 +- .../maps/impl/hash/LinkedOpenHashMap.template | 8 +- .../maps/impl/hash/OpenHashMap.template | 11 +- .../immutable/ImmutableOpenHashMap.template | 3 + .../maps/impl/misc/ArrayMap.template | 2 + .../templates/maps/impl/misc/EnumMap.template | 17 +- .../maps/impl/misc/LinkedEnumMap.template | 17 +- .../maps/impl/tree/AVLTreeMap.template | 6 +- .../maps/impl/tree/RBTreeMap.template | 6 +- .../templates/maps/interfaces/Map.template | 3 +- .../templates/sets/AVLTreeSet.template | 12 +- .../templates/sets/ArraySet.template | 346 +------------ .../sets/ImmutableOpenHashSet.template | 2 + .../sets/LinkedOpenCustomHashSet.template | 16 +- .../templates/sets/LinkedOpenHashSet.template | 16 +- .../templates/sets/OpenCustomHashSet.template | 8 +- .../templates/sets/OpenHashSet.template | 8 +- .../templates/sets/RBTreeSet.template | 12 +- 28 files changed, 642 insertions(+), 539 deletions(-) diff --git a/Changelog.md b/Changelog.md index e645f437..7be84f86 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,44 @@ # Changelog of versions +### Version 0.7.0 +- Added: Over 11 Million Unit Tests to this library to ensure quality. +- Added: ArrayList size constructor now throws IllegalStateException if the size parameter is negative +- Added: EnumMap specialized forEach implementation. +- Added: AbstractMap.remove now delegates to its primitive counterpart. +- Added: ConcurrentHashMap now implements ITrimmable +- Refactor: Removed a lot of disabled code from ArraySet. +- Removed: LinkedList.addAll(index, List) now delegates to LinkedList.addAll(index, Collection) due to no special optimization required. +- Fixed: AbstractList.SubList.get/set/swapRemove didn't calculate their List index Properly +- Fixed: AbstractList.SubList chains now properly if you create SubLists within SubLists. +- Fixed: AbstractList.Iterator.add now respects Immutable/UnmodifiableLists. +- Fixed: AbstractList.Iterator.skip/back now keep track of the last returned value for remove function to work properly. +- Fixed: CopyOnWriteArrayList.extract/removeElements(int, int) does now proper range checks and remove elements properly. +- Fixed: CopyOnWriteArrayList.SubList now works properly. (Reimplemented entirely) +- Fixed: CopyOnWriteArrayList.Iterator.previous() was returning the wrong values. +- Fixed: CopyOnWriteArrayList.Iterator.skip now skips the right amount of elements and stops where it should. +- Fixed: LinkedList.first/last/dequeue/dequeueLast now throws NoSuchElementException when empty instead of IllegalStateException. +- Fixed: LinkedList had an edge case where the entire reverse iterator would break if the wrong element was removed. +- Fixed: LinkedList.extractElement now returns the correct values. +- Fixed: AbstractMap.entrySet().remove(Object) now returns true if defaultReturnValue elements were removed. +- Fixed: ConcurrentHashMap.remove(Object, Object) checks if the type matches before comparing against null Values. +- Fixed: LinkedHashMap.clearAndTrim() was checking the wrong value for determining the full reset or clearing of a Map. +- Fixed: HashMap.trim/clearToTrim() was using the wrong value to determin if something should be done. +- Fixed: HashMap now compares empty values (0) against nullKeys when Object Variants of the type are used. +- Fixed: ImmutableMap now compares empty values (0) against nullKeys when Object Variants of the type are used. +- Fixed: ArrayMap.iterator(key) now throws NoSuchElementException when the element wasn't found. +- Fixed: Linked/EnumMap array constructor was creating the wrong size values array. +- Fixed: LinkedEnumMap.getAndMoveToFirst/Last was moving elements even if the element wasn't present. +- Fixed: AVL/RBTreeMap.getFirst/LastKey was not throwing a NoSuchElementException if the map was empty. +- Fixed: Map.Builder wasn't throwing a IllegalStateException when creating a negative size builder. +- Fixed: AVL/RBTreeSet.DecendingSet.subSet(from, fromInclusive, to, toInclusive) was creating a corrupt asending subset. +- Fixed: ArraySet throws now a IllegalStateException when trying to create it with a negative size. +- Fixed: ArraySet.addMoveToLast(key) was crashing when a key was already present. +- Fixed: Immutable/LinkedHashSet now keep track of their iteration index properly. +- Fixed: LinkedHashSet.moveToFirst/Last(key) would crash if the Set was empty. +- Fixed: LinkedHashSet.clearAndTrim() was checking the wrong value for determining the full reset or clearing of a Map. +- Fixed: HashSet.trim/clearToTrim() was using the wrong value to determin if something should be done. + + ### Version 0.6.2 - Added: Array only sorting function return the inputed array. This was done to allow for static final references to use the method in one go without having to make lambda wrappers. Cleaner code. - Added: Iterator Wrappers are now a bit more in Compliance with Java Standards. diff --git a/README.md b/README.md index 915e329c..71e7e2c9 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ [![Latest Release](https://jitpack.io/v/Speiger/Primitive-Collections.svg)](https://jitpack.io/#Speiger/Primitive-Collections) [![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) ![GitHub commit activity](https://img.shields.io/github/commit-activity/m/Speiger/Primitive-Collections) +[![codecov](https://codecov.io/gh/Speiger/Primitive-Collections/branch/debug/graph/badge.svg?token=WSTSNJM0EN)](https://codecov.io/gh/Speiger/Primitive-Collections) # Primitive-Collections This is a Simple Primitive Collections Library aimed to outperform Java's Collection Library and FastUtil. Both in Performance and Quality of Life Features. @@ -42,44 +43,14 @@ repositories { } } dependencies { - implementation 'com.github.Speiger:Primitive-Collections:0.6.2' -} -``` -Using Custom Gradle: -```gradle -repositories { - maven { - url = "https://maven.speiger.com/repository/main" - } -} -dependencies { - implementation 'de.speiger:Primitive-Collections:0.6.2' + implementation 'com.github.Speiger:Primitive-Collections:0.7.0' } ``` -
-Direct: -

- -| Version | Jar | Sources | Java Doc | -|--------- |------------------------------------------------------------------------------------------------------------------------------ |-------------------------------------------------------------------------------------------------------------------------------------- |-------------------------------------------------------------------------------------------------------------------------------------- | -| 0.6.2 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.6.2/Primitive-Collections-0.6.2.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.6.2/Primitive-Collections-0.6.2-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.6.2/Primitive-Collections-0.6.2-javadoc.jar) | -| 0.6.1 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.6.1/Primitive-Collections-0.6.1.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.6.1/Primitive-Collections-0.6.1-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.6.1/Primitive-Collections-0.6.1-javadoc.jar) | -| 0.6.0 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.6.0/Primitive-Collections-0.6.0.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.6.0/Primitive-Collections-0.6.0-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.6.0/Primitive-Collections-0.6.0-javadoc.jar) | -| 0.5.3 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.5.3/Primitive-Collections-0.5.3.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.5.3/Primitive-Collections-0.5.3-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.5.3/Primitive-Collections-0.5.3-javadoc.jar) | -| 0.5.2 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.5.2/Primitive-Collections-0.5.2.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.5.2/Primitive-Collections-0.5.2-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.5.2/Primitive-Collections-0.5.2-javadoc.jar) | -| 0.5.1 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.5.1/Primitive-Collections-0.5.1.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.5.1/Primitive-Collections-0.5.1-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.5.1/Primitive-Collections-0.5.1-javadoc.jar) | -| 0.5.0 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.5.0/Primitive-Collections-0.5.0.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.5.0/Primitive-Collections-0.5.0-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.5.0/Primitive-Collections-0.5.0-javadoc.jar) | -| 0.4.5 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.5/Primitive-Collections-0.4.5.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.5/Primitive-Collections-0.4.5-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.5/Primitive-Collections-0.4.5-javadoc.jar) | -| 0.4.4 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.4/Primitive-Collections-0.4.4.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.4/Primitive-Collections-0.4.4-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.4/Primitive-Collections-0.4.4-javadoc.jar) | -| 0.4.3 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.3/Primitive-Collections-0.4.3.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.3/Primitive-Collections-0.4.3-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.3/Primitive-Collections-0.4.3-javadoc.jar) | -| 0.4.2 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.2/Primitive-Collections-0.4.2.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.2/Primitive-Collections-0.4.2-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.2/Primitive-Collections-0.4.2-javadoc.jar) | -| 0.4.1 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.1/Primitive-Collections-0.4.1.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.1/Primitive-Collections-0.4.1-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.1/Primitive-Collections-0.4.1-javadoc.jar) | -| 0.4.0 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.0/Primitive-Collections-0.4.0.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.0/Primitive-Collections-0.4.0-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.4.0/Primitive-Collections-0.4.0-javadoc.jar) | -| 0.3.6 | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.3.6/Primitive-Collections-0.3.6.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.3.6/Primitive-Collections-0.3.6-sources.jar) | [Download](https://maven.speiger.com/repository/main/de/speiger/Primitive-Collections/0.3.6/Primitive-Collections-0.3.6-javadoc.jar) | - -

-
+# SourceCode +The generated Sourcecode can be automatically build, +but if you want to just browse around in it. +Check out the [Debug Branch](https://github.com/Speiger/Primitive-Collections/tree/debug/src/main/java/speiger/src/collections), which has the entire up to date code. # Contributing If you want to contribute. diff --git a/build.gradle b/build.gradle index 509f0ff3..88d223e1 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ repositories { } archivesBaseName = 'Primitive Collections' -version = '0.6.2.10-SNAPSHOT'; +version = '0.7.0'; sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = JavaVersion.current(); @@ -44,7 +44,7 @@ configurations { } dependencies { - builderImplementation 'de.speiger:Simple-Code-Generator:1.0.7' + builderImplementation 'de.speiger:Simple-Code-Generator:1.1.4' testImplementation 'junit:junit:4.12' testImplementation 'com.google.guava:guava-testlib:31.0.1-jre' diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/AbstractList.template b/src/builder/resources/speiger/assets/collections/templates/lists/AbstractList.template index e1bff7be..f1803f62 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/AbstractList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/AbstractList.template @@ -308,7 +308,7 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION public KEY_TYPE[] getElements(int from, KEY_TYPE[] a, int offset, int length) { SanityChecks.checkArrayCapacity(size, from, length); SanityChecks.checkArrayCapacity(a.length, offset, length); - return list.getElements(from+this.offset, a, offset, length); + return list.getElements(from+parentOffset, a, offset, length); } @Override @@ -344,19 +344,25 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION @Override public KEY_TYPE GET_KEY(int index) { checkSubRange(index); - return list.GET_KEY(offset+index); + return list.GET_KEY(parentOffset+index); } @Override public KEY_TYPE set(int index, KEY_TYPE element) { checkSubRange(index); - return list.set(offset+index, element); + return list.set(parentOffset+index, element); } @Override public KEY_TYPE swapRemove(int index) { checkSubRange(index); - KEY_TYPE result = list.swapRemove(index+parentOffset); + if(index == size-1) { + KEY_TYPE result = list.REMOVE(parentOffset+size-1); + size--; + return result; + } + KEY_TYPE result = list.set(index+parentOffset, list.GET_KEY(parentOffset+size-1)); + list.REMOVE(parentOffset+size-1); size--; return result; } @@ -386,7 +392,7 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION @Override public LIST KEY_GENERIC_TYPE subList(int fromIndex, int toIndex) { SanityChecks.checkArrayCapacity(size, fromIndex, toIndex-fromIndex); - return new SubList(list, offset, fromIndex, toIndex); + return new SubList(this, offset, fromIndex, toIndex); } protected void checkSubRange(int index) { @@ -460,15 +466,17 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION @Override public void add(KEY_TYPE e) { - list.add(index++, e); + list.add(index, e); + index++; lastReturned = -1; } @Override public int skip(int amount) { if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed"); - int steps = Math.min(amount, (list.size() - 1) - index); + int steps = Math.min(amount, size() - index); index += steps; + if(steps > 0) lastReturned = Math.min(index-1, size()-1); return steps; } @@ -477,6 +485,7 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed"); int steps = Math.min(amount, index); index -= steps; + if(steps > 0) lastReturned = Math.min(index, size()-1); return steps; } } diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template index 7822c9bc..1ec1b2b6 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template @@ -86,6 +86,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE * @param size the minimum initial size of the Backing array */ public ARRAY_LIST(int size) { + if(size < 0) throw new IllegalStateException("Size has to be 0 or greater"); data = NEW_KEY_ARRAY(size); } diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/CopyOnWriteList.template b/src/builder/resources/speiger/assets/collections/templates/lists/CopyOnWriteList.template index 70e7ee7c..806223f3 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/CopyOnWriteList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/CopyOnWriteList.template @@ -9,8 +9,9 @@ import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Objects; import java.util.concurrent.locks.ReentrantLock; -#if TYPE_OBJECT import java.util.function.Consumer; +import java.util.function.Supplier; +#if TYPE_OBJECT import java.util.function.BiFunction; #endif import java.util.function.Predicate; @@ -155,6 +156,14 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER #endif + private void setArray(KEY_TYPE[] data) { + this.data = data; + } + + private KEY_TYPE[] getArray() { + return data; + } + /** * Appends the specified element to the end of this list. * @@ -410,6 +419,8 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER */ @Override public void removeElements(int from, int to) { + checkRange(from); + checkAddRange(to); int length = to - from; if(length <= 0) return; ReentrantLock lock = this.lock; @@ -440,9 +451,11 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER */ @Override public K[] extractElements(int from, int to, Class type) { + checkRange(from); + checkAddRange(to); int length = to - from; + if(length <= 0) return ARRAYS.newArray(type, 0); K[] a = ARRAYS.newArray(type, length); - if(length <= 0) return a; ReentrantLock lock = this.lock; lock.lock(); try { @@ -450,9 +463,9 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER System.arraycopy(data, from, a, 0, length); if(to == data.length) this.data = Arrays.copyOf(data, data.length - length); else { - int size = data.length-1; - KEY_TYPE[] newElements = NEW_KEY_ARRAY(size); - System.arraycopy(data, 0, newElements, 0, from); + int size = data.length; + KEY_TYPE[] newElements = NEW_KEY_ARRAY(size-length); + if(from != 0) System.arraycopy(data, 0, newElements, 0, from); System.arraycopy(data, to, newElements, from, size - to); this.data = newElements; } @@ -472,6 +485,8 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER */ @Override public KEY_TYPE[] extractElements(int from, int to) { + checkRange(from); + checkAddRange(to); int length = to - from; if(length <= 0) return EMPTY_KEY_ARRAY; ReentrantLock lock = this.lock; @@ -482,9 +497,9 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER System.arraycopy(data, from, a, 0, length); if(to == data.length) this.data = Arrays.copyOf(data, data.length - length); else { - int size = data.length-1; - KEY_TYPE[] newElements = NEW_KEY_ARRAY(size); - System.arraycopy(data, 0, newElements, 0, from); + int size = data.length; + KEY_TYPE[] newElements = NEW_KEY_ARRAY(size-length); + if(from != 0) System.arraycopy(data, 0, newElements, 0, from); System.arraycopy(data, to, newElements, from, size - to); this.data = newElements; } @@ -720,7 +735,7 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER @Override public LIST KEY_GENERIC_TYPE subList(int fromIndex, int toIndex) { SanityChecks.checkArrayCapacity(data.length, fromIndex, toIndex-fromIndex); - return new COWSubListBRACES(this, 0, fromIndex, toIndex); + return new COWSubListBRACES(this, lock, this::setArray, this::getArray, 0, fromIndex, toIndex); } /** @@ -1389,7 +1404,12 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + data.length); } - #if PRIMITIVES + protected void checkAddRange(int index) { + if (index < 0 || index > data.length) + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + data.length); + } + +#if PRIMITIVES /** * Returns a Java-Type-Specific Stream to reduce boxing/unboxing. * @return a Stream of the closest java type @@ -1436,7 +1456,7 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER @Override public KEY_TYPE PREVIOUS() { if(!hasPrevious()) throw new NoSuchElementException(); - return data[index--]; + return data[--index]; } @Override @@ -1459,7 +1479,7 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER @Override public int skip(int amount) { if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed"); - int steps = Math.min(amount, (data.length - 1) - index); + int steps = Math.min(amount, data.length - index); index += steps; return steps; } @@ -1475,13 +1495,19 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER private static class COWSubList KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE { - final COPY_ON_WRITE_LIST KEY_GENERIC_TYPE list; + final ABSTRACT_LIST KEY_GENERIC_TYPE list; + final ReentrantLock lock; + final Consumer setter; + final Supplier getter; final int parentOffset; final int offset; int size; - public COWSubList(COPY_ON_WRITE_LIST KEY_GENERIC_TYPE list, int offset, int from, int to) { + public COWSubList(ABSTRACT_LIST KEY_GENERIC_TYPE list, ReentrantLock lock, Consumer setter, Supplier getter, int offset, int from, int to) { this.list = list; + this.lock = lock; + this.setter = setter; + this.getter = getter; this.parentOffset = from; this.offset = offset + from; this.size = to - from; @@ -1489,7 +1515,6 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER @Override public void add(int index, KEY_TYPE element) { - ReentrantLock lock = list.lock; lock.lock(); try { checkAddSubRange(index); @@ -1505,7 +1530,6 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER public boolean addAll(int index, Collection c) { int add = c.size(); if(add <= 0) return false; - ReentrantLock lock = list.lock; lock.lock(); try { checkAddSubRange(index); @@ -1522,7 +1546,6 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER public boolean addAll(int index, COLLECTION KEY_GENERIC_TYPE c) { int add = c.size(); if(add <= 0) return false; - ReentrantLock lock = list.lock; lock.lock(); try { checkAddSubRange(index); @@ -1539,7 +1562,6 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER public boolean addAll(int index, LIST KEY_GENERIC_TYPE c) { int add = c.size(); if(add <= 0) return false; - ReentrantLock lock = list.lock; lock.lock(); try { checkAddSubRange(index); @@ -1555,7 +1577,6 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER @Override public void addElements(int from, KEY_TYPE[] a, int offset, int length) { if(length <= 0) return; - ReentrantLock lock = list.lock; lock.lock(); try { checkAddSubRange(from); @@ -1571,13 +1592,12 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER public KEY_TYPE[] getElements(int from, KEY_TYPE[] a, int offset, int length) { SanityChecks.checkArrayCapacity(size, from, length); SanityChecks.checkArrayCapacity(a.length, offset, length); - return list.getElements(from+this.offset, a, offset, length); + return list.getElements(from+parentOffset, a, offset, length); } @Override public void removeElements(int from, int to) { if(to-from <= 0) return; - ReentrantLock lock = list.lock; lock.lock(); try { checkSubRange(from); @@ -1593,7 +1613,6 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER #if TYPE_OBJECT @Override public K[] extractElements(int from, int to, Class type) { - ReentrantLock lock = list.lock; lock.lock(); try { checkSubRange(from); @@ -1610,7 +1629,6 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER #else @Override public KEY_TYPE[] extractElements(int from, int to) { - ReentrantLock lock = list.lock; lock.lock(); try { checkSubRange(from); @@ -1628,16 +1646,15 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER @Override public KEY_TYPE GET_KEY(int index) { checkSubRange(index); - return list.GET_KEY(offset+index); + return list.GET_KEY(parentOffset+index); } @Override public KEY_TYPE set(int index, KEY_TYPE element) { - ReentrantLock lock = list.lock; lock.lock(); try { checkSubRange(index); - return list.set(offset+index, element); + return list.set(parentOffset+index, element); } finally { lock.unlock(); @@ -1646,11 +1663,18 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER @Override public KEY_TYPE swapRemove(int index) { - ReentrantLock lock = list.lock; + if(index < 0 || index >= size) throw new IndexOutOfBoundsException(); + ReentrantLock lock = this.lock; lock.lock(); try { - checkSubRange(index); - KEY_TYPE result = list.swapRemove(index+parentOffset); + KEY_TYPE[] data = getter.get(); + KEY_TYPE result = data[offset+index]; + KEY_TYPE[] newElements = NEW_KEY_ARRAY(data.length-1); + int end = offset+size-1; + System.arraycopy(data, 0, newElements, 0, end); + if(end != newElements.length) System.arraycopy(data, end+1, newElements, end, data.length-end-1); + if(index+offset != end) newElements[index+offset] = data[end]; + setter.accept(newElements); size--; return result; } @@ -1661,7 +1685,6 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER @Override public KEY_TYPE REMOVE(int index) { - ReentrantLock lock = list.lock; lock.lock(); try { checkSubRange(index); @@ -1674,6 +1697,371 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER } } +#if !TYPE_OBJECT + @Override + public boolean REMOVE_KEY(KEY_TYPE type) { + int index = indexOf(type); + if(index == -1) return false; + REMOVE(index); + return true; + } + +#else + @Override + public boolean remove(Object type) { + int index = indexOf(type); + if(index == -1) return false; + REMOVE(index); + return true; + } + +#endif + +#if TYPE_OBJECT + @Override + public void sort(Comparator c) { + ReentrantLock lock = this.lock; + lock.lock(); + try { + KEY_TYPE[] data = getter.get(); + KEY_TYPE[] newData = Arrays.copyOf(data, data.length); + if(c != null) ARRAYS.stableSort(newData, offset, offset+size, c); + else ARRAYS.stableSort(newData, offset, offset+size); + setter.accept(newData); + } + finally { + lock.unlock(); + } + } + + @Override + public void unstableSort(Comparator c) { + ReentrantLock lock = this.lock; + lock.lock(); + try { + KEY_TYPE[] data = getter.get(); + KEY_TYPE[] newData = Arrays.copyOf(data, data.length); + if(c != null) ARRAYS.unstableSort(newData, offset, offset+size, c); + else ARRAYS.unstableSort(newData, offset, offset+size); + setter.accept(newData); + } + finally { + lock.unlock(); + } + } + +#else + @Override + public void sort(COMPARATOR c) { + ReentrantLock lock = this.lock; + lock.lock(); + try { + KEY_TYPE[] data = getter.get(); + KEY_TYPE[] newData = Arrays.copyOf(data, data.length); + if(c != null) ARRAYS.stableSort(newData, offset, offset+size, c); + else ARRAYS.stableSort(newData, offset, offset+size); + setter.accept(newData); + } + finally { + lock.unlock(); + } + } + + @Override + public void unstableSort(COMPARATOR c) { + ReentrantLock lock = this.lock; + lock.lock(); + try { + KEY_TYPE[] data = getter.get(); + KEY_TYPE[] newData = Arrays.copyOf(data, data.length); + if(c != null) ARRAYS.unstableSort(newData, offset, offset+size, c); + else ARRAYS.unstableSort(newData, offset, offset+size); + setter.accept(newData); + } + finally { + lock.unlock(); + } + } + +#endif + + @Override + @Deprecated + public boolean removeAll(Collection c) { + if(c.isEmpty()) return false; + ReentrantLock lock = this.lock; + lock.lock(); + try { + KEY_TYPE[] data = getter.get(); + KEY_TYPE[] newElements = NEW_KEY_ARRAY(data.length); + int j = 0; + for(int i= 0,m=data.length;i= offset+size) newElements[j++] = data[i]; + else if(!c.contains(KEY_TO_OBJ(data[i]))) newElements[j++] = data[i]; + } + if(data.length != j) { + setter.accept(Arrays.copyOf(newElements, j)); + size -= data.length - j; + return true; + } + return false; + } + finally { + lock.unlock(); + } + } + + @Override + @Deprecated + public boolean retainAll(Collection c) { + ReentrantLock lock = this.lock; + lock.lock(); + try { + KEY_TYPE[] data = getter.get(); + if(c.isEmpty()) { + if(size > 0) { + clear(); + return true; + } + return false; + } + KEY_TYPE[] newElements = NEW_KEY_ARRAY(data.length); + int j = 0; + for(int i= 0,m=data.length;i= offset+size) newElements[j++] = data[i]; + else if(c.contains(KEY_TO_OBJ(data[i]))) newElements[j++] = data[i]; + } + if(data.length != j) { + setter.accept(Arrays.copyOf(newElements, j)); + size -= data.length - j; + return true; + } + return false; + } + finally { + lock.unlock(); + } + } + + @Override + @Deprecated + public boolean removeIf(Predicate filter) { + Objects.requireNonNull(filter); + ReentrantLock lock = this.lock; + lock.lock(); + try { + KEY_TYPE[] data = getter.get(); + KEY_TYPE[] newElements = NEW_KEY_ARRAY(data.length); + int j = 0; + for(int i= 0,m=data.length;i= offset+size) newElements[j++] = data[i]; + else if(!filter.test(KEY_TO_OBJ(data[i]))) newElements[j++] = data[i]; + } + if(data.length != j) { + setter.accept(Arrays.copyOf(newElements, j)); + size -= data.length - j; + return true; + } + return false; + } + finally { + lock.unlock(); + } + } + + @Override + public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) { + if(c.isEmpty()) return false; + ReentrantLock lock = this.lock; + lock.lock(); + try { + KEY_TYPE[] data = getter.get(); + KEY_TYPE[] newElements = NEW_KEY_ARRAY(data.length); + int j = 0; + for(int i= 0,m=data.length;i= offset+size) newElements[j++] = data[i]; + else if(!c.contains(data[i])) newElements[j++] = data[i]; + } + if(data.length != j) { + setter.accept(Arrays.copyOf(newElements, j)); + size -= data.length - j; + return true; + } + return false; + } + finally { + lock.unlock(); + } + } + + @Override + public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { + if(c.isEmpty()) return false; + ReentrantLock lock = this.lock; + lock.lock(); + try { + KEY_TYPE[] data = getter.get(); + KEY_TYPE[] newElements = NEW_KEY_ARRAY(data.length); + int j = 0; + for(int i= 0,m=data.length;i= offset+size) newElements[j++] = data[i]; + else if(!c.contains(data[i])) newElements[j++] = data[i]; + else r.accept(data[i]); + } + if(data.length != j) { + setter.accept(Arrays.copyOf(newElements, j)); + size -= data.length - j; + return true; + } + return false; + } + finally { + lock.unlock(); + } + } + + @Override + public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c) { + ReentrantLock lock = this.lock; + lock.lock(); + try { + KEY_TYPE[] data = getter.get(); + if(c.isEmpty()) { + if(size > 0) { + clear(); + return true; + } + return false; + } + KEY_TYPE[] newElements = NEW_KEY_ARRAY(data.length); + int j = 0; + for(int i= 0,m=data.length;i= offset+size) newElements[j++] = data[i]; + else if(c.contains(data[i])) newElements[j++] = data[i]; + } + if(data.length != j) { + setter.accept(Arrays.copyOf(newElements, j));; + size -= data.length - j; + return true; + } + return false; + } + finally { + lock.unlock(); + } + } + + @Override + public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { + ReentrantLock lock = this.lock; + lock.lock(); + try { + KEY_TYPE[] data = getter.get(); + if(c.isEmpty()) { + if(size > 0) { + forEach(r); + clear(); + return true; + } + return false; + } + KEY_TYPE[] newElements = NEW_KEY_ARRAY(data.length); + int j = 0; + for(int i= 0,m=data.length;i= offset+size) newElements[j++] = data[i]; + else if(c.contains(data[i])) newElements[j++] = data[i]; + else r.accept(data[i]); + } + if(data.length != j) { + setter.accept(Arrays.copyOf(newElements, j)); + size -= data.length - j; + return true; + } + return false; + } + finally { + lock.unlock(); + } + } + +#if PRIMITIVES + @Override + public boolean remIf(JAVA_PREDICATE filter) { + Objects.requireNonNull(filter); + ReentrantLock lock = this.lock; + lock.lock(); + try { + KEY_TYPE[] data = getter.get(); + KEY_TYPE[] newElements = NEW_KEY_ARRAY(data.length); + int j = 0; + for(int i=0,m=data.length;i= offset+size) newElements[j++] = data[i]; + else if(!filter.test(data[i])) newElements[j++] = data[i]; + } + if(data.length != j) { + setter.accept(Arrays.copyOf(newElements, j)); + size -= data.length - j; + return true; + } + return false; + } + finally { + lock.unlock(); + } + } + +#endif + + @Override + @Primitive + public void replaceAll(UnaryOperator o) { +#if PRIMITIVES + Objects.requireNonNull(o); +#if TYPE_BYTE || TYPE_SHORT || TYPE_CHAR || TYPE_FLOAT + REPLACE(T -> OBJ_TO_KEY(o.apply(KEY_TO_OBJ(SanityChecks.SANITY_CAST(T))))); +#else + REPLACE(T -> OBJ_TO_KEY(o.apply(KEY_TO_OBJ(T)))); +#endif +#else + Objects.requireNonNull(o); + ReentrantLock lock = this.lock; + lock.lock(); + try { + KEY_TYPE[] data = getter.get(); + KEY_TYPE[] newData = Arrays.copyOf(data, data.length); + for(int i = 0,m=size;i 0) { Entry KEY_GENERIC_TYPE prev = entry.prev; unlink(entry); @@ -733,7 +706,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE } return a; } - Entry KEY_GENERIC_TYPE entry = getNode(to); + Entry KEY_GENERIC_TYPE entry = getNode(to-1); for(int i = length-1;length > 0;i--, length--) { Entry KEY_GENERIC_TYPE prev = entry.prev; a[i] = (K)unlink(entry); @@ -759,7 +732,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE } return d; } - Entry KEY_GENERIC_TYPE entry = getNode(to); + Entry KEY_GENERIC_TYPE entry = getNode(to-1); for(int i = length-1;length > 0;i--, length--) { Entry KEY_GENERIC_TYPE prev = entry.prev; d[i] = unlink(entry); @@ -1141,6 +1114,11 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE this.prev = prev; this.next = next; } + + @Override + public String toString() { + return KEY_TO_STRING(value); + } } private class ListIter implements LIST_ITERATOR KEY_GENERIC_TYPE diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/abstracts/AbstractMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/abstracts/AbstractMap.template index ac4290c2..01530d90 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/abstracts/AbstractMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/abstracts/AbstractMap.template @@ -285,6 +285,16 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMapGuavas approach and backing array implementations. @@ -84,7 +85,7 @@ import speiger.src.collections.utils.HashUtil; * @Type(T) * @ValueType(V) */ -public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements CONCURRENT_MAP KEY_VALUE_GENERIC_TYPE +public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements CONCURRENT_MAP KEY_VALUE_GENERIC_TYPE, ITrimmable { /** Segment Limit */ private static final int MAX_SEGMENTS = 1 << 16; @@ -538,6 +539,30 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY } } + @Override + public boolean trim(int size) { + int segmentCapacity = size / segments.length; + if(segmentCapacity * segments.length < size) { + segmentCapacity++; + } + boolean result = false; + for(int i = 0, m=segments.length;i= size || this.size > Math.min((int)Math.ceil(request * loadFactor), request - 1)) return false; + if(request >= mask+1 || this.size > Math.min((int)Math.ceil(request * loadFactor), request - 1)) return false; long stamp = writeLock(); try { try { @@ -2218,7 +2247,7 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY protected void clearAndTrim(int size) { int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor))); - if(request >= size) { + if(request >= mask+1) { clear(); return; } @@ -2298,6 +2327,9 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY #endif protected int findIndex(int hash, Object key) { if(key == null) return containsNull ? nullIndex : -(nullIndex + 1); +#if !TYPE_OBJECT + if(KEY_EQUALS_NULL(CLASS_TO_KEY(key))) return containsNull ? nullIndex : -(nullIndex + 1); +#endif int pos = hash & mask; KEY_TYPE current = keys[pos]; if(KEY_EQUALS_NOT_NULL(current)) { diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/LinkedOpenCustomHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/LinkedOpenCustomHashMap.template index 83f017f4..913c3aff 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/LinkedOpenCustomHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/LinkedOpenCustomHashMap.template @@ -369,8 +369,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M public KEY_TYPE POLL_FIRST_ENTRY_KEY() { if(size == 0) throw new NoSuchElementException(); int pos = firstIndex; - firstIndex = (int)links[pos]; - if(0 <= firstIndex) links[firstIndex] |= 0xFFFFFFFF00000000L; + onNodeRemoved(pos); KEY_TYPE result = keys[pos]; size--; if(strategy.equals(result, EMPTY_KEY_VALUE)) { @@ -393,8 +392,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M public KEY_TYPE POLL_LAST_ENTRY_KEY() { if(size == 0) throw new NoSuchElementException(); int pos = lastIndex; - lastIndex = (int)(links[pos] >>> 32); - if(0 <= lastIndex) links[lastIndex] |= 0xFFFFFFFFL; + onNodeRemoved(pos); KEY_TYPE result = keys[pos]; size--; if(strategy.equals(result, EMPTY_KEY_VALUE)) { @@ -455,7 +453,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M @Override public void clearAndTrim(int size) { int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor))); - if(request >= size) { + if(request >= nullIndex) { clear(); return; } diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/OpenCustomHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/OpenCustomHashMap.template index 5241aeeb..b2f4c914 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/OpenCustomHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/OpenCustomHashMap.template @@ -394,7 +394,11 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL @Override public boolean remove(Object key, Object value) { Objects.requireNonNull(value); +#if TYPE_OBJECT if(key == null) { +#else + if(key == null || (key instanceof CLASS_TYPE && strategy.equals(CLASS_TO_KEY(key), EMPTY_KEY_VALUE))) { +#endif if(containsNull && EQUALS_VALUE_TYPE(values[nullIndex], value)) { removeNullIndex(); return true; @@ -628,7 +632,7 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL @Override public boolean trim(int size) { int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor))); - if(request >= size || this.size > Math.min((int)Math.ceil(request * loadFactor), request - 1)) return false; + if(request >= nullIndex || this.size > Math.min((int)Math.ceil(request * loadFactor), request - 1)) return false; try { rehash(request); } @@ -639,7 +643,7 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL @Override public void clearAndTrim(int size) { int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor))); - if(request >= size) { + if(request >= nullIndex) { clear(); return; } @@ -669,6 +673,9 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL protected int findIndex(CLASS_TYPE key) { if(key == null) return containsNull ? nullIndex : -(nullIndex + 1); KEY_TYPE keyType = CLASS_TO_KEY(key); +#if !TYPE_OBJECT + if(strategy.equals(keyType, EMPTY_KEY_VALUE)) return containsNull ? nullIndex : -(nullIndex + 1); +#endif int pos = HashUtil.mix(strategy.hashCode(keyType)) & mask; KEY_TYPE current = keys[pos]; if(!strategy.equals(current, EMPTY_KEY_VALUE)) { diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/LinkedOpenHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/LinkedOpenHashMap.template index 7ff28314..f52b091a 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/LinkedOpenHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/LinkedOpenHashMap.template @@ -373,8 +373,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G public KEY_TYPE POLL_FIRST_ENTRY_KEY() { if(size == 0) throw new NoSuchElementException(); int pos = firstIndex; - firstIndex = (int)links[pos]; - if(0 <= firstIndex) links[firstIndex] |= 0xFFFFFFFF00000000L; + onNodeRemoved(pos); KEY_TYPE result = keys[pos]; size--; if(KEY_EQUALS_NULL(result)) { @@ -397,8 +396,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G public KEY_TYPE POLL_LAST_ENTRY_KEY() { if(size == 0) throw new NoSuchElementException(); int pos = lastIndex; - lastIndex = (int)(links[pos] >>> 32); - if(0 <= lastIndex) links[lastIndex] |= 0xFFFFFFFFL; + onNodeRemoved(pos); KEY_TYPE result = keys[pos]; size--; if(KEY_EQUALS_NULL(result)) { @@ -459,7 +457,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G @Override public void clearAndTrim(int size) { int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor))); - if(request >= size) { + if(request >= nullIndex) { clear(); return; } diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/OpenHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/OpenHashMap.template index d32d0dce..7bea7f9f 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/OpenHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/OpenHashMap.template @@ -362,7 +362,11 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE @Override public boolean remove(Object key, Object value) { Objects.requireNonNull(value); +#if TYPE_OBJECT if(key == null) { +#else + if(key == null || (key instanceof CLASS_TYPE && KEY_EQUALS_NULL(CLASS_TO_KEY(key)))) { +#endif if(containsNull && EQUALS_VALUE_TYPE(values[nullIndex], value)) { removeNullIndex(); return true; @@ -589,7 +593,7 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE @Override public boolean trim(int size) { int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor))); - if(request >= size || this.size > Math.min((int)Math.ceil(request * loadFactor), request - 1)) return false; + if(request >= nullIndex || this.size >= Math.min((int)Math.ceil(request * loadFactor), request - 1)) return false; try { rehash(request); } @@ -600,7 +604,7 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE @Override public void clearAndTrim(int size) { int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor))); - if(request >= size) { + if(request >= nullIndex) { clear(); return; } @@ -629,6 +633,9 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE #endif protected int findIndex(Object key) { if(key == null) return containsNull ? nullIndex : -(nullIndex + 1); +#if !TYPE_OBJECT + if(key instanceof CLASS_TYPE && KEY_EQUALS_NULL(CLASS_TO_KEY(key))) return containsNull ? nullIndex : -(nullIndex + 1); +#endif int pos = HashUtil.mix(key.hashCode()) & mask; KEY_TYPE current = keys[pos]; if(KEY_EQUALS_NOT_NULL(current)) { diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/immutable/ImmutableOpenHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/immutable/ImmutableOpenHashMap.template index 11a5754b..780cac6c 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/immutable/ImmutableOpenHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/immutable/ImmutableOpenHashMap.template @@ -517,6 +517,9 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ #endif protected int findIndex(Object key) { if(key == null) return containsNull ? nullIndex : -(nullIndex + 1); +#if !TYPE_OBJECT + if(KEY_EQUALS_NULL(CLASS_TO_KEY(key))) return containsNull ? nullIndex : -(nullIndex + 1); +#endif int pos = HashUtil.mix(key.hashCode()) & mask; KEY_TYPE current = keys[pos]; if(KEY_EQUALS_NOT_NULL(current)) { diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template index 55630145..6aca5e25 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template @@ -1233,6 +1233,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN public EntryIterator() {} public EntryIterator(KEY_TYPE from) { index = findIndex(from); + if(index == -1) throw new NoSuchElementException(); } @Override @@ -1263,6 +1264,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN public KeyIterator() {} public KeyIterator(KEY_TYPE element) { index = findIndex(element); + if(index == -1) throw new NoSuchElementException(); } @Override diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/EnumMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/EnumMap.template index 4879f170..615e4346 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/EnumMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/EnumMap.template @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.function.Consumer; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION; @@ -76,8 +77,8 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE if(keys.length != values.length) throw new IllegalArgumentException("Keys and Values have to be the same size"); keyType = keys[0].getDeclaringClass(); this.keys = getKeyUniverse(keyType); - this.values = NEW_VALUE_ARRAY(keys.length); - present = new long[((keys.length - 1) >> 6) + 1]; + this.values = NEW_VALUE_ARRAY(this.keys.length); + present = new long[((this.keys.length - 1) >> 6) + 1]; putAll(keys, values); } @@ -93,8 +94,8 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE if(keys.length != values.length) throw new IllegalArgumentException("Keys and Values have to be the same size"); keyType = keys[0].getDeclaringClass(); this.keys = getKeyUniverse(keyType); - this.values = NEW_VALUE_ARRAY(keys.length); - present = new long[((keys.length - 1) >> 6) + 1]; + this.values = NEW_VALUE_ARRAY(this.keys.length); + present = new long[((this.keys.length - 1) >> 6) + 1]; putAll(keys, values); } @@ -530,6 +531,14 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE return false; } + @Override + public void forEach(Consumer action) { + if(size() <= 0) return; + for(int i = 0,m=keys.length;i iterator() { return new EntryIterator(); diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/LinkedEnumMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/LinkedEnumMap.template index c4af00c1..9531720a 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/LinkedEnumMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/LinkedEnumMap.template @@ -66,9 +66,9 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA if(keys.length != values.length) throw new IllegalArgumentException("Keys and Values have to be the same size"); keyType = keys[0].getDeclaringClass(); this.keys = getKeyUniverse(keyType); - this.values = NEW_VALUE_ARRAY(keys.length); - present = new long[((keys.length - 1) >> 6) + 1]; - links = new long[keys.length]; + this.values = NEW_VALUE_ARRAY(this.keys.length); + present = new long[((this.keys.length - 1) >> 6) + 1]; + links = new long[this.keys.length]; putAll(keys, values); } @@ -84,9 +84,9 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA if(keys.length != values.length) throw new IllegalArgumentException("Keys and Values have to be the same size"); keyType = keys[0].getDeclaringClass(); this.keys = getKeyUniverse(keyType); - this.values = NEW_VALUE_ARRAY(keys.length); - present = new long[((keys.length - 1) >> 6) + 1]; - links = new long[keys.length]; + this.values = NEW_VALUE_ARRAY(this.keys.length); + present = new long[((this.keys.length - 1) >> 6) + 1]; + links = new long[this.keys.length]; putAll(keys, values); } @@ -239,7 +239,7 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA @Override public VALUE_TYPE getAndMoveToFirst(T key) { int index = key.ordinal(); - if(index < 0) return getDefaultReturnValue(); + if(!isSet(index)) return getDefaultReturnValue(); moveToFirstIndex(index); return values[index]; } @@ -247,7 +247,7 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA @Override public VALUE_TYPE getAndMoveToLast(T key) { int index = key.ordinal(); - if(index < 0) return getDefaultReturnValue(); + if(!isSet(index)) return getDefaultReturnValue(); moveToLastIndex(index); return values[index]; } @@ -828,6 +828,7 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA previous = from.ordinal() - 1; index = from.ordinal(); next = from.ordinal(); + if(!isSet(index)) throw new NoSuchElementException(); } public boolean hasNext() { diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/AVLTreeMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/AVLTreeMap.template index 7233cf86..08c52405 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/AVLTreeMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/AVLTreeMap.template @@ -1578,13 +1578,15 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public VALUE_TYPE FIRST_ENTRY_VALUE() { Node KEY_VALUE_GENERIC_TYPE entry = subLowest(); - return entry == null ? map.getDefaultReturnValue() : entry.value; + if(entry == null) throw new NoSuchElementException(); + return entry.value; } @Override public VALUE_TYPE LAST_ENTRY_VALUE() { Node KEY_VALUE_GENERIC_TYPE entry = subHighest(); - return entry == null ? map.getDefaultReturnValue() : entry.value; + if(entry == null) throw new NoSuchElementException(); + return entry.value; } @Override diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template index bcdc169a..09bc91b0 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template @@ -1645,13 +1645,15 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public VALUE_TYPE FIRST_ENTRY_VALUE() { Node KEY_VALUE_GENERIC_TYPE entry = subLowest(); - return entry == null ? map.getDefaultReturnValue() : entry.value; + if(entry == null) throw new NoSuchElementException(); + return entry.value; } @Override public VALUE_TYPE LAST_ENTRY_VALUE() { Node KEY_VALUE_GENERIC_TYPE entry = subHighest(); - return entry == null ? map.getDefaultReturnValue() : entry.value; + if(entry == null) throw new NoSuchElementException(); + return entry.value; } @Override diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/Map.template b/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/Map.template index 80f96a3e..1dcce952 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/Map.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/Map.template @@ -557,8 +557,8 @@ public interface MAP KEY_VALUE_GENERIC_TYPE extends Map size) a[size] = null; return a; } - -// Disabled until a Proper Implementation can be thought out or it is decided that the interface is thrown out. -// private class SubSet extends ABSTRACT_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE { -// int offset; -// int length; -// -// SubSet(int offset, int length) { -// this.offset = offset; -// this.length = length; -// } -// -// int end() { return offset+length; } -// -// @Override -// public boolean add(KEY_TYPE o) { -// int index = findIndex(o); -// if(index == -1) { -// if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2); -// if(end() != size) System.arraycopy(data, end(), data, end()+1, size-(offset+length)); -// data[end()] = o; -// size++; -// length++; -// return true; -// } -// return false; -// } -// -// @Override -// public boolean addAndMoveToFirst(KEY_TYPE o) { -// int index = findIndex(o); -// if(index == -1) { -// if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2); -// System.arraycopy(data, offset, data, offset+1, size-offset); -// data[offset] = o; -// size++; -// length++; -// return true; -// } -// else if(index != 0) { -// o = data[index]; -// System.arraycopy(data, offset, data, offset+1, index-offset); -// data[offset] = o; -// } -// return false; -// } -// -// @Override -// public boolean addAndMoveToLast(KEY_TYPE o) { -// int index = findIndex(o); -// if(index == -1) { -// if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2); -// System.arraycopy(data, end()+1, data, end(), size-end()); -// data[end()] = o; -// size++; -// length++; -// return true; -// } -// else if(index != 0) { -// o = data[index]; -// System.arraycopy(data, offset+1, data, offset, index-offset); -// data[offset+length] = o; -// } -// return false; -// } -// -// @Override -// public boolean moveToFirst(KEY_TYPE o) { -// int index = findIndex(o); -// if(index > offset) { -// o = data[index]; -// System.arraycopy(data, offset, data, offset+1, index-offset); -// data[offset] = o; -// return true; -// } -// return false; -// } -// -// @Override -// public boolean moveToLast(KEY_TYPE o) { -// int index = findIndex(o); -// if(index != -1 && index < end()-1) { -// o = data[index]; -// System.arraycopy(data, index+1, data, index, end()-index-1); -// data[end()-1] = o; -// return true; -// } -// return false; -// } -// -//#if !TYPE_OBJECT -// @Override -// public boolean contains(KEY_TYPE e) { -// return findIndex(e) != -1; -// } -// -//#endif -// @Override -// public boolean contains(Object e) { -// return findIndex(e) != -1; -// } -// -// @Override -// public KEY_TYPE FIRST_KEY() { -// if(length == 0) throw new NoSuchElementException(); -// return data[offset]; -// } -// -// @Override -// public KEY_TYPE LAST_KEY() { -// if(length == 0) throw new NoSuchElementException(); -// return data[end()-1]; -// } -// -//#if !TYPE_OBJECT -// @Override -// public boolean remove(KEY_TYPE o) { -// int index = findIndex(o); -// if(index != -1) { -// size--; -// length--; -// if(index != size) System.arraycopy(data, index+1, data, index, size - index); -// return true; -// } -// return false; -// } -// -//#endif -// @Override -// public boolean remove(Object o) { -// int index = findIndex(o); -// if(index != -1) { -// size--; -// length--; -// if(index != size) System.arraycopy(data, index+1, data, index, size - index); -//#if TYPE_OBJECT -// data[size] = EMPTY_KEY_VALUE; -//#endif -// return true; -// } -// return false; -// } -// -// @Override -// public KEY_TYPE POLL_FIRST_KEY() { -// if(length == 0) throw new NoSuchElementException(); -// size--; -// length--; -// KEY_TYPE result = data[offset]; -// System.arraycopy(data, offset+1, data, offset, size-offset); -//#if TYPE_OBJECT -// data[size] = EMPTY_KEY_VALUE; -//#endif -// return result; -// } -// -// @Override -// public KEY_TYPE POLL_LAST_KEY() { -// if(length == 0) throw new NoSuchElementException(); -// KEY_TYPE result = data[offset+length]; -// size--; -// length--; -// System.arraycopy(data, end()+1, data, end(), size-end()); -//#if TYPE_OBJECT -// data[size] = EMPTY_KEY_VALUE; -//#endif -// return result; -// } -// -// @Override -// public COMPARATOR KEY_GENERIC_TYPE comparator() { -// return null; -// } -// -// @Override -// public BI_ITERATOR KEY_GENERIC_TYPE iterator() { -// return new SetIterator(offset); -// } -// -// @Override -// public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { -// int index = findIndex(fromElement); -// if(index != -1) return new SetIterator(index); -// throw new NoSuchElementException(); -// } -// -// @Override -// public SubSet copy() { throw new UnsupportedOperationException(); } -// -// @Override -// public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { -// int fromIndex = findIndex(fromElement); -// int toIndex = findIndex(toElement); -// if(fromIndex == -1 || toIndex == -1) throw new NoSuchElementException(); -// return new SubSet(fromIndex, toIndex - fromIndex + 1); -// } -// -// @Override -// public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { -// int toIndex = findIndex(toElement); -// if(toIndex == -1) throw new NoSuchElementException(); -// return new SubSet(0, toIndex+1); -// } -// -// @Override -// public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { -// int fromIndex = findIndex(fromElement); -// if(fromIndex == -1) throw new NoSuchElementException(); -// return new SubSet(fromIndex, size - fromIndex); -// } -// -// @Override -// public int size() { -// return length; -// } -// -//#if !TYPE_OBJECT -// @Override -// public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) { -// if(a == null || a.length < size()) return Arrays.copyOfRange(data, offset, end()); -// System.arraycopy(data, offset, a, 0, size()); -// return a; -// } -// -//#endif -// @Override -// @Deprecated -// public Object[] toArray() { -// Object[] obj = new Object[size()]; -// for(int i = 0;i E[] toArray(E[] a) { -// if(a == null) a = (E[])new Object[size()]; -// else if(a.length < size()) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size()); -// for(int i = 0;i=0;i--) -// if(KEY_EQUALS(data[offset+i], o)) return i + offset; -// return -1; -// } -// -//#endif -// protected int findIndex(Object o) { -// for(int i = length-1;i>=0;i--) -// if(EQUALS_KEY_TYPE(data[offset+i], o)) return i + offset; -// return -1; -// } -// -// private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE { -// int index; -// int lastReturned = -1; -// -// public SetIterator(int index) { -// this.index = index; -// } -// -// @Override -// public boolean hasNext() { -// return index < size(); -// } -// -// @Override -// public KEY_TYPE NEXT() { -// if(!hasNext()) throw new NoSuchElementException(); -// lastReturned = index; -// return data[index++]; -// } -// -// @Override -// public boolean hasPrevious() { -// return index > 0; -// } -// -// @Override -// public KEY_TYPE PREVIOUS() { -// if(!hasPrevious()) throw new NoSuchElementException(); -// lastReturned = index; -// return data[index--]; -// } -// -// @Override -// public int nextIndex() { -// return index; -// } -// -// @Override -// public int previousIndex() { -// return index-1; -// } -// -// @Override -// public void remove() { -// if(lastReturned == -1) -// throw new IllegalStateException(); -// SubSet.this.remove(data[lastReturned]); -// if(lastReturned < index) -// index--; -// lastReturned = -1; -// } -// -// #if TYPE_OBJECT -// @Override -// public void set(Object e) { throw new UnsupportedOperationException(); } -// -// @Override -// public void add(Object e) { throw new UnsupportedOperationException(); } -// -// #else -// @Override -// public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); } -// -// @Override -// public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } -// -// #endif -// @Override -// public int skip(int amount) { -// if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed"); -// int steps = Math.min(amount, (size() - 1) - index); -// index += steps; -// return steps; -// } -// -// @Override -// public int back(int amount) { -// if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed"); -// int steps = Math.min(amount, index); -// index -= steps; -// return steps; -// } -// } -// } - + private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE { int index; int lastReturned = -1; diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/ImmutableOpenHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/ImmutableOpenHashSet.template index 90ebecf2..2b2a4e7d 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/ImmutableOpenHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/ImmutableOpenHashSet.template @@ -510,6 +510,7 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI next = (int)(links[current]); result++; } + if(index >= 0) index+=result; return result; } @@ -521,6 +522,7 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI previous = (int)(links[current] >> 32); result++; } + if(index >= 0) index-=result; return result; } diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template index 8b91d54a..028c9438 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template @@ -123,7 +123,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY * @throws IllegalStateException if offset and length causes to step outside of the arrays range */ public LINKED_CUSTOM_HASH_SET(KEY_TYPE[] array, int offset, int length, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) { - this(length < 0 ? 0 : length, strategy); + this(length, strategy); SanityChecks.checkArrayCapacity(array.length, offset, length); for(int i = 0;i>> 32); - if(0 <= lastIndex) links[lastIndex] |= 0xFFFFFFFFL; + onNodeRemoved(pos); KEY_TYPE result = keys[pos]; size--; if(strategy.equals(result, EMPTY_KEY_VALUE)) { @@ -510,7 +508,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY @Override public void clearAndTrim(int size) { int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor))); - if(request >= size) { + if(request >= nullIndex) { clear(); return; } @@ -725,6 +723,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY next = (int)(links[current]); result++; } + if(index >= 0) index+=result; return result; } @@ -736,6 +735,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY previous = (int)(links[current] >> 32); result++; } + if(index >= 0) index-=result; return result; } diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template index 305b90d7..f7d928dd 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template @@ -110,7 +110,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE * @throws IllegalStateException if offset and length causes to step outside of the arrays range */ public LINKED_HASH_SET(KEY_TYPE[] array, int offset, int length, float loadFactor) { - this(length < 0 ? 0 : length); + this(length); SanityChecks.checkArrayCapacity(array.length, offset, length); for(int i = 0;i>> 32); - if(0 <= lastIndex) links[lastIndex] |= 0xFFFFFFFFL; + onNodeRemoved(pos); KEY_TYPE result = keys[pos]; size--; if(KEY_EQUALS_NULL(result)) { @@ -601,7 +599,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE @Override public void clearAndTrim(int size) { int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor))); - if(request >= size) { + if(request >= nullIndex) { clear(); return; } @@ -686,6 +684,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE next = (int)(links[current]); result++; } + if(index >= 0) index+=result; return result; } @@ -697,6 +696,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE previous = (int)(links[current] >> 32); result++; } + if(index >= 0) index-=result; return result; } diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template index 00af4a1c..b1e7a9d9 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template @@ -373,10 +373,10 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T #endif @Override public boolean trim(int size) { - int newSize = HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)); - if(newSize >= nullIndex || size >= Math.min((int)Math.ceil(newSize * loadFactor), newSize - 1)) return false; + int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor))); + if(request >= nullIndex || this.size >= Math.min((int)Math.ceil(request * loadFactor), request - 1)) return false; try { - rehash(newSize); + rehash(request); } catch(OutOfMemoryError e) { return false; } return true; @@ -385,7 +385,7 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T @Override public void clearAndTrim(int size) { int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor))); - if(request >= size) { + if(request >= nullIndex) { clear(); return; } diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template index 4511e03f..5cac2a92 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template @@ -340,10 +340,10 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp #endif @Override public boolean trim(int size) { - int newSize = HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor)); - if(newSize >= nullIndex || size >= Math.min((int)Math.ceil(newSize * loadFactor), newSize - 1)) return false; + int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor))); + if(request >= nullIndex || this.size >= Math.min((int)Math.ceil(request * loadFactor), request - 1)) return false; try { - rehash(newSize); + rehash(request); } catch(OutOfMemoryError e) { return false; } return true; @@ -352,7 +352,7 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp @Override public void clearAndTrim(int size) { int request = Math.max(minCapacity, HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor))); - if(request >= size) { + if(request >= nullIndex) { clear(); return; } diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template index 5841a052..b6cc387e 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template @@ -949,15 +949,11 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE @Override public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; } - public KEY_TYPE getDefaultMaxValue() { return super.getDefaultMinValue(); } - - public KEY_TYPE getDefaultMinValue() { return super.getDefaultMaxValue(); } - @Override public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { if(!inRange(fromElement, fromInclusive)) throw new IllegalArgumentException("fromElement out of range"); if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement out of range"); - return new DescendingSubSetBRACES(set, false, fromElement, fromInclusive, false, toElement, toInclusive); + return new DescendingSubSetBRACES(set, false, toElement, toInclusive, false, fromElement, fromInclusive); } @Override @@ -1034,21 +1030,15 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE #if !TYPE_OBJECT @Override public void setDefaultMaxValue(KEY_TYPE value) { set.setDefaultMaxValue(value); } - @Override public KEY_TYPE getDefaultMaxValue() { return set.getDefaultMaxValue(); } - @Override public void setDefaultMinValue(KEY_TYPE value) { set.setDefaultMinValue(value); } - @Override public KEY_TYPE getDefaultMinValue() { return set.getDefaultMinValue(); } - #else public KEY_TYPE getDefaultMaxValue() { return null; } - public KEY_TYPE getDefaultMinValue() { return null; } - #endif @Override public abstract BI_ITERATOR KEY_GENERIC_TYPE iterator();