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 b12af83..7a9084c 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template @@ -921,6 +921,20 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE return true; } + /** + * Trims the collection down to the requested size and clears all elements while doing so + * @param size the amount of elements that should be allowed + * @note this will enforce minimum size of the collection itself + */ + @Override + public void clearAndTrim(int size) { + if(data.length <= size) { + clear(); + return; + } + data = size == 0 ? EMPTY_KEY_ARRAY : NEW_KEY_ARRAY(size); + } + /** * Increases the capacity of this implementation instance, if necessary, * to ensure that it can hold at least the number of elements specified by diff --git a/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template b/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template index 6337a0e..1e179d4 100644 --- a/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template +++ b/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template @@ -237,6 +237,22 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G return true; } + /** + * Trims the collection down to the requested size and clears all elements while doing so + * @param size the amount of elements that should be allowed + * @note this will enforce minimum size of the collection itself + */ + @Override + public void clearAndTrim(int size) { + int newSize = Math.max(MIN_CAPACITY, size); + if(array.length <= newSize) { + clear(); + return; + } + first = last = 0; + array = NEW_KEY_ARRAY(newSize); + } + @Override public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] input) { if(input == null || input.length < size()) input = NEW_KEY_ARRAY(size()); 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 fb8fad8..2a67b26 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template @@ -501,6 +501,21 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY firstIndex = lastIndex = -1; } + @Override + public void clearAndTrim(int size) { + if(nullIndex <= size) { + clear(); + return; + } + nullIndex = size; + mask = nullIndex - 1; + maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1); + keys = NEW_KEY_ARRAY(nullIndex + 1); + links = new long[nullIndex + 1]; + firstIndex = -1; + lastIndex = -1; + } + @Override public LIST_ITERATOR KEY_GENERIC_TYPE iterator() { return new SetIterator(); 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 9adfb5e..89bd7a5 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template @@ -473,6 +473,21 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE firstIndex = lastIndex = -1; } + @Override + public void clearAndTrim(int size) { + if(nullIndex <= size) { + clear(); + return; + } + nullIndex = size; + mask = nullIndex - 1; + maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1); + keys = NEW_KEY_ARRAY(nullIndex + 1); + links = new long[nullIndex + 1]; + firstIndex = -1; + lastIndex = -1; + } + @Override public LIST_ITERATOR KEY_GENERIC_TYPE iterator() { return new SetIterator(); 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 1966e58..0942970 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template @@ -348,6 +348,18 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T return true; } + @Override + public void clearAndTrim(int size) { + if(nullIndex <= size) { + clear(); + return; + } + nullIndex = size; + mask = nullIndex - 1; + maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1); + keys = NEW_KEY_ARRAY(nullIndex + 1); + } + private void ensureCapacity(int newCapacity) { int size = HashUtil.arraySize(newCapacity, loadFactor); if(size > nullIndex) rehash(size); 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 ebd34d4..63cce66 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template @@ -306,6 +306,18 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp return true; } + @Override + public void clearAndTrim(int size) { + if(nullIndex <= size) { + clear(); + return; + } + nullIndex = size; + mask = nullIndex - 1; + maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1); + keys = NEW_KEY_ARRAY(nullIndex + 1); + } + private void ensureCapacity(int newCapacity) { int size = HashUtil.arraySize(newCapacity, loadFactor); if(size > nullIndex) rehash(size); diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template b/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template index ce12180..9f7a6fb 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template @@ -146,6 +146,9 @@ public class LISTS @Override public boolean trim(int size) { synchronized(mutex) { return l.trim(size); } } + @Override + public void clearAndTrim(int size) { synchronized(mutex) { l.clearAndTrim(size); } } + @Override public KEY_TYPE[] elements() { synchronized(mutex) { return l.elements(); } } diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Sets.template b/src/builder/resources/speiger/assets/collections/templates/utils/Sets.template index feb5b32..e33ff28 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Sets.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Sets.template @@ -358,6 +358,9 @@ public class SETS @Override public boolean trim(int size) { synchronized(mutex) { return trim.trim(size); } } + + @Override + public void clearAndTrim(int size) { synchronized(mutex) { trim.clearAndTrim(size); } } } private static class SynchronizedNavigableSet KEY_GENERIC_TYPE extends SynchronizedSortedSet KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE @@ -450,6 +453,9 @@ public class SETS @Override public boolean trim(int size) { synchronized(mutex) { return trim.trim(size); } } + + @Override + public void clearAndTrim(int size) { synchronized(mutex) { trim.clearAndTrim(size); } } } private static class SynchronizedSortedSet KEY_GENERIC_TYPE extends SynchronizedSet KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE @@ -525,6 +531,9 @@ public class SETS @Override public boolean trim(int size) { synchronized(mutex) { return trim.trim(size); } } + + @Override + public void clearAndTrim(int size) { synchronized(mutex) { trim.clearAndTrim(size); } } } private static class SynchronizedSet KEY_GENERIC_TYPE extends SynchronizedCollection KEY_GENERIC_TYPE implements SET KEY_GENERIC_TYPE diff --git a/src/main/java/speiger/src/collections/utils/ITrimmable.java b/src/main/java/speiger/src/collections/utils/ITrimmable.java index d7af6ce..8026ee1 100644 --- a/src/main/java/speiger/src/collections/utils/ITrimmable.java +++ b/src/main/java/speiger/src/collections/utils/ITrimmable.java @@ -20,4 +20,18 @@ public interface ITrimmable * @return if the internal array has been trimmed. */ public boolean trim(int size); + + /** + * Trims the collection down to the original size and clears all present elements with it. + */ + public default void clearAndTrim() { + clearAndTrim(0); + } + + /** + * Trims the collection down to the requested size and clears all elements while doing so + * @param size the amount of elements that should be allowed + * @note this will enforce minimum size of the collection itself + */ + public void clearAndTrim(int size); }