From b90a9ec7d826ce54a13895e01cafee1e799c76ee Mon Sep 17 00:00:00 2001 From: Speiger Date: Sun, 24 Oct 2021 01:11:11 +0200 Subject: [PATCH] Added new Remove/RetainAll function to Collection --- Changelog.md | 3 ++ .../collections/AbstractCollection.template | 46 ++++++++++++++++++ .../templates/collections/Collection.template | 25 ++++++++++ .../templates/lists/ArrayList.template | 37 +++++++++++++++ .../templates/lists/ImmutableList.template | 4 ++ .../templates/lists/LinkedList.template | 47 +++++++++++++++++++ .../templates/sets/ArraySet.template | 30 ++++++++++++ .../templates/utils/Collections.template | 8 ++++ 8 files changed, 200 insertions(+) diff --git a/Changelog.md b/Changelog.md index 7fb41a26..6e4f56dd 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,8 @@ # Changelog of versions +### Version 0.4.5 +- Added: removeAll/retainAll(Collection c, Consumer r) which receives all the elements that got deleted from the collection + ### Version 0.4.4 - Fixed: ObjectArrayList.of was causing crashes because of a Poor implementation. - Added: Unsorted HashMaps/Sets now throw Concurrent exceptions if they were modified during a rehash. diff --git a/src/builder/resources/speiger/assets/collections/templates/collections/AbstractCollection.template b/src/builder/resources/speiger/assets/collections/templates/collections/AbstractCollection.template index 6f2de724..1ec6f1f7 100644 --- a/src/builder/resources/speiger/assets/collections/templates/collections/AbstractCollection.template +++ b/src/builder/resources/speiger/assets/collections/templates/collections/AbstractCollection.template @@ -3,9 +3,13 @@ package speiger.src.collections.PACKAGE.collections; import java.util.Collection; import java.util.Objects; import java.util.AbstractCollection; +#if TYPE_OBJECT +import java.util.function.Consumer; +#endif import speiger.src.collections.PACKAGE.collections.COLLECTION; #if !TYPE_OBJECT +import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.utils.ITERATORS; #endif @@ -79,6 +83,7 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle @Override public boolean containsAll(COLLECTION KEY_GENERIC_TYPE c) { Objects.requireNonNull(c); + if(c.isEmpty()) return true; for(ITERATOR KEY_GENERIC_TYPE iter = c.iterator();iter.hasNext();) if(!contains(iter.NEXT())) return false; @@ -96,6 +101,7 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle @Primitive public boolean containsAny(Collection c) { Objects.requireNonNull(c); + if(c.isEmpty()) return false; for(Object e : c) if(contains(e)) return true; @@ -111,6 +117,7 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle @Override public boolean containsAny(COLLECTION KEY_GENERIC_TYPE c) { Objects.requireNonNull(c); + if(c.isEmpty()) return false; for(ITERATOR KEY_GENERIC_TYPE iter = c.iterator();iter.hasNext();) if(contains(iter.NEXT())) return true; @@ -153,6 +160,7 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle @Override public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) { Objects.requireNonNull(c); + if(c.isEmpty()) return false; boolean modified = false; for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) { if(c.contains(iter.NEXT())) { @@ -162,6 +170,23 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle } return modified; } + + @Override + public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { + Objects.requireNonNull(c); + if(c.isEmpty()) return false; + Objects.requireNonNull(r); + boolean modified = false; + for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) { + KEY_TYPE e = iter.NEXT(); + if(c.contains(e)) { + r.accept(e); + iter.remove(); + modified = true; + } + } + return modified; + } /** * A Type-Specific implementation of retainAll. This Implementation iterates over all elements and removes them as they were not found in the other collection. @@ -187,6 +212,27 @@ public abstract class ABSTRACT_COLLECTION KEY_GENERIC_TYPE extends AbstractColle return modified; } + @Override + public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { + Objects.requireNonNull(c); + Objects.requireNonNull(r); + if(c.isEmpty()) { + boolean modified = !isEmpty(); + forEach(r); + clear(); + return modified; + } + boolean modified = false; + for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) { + KEY_TYPE e = iter.NEXT(); + if(!c.contains(e)) { + iter.remove(); + modified = true; + } + } + return modified; + } + #if !TYPE_OBJECT /** * A Type-Specific implementation of toArray that links to {@link #TO_ARRAY(KEY_TYPE[])} with a newly created array. diff --git a/src/builder/resources/speiger/assets/collections/templates/collections/Collection.template b/src/builder/resources/speiger/assets/collections/templates/collections/Collection.template index 24b14a4b..d42ce308 100644 --- a/src/builder/resources/speiger/assets/collections/templates/collections/Collection.template +++ b/src/builder/resources/speiger/assets/collections/templates/collections/Collection.template @@ -8,6 +8,11 @@ import java.util.function.Predicate; import java.util.stream.JAVA_STREAM; import java.util.stream.StreamSupport; #endif +#if TYPE_OBJECT +import java.util.function.Consumer; +#else +import speiger.src.collections.PACKAGE.functions.CONSUMER; +#endif import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS; import speiger.src.collections.utils.SanityChecks; @@ -115,6 +120,16 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection, ITE */ public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c); + /** + * A Type-Specific removeAll function that reduces (un)boxing. + * It also notifies the remover of which exact element is going to be removed. + * @param c the collection of elements that should be removed + * @param r elements that got removed + * @return true if any element was removed + * @see Collection#removeAll(Collection) + */ + public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r); + /** * A Type-Specific retainAll function that reduces (un)boxing. * @param c the collection of elements that should be kept @@ -123,6 +138,16 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection, ITE */ public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c); + /** + * A Type-Specific retainAll function that reduces (un)boxing. + * It also notifies the remover of which exact element is going to be removed. + * @param c the collection of elements that should be kept + * @param r elements that got removed + * @return true if any element was removed + * @see Collection#retainAll(Collection) + */ + public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r); + /** * A Function that does a shallow clone of the Collection itself. * This function is more optimized then a copy constructor since the Collection does not have to be unsorted/resorted. 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 ee0579f6..c5422df5 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template @@ -864,6 +864,22 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE return modified; } + @Override + public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { + if(c.isEmpty()) return false; + int j = 0; + for(int i = 0;i 0; + forEach(r); + clear(); + return modifed; + } + int j = 0; + for(int i = 0;i 0; + forEach(r); + clear(); + return changed; + } + boolean modified = false; + int j = 0; + for(Entry KEY_GENERIC_TYPE entry = first;entry != null;) { + if(!c.contains(entry.value)) { + r.accept(entry.value); + Entry KEY_GENERIC_TYPE next = entry.next; + unlink(entry); + entry = next; + modified = true; + continue; + } + else j++; + entry = entry.next; + } + size = j; + return modified; + } + @Override @Primitive public boolean removeIf(Predicate filter) { diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template b/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template index 253c3a7c..93d246f9 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template @@ -227,6 +227,21 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im return result; } + @Override + public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { + int j = 0; + for(int i = 0;i c) { diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template b/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template index 089ac445..9c187ad9 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template @@ -148,7 +148,11 @@ public class COLLECTIONS @Override public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) { synchronized(mutex) { return this.c.removeAll(c); } } @Override + public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { synchronized(mutex) { return this.c.removeAll(c, r); } } + @Override public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c) { synchronized(mutex) { return this.c.retainAll(c); } } + @Override + public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { synchronized(mutex) { return this.c.retainAll(c, r); } } #if PRIMITIVES @Override public boolean remIf(JAVA_PREDICATE filter){ synchronized(mutex) { return c.remIf(filter); } } @@ -258,7 +262,11 @@ public class COLLECTIONS @Override public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); } @Override + public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { throw new UnsupportedOperationException(); } + @Override public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); } + @Override + public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c, CONSUMER KEY_GENERIC_TYPE r) { throw new UnsupportedOperationException(); } #if PRIMITIVES @Override public boolean remIf(JAVA_PREDICATE filter){ throw new UnsupportedOperationException(); }