diff --git a/Changelog.md b/Changelog.md index bdb50bb..fee4711 100644 --- a/Changelog.md +++ b/Changelog.md @@ -6,6 +6,7 @@ - Added: Java Iterator/Iterable support for Stream replacing methods - Added: Suppliers. - Added: SupplyIfAbsent but value generator is a supplier +- Added: Count feature into Iterable ### Version 0.4.0 - Changed: Iterable specific helper functions were moved out of Iterators and moved into Iterables diff --git a/README.md b/README.md index d07bb37..10455f8 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ To highlight things that may be wanted. - findFirst: Allows to find the first element of a Predicated Iterable. - filter: Allows to filter unwanted elements for wrapped Iterable - matchAny/matchNone/matchAll: Allows to find elements in a collection. + - count: counts all valid elements in a collection - forEach: Allows to input a second element into a forEach move allowing for more flexibility for Method References - Collection: - containsAny: Allows to test if another collection contains an of the elements of the tested collection. @@ -46,7 +47,7 @@ To highlight things that may be wanted. - addToAll: Same as addTo but bulkVersion. - removeOrDefault: removes a Element and if not present returns the default value instead of the present value. - mergeAll: BulkVersion of Merge function. - - computeIfAbsent: A Supplier based generator that covers a lot more reference method cases + - supplyIfAbsent: A Supplier based computeIfAbsent - Sorted Map: - addAndMoveToFirst/Last (From FastUtil but moved to Interface): Allows to add a element to the first/last position of a sorted Map. - moveToFirst/Last: Moves the desired element at the first/last position of the Map. diff --git a/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template b/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template index 674fa35..b859fa0 100644 --- a/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template +++ b/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template @@ -170,4 +170,18 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable } return EMPTY_VALUE; } + + /** + * Helper function to reduce stream usage that allows to count the valid elements. + * @param filter that should be applied + * @return the amount of Valid Elements + */ + default int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + int result = 0; + for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) { + if(filter.TEST_VALUE(iter.NEXT())) result++; + } + return result; + } } \ No newline at end of file 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 4a04b00..cc91ddd 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template @@ -643,6 +643,16 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE return EMPTY_VALUE; } + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + int result = 0; + for(int i = 0;i filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); + int result = 0; + int index = firstIndex; + while(index != -1) { + entry.set(keys[index], values[index]); + if(filter.getBoolean(entry)) result++; + index = (int)links[index]; + } + return result; + } + @Override @Deprecated public boolean contains(Object o) { @@ -944,6 +959,19 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M return EMPTY_KEY_VALUE; } + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + int index = firstIndex; + while(index != -1){ + if(filter.TEST_VALUE(keys[index])) result++; + index = (int)links[index]; + } + return result; + } + @Override public COMPARATOR KEY_GENERIC_TYPE comparator() { return null; } @@ -1059,6 +1087,19 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M } return EMPTY_VALUE; } + + @Override + public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + int index = firstIndex; + while(index != -1){ + if(filter.VALUE_TEST_VALUE(values[index])) result++; + index = (int)links[index]; + } + return result; + } } private class FastEntryIterator extends MapIterator implements ObjectListIterator { 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 f386001..e4ab8bb 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 @@ -874,6 +874,25 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL return null; } + @Override + public int count(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); + int result = 0; + if(containsNull) { + entry.set(keys[nullIndex], values[nullIndex]); + if(filter.getBoolean(entry)) result++; + } + for(int i = nullIndex-1;i>=0;i--) { + if(!strategy.equals(keys[i], EMPTY_KEY_VALUE)) { + entry.set(keys[i], values[i]); + if(filter.getBoolean(entry)) result++; + } + } + return result; + } + @Override public int size() { return CUSTOM_HASH_MAP.this.size(); @@ -1015,6 +1034,18 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL } return EMPTY_KEY_VALUE; } + + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + if(containsNull && filter.TEST_VALUE(keys[nullIndex])) result++; + for(int i = nullIndex-1;i>=0;i--) { + if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.TEST_VALUE(keys[i])) result++; + } + return result; + } } private class Values extends VALUE_ABSTRACT_COLLECTION VALUE_GENERIC_TYPE { @@ -1111,6 +1142,18 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL } return EMPTY_VALUE; } + + @Override + public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + if(containsNull && filter.VALUE_TEST_VALUE(values[nullIndex])) result++; + for(int i = nullIndex-1;i>=0;i--) { + if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.VALUE_TEST_VALUE(values[i])) result++; + } + return result; + } } private class FastEntryIterator extends MapIterator implements ObjectIterator { 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 d20a3b1..d292f08 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 @@ -717,6 +717,21 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G return null; } + @Override + public int count(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); + int index = firstIndex; + while(index != -1) { + entry.set(keys[index], values[index]); + if(filter.getBoolean(entry)) result++; + index = (int)links[index]; + } + return result; + } + @Override @Deprecated public boolean contains(Object o) { @@ -921,6 +936,19 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G return EMPTY_KEY_VALUE; } + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + int index = firstIndex; + while(index != -1){ + if(filter.TEST_VALUE(keys[index])) return result++; + index = (int)links[index]; + } + return result; + } + @Override public COMPARATOR KEY_GENERIC_TYPE comparator() { return null; } @@ -1037,6 +1065,19 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G } return EMPTY_VALUE; } + + @Override + public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + int index = firstIndex; + while(index != -1){ + if(filter.VALUE_TEST_VALUE(values[index])) result++; + index = (int)links[index]; + } + return result; + } } private class FastEntryIterator extends MapIterator implements ObjectListIterator { 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 f929f7c..a4111e4 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 @@ -834,6 +834,25 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE return null; } + @Override + public int count(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); + int result = 0; + if(containsNull) { + entry.set(keys[nullIndex], values[nullIndex]); + if(filter.getBoolean(entry)) result++; + } + for(int i = nullIndex-1;i>=0;i--) { + if(KEY_EQUALS_NOT_NULL(keys[i])) { + entry.set(keys[i], values[i]); + if(filter.getBoolean(entry)) result++; + } + } + return result; + } + @Override public int size() { return HASH_MAP.this.size(); @@ -975,6 +994,18 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE } return EMPTY_KEY_VALUE; } + + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + if(containsNull && filter.TEST_VALUE(keys[nullIndex])) result++; + for(int i = nullIndex-1;i>=0;i--) { + if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.TEST_VALUE(keys[i])) result++; + } + return result; + } } private class Values extends VALUE_ABSTRACT_COLLECTION VALUE_GENERIC_TYPE { @@ -1071,6 +1102,18 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE } return EMPTY_VALUE; } + + @Override + public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + if(containsNull && filter.VALUE_TEST_VALUE(values[nullIndex])) result++; + for(int i = nullIndex-1;i>=0;i--) { + if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.VALUE_TEST_VALUE(values[i])) result++; + } + return result; + } } private class FastEntryIterator extends MapIterator implements ObjectIterator { 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 5e13c38..f4d2517 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 @@ -629,6 +629,7 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ while(index != -1) { entry.set(keys[index], values[index]); if(filter.getBoolean(entry)) return true; + index = (int)links[index]; } return false; } @@ -642,6 +643,7 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ while(index != -1) { entry.set(keys[index], values[index]); if(filter.getBoolean(entry)) return false; + index = (int)links[index]; } return true; } @@ -655,6 +657,7 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ while(index != -1) { entry.set(keys[index], values[index]); if(!filter.getBoolean(entry)) return false; + index = (int)links[index]; } return true; } @@ -668,10 +671,26 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ while(index != -1) { entry.set(keys[index], values[index]); if(filter.getBoolean(entry)) return entry; + index = (int)links[index]; } return null; } + @Override + public int count(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); + int result = 0; + int index = firstIndex; + while(index != -1) { + entry.set(keys[index], values[index]); + if(filter.getBoolean(entry)) result++; + index = (int)links[index]; + } + return result; + } + @Override @Deprecated public boolean contains(Object o) { @@ -846,6 +865,19 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ return EMPTY_KEY_VALUE; } + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int index = firstIndex; + int result = 0; + while(index != -1){ + if(filter.TEST_VALUE(keys[index])) result++; + index = (int)links[index]; + } + return result; + } + @Override public COMPARATOR KEY_GENERIC_TYPE comparator() { return null; } @@ -959,6 +991,19 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ } return EMPTY_VALUE; } + + @Override + public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + int index = firstIndex; + while(index != -1){ + if(filter.VALUE_TEST_VALUE(values[index])) result++; + index = (int)links[index]; + } + return result; + } } private class FastEntryIterator extends MapIterator implements ObjectListIterator { 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 5d781d8..c7064e4 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 @@ -1206,6 +1206,19 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN return null; } + @Override + public int count(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); + for(int i = 0;i { @@ -1738,6 +1772,19 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN return null; } + @Override + public int count(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); + for(int i = 0;i { 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 0c527f4..e47ef45 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 @@ -1554,6 +1554,19 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ } return null; } + + @Override + public int count(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + subEntry.set(entry.key, entry.value); + if(filter.getBoolean(subEntry)) result++; + } + return result; + } } final class SubMapValues extends VALUE_ABSTRACT_COLLECTION VALUE_GENERIC_TYPE { @@ -1631,6 +1644,15 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ if(filter.VALUE_TEST_VALUE(entry.value)) return entry.value; return EMPTY_VALUE; } + + @Override + public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + int result = 0; + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + if(filter.VALUE_TEST_VALUE(entry.value)) result++; + return result; + } } final class SubMapEntrySetIterator extends SubMapEntryIterator implements ObjectListIterator { @@ -2043,6 +2065,15 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ if(filter.TEST_VALUE(entry.key)) return entry.key; return EMPTY_KEY_VALUE; } + + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + int result = 0; + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + if(filter.TEST_VALUE(entry.key)) result++; + return result; + } } final class Values extends VALUE_ABSTRACT_COLLECTION VALUE_GENERIC_TYPE { @@ -2121,6 +2152,15 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ if(filter.VALUE_TEST_VALUE(entry.value)) return entry.value; return EMPTY_VALUE; } + + @Override + public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + int result = 0; + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + if(filter.VALUE_TEST_VALUE(entry.value)) result++; + return result; + } } final class EntryIterator extends MapEntryIterator implements ObjectListIterator { 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 42e31ef..68ec382 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 @@ -1607,6 +1607,19 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G } return null; } + + @Override + public int count(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + subEntry.set(entry.key, entry.value); + if(filter.getBoolean(subEntry)) result++; + } + return result; + } } final class SubMapValues extends VALUE_ABSTRACT_COLLECTION VALUE_GENERIC_TYPE { @@ -1684,6 +1697,15 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G if(filter.VALUE_TEST_VALUE(entry.value)) return entry.value; return EMPTY_VALUE; } + + @Override + public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + int result = 0; + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + if(filter.VALUE_TEST_VALUE(entry.value)) result++; + return result; + } } final class SubMapEntrySetIterator extends SubMapEntryIterator implements ObjectListIterator { @@ -1919,6 +1941,19 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G } return null; } + + @Override + public int count(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + subEntry.set(entry.key, entry.value); + if(filter.getBoolean(subEntry)) result++; + } + return result; + } } static final class KeySet KEY_VALUE_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE { @@ -2096,6 +2131,15 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G if(filter.TEST_VALUE(entry.key)) return entry.key; return EMPTY_KEY_VALUE; } + + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + int result = 0; + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + if(filter.TEST_VALUE(entry.key)) result++; + return result; + } } final class Values extends VALUE_ABSTRACT_COLLECTION VALUE_GENERIC_TYPE { @@ -2174,6 +2218,15 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G if(filter.VALUE_TEST_VALUE(entry.value)) return entry.value; return EMPTY_VALUE; } + + @Override + public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + int result = 0; + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + if(filter.VALUE_TEST_VALUE(entry.value)) result++; + return result; + } } final class EntryIterator extends MapEntryIterator implements ObjectListIterator { 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 60b3c19..af10a7d 100644 --- a/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template +++ b/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template @@ -284,6 +284,16 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE implements PRIORITY_DEQUEUE KEY_G return EMPTY_VALUE; } + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + int result = 0; + for(int i = 0,m=size();i=0;i--) 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 ac44ac2..ba08d82 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/ImmutableOpenHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/ImmutableOpenHashSet.template @@ -312,6 +312,7 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI int index = firstIndex; while(index != -1) { if(filter.TEST_VALUE(keys[index])) return true; + index = (int)links[index]; } return false; } @@ -322,6 +323,7 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI int index = firstIndex; while(index != -1) { if(filter.TEST_VALUE(keys[index])) return false; + index = (int)links[index]; } return true; } @@ -332,6 +334,7 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI int index = firstIndex; while(index != -1) { if(!filter.TEST_VALUE(keys[index])) return false; + index = (int)links[index]; } return true; } @@ -342,10 +345,23 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI int index = firstIndex; while(index != -1) { if(filter.TEST_VALUE(keys[index])) return keys[index]; + index = (int)links[index]; } return EMPTY_VALUE; } + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + int result = 0; + int index = firstIndex; + while(index != -1) { + if(filter.TEST_VALUE(keys[index])) result++; + index = (int)links[index]; + } + return result; + } + @Override public LIST_ITERATOR KEY_GENERIC_TYPE iterator() { return new SetIterator(); 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 e31fbda..e521eec 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template @@ -549,6 +549,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY int index = firstIndex; while(index != -1) { if(filter.TEST_VALUE(keys[index])) return true; + index = (int)links[index]; } return false; } @@ -559,6 +560,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY int index = firstIndex; while(index != -1) { if(filter.TEST_VALUE(keys[index])) return false; + index = (int)links[index]; } return true; } @@ -569,6 +571,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY int index = firstIndex; while(index != -1) { if(!filter.TEST_VALUE(keys[index])) return false; + index = (int)links[index]; } return true; } @@ -579,10 +582,23 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY int index = firstIndex; while(index != -1) { if(filter.TEST_VALUE(keys[index])) return keys[index]; + index = (int)links[index]; } return EMPTY_VALUE; } + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + int result = 0; + int index = firstIndex; + while(index != -1) { + if(filter.TEST_VALUE(keys[index])) result++; + index = (int)links[index]; + } + return result; + } + @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 d2f0fa4..21fcde3 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template @@ -404,6 +404,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE int index = firstIndex; while(index != -1) { if(filter.TEST_VALUE(keys[index])) return true; + index = (int)links[index]; } return false; } @@ -414,6 +415,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE int index = firstIndex; while(index != -1) { if(filter.TEST_VALUE(keys[index])) return false; + index = (int)links[index]; } return true; } @@ -424,6 +426,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE int index = firstIndex; while(index != -1) { if(!filter.TEST_VALUE(keys[index])) return false; + index = (int)links[index]; } return true; } @@ -434,10 +437,23 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE int index = firstIndex; while(index != -1) { if(filter.TEST_VALUE(keys[index])) return keys[index]; + index = (int)links[index]; } return EMPTY_VALUE; } + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + int result = 0; + int index = firstIndex; + while(index != -1) { + if(filter.TEST_VALUE(keys[index])) result++; + index = (int)links[index]; + } + return result; + } + @Override protected void onNodeAdded(int pos) { if(size == 0) { 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 16e71ac..337df70 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template @@ -521,6 +521,18 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T return EMPTY_VALUE; } + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + if(containsNull && filter.TEST_VALUE(keys[nullIndex])) result++; + for(int i = nullIndex-1;i>=0;i--) { + if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.TEST_VALUE(keys[i])) result++; + } + return result; + } + private class SetIterator implements ITERATOR KEY_GENERIC_TYPE { int pos = nullIndex; int lastReturned = -1; 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 14d5646..2ba3ed8 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template @@ -390,6 +390,18 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp return EMPTY_VALUE; } + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + if(containsNull && filter.TEST_VALUE(keys[nullIndex])) result++; + for(int i = nullIndex-1;i>=0;i--) { + if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.TEST_VALUE(keys[i])) result++; + } + return result; + } + 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/RBTreeSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template index 2ada1d3..1860c0b 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template @@ -346,6 +346,16 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE return EMPTY_VALUE; } + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + int result = 0; + for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + if(filter.TEST_VALUE(entry.key)) result++; + } + return result; + } + protected Entry KEY_GENERIC_TYPE findNode(KEY_TYPE o) { Entry KEY_GENERIC_TYPE node = tree; int compare; @@ -1111,6 +1121,16 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE return EMPTY_VALUE; } + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + int result = 0; + for(Entry KEY_GENERIC_TYPE entry = start();entry != null && inRange(entry.key);entry = next(entry)) { + if(filter.TEST_VALUE(entry.key)) result++; + } + return result; + } + class SubSetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE { Entry KEY_GENERIC_TYPE previous;