Started working on the next patch.
- Added: ISizeProvider interface (Optimization Helper) - Added: ISizeProvider into most Iterable implementations (Distinct/Filter/FlatMap/ArrayFlatMap don't support it, for obvious reasons) - Added: ToArray function into Iterable which uses ISizeProvider to reduce overhead of duplicating arrays. - Fixed: putIfAbsent now replaces defaultValues
This commit is contained in:
+43
-6
@@ -25,6 +25,7 @@ import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
|
||||
import speiger.src.collections.PACKAGE.sets.HASH_SET;
|
||||
import speiger.src.collections.PACKAGE.sets.SET;
|
||||
#endif
|
||||
import speiger.src.collections.utils.ISizeProvider;
|
||||
|
||||
/**
|
||||
* A Helper class for Iterables
|
||||
@@ -247,7 +248,7 @@ public class ITERABLES
|
||||
return new WrappedIterableBRACES(iterable);
|
||||
}
|
||||
|
||||
private static class WrappedIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE
|
||||
private static class WrappedIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE, ISizeProvider
|
||||
{
|
||||
Iterable<? extends CLASS_TYPE> iterable;
|
||||
|
||||
@@ -259,6 +260,12 @@ public class ITERABLES
|
||||
return ITERATORS.wrap(iterable.iterator());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
ISizeProvider prov = ISizeProvider.of(iterable);
|
||||
return prov == null ? -1 : prov.size();
|
||||
}
|
||||
|
||||
#if !TYPE_OBJECT
|
||||
@Override
|
||||
public void forEach(CONSUMER action) {
|
||||
@@ -273,7 +280,7 @@ public class ITERABLES
|
||||
#endif
|
||||
}
|
||||
|
||||
private static class MappedIterable KSS_GENERIC_TYPE<E, T> implements ObjectIterable<T>
|
||||
private static class MappedIterable KSS_GENERIC_TYPE<E, T> implements ObjectIterable<T>, ISizeProvider
|
||||
{
|
||||
ITERABLE KEY_SPECIAL_GENERIC_TYPE<E> iterable;
|
||||
TO_OBJECT_FUNCTION KSS_GENERIC_TYPE<E, T> mapper;
|
||||
@@ -287,6 +294,12 @@ public class ITERABLES
|
||||
return ITERATORS.map(iterable.iterator(), mapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
ISizeProvider prov = ISizeProvider.of(this);
|
||||
return prov == null ? -1 : prov.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void forEach(Consumer<? super T> action) {
|
||||
Objects.requireNonNull(action);
|
||||
@@ -342,7 +355,7 @@ public class ITERABLES
|
||||
}
|
||||
}
|
||||
|
||||
private static class RepeatingIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE
|
||||
private static class RepeatingIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE, ISizeProvider
|
||||
{
|
||||
ITERABLE KEY_GENERIC_TYPE iterable;
|
||||
int repeats;
|
||||
@@ -357,6 +370,12 @@ public class ITERABLES
|
||||
return ITERATORS.repeat(iterable.iterator(), repeats);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
ISizeProvider prov = ISizeProvider.of(iterable);
|
||||
return prov == null ? -1 : prov.size() * (repeats+1);
|
||||
}
|
||||
|
||||
#if !TYPE_OBJECT
|
||||
@Override
|
||||
public void forEach(CONSUMER action) {
|
||||
@@ -407,7 +426,7 @@ public class ITERABLES
|
||||
#endif
|
||||
}
|
||||
|
||||
private static class LimitedIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE
|
||||
private static class LimitedIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE, ISizeProvider
|
||||
{
|
||||
ITERABLE KEY_GENERIC_TYPE iterable;
|
||||
long limit;
|
||||
@@ -422,6 +441,12 @@ public class ITERABLES
|
||||
return ITERATORS.limit(iterable.iterator(), limit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
ISizeProvider prov = ISizeProvider.of(iterable);
|
||||
return prov == null ? -1 : (int)Math.min(prov.size(), limit);
|
||||
}
|
||||
|
||||
#if !TYPE_OBJECT
|
||||
@Override
|
||||
public void forEach(CONSUMER action) {
|
||||
@@ -446,7 +471,7 @@ public class ITERABLES
|
||||
#endif
|
||||
}
|
||||
|
||||
private static class SortedIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE
|
||||
private static class SortedIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE, ISizeProvider
|
||||
{
|
||||
ITERABLE KEY_GENERIC_TYPE iterable;
|
||||
COMPARATOR KEY_GENERIC_TYPE sorter;
|
||||
@@ -461,6 +486,12 @@ public class ITERABLES
|
||||
return ITERATORS.sorted(iterable.iterator(), sorter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
ISizeProvider prov = ISizeProvider.of(iterable);
|
||||
return prov == null ? -1 : prov.size();
|
||||
}
|
||||
|
||||
#if !TYPE_OBJECT
|
||||
@Override
|
||||
public void forEach(CONSUMER action) {
|
||||
@@ -520,7 +551,7 @@ public class ITERABLES
|
||||
#endif
|
||||
}
|
||||
|
||||
private static class PeekIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE
|
||||
private static class PeekIterable KEY_GENERIC_TYPE implements ITERABLE KEY_GENERIC_TYPE, ISizeProvider
|
||||
{
|
||||
ITERABLE KEY_GENERIC_TYPE iterable;
|
||||
CONSUMER KEY_GENERIC_TYPE action;
|
||||
@@ -535,6 +566,12 @@ public class ITERABLES
|
||||
return ITERATORS.peek(iterable.iterator(), action);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
ISizeProvider prov = ISizeProvider.of(iterable);
|
||||
return prov == null ? -1 : prov.size();
|
||||
}
|
||||
|
||||
#if !TYPE_OBJECT
|
||||
@Override
|
||||
public void forEach(CONSUMER action) {
|
||||
|
||||
Reference in New Issue
Block a user