7 Commits
Author SHA1 Message Date
Speiger 8ff1d4f0b5 Last Sync for now 2026-05-14 21:33:31 +02:00
Speiger e56e136459 Sync with latest changes 2026-05-14 19:24:20 +02:00
Speiger 4db9c69a54 Source Sync 2026-05-14 02:54:57 +02:00
Speiger 91aabe2096 Sync Source Changes :) 2026-05-12 13:40:48 +02:00
Speiger 4c565f2cf3 Sync Latest Update 2026-05-11 18:02:31 +02:00
Speiger 720fa44eed Whoops forgot the special cases 2026-05-11 00:25:20 +02:00
Speiger c46baa10b4 Updated Workflow 2026-05-11 00:21:34 +02:00
1006 changed files with 60091 additions and 18302 deletions
+2 -2
View File
@@ -8,11 +8,11 @@ jobs:
name: Code Quality Check name: Code Quality Check
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v6
with: with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Set up JDK 11 - name: Set up JDK 11
uses: actions/setup-java@v3 uses: actions/setup-java@v5
with: with:
distribution: 'temurin' distribution: 'temurin'
java-version: 11 java-version: 11
+1 -1
View File
@@ -48,7 +48,7 @@ dependencies {
builderImplementation 'com.google.code.gson:gson:2.10' builderImplementation 'com.google.code.gson:gson:2.10'
builderImplementation 'de.speiger:Simple-Code-Generator:1.3.0' builderImplementation 'de.speiger:Simple-Code-Generator:1.3.0'
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
testImplementation 'com.google.guava:guava-testlib:31.0.1-jre' testImplementation 'com.google.guava:guava-testlib:33.6.0-jre'
} }
@@ -34,7 +34,8 @@ public class FunctionModule extends BaseModule
protected void loadBlockades() protected void loadBlockades()
{ {
if(keyType.isObject()) addBlockedFiles("Consumer", "Comparator"); if(keyType.isObject()) addBlockedFiles("Consumer", "Comparator");
if(!MODULE.isEnabled()) addBlockedFiles("Consumer", "BiConsumer", "Comparator", "Supplier", "Function", "UnaryOperator"); if(!MODULE.isEnabled()) addBlockedFiles("Consumer", "BiConsumer", "Comparator", "Supplier", "Optional", "Function", "UnaryOperator");
if(!keyType.needsCustomJDKType()) addBlockedFiles("Optional");
} }
@Override @Override
@@ -60,6 +61,7 @@ public class FunctionModule extends BaseModule
} }
else addBiRequirement("Function"); else addBiRequirement("Function");
addRemapper("BiConsumer", "%sConsumer"); addRemapper("BiConsumer", "%sConsumer");
addRemapper("Optional", "Optional%s");
} }
@Override @Override
@@ -94,6 +96,8 @@ public class FunctionModule extends BaseModule
addFunctionMappers("PREDICATE", "%sPredicate"); addFunctionMappers("PREDICATE", "%sPredicate");
addClassMapper("SUPPLIER", "Supplier"); addClassMapper("SUPPLIER", "Supplier");
addSimpleMapper("OPTIONAL", keyType.isObject() ? "Optional" : String.format("Optional%s", keyType.getFileType()));
addSimpleMapper("VALUE_OPTIONAL", valueType.isObject() ? "Optional" : String.format("Optional%s", valueType.getFileType()));
addAbstractMapper("SINGLE_UNARY_OPERATOR", "%1$s%1$sUnaryOperator"); addAbstractMapper("SINGLE_UNARY_OPERATOR", "%1$s%1$sUnaryOperator");
addBiClassMapper("UNARY_OPERATOR", "UnaryOperator", ""); addBiClassMapper("UNARY_OPERATOR", "UnaryOperator", "");
if(keyType.isObject()) if(keyType.isObject())
@@ -60,6 +60,8 @@ public class JavaModule extends BaseModule
addSimpleMapper("APPLY_KEY_VALUE", keyType.isObject() ? "apply" : "applyAs"+keyType.getNonFileType()); addSimpleMapper("APPLY_KEY_VALUE", keyType.isObject() ? "apply" : "applyAs"+keyType.getNonFileType());
addSimpleMapper("APPLY_VALUE", valueType.isObject() ? "apply" : "applyAs"+valueType.getNonFileType()); addSimpleMapper("APPLY_VALUE", valueType.isObject() ? "apply" : "applyAs"+valueType.getNonFileType());
addSimpleMapper("APPLY_CAST", "applyAs"+keyType.getCustomJDKType().getNonFileType()); addSimpleMapper("APPLY_CAST", "applyAs"+keyType.getCustomJDKType().getNonFileType());
addSimpleMapper("GET_OPTIONAL", keyType.isObject() ? "ofNullable" : "of");
addSimpleMapper("GET_OPTIONAL_VALUE", valueType.isObject() ? "ofNullable" : "of");
//Shared by Maps and Pairs so moved to java. //Shared by Maps and Pairs so moved to java.
addFunctionMappers("ENTRY_KEY", "get%sKey"); addFunctionMappers("ENTRY_KEY", "get%sKey");
@@ -36,6 +36,10 @@ public class MapModule extends BaseModule
public static final FunctionDependency ENUM_MAP = MODULE.createDependency("EnumMap").addEntryDependency(IMPLEMENTATION); public static final FunctionDependency ENUM_MAP = MODULE.createDependency("EnumMap").addEntryDependency(IMPLEMENTATION);
public static final FunctionDependency LINKED_ENUM_MAP = MODULE.createDependency("LinkedEnumMap").addEntryDependency(ENUM_MAP).addEntryDependency(ORDERED_MAP); public static final FunctionDependency LINKED_ENUM_MAP = MODULE.createDependency("LinkedEnumMap").addEntryDependency(ENUM_MAP).addEntryDependency(ORDERED_MAP);
public static final FunctionDependency REF_MAP = MODULE.createDependency("ReferenceHashMap").addEntryDependency(IMPLEMENTATION);
public static final FunctionDependency LINKED_REF_MAP = MODULE.createDependency("LinkedReferenceMap").addEntryDependency(REF_MAP).addEntryDependency(ORDERED_MAP);
public static final FunctionDependency CONCURRENT_MAP = MODULE.createDependency("ConcurrentMap").addEntryDependency(IMPLEMENTATION); public static final FunctionDependency CONCURRENT_MAP = MODULE.createDependency("ConcurrentMap").addEntryDependency(IMPLEMENTATION);
public static final FunctionDependency AVL_TREE_MAP = MODULE.createDependency("AVLTreeMap").addEntryDependency(SORTED_MAP).addEntryDependency(IMPLEMENTATION); public static final FunctionDependency AVL_TREE_MAP = MODULE.createDependency("AVLTreeMap").addEntryDependency(SORTED_MAP).addEntryDependency(IMPLEMENTATION);
public static final FunctionDependency RB_TREE_MAP = MODULE.createDependency("RBTreeMap").addEntryDependency(SORTED_MAP).addEntryDependency(IMPLEMENTATION); public static final FunctionDependency RB_TREE_MAP = MODULE.createDependency("RBTreeMap").addEntryDependency(SORTED_MAP).addEntryDependency(IMPLEMENTATION);
@@ -51,7 +55,7 @@ public class MapModule extends BaseModule
@Override @Override
public List<IDependency> getDependencies(ClassType keyType, ClassType valueType) { public List<IDependency> getDependencies(ClassType keyType, ClassType valueType) {
List<IDependency> dependencies = new ArrayList<>(Arrays.asList(MODULE, ORDERED_MAP, SORTED_MAP, IMPLEMENTATION, WRAPPERS, ARRAY_MAP, IMMUTABLE_MAP, HASH_MAP, LINKED_MAP, CUSTOM_MAP, LINKED_CUSTOM_MAP, CONCURRENT_MAP, AVL_TREE_MAP, RB_TREE_MAP)); List<IDependency> dependencies = new ArrayList<>(Arrays.asList(MODULE, ORDERED_MAP, SORTED_MAP, IMPLEMENTATION, WRAPPERS, ARRAY_MAP, IMMUTABLE_MAP, HASH_MAP, LINKED_MAP, CUSTOM_MAP, LINKED_CUSTOM_MAP, CONCURRENT_MAP, AVL_TREE_MAP, RB_TREE_MAP));
if(keyType == ClassType.OBJECT) dependencies.addAll(Arrays.asList(ENUM_MAP, LINKED_ENUM_MAP)); if(keyType == ClassType.OBJECT) dependencies.addAll(Arrays.asList(ENUM_MAP, LINKED_ENUM_MAP, REF_MAP, LINKED_REF_MAP));
return dependencies; return dependencies;
} }
@@ -70,6 +74,10 @@ public class MapModule extends BaseModule
if(AVL_TREE_MAP.isEnabled()) addFlag("AVL_TREE_MAP_FEATURE"); if(AVL_TREE_MAP.isEnabled()) addFlag("AVL_TREE_MAP_FEATURE");
if(RB_TREE_MAP.isEnabled()) addFlag("RB_TREE_MAP_FEATURE"); if(RB_TREE_MAP.isEnabled()) addFlag("RB_TREE_MAP_FEATURE");
if(REF_MAP.isEnabled()) addFlag("REF_MAP_FEATURE");
if(LINKED_REF_MAP.isEnabled()) addFlag("LINKED_REF_MAP_FEATURE");
if(CONCURRENT_MAP.isEnabled()) addFlag("CONCURRENT_MAP_FEATURE"); if(CONCURRENT_MAP.isEnabled()) addFlag("CONCURRENT_MAP_FEATURE");
if(IMMUTABLE_MAP.isEnabled()) addFlag("IMMUTABLE_MAP_FEATURE"); if(IMMUTABLE_MAP.isEnabled()) addFlag("IMMUTABLE_MAP_FEATURE");
if(HASH_MAP.isEnabled()) addFlag("MAP_FEATURE"); if(HASH_MAP.isEnabled()) addFlag("MAP_FEATURE");
@@ -85,6 +93,8 @@ public class MapModule extends BaseModule
if(!IMMUTABLE_MAP.isEnabled()) addBlockedFiles("ImmutableOpenHashMap"); if(!IMMUTABLE_MAP.isEnabled()) addBlockedFiles("ImmutableOpenHashMap");
if(!CONCURRENT_MAP.isEnabled()) addBlockedFiles("ConcurrentMap", "ConcurrentOpenHashMap"); if(!CONCURRENT_MAP.isEnabled()) addBlockedFiles("ConcurrentMap", "ConcurrentOpenHashMap");
if(!ORDERED_MAP.isEnabled()) addBlockedFiles("OrderedMap"); if(!ORDERED_MAP.isEnabled()) addBlockedFiles("OrderedMap");
if(!REF_MAP.isEnabled()) addBlockedFiles("ReferenceHashMap");
if(!LINKED_REF_MAP.isEnabled()) addBlockedFiles("LinkedReferenceHashMap");
if(!HASH_MAP.isEnabled()) addBlockedFiles("OpenHashMap"); if(!HASH_MAP.isEnabled()) addBlockedFiles("OpenHashMap");
if(!LINKED_MAP.isEnabled()) addBlockedFiles("LinkedOpenHashMap"); if(!LINKED_MAP.isEnabled()) addBlockedFiles("LinkedOpenHashMap");
if(!CUSTOM_MAP.isEnabled()) addBlockedFiles("OpenCustomHashMap"); if(!CUSTOM_MAP.isEnabled()) addBlockedFiles("OpenCustomHashMap");
@@ -127,6 +137,8 @@ public class MapModule extends BaseModule
addBiRequirement("AbstractMap"); addBiRequirement("AbstractMap");
addEnumRequirement("EnumMap"); addEnumRequirement("EnumMap");
addEnumRequirement("LinkedEnumMap"); addEnumRequirement("LinkedEnumMap");
addEnumRequirement("ReferenceHashMap");
addEnumRequirement("LinkedReferenceHashMap");
addBiRequirement("ConcurrentOpenHashMap"); addBiRequirement("ConcurrentOpenHashMap");
addBiRequirement("ImmutableOpenHashMap"); addBiRequirement("ImmutableOpenHashMap");
addBiRequirement("OpenHashMap"); addBiRequirement("OpenHashMap");
@@ -141,6 +153,8 @@ public class MapModule extends BaseModule
addRemapper("AbstractMap", "Abstract%sMap"); addRemapper("AbstractMap", "Abstract%sMap");
addRemapper("EnumMap", "Enum2%sMap"); addRemapper("EnumMap", "Enum2%sMap");
addRemapper("LinkedEnumMap", "LinkedEnum2%sMap"); addRemapper("LinkedEnumMap", "LinkedEnum2%sMap");
addRemapper("ReferenceHashMap", "Reference2%sHashMap");
addRemapper("LinkedReferenceHashMap", "Reference2%sLinkedHashMap");
addRemapper("ImmutableOpenHashMap", "Immutable%sOpenHashMap"); addRemapper("ImmutableOpenHashMap", "Immutable%sOpenHashMap");
//Test Classes //Test Classes
@@ -198,6 +212,7 @@ public class MapModule extends BaseModule
addBiRequirement("SortedMapNavigationTester"); addBiRequirement("SortedMapNavigationTester");
addBiRequirement("OrderedMapNavigationTester"); addBiRequirement("OrderedMapNavigationTester");
addBiRequirement("OrderedMapMoveTester"); addBiRequirement("OrderedMapMoveTester");
addBiRequirement("OrderedMapPutTester");
addBiRequirement("MapConstructorTester"); addBiRequirement("MapConstructorTester");
addRemapper("TestMapGenerator", "Test%sMapGenerator"); addRemapper("TestMapGenerator", "Test%sMapGenerator");
@@ -248,6 +263,8 @@ public class MapModule extends BaseModule
addBiClassMapper("RB_TREE_MAP", "RBTreeMap", "2"); addBiClassMapper("RB_TREE_MAP", "RBTreeMap", "2");
addFunctionValueMappers("LINKED_ENUM_MAP", valueType.isObject() ? "LinkedEnum2ObjectMap" : "LinkedEnum2%sMap"); addFunctionValueMappers("LINKED_ENUM_MAP", valueType.isObject() ? "LinkedEnum2ObjectMap" : "LinkedEnum2%sMap");
addFunctionValueMappers("ENUM_MAP", valueType.isObject() ? "Enum2ObjectMap" : "Enum2%sMap"); addFunctionValueMappers("ENUM_MAP", valueType.isObject() ? "Enum2ObjectMap" : "Enum2%sMap");
addFunctionValueMappers("REF_MAP", valueType.isObject() ? "Reference2ObjectHashMap" : "Reference2%sHashMap");
addFunctionValueMappers("LINKED_REF_MAP", valueType.isObject() ? "Reference2ObjectLinkedHashMap" : "Reference2%sLinkedHashMap");
addBiClassMapper("HASH_MAP", "OpenHashMap", "2"); addBiClassMapper("HASH_MAP", "OpenHashMap", "2");
addBiClassMapper("ARRAY_MAP", "ArrayMap", "2"); addBiClassMapper("ARRAY_MAP", "ArrayMap", "2");
@@ -5,6 +5,9 @@ import java.util.function.Consumer;
#if JDK_FUNCTION #if JDK_FUNCTION
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
#endif #endif
#if JDK_TYPE
import java.util.OPTIONAL;
#endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -30,6 +33,9 @@ import speiger.src.collections.PACKAGE.collections.OUTPUT_ITERABLE;
#endif #endif
#enditerate #enditerate
#endif #endif
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
import speiger.src.collections.PACKAGE.functions.function.TO_OBJECT_FUNCTION; import speiger.src.collections.PACKAGE.functions.function.TO_OBJECT_FUNCTION;
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER; import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
@@ -408,13 +414,13 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
* @param filter that should be applied * @param filter that should be applied
* @return the found value or the null equivalent variant. * @return the found value or the null equivalent variant.
*/ */
default KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { default OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) { for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) {
KEY_TYPE entry = iter.NEXT(); KEY_TYPE entry = iter.NEXT();
if(filter.test(entry)) return entry; if(filter.test(entry)) return OPTIONAL.GET_OPTIONAL(entry);
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -459,7 +465,7 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
* @param operator the operation that should be applied * @param operator the operation that should be applied
* @return the reduction result, returns null value if nothing was found * @return the reduction result, returns null value if nothing was found
*/ */
default KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { default OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -471,7 +477,7 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable<CLASS_TYPE>
} }
state = operator.APPLY_VALUE(state, iter.NEXT()); state = operator.APPLY_VALUE(state, iter.NEXT());
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
/** /**
@@ -0,0 +1,89 @@
package speiger.src.collections.PACKAGE.functions;
import java.util.NoSuchElementException;
import java.util.function.Supplier;
public final class OPTIONAL KEY_GENERIC_TYPE {
private static final OPTIONAL NO_GENERIC_TYPE EMPTY = new OPTIONALBRACES();
private final boolean isPresent;
private final KEY_TYPE value;
private OPTIONAL() {
this.isPresent = false;
this.value = EMPTY_VALUE;
}
private OPTIONAL(KEY_TYPE value) {
this.isPresent = true;
this.value = value;
}
public static OPTIONAL KEY_GENERIC_TYPE empty() {
return EMPTY;
}
public static OPTIONAL KEY_GENERIC_TYPE of(KEY_TYPE value) {
return new OPTIONALBRACES(value);
}
public KEY_TYPE SUPPLY_GET() {
if(!isPresent) throw new NoSuchElementException("No value present");
return value;
}
public boolean isPresent() {
return isPresent;
}
public boolean isEmpty() {
return !isPresent;
}
public void ifPresent(CONSUMER KEY_GENERIC_TYPE consumer) {
if(isPresent) consumer.accept(value);
}
public void ifPresentOrElse(CONSUMER KEY_GENERIC_TYPE action, Runnable emptyAction) {
if (isPresent) action.accept(value);
else emptyAction.run();
}
public KEY_TYPE orElse(KEY_TYPE other) {
return isPresent ? value : other;
}
public KEY_TYPE orElseGet(SUPPLIER KEY_GENERIC_TYPE other) {
return isPresent ? value : other.SUPPLY_GET();
}
public KEY_TYPE orElseThrow() {
if (!isPresent) throw new NoSuchElementException("No value present");
return value;
}
public <X extends Throwable> KEY_TYPE orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if(isPresent) return value;
else throw exceptionSupplier.get();
}
@Override
public boolean equals(Object obj) {
if(obj == this) return true;
if(obj instanceof OPTIONAL) {
OPTIONAL KEY_GENERIC_TYPE other = (OPTIONAL KEY_GENERIC_TYPE)obj;
return (isPresent && other.isPresent ? KEY_EQUALS(value, other.value) : isPresent == other.isPresent);
}
return false;
}
@Override
public int hashCode() {
return isPresent ? KEY_TO_HASH(value) : 0;
}
@Override
public String toString() {
return isPresent ? "OPTIONAL["+value+"]" : "OPTIONAL.empty";
}
}
@@ -7,6 +7,16 @@ import java.util.Comparator;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.Objects; import java.util.Objects;
#if JDK_TYPE
import java.util.OPTIONAL;
#if SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
import java.util.stream.Stream;
import java.util.stream.Collector;
#endif
#endif
#endif
#if TYPE_OBJECT #if TYPE_OBJECT
#if IARRAY_FEATURE || TYPE_OBJECT #if IARRAY_FEATURE || TYPE_OBJECT
import java.util.function.Consumer; import java.util.function.Consumer;
@@ -33,6 +43,9 @@ import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.STACK; import speiger.src.collections.PACKAGE.collections.STACK;
#endif #endif
import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.CONSUMER;
@@ -219,6 +232,42 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
return list; return list;
} }
#endif
#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
/**
* Creates a Collector for a ArrayList
* @Type(T)
* @return a collector
*/
public static <T> Collector<T, ObjectArrayList<T>, ObjectArrayList<T>> toList() {
return Collector.of(ObjectArrayList::new, ObjectArrayList::add, ObjectArrayList::merge);
}
/**
* Collects a Stream to a ArrayList
* @Type(T)
* @return a list with the contents of the Stream
*/
public static <T> ARRAY_LIST KEY_GENERIC_TYPE toList(Stream<T> stream) {
return stream.collect(ObjectArrayList::new, ObjectArrayList::add, ObjectArrayList::merge);
}
private ObjectArrayList<T> merge(ObjectArrayList<T> a) {
addAll(a);
return this;
}
#else
/**
* Collects a Stream to a ArrayList
* @return a list with the contents of the Stream
*/
public static ARRAY_LIST toList(JAVA_STREAM stream) {
return stream.collect(ARRAY_LIST::new, ARRAY_LIST::add, ARRAY_LIST::addAll);
}
#endif
#endif #endif
/** /**
* Appends the specified element to the end of this list. * Appends the specified element to the end of this list.
@@ -682,12 +731,12 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) { for(int i = 0;i<size;i++) {
if(filter.test(data[i])) return data[i]; if(filter.test(data[i])) return OPTIONAL.GET_OPTIONAL(data[i]);
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -714,7 +763,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -726,7 +775,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
} }
state = operator.APPLY_VALUE(state, data[i]); state = operator.APPLY_VALUE(state, data[i]);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
@@ -8,6 +8,15 @@ import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
#if JDK_TYPE
import java.util.OPTIONAL;
#if SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
import java.util.stream.Stream;
import java.util.stream.Collector;
#endif
#endif
#endif
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Supplier; import java.util.function.Supplier;
@@ -33,6 +42,9 @@ import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.STACK; import speiger.src.collections.PACKAGE.collections.STACK;
#endif #endif
import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.CONSUMER;
@@ -166,7 +178,42 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER
} }
#endif #endif
#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
/**
* Creates a Collector for a CopyOnWriteArrayList
* @Type(T)
* @return a collector
*/
public static <T> Collector<T, CopyOnWriteObjectArrayList<T>, CopyOnWriteObjectArrayList<T>> toList() {
return Collector.of(CopyOnWriteObjectArrayList::new, CopyOnWriteObjectArrayList::add, CopyOnWriteObjectArrayList::merge);
}
/**
* Collects a Stream to a CopyOnWriteArrayList
* @Type(T)
* @return a list with the contents of the Stream
*/
public static <T> CopyOnWriteObjectArrayList KEY_GENERIC_TYPE toList(Stream<T> stream) {
return stream.collect(CopyOnWriteObjectArrayList::new, CopyOnWriteObjectArrayList::add, CopyOnWriteObjectArrayList::merge);
}
private CopyOnWriteObjectArrayList<T> merge(CopyOnWriteObjectArrayList<T> a) {
addAll(a);
return this;
}
#else
/**
* Collects a Stream to a CopyOnWriteArrayList
* @return a list with the contents of the Stream
*/
public static COPY_ON_WRITE_LIST toList(JAVA_STREAM stream) {
return stream.collect(COPY_ON_WRITE_LIST::new, COPY_ON_WRITE_LIST::add, COPY_ON_WRITE_LIST::addAll);
}
#endif
#endif
private void setArray(KEY_TYPE[] data) { private void setArray(KEY_TYPE[] data) {
this.data = data; this.data = data;
} }
@@ -810,13 +857,13 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
KEY_TYPE[] data = this.data; KEY_TYPE[] data = this.data;
for(int i = 0,m=data.length;i<m;i++) { for(int i = 0,m=data.length;i<m;i++) {
if(filter.test(data[i])) return data[i]; if(filter.test(data[i])) return OPTIONAL.GET_OPTIONAL(data[i]);
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -845,7 +892,7 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE[] data = this.data; KEY_TYPE[] data = this.data;
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
@@ -858,7 +905,7 @@ public class COPY_ON_WRITE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENER
} }
state = operator.APPLY_VALUE(state, data[i]); state = operator.APPLY_VALUE(state, data[i]);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
@@ -3,10 +3,26 @@ package speiger.src.collections.PACKAGE.lists;
import java.util.Arrays; import java.util.Arrays;
#if TYPE_OBJECT #if TYPE_OBJECT
import java.util.Comparator; import java.util.Comparator;
#else if !TYPE_BOOLEAN
import java.util.stream.JAVA_STREAM;
import java.util.stream.StreamSupport;
#endif #endif
import java.util.Collection; import java.util.Collection;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
#if JDK_TYPE
import java.util.OPTIONAL;
#if SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if PRIMITIVES
import java.util.function.Supplier;
#else
import java.util.stream.Stream;
import java.util.stream.Collector;
#endif
import speiger.src.collections.PACKAGE.utils.COLLECTIONS;
#endif
#endif
#if TYPE_OBJECT #if TYPE_OBJECT
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Consumer; import java.util.function.Consumer;
@@ -20,6 +36,7 @@ import java.util.function.PREDICATE;
#if !JDK_FUNCTION #if !JDK_FUNCTION
import java.util.function.JAVA_PREDICATE; import java.util.function.JAVA_PREDICATE;
#endif #endif
import java.util.function.JAVA_UNARY_OPERATOR; import java.util.function.JAVA_UNARY_OPERATOR;
#endif #endif
@@ -27,6 +44,9 @@ import speiger.src.collections.PACKAGE.collections.COLLECTION;
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.CONSUMER;
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
import speiger.src.collections.PACKAGE.utils.ARRAYS; import speiger.src.collections.PACKAGE.utils.ARRAYS;
#endif #endif
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
@@ -36,10 +56,6 @@ import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.objects.utils.ObjectArrays; import speiger.src.collections.objects.utils.ObjectArrays;
import speiger.src.collections.PACKAGE.utils.ITERATORS; import speiger.src.collections.PACKAGE.utils.ITERATORS;
#if PRIMITIVES && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
import java.util.stream.JAVA_STREAM;
import java.util.stream.StreamSupport;
#endif
#if SPLIT_ITERATOR_FEATURE #if SPLIT_ITERATOR_FEATURE
import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR; import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR;
import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS; import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS;
@@ -120,6 +136,42 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
data = Arrays.copyOfRange(a, offset, offset+length); data = Arrays.copyOfRange(a, offset, offset+length);
} }
#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
/**
* Creates a Collector for a ImmutableList
* @Type(T)
* @return a collector
*/
public static <T> Collector<T, COLLECTION<T>, ImmutableObjectList<T>> toList() {
return Collector.of(COLLECTIONS::wrapper, COLLECTION::add, ImmutableObjectList::merge, E -> new IMMUTABLE_LIST<>(E.toArray((T[])new Object[E.size()])));
}
/**
* Collects a Stream to a ImmutableList
* @Type(T)
* @return a list with the contents of the Stream
*/
public static <T> ImmutableObjectList KEY_GENERIC_TYPE toList(Stream<T> stream) {
return stream.collect(toList());
}
private static <T> COLLECTION<T> merge(COLLECTION<T> a, COLLECTION<T> b) {
a.addAll(b);
return a;
}
#else
/**
* Collects a Stream to a ImmutableList
* @return a list with the contents of the Stream
*/
public static IMMUTABLE_LIST toList(JAVA_STREAM stream) {
return new IMMUTABLE_LIST(stream.collect((Supplier<COLLECTION>)COLLECTIONS::wrapper, COLLECTION::add, COLLECTION::addAll).TO_ARRAY());
}
#endif
#endif
@Override @Override
public boolean add(KEY_TYPE e) { throw new UnsupportedOperationException(); } public boolean add(KEY_TYPE e) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -335,12 +387,12 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0,m=data.length;i<m;i++) { for(int i = 0,m=data.length;i<m;i++) {
if(filter.test(data[i])) return data[i]; if(filter.test(data[i])) return OPTIONAL.GET_OPTIONAL(data[i]);
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -367,7 +419,7 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -379,7 +431,7 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T
} }
state = operator.APPLY_VALUE(state, data[i]); state = operator.APPLY_VALUE(state, data[i]);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
@@ -4,7 +4,6 @@ package speiger.src.collections.PACKAGE.lists;
#if DEQUEUE_FEATURE #if DEQUEUE_FEATURE
import java.util.Comparator; import java.util.Comparator;
#endif #endif
import java.util.function.BiFunction; import java.util.function.BiFunction;
#else if PRIMITIVES #else if PRIMITIVES
import java.nio.JAVA_BUFFER; import java.nio.JAVA_BUFFER;
@@ -12,6 +11,15 @@ import java.nio.JAVA_BUFFER;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.Objects; import java.util.Objects;
#if JDK_TYPE
import java.util.OPTIONAL;
#if SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
import java.util.stream.Stream;
import java.util.stream.Collector;
#endif
#endif
#endif
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
#if SPLIT_ITERATOR_FEATURE #if SPLIT_ITERATOR_FEATURE
import java.util.Spliterator; import java.util.Spliterator;
@@ -34,6 +42,9 @@ import java.util.function.JAVA_PREDICATE;
#endif #endif
import java.util.function.JAVA_UNARY_OPERATOR; import java.util.function.JAVA_UNARY_OPERATOR;
#endif #endif
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER; import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION #if !JDK_FUNCTION
@@ -155,6 +166,42 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
for(int i = offset,m=offset+length;i<m;add(a[i++])); for(int i = offset,m=offset+length;i<m;add(a[i++]));
} }
#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
/**
* Creates a Collector for a LinkedList
* @Type(T)
* @return a collector
*/
public static <T> Collector<T, ObjectLinkedList<T>, ObjectLinkedList<T>> toList() {
return Collector.of(ObjectLinkedList::new, ObjectLinkedList::add, ObjectLinkedList::merge);
}
/**
* Collects a Stream to a LinkedList
* @Type(T)
* @return a list with the contents of the Stream
*/
public static <T> ObjectLinkedList KEY_GENERIC_TYPE toList(Stream<T> stream) {
return stream.collect(ObjectLinkedList::new, ObjectLinkedList::add, ObjectLinkedList::merge);
}
private ObjectLinkedList<T> merge(ObjectLinkedList<T> a) {
addAll(a);
return this;
}
#else
/**
* Collects a Stream to a LinkedList
* @return a list with the contents of the Stream
*/
public static LINKED_LIST toList(JAVA_STREAM stream) {
return stream.collect(LINKED_LIST::new, LINKED_LIST::add, LINKED_LIST::addAll);
}
#endif
#endif
@Override @Override
public boolean add(KEY_TYPE e) { public boolean add(KEY_TYPE e) {
add(size(), e); add(size(), e);
@@ -493,12 +540,12 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) { for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) {
if(filter.test(entry.value)) return entry.value; if(filter.test(entry.value)) return OPTIONAL.GET_OPTIONAL(entry.value);
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -525,7 +572,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -537,7 +584,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
} }
state = operator.APPLY_VALUE(state, entry.value); state = operator.APPLY_VALUE(state, entry.value);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
@@ -4,15 +4,18 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.locks.StampedLock; import java.util.concurrent.locks.StampedLock;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.BiFunction; import java.util.function.BiFunction;
#if !TYPE_OBJECT && JDK_TYPE #if !TYPE_OBJECT && JDK_TYPE
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
import java.util.OPTIONAL;
#endif #endif
#if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT #if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT
import java.util.function.VALUE_PREDICATE; import java.util.function.VALUE_PREDICATE;
import java.util.VALUE_OPTIONAL;
#endif #endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -40,9 +43,12 @@ import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
#if !SAME_TYPE #if !SAME_TYPE
import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR; import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR;
#endif #endif
#if !TYPE_OBJECT && !VALUE_BOOLEAN && !JDK_TYPE #if !TYPE_OBJECT && !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#if!VALUE_BOOLEAN
import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif #endif
#endif
import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP; import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.MAP; import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.CONCURRENT_MAP; import speiger.src.collections.PACKAGE.maps.interfaces.CONCURRENT_MAP;
@@ -78,6 +84,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C
#endif #endif
#if !JDK_VALUE #if !JDK_VALUE
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE;
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_OPTIONAL;
#endif #endif
#endif #endif
#if VALUE_OBJECT #if VALUE_OBJECT
@@ -839,7 +846,7 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; MAP.Entry KEY_VALUE_GENERIC_TYPE state = null;
boolean empty = true; boolean empty = true;
@@ -863,11 +870,11 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
@@ -877,7 +884,7 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY
try { try {
while(index != -1) { while(index != -1) {
entry.set(index, i); entry.set(index, i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -885,7 +892,7 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1186,7 +1193,7 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY
#endif #endif
@Override @Override
public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_KEY_VALUE; KEY_TYPE state = EMPTY_KEY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1210,11 +1217,11 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment KEY_VALUE_GENERIC_TYPE seg = segments[i]; Segment KEY_VALUE_GENERIC_TYPE seg = segments[i];
@@ -1222,7 +1229,7 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.keys[index])) return seg.keys[index]; if(filter.test(seg.keys[index])) return OPTIONAL.GET_OPTIONAL(seg.keys[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1230,7 +1237,7 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return EMPTY_KEY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -1454,7 +1461,7 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY
#endif #endif
@Override @Override
public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
VALUE_TYPE state = EMPTY_VALUE; VALUE_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1478,20 +1485,20 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state);
} }
@Override @Override
public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_VALUE; if(size() <= 0) return VALUE_OPTIONAL.empty();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment KEY_VALUE_GENERIC_TYPE seg = segments[i]; Segment KEY_VALUE_GENERIC_TYPE seg = segments[i];
long stamp = seg.readLock(); long stamp = seg.readLock();
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.values[index])) return seg.values[index]; if(filter.test(seg.values[index])) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(seg.values[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1499,7 +1506,7 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return EMPTY_VALUE; return VALUE_OPTIONAL.empty();
} }
@Override @Override
@@ -4,14 +4,17 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
#if !TYPE_OBJECT && JDK_TYPE #if !TYPE_OBJECT && JDK_TYPE
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
import java.util.OPTIONAL;
#endif #endif
#if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT #if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT
import java.util.function.VALUE_PREDICATE; import java.util.function.VALUE_PREDICATE;
import java.util.VALUE_OPTIONAL;
#endif #endif
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
@@ -27,6 +30,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
import speiger.src.collections.ints.functions.consumer.VALUE_BI_FROM_INT_CONSUMER; import speiger.src.collections.ints.functions.consumer.VALUE_BI_FROM_INT_CONSUMER;
#endif #endif
#if !TYPE_OBJECT && !JDK_TYPE #if !TYPE_OBJECT && !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif #endif
#if !TYPE_INT || !SAME_TYPE #if !TYPE_INT || !SAME_TYPE
@@ -59,6 +63,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C
#endif #endif
#if !JDK_VALUE #if !JDK_VALUE
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE;
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_OPTIONAL;
#endif #endif
#endif #endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -233,13 +238,13 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
if(containsNull) { if(containsNull) {
VALUE_TYPE lastValue = values[nullIndex]; VALUE_TYPE lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -247,7 +252,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
VALUE_TYPE lastValue = values[pos]; VALUE_TYPE lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -255,7 +260,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -267,13 +272,13 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
if(containsNull) { if(containsNull) {
VALUE_TYPE lastValue = values[nullIndex]; VALUE_TYPE lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -281,7 +286,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
VALUE_TYPE lastValue = values[pos]; VALUE_TYPE lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToLastIndex(pos); moveToLastIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -289,7 +294,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -302,7 +307,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -313,7 +318,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -326,7 +331,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -337,7 +342,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -348,7 +353,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
if(isEmpty() || strategy.equals(FIRST_ENTRY_KEY(), key)) return false; if(isEmpty() || strategy.equals(FIRST_ENTRY_KEY(), key)) return false;
if(strategy.equals(key, EMPTY_KEY_VALUE)) { if(strategy.equals(key, EMPTY_KEY_VALUE)) {
if(containsNull) { if(containsNull) {
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -356,7 +361,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], EMPTY_KEY_VALUE)) { while(!strategy.equals(keys[pos], EMPTY_KEY_VALUE)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -370,7 +375,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
if(isEmpty() || strategy.equals(LAST_ENTRY_KEY(), key)) return false; if(isEmpty() || strategy.equals(LAST_ENTRY_KEY(), key)) return false;
if(strategy.equals(key, EMPTY_KEY_VALUE)) { if(strategy.equals(key, EMPTY_KEY_VALUE)) {
if(containsNull) { if(containsNull) {
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -378,7 +383,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], EMPTY_KEY_VALUE)) { while(!strategy.equals(keys[pos], EMPTY_KEY_VALUE)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToLastIndex(pos); moveToLastIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -391,7 +396,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
public VALUE_TYPE getAndMoveToFirst(KEY_TYPE key) { public VALUE_TYPE getAndMoveToFirst(KEY_TYPE key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToFirstIndex(index); moveToFirstIndex(index, false);
return values[index]; return values[index];
} }
@@ -399,7 +404,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) { public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToLastIndex(index); moveToLastIndex(index, false);
return values[index]; return values[index];
} }
@@ -575,8 +580,8 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
containsNull = false; containsNull = false;
} }
protected void moveToFirstIndex(int startPos) { protected void moveToFirstIndex(int startPos, boolean adding) {
if(size == 1 || firstIndex == startPos) return; if(size == (adding ? 0 : 1) || firstIndex == startPos) return;
if(lastIndex == startPos) { if(lastIndex == startPos) {
lastIndex = (int)(links[startPos] >>> 32); lastIndex = (int)(links[startPos] >>> 32);
links[lastIndex] |= 0xFFFFFFFFL; links[lastIndex] |= 0xFFFFFFFFL;
@@ -593,8 +598,8 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
firstIndex = startPos; firstIndex = startPos;
} }
protected void moveToLastIndex(int startPos) { protected void moveToLastIndex(int startPos, boolean adding) {
if(size == 1 || lastIndex == startPos) return; if(size == (adding ? 0 : 1) || lastIndex == startPos) return;
if(firstIndex == startPos) { if(firstIndex == startPos) {
firstIndex = (int)links[startPos]; firstIndex = (int)links[startPos];
links[lastIndex] |= 0xFFFFFFFF00000000L; links[lastIndex] |= 0xFFFFFFFF00000000L;
@@ -708,6 +713,10 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
} }
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE { private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE {
@Override
public void addFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -873,7 +882,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; MAP.Entry KEY_VALUE_GENERIC_TYPE state = null;
boolean empty = true; boolean empty = true;
@@ -888,21 +897,21 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
state = operator.apply(state, new ValueMapEntry(index)); state = operator.apply(state, new ValueMapEntry(index));
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
int index = firstIndex; int index = firstIndex;
while(index != -1) { while(index != -1) {
entry.set(index); entry.set(index);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)links[index]; index = (int)links[index];
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1000,9 +1009,12 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void addFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@@ -1160,7 +1172,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
#endif #endif
@Override @Override
public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_KEY_VALUE; KEY_TYPE state = EMPTY_KEY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1175,19 +1187,19 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
state = operator.APPLY_KEY_VALUE(state, keys[index]); state = operator.APPLY_KEY_VALUE(state, keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_KEY_VALUE; if(size() <= 0) return OPTIONAL.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(keys[index])) return keys[index]; if(filter.test(keys[index])) return OPTIONAL.GET_OPTIONAL(keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return EMPTY_KEY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -1343,7 +1355,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
#endif #endif
@Override @Override
public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
VALUE_TYPE state = EMPTY_VALUE; VALUE_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1358,19 +1370,19 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
state = operator.APPLY_VALUE(state, values[index]); state = operator.APPLY_VALUE(state, values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state);
} }
@Override @Override
public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_VALUE; if(size() <= 0) return VALUE_OPTIONAL.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(values[index])) return values[index]; if(filter.test(values[index])) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return EMPTY_VALUE; return VALUE_OPTIONAL.empty();
} }
@Override @Override
@@ -5,14 +5,17 @@ import java.util.ConcurrentModificationException;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
#if !TYPE_OBJECT && JDK_TYPE #if !TYPE_OBJECT && JDK_TYPE
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
import java.util.OPTIONAL;
#endif #endif
#if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT #if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT
import java.util.function.VALUE_PREDICATE; import java.util.function.VALUE_PREDICATE;
import java.util.VALUE_OPTIONAL;
#endif #endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -37,9 +40,12 @@ import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
#if !SAME_TYPE #if !SAME_TYPE
import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR; import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR;
#endif #endif
#if !TYPE_OBJECT && !VALUE_BOOLEAN && !JDK_TYPE #if !TYPE_OBJECT && !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#if !VALUE_BOOLEAN
import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif #endif
#endif
import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP; import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.MAP; import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -75,6 +81,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C
#endif #endif
#if !JDK_VALUE #if !JDK_VALUE
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE;
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_OPTIONAL;
#endif #endif
#endif #endif
import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.collections.ObjectIterator;
@@ -1090,7 +1097,7 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; MAP.Entry KEY_VALUE_GENERIC_TYPE state = null;
boolean empty = true; boolean empty = true;
@@ -1107,25 +1114,25 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
} }
state = operator.apply(state, new ValueMapEntry(i)); state = operator.apply(state, new ValueMapEntry(i));
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
if(containsNull) { if(containsNull) {
entry.set(nullIndex); entry.set(nullIndex);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE)) { if(!strategy.equals(keys[i], EMPTY_KEY_VALUE)) {
entry.set(i); entry.set(i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1331,7 +1338,7 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
#endif #endif
@Override @Override
public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_KEY_VALUE; KEY_TYPE state = EMPTY_KEY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1348,18 +1355,18 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
} }
state = operator.APPLY_KEY_VALUE(state, keys[i]); state = operator.APPLY_KEY_VALUE(state, keys[i]);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_KEY_VALUE; if(size() <= 0) return OPTIONAL.empty();
if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; if(containsNull && filter.test(keys[nullIndex])) return OPTIONAL.GET_OPTIONAL(keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.test(keys[i])) return keys[i]; if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.test(keys[i])) return OPTIONAL.GET_OPTIONAL(keys[i]);
} }
return EMPTY_KEY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -1497,7 +1504,7 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
#endif #endif
@Override @Override
public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
VALUE_TYPE state = EMPTY_VALUE; VALUE_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1514,18 +1521,18 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
} }
state = operator.APPLY_VALUE(state, values[i]); state = operator.APPLY_VALUE(state, values[i]);
} }
return state; return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state);
} }
@Override @Override
public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_VALUE; if(size() <= 0) return VALUE_OPTIONAL.empty();
if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; if(containsNull && filter.test(values[nullIndex])) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(values[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.test(values[i])) return values[i]; if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.test(values[i])) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(values[i]);
} }
return EMPTY_VALUE; return VALUE_OPTIONAL.empty();
} }
@Override @Override
@@ -4,14 +4,17 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
#if !TYPE_OBJECT && JDK_TYPE #if !TYPE_OBJECT && JDK_TYPE
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
import java.util.OPTIONAL;
#endif #endif
#if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT #if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT
import java.util.function.VALUE_PREDICATE; import java.util.function.VALUE_PREDICATE;
import java.util.VALUE_OPTIONAL;
#endif #endif
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
@@ -27,6 +30,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
import speiger.src.collections.ints.functions.consumer.VALUE_BI_FROM_INT_CONSUMER; import speiger.src.collections.ints.functions.consumer.VALUE_BI_FROM_INT_CONSUMER;
#endif #endif
#if !TYPE_OBJECT && !JDK_TYPE #if !TYPE_OBJECT && !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif #endif
#if !TYPE_INT || !SAME_TYPE #if !TYPE_INT || !SAME_TYPE
@@ -58,6 +62,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C
#endif #endif
#if !JDK_VALUE #if !JDK_VALUE
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE;
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_OPTIONAL;
#endif #endif
#endif #endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -210,13 +215,13 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
if(containsNull) { if(containsNull) {
VALUE_TYPE lastValue = values[nullIndex]; VALUE_TYPE lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask; int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask;
@@ -224,7 +229,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
if(KEY_EQUALS(keys[pos], key)) { if(KEY_EQUALS(keys[pos], key)) {
VALUE_TYPE lastValue = values[pos]; VALUE_TYPE lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -232,7 +237,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -244,13 +249,13 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
if(containsNull) { if(containsNull) {
VALUE_TYPE lastValue = values[nullIndex]; VALUE_TYPE lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask; int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask;
@@ -258,7 +263,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
if(KEY_EQUALS(keys[pos], key)) { if(KEY_EQUALS(keys[pos], key)) {
VALUE_TYPE lastValue = values[pos]; VALUE_TYPE lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToLastIndex(pos); moveToLastIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -266,7 +271,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -279,18 +284,18 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask; int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask;
while(KEY_EQUALS_NULL(key)) { while(KEY_EQUALS_NOT_NULL(keys[pos])) {
if(KEY_EQUALS(keys[pos], key)) return values[pos]; if(KEY_EQUALS(keys[pos], key)) return values[pos];
pos = ++pos & mask; pos = ++pos & mask;
} }
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -303,18 +308,18 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask; int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask;
while(KEY_EQUALS_NULL(key)) { while(KEY_EQUALS_NOT_NULL(keys[pos])) {
if(KEY_EQUALS(keys[pos], key)) return values[pos]; if(KEY_EQUALS(keys[pos], key)) return values[pos];
pos = ++pos & mask; pos = ++pos & mask;
} }
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -325,7 +330,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
if(isEmpty() || KEY_EQUALS(FIRST_ENTRY_KEY(), key)) return false; if(isEmpty() || KEY_EQUALS(FIRST_ENTRY_KEY(), key)) return false;
if(KEY_EQUALS_NULL(key)) { if(KEY_EQUALS_NULL(key)) {
if(containsNull) { if(containsNull) {
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -333,7 +338,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask; int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask;
while(KEY_EQUALS_NOT_NULL(keys[pos])) { while(KEY_EQUALS_NOT_NULL(keys[pos])) {
if(KEY_EQUALS(keys[pos], key)) { if(KEY_EQUALS(keys[pos], key)) {
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -347,7 +352,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
if(isEmpty() || KEY_EQUALS(LAST_ENTRY_KEY(), key)) return false; if(isEmpty() || KEY_EQUALS(LAST_ENTRY_KEY(), key)) return false;
if(KEY_EQUALS_NULL(key)) { if(KEY_EQUALS_NULL(key)) {
if(containsNull) { if(containsNull) {
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -355,7 +360,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask; int pos = HashUtil.mix(KEY_TO_HASH(key)) & mask;
while(KEY_EQUALS_NOT_NULL(keys[pos])) { while(KEY_EQUALS_NOT_NULL(keys[pos])) {
if(KEY_EQUALS(keys[pos], key)) { if(KEY_EQUALS(keys[pos], key)) {
moveToLastIndex(pos); moveToLastIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -368,7 +373,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
public VALUE_TYPE getAndMoveToFirst(KEY_TYPE key) { public VALUE_TYPE getAndMoveToFirst(KEY_TYPE key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToFirstIndex(index); moveToFirstIndex(index, false);
return values[index]; return values[index];
} }
@@ -376,7 +381,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) { public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToLastIndex(index); moveToLastIndex(index, false);
return values[index]; return values[index];
} }
@@ -579,8 +584,8 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
containsNull = false; containsNull = false;
} }
protected void moveToFirstIndex(int startPos) { protected void moveToFirstIndex(int startPos, boolean adding) {
if(size == 1 || firstIndex == startPos) return; if(size == (adding ? 0 : 1) || firstIndex == startPos) return;
if(lastIndex == startPos) { if(lastIndex == startPos) {
lastIndex = (int)(links[startPos] >>> 32); lastIndex = (int)(links[startPos] >>> 32);
links[lastIndex] |= 0xFFFFFFFFL; links[lastIndex] |= 0xFFFFFFFFL;
@@ -597,8 +602,8 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
firstIndex = startPos; firstIndex = startPos;
} }
protected void moveToLastIndex(int startPos) { protected void moveToLastIndex(int startPos, boolean adding) {
if(size == 1 || lastIndex == startPos) return; if(size == (adding ? 0 : 1) || lastIndex == startPos) return;
if(firstIndex == startPos) { if(firstIndex == startPos) {
firstIndex = (int)links[startPos]; firstIndex = (int)links[startPos];
links[lastIndex] |= 0xFFFFFFFF00000000L; links[lastIndex] |= 0xFFFFFFFF00000000L;
@@ -712,6 +717,10 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
} }
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE { private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE {
@Override
public void addFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -877,7 +886,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; MAP.Entry KEY_VALUE_GENERIC_TYPE state = null;
boolean empty = true; boolean empty = true;
@@ -892,21 +901,21 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
state = operator.apply(state, new ValueMapEntry(index)); state = operator.apply(state, new ValueMapEntry(index));
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
int index = firstIndex; int index = firstIndex;
while(index != -1) { while(index != -1) {
entry.set(index); entry.set(index);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)links[index]; index = (int)links[index];
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -999,6 +1008,12 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
@Override @Override
public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); } public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public void addFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@@ -1159,7 +1174,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
#endif #endif
@Override @Override
public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_KEY_VALUE; KEY_TYPE state = EMPTY_KEY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1174,19 +1189,19 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
state = operator.APPLY_KEY_VALUE(state, keys[index]); state = operator.APPLY_KEY_VALUE(state, keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_KEY_VALUE; if(size() <= 0) return OPTIONAL.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(keys[index])) return keys[index]; if(filter.test(keys[index])) return OPTIONAL.GET_OPTIONAL(keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return EMPTY_KEY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -1344,7 +1359,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
#endif #endif
@Override @Override
public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
VALUE_TYPE state = EMPTY_VALUE; VALUE_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1359,19 +1374,19 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
state = operator.APPLY_VALUE(state, values[index]); state = operator.APPLY_VALUE(state, values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state);
} }
@Override @Override
public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_VALUE; if(size() <= 0) return VALUE_OPTIONAL.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(values[index])) return values[index]; if(filter.test(values[index])) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return EMPTY_VALUE; return VALUE_OPTIONAL.empty();
} }
@Override @Override
@@ -5,14 +5,17 @@ import java.util.ConcurrentModificationException;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
#if !TYPE_OBJECT && JDK_TYPE #if !TYPE_OBJECT && JDK_TYPE
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
import java.util.OPTIONAL;
#endif #endif
#if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT #if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT
import java.util.function.VALUE_PREDICATE; import java.util.function.VALUE_PREDICATE;
import java.util.VALUE_OPTIONAL;
#endif #endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -37,9 +40,12 @@ import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
#if !SAME_TYPE #if !SAME_TYPE
import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR; import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR;
#endif #endif
#if !TYPE_OBJECT && !VALUE_BOOLEAN && !JDK_TYPE #if !TYPE_OBJECT && !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#if !VALUE_BOOLEAN
import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif #endif
#endif
import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP; import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.MAP; import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -73,6 +79,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C
#if !JDK_VALUE #if !JDK_VALUE
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE;
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_OPTIONAL;
#endif #endif
#endif #endif
import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.collections.ObjectIterator;
@@ -1049,7 +1056,7 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; MAP.Entry KEY_VALUE_GENERIC_TYPE state = null;
boolean empty = true; boolean empty = true;
@@ -1066,25 +1073,25 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE
} }
state = operator.apply(state, new ValueMapEntry(i)); state = operator.apply(state, new ValueMapEntry(i));
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
if(containsNull) { if(containsNull) {
entry.set(nullIndex); entry.set(nullIndex);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(KEY_EQUALS_NOT_NULL(keys[i])) { if(KEY_EQUALS_NOT_NULL(keys[i])) {
entry.set(i); entry.set(i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1286,7 +1293,7 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE
#endif #endif
@Override @Override
public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_KEY_VALUE; KEY_TYPE state = EMPTY_KEY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1303,18 +1310,18 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE
} }
state = operator.APPLY_KEY_VALUE(state, keys[i]); state = operator.APPLY_KEY_VALUE(state, keys[i]);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_KEY_VALUE; if(size() <= 0) return OPTIONAL.empty();
if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; if(containsNull && filter.test(keys[nullIndex])) return OPTIONAL.GET_OPTIONAL(keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.test(keys[i])) return keys[i]; if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.test(keys[i])) return OPTIONAL.GET_OPTIONAL(keys[i]);
} }
return EMPTY_KEY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -1452,7 +1459,7 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE
#endif #endif
@Override @Override
public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
VALUE_TYPE state = EMPTY_VALUE; VALUE_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1469,18 +1476,18 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE
} }
state = operator.APPLY_VALUE(state, values[i]); state = operator.APPLY_VALUE(state, values[i]);
} }
return state; return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state);
} }
@Override @Override
public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_VALUE; if(size() <= 0) return VALUE_OPTIONAL.empty();
if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; if(containsNull && filter.test(values[nullIndex])) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(values[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.test(values[i])) return values[i]; if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.test(values[i])) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(values[i]);
} }
return EMPTY_VALUE; return VALUE_OPTIONAL.empty();
} }
@Override @Override
@@ -4,14 +4,17 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
#if !TYPE_OBJECT && JDK_TYPE #if !TYPE_OBJECT && JDK_TYPE
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
import java.util.OPTIONAL;
#endif #endif
#if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT #if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT
import java.util.function.VALUE_PREDICATE; import java.util.function.VALUE_PREDICATE;
import java.util.VALUE_OPTIONAL;
#endif #endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -26,9 +29,12 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
#if !SAME_TYPE && !TYPE_INT #if !SAME_TYPE && !TYPE_INT
import speiger.src.collections.ints.functions.consumer.VALUE_BI_FROM_INT_CONSUMER; import speiger.src.collections.ints.functions.consumer.VALUE_BI_FROM_INT_CONSUMER;
#endif #endif
#if !TYPE_OBJECT && !VALUE_BOOLEAN && !JDK_TYPE #if !TYPE_OBJECT && !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#if !VALUE_BOOLEAN
import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif #endif
#endif
#if !TYPE_INT || !SAME_TYPE #if !TYPE_INT || !SAME_TYPE
import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER; import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER;
#endif #endif
@@ -59,6 +65,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C
#endif #endif
#if !JDK_VALUE #if !JDK_VALUE
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE;
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_OPTIONAL;
#endif #endif
#endif #endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -630,6 +637,10 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
} }
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE { private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE {
@Override
public void addFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -783,7 +794,7 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; MAP.Entry KEY_VALUE_GENERIC_TYPE state = null;
boolean empty = true; boolean empty = true;
@@ -798,21 +809,21 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
state = operator.apply(state, new BasicEntryKV_BRACES(keys[index], values[index])); state = operator.apply(state, new BasicEntryKV_BRACES(keys[index], values[index]));
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
int index = firstIndex; int index = firstIndex;
while(index != -1) { while(index != -1) {
entry.set(keys[index], values[index]); entry.set(keys[index], values[index]);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)links[index]; index = (int)links[index];
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -887,6 +898,12 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void addFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@@ -1037,7 +1054,7 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
#endif #endif
@Override @Override
public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_KEY_VALUE; KEY_TYPE state = EMPTY_KEY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1052,19 +1069,19 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
state = operator.APPLY_KEY_VALUE(state, keys[index]); state = operator.APPLY_KEY_VALUE(state, keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_KEY_VALUE; if(size() <= 0) return OPTIONAL.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(keys[index])) return keys[index]; if(filter.test(keys[index])) return OPTIONAL.GET_OPTIONAL(keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return EMPTY_KEY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -1213,7 +1230,7 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
#endif #endif
@Override @Override
public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
VALUE_TYPE state = EMPTY_VALUE; VALUE_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1228,19 +1245,19 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
state = operator.APPLY_VALUE(state, values[index]); state = operator.APPLY_VALUE(state, values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state);
} }
@Override @Override
public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_VALUE; if(size() <= 0) return VALUE_OPTIONAL.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(values[index])) return values[index]; if(filter.test(values[index])) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return EMPTY_VALUE; return VALUE_OPTIONAL.empty();
} }
@Override @Override
@@ -4,14 +4,17 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
#if !TYPE_OBJECT && JDK_TYPE #if !TYPE_OBJECT && JDK_TYPE
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
import java.util.OPTIONAL;
#endif #endif
#if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT #if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT
import java.util.function.VALUE_PREDICATE; import java.util.function.VALUE_PREDICATE;
import java.util.VALUE_OPTIONAL;
#endif #endif
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
@@ -26,9 +29,12 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
#if !SAME_TYPE && !TYPE_INT #if !SAME_TYPE && !TYPE_INT
import speiger.src.collections.ints.functions.consumer.VALUE_BI_FROM_INT_CONSUMER; import speiger.src.collections.ints.functions.consumer.VALUE_BI_FROM_INT_CONSUMER;
#endif #endif
#if !TYPE_OBJECT && !VALUE_BOOLEAN && !JDK_TYPE #if !TYPE_OBJECT && !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#if!VALUE_BOOLEAN
import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif #endif
#endif
#if !TYPE_INT || !SAME_TYPE #if !TYPE_INT || !SAME_TYPE
import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER; import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER;
#endif #endif
@@ -66,6 +72,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C
#endif #endif
#if !JDK_VALUE #if !JDK_VALUE
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE;
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_OPTIONAL;
#endif #endif
#endif #endif
#if !VALUE_OBJECT #if !VALUE_OBJECT
@@ -861,6 +868,10 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
} }
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE { private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE {
@Override
public void addFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -1009,7 +1020,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; MAP.Entry KEY_VALUE_GENERIC_TYPE state = null;
boolean empty = true; boolean empty = true;
@@ -1021,19 +1032,19 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
} }
state = operator.apply(state, new ValueMapEntry(i)); state = operator.apply(state, new ValueMapEntry(i));
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
for(int i = 0;i<size;i++) { for(int i = 0;i<size;i++) {
entry.set(i); entry.set(i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1119,6 +1130,10 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
@Override @Override
public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); } public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
public void addFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@@ -1218,7 +1233,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
#endif #endif
@Override @Override
public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_KEY_VALUE; KEY_TYPE state = EMPTY_KEY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1230,16 +1245,16 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
} }
state = operator.APPLY_KEY_VALUE(state, keys[i]); state = operator.APPLY_KEY_VALUE(state, keys[i]);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) { for(int i = 0;i<size;i++) {
if(filter.test(keys[i])) return keys[i]; if(filter.test(keys[i])) return OPTIONAL.GET_OPTIONAL(keys[i]);
} }
return EMPTY_KEY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -1359,7 +1374,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
#endif #endif
@Override @Override
public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
VALUE_TYPE state = EMPTY_VALUE; VALUE_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1371,16 +1386,16 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
} }
state = operator.APPLY_VALUE(state, values[i]); state = operator.APPLY_VALUE(state, values[i]);
} }
return state; return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state);
} }
@Override @Override
public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) { for(int i = 0;i<size;i++) {
if(filter.test(values[i])) return values[i]; if(filter.test(values[i])) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(values[i]);
} }
return EMPTY_VALUE; return VALUE_OPTIONAL.empty();
} }
@Override @Override
@@ -473,6 +473,10 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
} }
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE { private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE {
@Override
public void addFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -637,7 +641,10 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
public boolean add(T o) { public boolean add(T o) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void addFirst(T o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(T o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(T o) { throw new UnsupportedOperationException(); }
@@ -6,15 +6,18 @@ import java.util.Map;
import java.util.Comparator; import java.util.Comparator;
#endif #endif
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
#if !TYPE_OBJECT && JDK_TYPE #if !TYPE_OBJECT && JDK_TYPE
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
import java.util.OPTIONAL;
#endif #endif
#if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT #if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT
import java.util.function.VALUE_PREDICATE; import java.util.function.VALUE_PREDICATE;
import java.util.VALUE_OPTIONAL;
#endif #endif
@@ -37,9 +40,12 @@ import speiger.src.collections.PACKAGE.functions.function.FUNCTION;
#if !TYPE_INT || !SAME_TYPE #if !TYPE_INT || !SAME_TYPE
import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER; import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER;
#endif #endif
#if !TYPE_OBJECT && !VALUE_BOOLEAN && !JDK_TYPE #if !TYPE_OBJECT && !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#if!VALUE_BOOLEAN
import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif #endif
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
#if !SAME_TYPE #if !SAME_TYPE
import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR; import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR;
@@ -73,6 +79,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C
#endif #endif
#if !JDK_VALUE #if !JDK_VALUE
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE;
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_OPTIONAL;
#endif #endif
#endif #endif
#if !TYPE_OBJECT && !VALUE_OBJECT #if !TYPE_OBJECT && !VALUE_OBJECT
@@ -1323,7 +1330,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
#endif #endif
@Override @Override
public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_KEY_VALUE; KEY_TYPE state = EMPTY_KEY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1335,15 +1342,15 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
} }
state = operator.APPLY_KEY_VALUE(state, entry.key); state = operator.APPLY_KEY_VALUE(state, entry.key);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry))
if(filter.test(entry.key)) return entry.key; if(filter.test(entry.key)) return OPTIONAL.GET_OPTIONAL(entry.key);
return EMPTY_KEY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -2026,7 +2033,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; MAP.Entry KEY_VALUE_GENERIC_TYPE state = null;
boolean empty = true; boolean empty = true;
@@ -2038,19 +2045,19 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
} }
state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value));
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES();
for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) {
subEntry.set(entry.key, entry.value); subEntry.set(entry.key, entry.value);
if(filter.test(subEntry)) return subEntry; if(filter.test(subEntry)) return Optional.ofNullable(subEntry);
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -2165,7 +2172,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
#endif #endif
@Override @Override
public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
VALUE_TYPE state = EMPTY_VALUE; VALUE_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -2177,15 +2184,15 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
} }
state = operator.APPLY_VALUE(state, entry.value); state = operator.APPLY_VALUE(state, entry.value);
} }
return state; return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state);
} }
@Override @Override
public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry))
if(filter.test(entry.value)) return entry.value; if(filter.test(entry.value)) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(entry.value);
return EMPTY_VALUE; return VALUE_OPTIONAL.empty();
} }
@Override @Override
@@ -2521,7 +2528,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
#endif #endif
@Override @Override
public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
VALUE_TYPE state = EMPTY_VALUE; VALUE_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -2533,15 +2540,15 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
} }
state = operator.APPLY_VALUE(state, entry.value); state = operator.APPLY_VALUE(state, entry.value);
} }
return state; return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state);
} }
@Override @Override
public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next())
if(filter.test(entry.value)) return entry.value; if(filter.test(entry.value)) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(entry.value);
return EMPTY_VALUE; return VALUE_OPTIONAL.empty();
} }
@Override @Override
@@ -2687,7 +2694,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; MAP.Entry KEY_VALUE_GENERIC_TYPE state = null;
boolean empty = true; boolean empty = true;
@@ -2699,19 +2706,19 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
} }
state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value));
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES();
for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) {
subEntry.set(entry.key, entry.value); subEntry.set(entry.key, entry.value);
if(filter.test(subEntry)) return subEntry; if(filter.test(subEntry)) return Optional.ofNullable(subEntry);
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -6,15 +6,18 @@ import java.util.Map;
import java.util.Comparator; import java.util.Comparator;
#endif #endif
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
#if !TYPE_OBJECT && JDK_TYPE #if !TYPE_OBJECT && JDK_TYPE
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
import java.util.OPTIONAL;
#endif #endif
#if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT #if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT
import java.util.function.VALUE_PREDICATE; import java.util.function.VALUE_PREDICATE;
import java.util.VALUE_OPTIONAL;
#endif #endif
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
@@ -36,9 +39,12 @@ import speiger.src.collections.PACKAGE.functions.function.FUNCTION;
#if !TYPE_INT || !SAME_TYPE #if !TYPE_INT || !SAME_TYPE
import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER; import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER;
#endif #endif
#if !TYPE_OBJECT && !VALUE_BOOLEAN && !JDK_TYPE #if !TYPE_OBJECT && !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#if!VALUE_BOOLEAN
import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif #endif
#endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
#if !SAME_TYPE #if !SAME_TYPE
import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR; import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR;
@@ -72,6 +78,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C
#endif #endif
#if !JDK_VALUE #if !JDK_VALUE
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE;
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_OPTIONAL;
#endif #endif
#endif #endif
#if !TYPE_OBJECT && !VALUE_OBJECT #if !TYPE_OBJECT && !VALUE_OBJECT
@@ -1379,7 +1386,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
#endif #endif
@Override @Override
public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_KEY_VALUE; KEY_TYPE state = EMPTY_KEY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1391,15 +1398,15 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
} }
state = operator.APPLY_KEY_VALUE(state, entry.key); state = operator.APPLY_KEY_VALUE(state, entry.key);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry))
if(filter.test(entry.key)) return entry.key; if(filter.test(entry.key)) return OPTIONAL.GET_OPTIONAL(entry.key);
return EMPTY_KEY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -2092,7 +2099,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; MAP.Entry KEY_VALUE_GENERIC_TYPE state = null;
boolean empty = true; boolean empty = true;
@@ -2104,19 +2111,19 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
} }
state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value));
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES();
for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) {
subEntry.set(entry.key, entry.value); subEntry.set(entry.key, entry.value);
if(filter.test(subEntry)) return subEntry; if(filter.test(subEntry)) return Optional.ofNullable(subEntry);
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -2231,7 +2238,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
#endif #endif
@Override @Override
public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
VALUE_TYPE state = EMPTY_VALUE; VALUE_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -2243,15 +2250,15 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
} }
state = operator.APPLY_VALUE(state, entry.value); state = operator.APPLY_VALUE(state, entry.value);
} }
return state; return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state);
} }
@Override @Override
public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry))
if(filter.test(entry.value)) return entry.value; if(filter.test(entry.value)) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(entry.value);
return EMPTY_VALUE; return VALUE_OPTIONAL.empty();
} }
@Override @Override
@@ -2587,7 +2594,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
#endif #endif
@Override @Override
public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
VALUE_TYPE state = EMPTY_VALUE; VALUE_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -2599,15 +2606,15 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
} }
state = operator.APPLY_VALUE(state, entry.value); state = operator.APPLY_VALUE(state, entry.value);
} }
return state; return empty ? VALUE_OPTIONAL.empty() : VALUE_OPTIONAL.GET_OPTIONAL_VALUE(state);
} }
@Override @Override
public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { public VALUE_OPTIONAL VALUE_GENERIC_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next())
if(filter.test(entry.value)) return entry.value; if(filter.test(entry.value)) return VALUE_OPTIONAL.GET_OPTIONAL_VALUE(entry.value);
return EMPTY_VALUE; return VALUE_OPTIONAL.empty();
} }
@Override @Override
@@ -2753,7 +2760,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; MAP.Entry KEY_VALUE_GENERIC_TYPE state = null;
boolean empty = true; boolean empty = true;
@@ -2765,19 +2772,19 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
} }
state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value));
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) { public Optional<MAP.Entry KEY_VALUE_GENERIC_TYPE> findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES();
for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) {
subEntry.set(entry.key, entry.value); subEntry.set(entry.key, entry.value);
if(filter.test(subEntry)) return subEntry; if(filter.test(subEntry)) return Optional.ofNullable(subEntry);
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -7,12 +7,26 @@ import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
#endif #endif
import java.util.Objects; import java.util.Objects;
#if JDK_TYPE
import java.util.OPTIONAL;
#if SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if !TYPE_OBJECT
import java.util.stream.JAVA_STREAM;
#else
import java.util.stream.Stream;
import java.util.stream.Collector;
#endif
#endif
#endif
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
#if JDK_FUNCTION #if JDK_FUNCTION
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
#endif #endif
import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.CONSUMER;
@@ -99,6 +113,47 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE K
this(MIN_CAPACITY); this(MIN_CAPACITY);
} }
#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
/**
* Creates a Collector for a ArrayFIFOQueue
* @Type(T)
* @return a collector
*/
public static <T> Collector<T, ARRAY_FIFO_QUEUE<T>, ARRAY_FIFO_QUEUE<T>> toQueue() {
return Collector.of(ARRAY_FIFO_QUEUE::new, ARRAY_FIFO_QUEUE::enqueue, ARRAY_FIFO_QUEUE::merge);
}
/**
* Collects a Stream to a ArrayFIFOQueue
* @Type(T)
* @return a queue with the contents of the Stream
*/
public static <T> ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE toQueue(Stream<T> stream) {
return stream.collect(ARRAY_FIFO_QUEUE::new, ARRAY_FIFO_QUEUE::enqueue, ARRAY_FIFO_QUEUE::merge);
}
private ARRAY_FIFO_QUEUE<T> merge(ARRAY_FIFO_QUEUE<T> a) {
enqueueAll(a.toArray((T[])new Object[a.size()]));
return this;
}
#else
/**
* Collects a Stream to a ArrayFIFOQueue
* @return a queue with the contents of the Stream
*/
public static ARRAY_FIFO_QUEUE toQueue(JAVA_STREAM stream) {
return stream.collect(ARRAY_FIFO_QUEUE::new, ARRAY_FIFO_QUEUE::enqueue, ARRAY_FIFO_QUEUE::merge);
}
private ARRAY_FIFO_QUEUE merge(ARRAY_FIFO_QUEUE a) {
enqueueAll(a.TO_ARRAY());
return this;
}
#endif
#endif
@Override @Override
public ITERATOR KEY_GENERIC_TYPE iterator() { public ITERATOR KEY_GENERIC_TYPE iterator() {
return new Iter(); return new Iter();
@@ -318,17 +373,17 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE K
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0,m=size();i<m;i++) { for(int i = 0,m=size();i<m;i++) {
int index = (first + i) % array.length; int index = (first + i) % array.length;
if(filter.test(array[index])) { if(filter.test(array[index])) {
KEY_TYPE data = array[index]; KEY_TYPE data = array[index];
removeIndex(index); removeIndex(index);
return data; return OPTIONAL.GET_OPTIONAL(data);
} }
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -355,7 +410,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE K
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -367,7 +422,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE K
} }
state = operator.APPLY_VALUE(state, array[(first + i) % array.length]); state = operator.APPLY_VALUE(state, array[(first + i) % array.length]);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
@@ -8,12 +8,26 @@ import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
#endif #endif
import java.util.Objects; import java.util.Objects;
#if JDK_TYPE
import java.util.OPTIONAL;
#if SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if !TYPE_OBJECT
import java.util.stream.JAVA_STREAM;
#else
import java.util.stream.Stream;
import java.util.stream.Collector;
#endif
#endif
#endif
#if JDK_FUNCTION #if JDK_FUNCTION
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
#endif #endif
import speiger.src.collections.PACKAGE.collections.COLLECTION; import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.CONSUMER;
@@ -190,7 +204,47 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUE
queue.size = size; queue.size = size;
return queue; return queue;
} }
#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
/**
* Creates a Collector for a ArrayPriorityQueue
* @Type(T)
* @return a collector
*/
public static <T> Collector<T, ARRAY_PRIORITY_QUEUE<T>, ARRAY_PRIORITY_QUEUE<T>> toQueue() {
return Collector.of(ARRAY_PRIORITY_QUEUE::new, ARRAY_PRIORITY_QUEUE::enqueue, ARRAY_PRIORITY_QUEUE::merge);
}
/**
* Collects a Stream to a ArrayPriorityQueue
* @Type(T)
* @return a queue with the contents of the Stream
*/
public static <T> ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE toQueue(Stream<T> stream) {
return stream.collect(ARRAY_PRIORITY_QUEUE::new, ARRAY_PRIORITY_QUEUE::enqueue, ARRAY_PRIORITY_QUEUE::merge);
}
private ARRAY_PRIORITY_QUEUE<T> merge(ARRAY_PRIORITY_QUEUE<T> a) {
enqueueAll(a.toArray((T[])new Object[a.size()]));
return this;
}
#else
/**
* Collects a Stream to a ArrayPriorityQueue
* @return a queue with the contents of the Stream
*/
public static ARRAY_PRIORITY_QUEUE toQueue(JAVA_STREAM stream) {
return stream.collect(ARRAY_PRIORITY_QUEUE::new, ARRAY_PRIORITY_QUEUE::enqueue, ARRAY_PRIORITY_QUEUE::merge);
}
private ARRAY_PRIORITY_QUEUE merge(ARRAY_PRIORITY_QUEUE a) {
enqueueAll(a.TO_ARRAY());
return this;
}
#endif
#endif
@Override @Override
public void enqueue(KEY_TYPE e) { public void enqueue(KEY_TYPE e) {
if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (long)(array.length >> 1), (long)SanityChecks.MAX_ARRAY_SIZE), size+1)); if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (long)(array.length >> 1), (long)SanityChecks.MAX_ARRAY_SIZE), size+1));
@@ -346,7 +400,7 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUE
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -358,20 +412,20 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUE
} }
state = operator.APPLY_VALUE(state, array[i]); state = operator.APPLY_VALUE(state, array[i]);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) { for(int i = 0;i<size;i++) {
if(filter.test(array[i])) { if(filter.test(array[i])) {
KEY_TYPE data = array[i]; KEY_TYPE data = array[i];
removeIndex(i); removeIndex(i);
return data; return OPTIONAL.GET_OPTIONAL(data);
} }
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -8,12 +8,26 @@ import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
#endif #endif
import java.util.Objects; import java.util.Objects;
#if JDK_TYPE
import java.util.OPTIONAL;
#if SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if !TYPE_OBJECT
import java.util.stream.JAVA_STREAM;
#else
import java.util.stream.Stream;
import java.util.stream.Collector;
#endif
#endif
#endif
#if JDK_FUNCTION #if JDK_FUNCTION
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
#endif #endif
import speiger.src.collections.PACKAGE.collections.COLLECTION; import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.CONSUMER;
@@ -193,6 +207,47 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEU
return queue; return queue;
} }
#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
/**
* Creates a Collector for a ArrayFIFOQueue
* @Type(T)
* @return a collector
*/
public static <T> Collector<T, HEAP_PRIORITY_QUEUE<T>, HEAP_PRIORITY_QUEUE<T>> toQueue() {
return Collector.of(HEAP_PRIORITY_QUEUE::new, HEAP_PRIORITY_QUEUE::enqueue, HEAP_PRIORITY_QUEUE::merge);
}
/**
* Collects a Stream to a ArrayFIFOQueue
* @Type(T)
* @return a queue with the contents of the Stream
*/
public static <T> HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE toQueue(Stream<T> stream) {
return stream.collect(HEAP_PRIORITY_QUEUE::new, HEAP_PRIORITY_QUEUE::enqueue, HEAP_PRIORITY_QUEUE::merge);
}
private HEAP_PRIORITY_QUEUE<T> merge(HEAP_PRIORITY_QUEUE<T> a) {
enqueueAll(a.toArray((T[])new Object[a.size()]));
return this;
}
#else
/**
* Collects a Stream to a ArrayFIFOQueue
* @return a queue with the contents of the Stream
*/
public static HEAP_PRIORITY_QUEUE toQueue(JAVA_STREAM stream) {
return stream.collect(HEAP_PRIORITY_QUEUE::new, HEAP_PRIORITY_QUEUE::enqueue, HEAP_PRIORITY_QUEUE::merge);
}
private HEAP_PRIORITY_QUEUE merge(HEAP_PRIORITY_QUEUE a) {
enqueueAll(a.TO_ARRAY());
return this;
}
#endif
#endif
@Override @Override
public int size() { public int size() {
return size; return size;
@@ -326,7 +381,7 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEU
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -338,20 +393,20 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEU
} }
state = operator.APPLY_VALUE(state, array[i]); state = operator.APPLY_VALUE(state, array[i]);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) { for(int i = 0;i<size;i++) {
if(filter.test(array[i])) { if(filter.test(array[i])) {
KEY_TYPE data = array[i]; KEY_TYPE data = array[i];
removeIndex(i); removeIndex(i);
return data; return OPTIONAL.GET_OPTIONAL(data);
} }
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -10,6 +10,15 @@ import java.util.function.BiFunction;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
#if JDK_TYPE
import java.util.OPTIONAL;
#if !TYPE_OBJECT
import java.util.stream.JAVA_STREAM;
#else
import java.util.stream.Stream;
import java.util.stream.Collector;
#endif
#endif
#if JDK_FUNCTION #if JDK_FUNCTION
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
#endif #endif
@@ -19,6 +28,9 @@ import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif #endif
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER; import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION #if !JDK_FUNCTION
@@ -208,6 +220,42 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
while(iterator.hasNext()) add(iterator.NEXT()); while(iterator.hasNext()) add(iterator.NEXT());
} }
#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
/**
* Creates a Collector for a AVLTreeSet
* @Type(T)
* @return a collector
*/
public static <T> Collector<T, AVL_TREE_SET<T>, AVL_TREE_SET<T>> toSet() {
return Collector.of(AVL_TREE_SET::new, AVL_TREE_SET::add, AVL_TREE_SET::merge);
}
/**
* Collects a Stream to a AVLTreeSet
* @Type(T)
* @return a set with the contents of the Stream
*/
public static <T> AVL_TREE_SET KEY_GENERIC_TYPE toSet(Stream<T> stream) {
return stream.collect(AVL_TREE_SET::new, AVL_TREE_SET::add, AVL_TREE_SET::merge);
}
private AVL_TREE_SET<T> merge(AVL_TREE_SET<T> a) {
addAll(a);
return this;
}
#else
/**
* Collects a Stream to a AVLTreeSet
* @return a set with the contents of the Stream
*/
public static AVL_TREE_SET toSet(JAVA_STREAM stream) {
return stream.collect(AVL_TREE_SET::new, AVL_TREE_SET::add, AVL_TREE_SET::addAll);
}
#endif
#endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
@Override @Override
public void setDefaultMaxValue(KEY_TYPE value) { defaultMaxNotFound = value; } public void setDefaultMaxValue(KEY_TYPE value) { defaultMaxNotFound = value; }
@@ -405,12 +453,12 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) {
if(filter.test(entry.key)) return entry.key; if(filter.test(entry.key)) return OPTIONAL.GET_OPTIONAL(entry.key);
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -437,7 +485,7 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -449,7 +497,7 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
} }
state = operator.APPLY_VALUE(state, entry.key); state = operator.APPLY_VALUE(state, entry.key);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
@@ -1314,7 +1362,7 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1326,16 +1374,16 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
} }
state = operator.APPLY_VALUE(state, entry.key); state = operator.APPLY_VALUE(state, entry.key);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(Entry KEY_GENERIC_TYPE entry = start();entry != null && inRange(entry.key);entry = next(entry)) { for(Entry KEY_GENERIC_TYPE entry = start();entry != null && inRange(entry.key);entry = next(entry)) {
if(filter.test(entry.key)) return entry.key; if(filter.test(entry.key)) return OPTIONAL.GET_OPTIONAL(entry.key);
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -74,6 +74,16 @@ public abstract class ABSTRACT_SET KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
return set.iterator(); return set.iterator();
} }
@Override
public void addFirst(KEY_TYPE o) {
set.addLast(o);
}
@Override
public void addLast(KEY_TYPE o) {
set.addFirst(o);
}
@Override @Override
#if !TYPE_OBJECT #if !TYPE_OBJECT
public boolean remove(KEY_TYPE o) { public boolean remove(KEY_TYPE o) {
@@ -8,6 +8,15 @@ import java.util.function.BiFunction;
#endif #endif
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
#if JDK_TYPE
import java.util.OPTIONAL;
#if !TYPE_OBJECT
import java.util.stream.JAVA_STREAM;
#else
import java.util.stream.Stream;
import java.util.stream.Collector;
#endif
#endif
import java.util.Set; import java.util.Set;
#if JDK_FUNCTION #if JDK_FUNCTION
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
@@ -18,7 +27,9 @@ import java.util.function.JAVA_PREDICATE;
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
import speiger.src.collections.PACKAGE.collections.COLLECTION; import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER; import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION #if !JDK_FUNCTION
@@ -69,7 +80,7 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
* @param array the array that should be used for set. * @param array the array that should be used for set.
*/ */
public ARRAY_SET(KEY_TYPE[] array) { public ARRAY_SET(KEY_TYPE[] array) {
this(array, array.length); this(array, 0, array.length);
} }
/** /**
@@ -83,6 +94,18 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
addAll(array, length); addAll(array, length);
} }
/**
* Constructur using initial Array
* @param array the array that should be used for set.
* @param offset the starting offset of where the array should be copied from
* @param length the amount of elements present within the array
* @throws NegativeArraySizeException if the length is negative
*/
public ARRAY_SET(KEY_TYPE[] array, int offset, int length) {
this(length);
addAll(array, offset, length);
}
/** /**
* A Helper constructor that allows to create a Set with exactly the same values as the provided collection. * A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
* @param c the elements that should be added to the set. * @param c the elements that should be added to the set.
@@ -126,6 +149,42 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
for(ITERATOR KEY_GENERIC_TYPE iter = s.iterator();iter.hasNext();data[size++] = iter.NEXT()); for(ITERATOR KEY_GENERIC_TYPE iter = s.iterator();iter.hasNext();data[size++] = iter.NEXT());
} }
#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
/**
* Creates a Collector for a ArraySet
* @Type(T)
* @return a collector
*/
public static <T> Collector<T, ARRAY_SET<T>, ARRAY_SET<T>> toList() {
return Collector.of(ARRAY_SET::new, ARRAY_SET::add, ARRAY_SET::merge);
}
/**
* Collects a Stream to a ArraySet
* @Type(T)
* @return a set with the contents of the Stream
*/
public static <T> ARRAY_SET KEY_GENERIC_TYPE toList(Stream<T> stream) {
return stream.collect(ARRAY_SET::new, ARRAY_SET::add, ARRAY_SET::merge);
}
private ARRAY_SET<T> merge(ARRAY_SET<T> a) {
addAll(a);
return this;
}
#else
/**
* Collects a Stream to a ArraySet
* @return a set with the contents of the Stream
*/
public static ARRAY_SET toList(JAVA_STREAM stream) {
return stream.collect(ARRAY_SET::new, ARRAY_SET::add, ARRAY_SET::addAll);
}
#endif
#endif
@Override @Override
public boolean add(KEY_TYPE o) { public boolean add(KEY_TYPE o) {
int index = findIndex(o); int index = findIndex(o);
@@ -148,6 +207,25 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
} }
#endif #endif
@Override
public void addFirst(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, 0, data, 1, size++);
data[0] = o;
}
}
@Override
public void addLast(KEY_TYPE o) {
int index = findIndex(o);
if(index == -1) {
if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2);
data[size++] = o;
}
}
@Override @Override
public boolean addAndMoveToFirst(KEY_TYPE o) { public boolean addAndMoveToFirst(KEY_TYPE o) {
int index = findIndex(o); int index = findIndex(o);
@@ -438,12 +516,12 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) { for(int i = 0;i<size;i++) {
if(filter.test(data[i])) return data[i]; if(filter.test(data[i])) return OPTIONAL.GET_OPTIONAL(data[i]);
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
#if !TYPE_OBJECT #if !TYPE_OBJECT
@@ -470,7 +548,7 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -482,7 +560,7 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
} }
state = operator.APPLY_VALUE(state, data[i]); state = operator.APPLY_VALUE(state, data[i]);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
@@ -9,6 +9,9 @@ import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
#if JDK_TYPE
import java.util.OPTIONAL;
#endif
#if JDK_FUNCTION #if JDK_FUNCTION
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
#endif #endif
@@ -19,6 +22,9 @@ import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif #endif
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER; import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION #if !JDK_FUNCTION
@@ -258,6 +264,10 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
@Override @Override
public boolean addAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); } public boolean addAll(COLLECTION KEY_GENERIC_TYPE c) { throw new UnsupportedOperationException(); }
@Override @Override
public void addFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@@ -414,7 +424,7 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -427,18 +437,18 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
else state = operator.APPLY_VALUE(state, keys[index]); else state = operator.APPLY_VALUE(state, keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
int index = firstIndex; int index = firstIndex;
while(index != -1) { while(index != -1) {
if(filter.test(keys[index])) return keys[index]; if(filter.test(keys[index])) return OPTIONAL.GET_OPTIONAL(keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -9,6 +9,15 @@ import java.util.Objects;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
#if JDK_TYPE
import java.util.OPTIONAL;
#if !TYPE_OBJECT
import java.util.stream.JAVA_STREAM;
#else
import java.util.stream.Stream;
import java.util.stream.Collector;
#endif
#endif
#if JDK_FUNCTION #if JDK_FUNCTION
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
#endif #endif
@@ -18,6 +27,9 @@ import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.ITERATOR;
#endif #endif
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR; import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.utils.ITERATORS; import speiger.src.collections.PACKAGE.utils.ITERATORS;
@@ -235,6 +247,82 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
} }
#endif #endif
#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
/**
* Creates a Collector for a LinkedCustomHashSet
* @Type(T)
* @return a collector
*/
public static <T> Collector<T, LINKED_CUSTOM_HASH_SET<T>, LINKED_CUSTOM_HASH_SET<T>> toLinkedSet(STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
return Collector.of(() -> new LINKED_CUSTOM_HASH_SET<>(strategy), LINKED_CUSTOM_HASH_SET::add, LINKED_CUSTOM_HASH_SET::merge);
}
/**
* Collects a Stream to a LinkedCustomHashSet
* @Type(T)
* @return a set with the contents of the Stream
*/
public static <T> LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE toLinkedSet(Stream<T> stream, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
return stream.collect(() -> new LINKED_CUSTOM_HASH_SET<>(strategy), LINKED_CUSTOM_HASH_SET::add, LINKED_CUSTOM_HASH_SET::merge);
}
private LINKED_CUSTOM_HASH_SET<T> merge(LINKED_CUSTOM_HASH_SET<T> a) {
addAll(a);
return this;
}
#else
/**
* Collects a Stream to a LinkedCustomHashSet
* @return a set with the contents of the Stream
*/
public static LINKED_CUSTOM_HASH_SET toList(JAVA_STREAM stream, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
return stream.collect(() -> new LINKED_CUSTOM_HASH_SET(strategy), LINKED_CUSTOM_HASH_SET::add, LINKED_CUSTOM_HASH_SET::addAll);
}
#endif
#endif
@Override
public void addFirst(KEY_TYPE o) {
if(strategy.equals(o, EMPTY_KEY_VALUE)) {
if(containsNull) return;
containsNull = true;
onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex);
}
else {
int pos = HashUtil.mix(strategy.hashCode(o)) & mask;
while(!strategy.equals(keys[pos], EMPTY_KEY_VALUE)) {
if(strategy.equals(keys[pos], o)) return;
pos = ++pos & mask;
}
keys[pos] = o;
onNodeAdded(pos);
moveToFirstIndex(pos);
}
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
}
@Override
public void addLast(KEY_TYPE o) {
if(strategy.equals(o, EMPTY_KEY_VALUE)) {
if(containsNull) return;
containsNull = true;
onNodeAdded(nullIndex);
}
else {
int pos = HashUtil.mix(strategy.hashCode(o)) & mask;
while(!strategy.equals(keys[pos], EMPTY_KEY_VALUE)) {
if(strategy.equals(keys[pos], o)) return;
pos = ++pos & mask;
}
keys[pos] = o;
onNodeAdded(pos);
}
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
}
@Override @Override
public boolean addAndMoveToFirst(KEY_TYPE o) { public boolean addAndMoveToFirst(KEY_TYPE o) {
if(strategy.equals(o, EMPTY_KEY_VALUE)) { if(strategy.equals(o, EMPTY_KEY_VALUE)) {
@@ -656,7 +744,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -669,18 +757,18 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
else state = operator.APPLY_VALUE(state, keys[index]); else state = operator.APPLY_VALUE(state, keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
int index = firstIndex; int index = firstIndex;
while(index != -1) { while(index != -1) {
if(filter.test(keys[index])) return keys[index]; if(filter.test(keys[index])) return OPTIONAL.GET_OPTIONAL(keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -5,6 +5,15 @@ import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
#if JDK_TYPE
import java.util.OPTIONAL;
#if !TYPE_OBJECT
import java.util.stream.JAVA_STREAM;
#else
import java.util.stream.Stream;
import java.util.stream.Collector;
#endif
#endif
#if TYPE_OBJECT #if TYPE_OBJECT
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
@@ -21,6 +30,9 @@ import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif #endif
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER; import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION #if !JDK_FUNCTION
@@ -206,6 +218,82 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
} }
#endif #endif
#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
/**
* Creates a Collector for a LinkedHashSet
* @Type(T)
* @return a collector
*/
public static <T> Collector<T, LINKED_HASH_SET<T>, LINKED_HASH_SET<T>> toLinkedSet() {
return Collector.of(LINKED_HASH_SET::new, LINKED_HASH_SET::add, LINKED_HASH_SET::merge);
}
/**
* Collects a Stream to a LinkedHashSet
* @Type(T)
* @return a set with the contents of the Stream
*/
public static <T> LINKED_HASH_SET KEY_GENERIC_TYPE toLinkedSet(Stream<T> stream) {
return stream.collect(LINKED_HASH_SET::new, LINKED_HASH_SET::add, LINKED_HASH_SET::merge);
}
private LINKED_HASH_SET<T> merge(LINKED_HASH_SET<T> a) {
addAll(a);
return this;
}
#else
/**
* Collects a Stream to a LinkedHashSet
* @return a set with the contents of the Stream
*/
public static LINKED_HASH_SET toLinkedSet(JAVA_STREAM stream) {
return stream.collect(LINKED_HASH_SET::new, LINKED_HASH_SET::add, LINKED_HASH_SET::addAll);
}
#endif
#endif
@Override
public void addFirst(KEY_TYPE o) {
if(KEY_EQUALS_NULL(o)) {
if(containsNull) return;
containsNull = true;
onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex);
}
else {
int pos = HashUtil.mix(KEY_TO_HASH(o)) & mask;
while(KEY_EQUALS_NOT_NULL(keys[pos])) {
if(KEY_EQUALS(keys[pos], o)) return;
pos = ++pos & mask;
}
keys[pos] = o;
onNodeAdded(pos);
moveToFirstIndex(pos);
}
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
}
@Override
public void addLast(KEY_TYPE o) {
if(KEY_EQUALS_NULL(o)) {
if(containsNull) return;
containsNull = true;
onNodeAdded(nullIndex);
}
else {
int pos = HashUtil.mix(KEY_TO_HASH(o)) & mask;
while(KEY_EQUALS_NOT_NULL(keys[pos])) {
if(KEY_EQUALS(keys[pos], o)) return;
pos = ++pos & mask;
}
keys[pos] = o;
onNodeAdded(pos);
}
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
}
@Override @Override
public boolean addAndMoveToFirst(KEY_TYPE o) { public boolean addAndMoveToFirst(KEY_TYPE o) {
if(KEY_EQUALS_NULL(o)) { if(KEY_EQUALS_NULL(o)) {
@@ -511,7 +599,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -524,18 +612,18 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
else state = operator.APPLY_VALUE(state, keys[index]); else state = operator.APPLY_VALUE(state, keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
int index = firstIndex; int index = firstIndex;
while(index != -1) { while(index != -1) {
if(filter.test(keys[index])) return keys[index]; if(filter.test(keys[index])) return OPTIONAL.GET_OPTIONAL(keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -6,6 +6,15 @@ import java.util.ConcurrentModificationException;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
#if JDK_TYPE
import java.util.OPTIONAL;
#if !TYPE_OBJECT
import java.util.stream.JAVA_STREAM;
#else
import java.util.stream.Stream;
import java.util.stream.Collector;
#endif
#endif
#if TYPE_OBJECT #if TYPE_OBJECT
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
@@ -16,6 +25,9 @@ import java.util.function.PREDICATE;
import speiger.src.collections.PACKAGE.collections.COLLECTION; import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.utils.ITERATORS; import speiger.src.collections.PACKAGE.utils.ITERATORS;
import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.CONSUMER;
@@ -251,6 +263,42 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
while(iterator.hasNext()) add(iterator.NEXT()); while(iterator.hasNext()) add(iterator.NEXT());
} }
#endif
#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
/**
* Creates a Collector for a CustomHashSet
* @Type(T)
* @return a collector
*/
public static <T> Collector<T, CUSTOM_HASH_SET<T>, CUSTOM_HASH_SET<T>> toSet(STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
return Collector.of(() -> new CUSTOM_HASH_SET<>(strategy), CUSTOM_HASH_SET::add, CUSTOM_HASH_SET::merge);
}
/**
* Collects a Stream to a CustomHashSet
* @Type(T)
* @return a set with the contents of the Stream
*/
public static <T> CUSTOM_HASH_SET KEY_GENERIC_TYPE toSet(Stream<T> stream, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
return stream.collect(() -> new CUSTOM_HASH_SET<>(strategy), CUSTOM_HASH_SET::add, CUSTOM_HASH_SET::merge);
}
private CUSTOM_HASH_SET<T> merge(CUSTOM_HASH_SET<T> a) {
addAll(a);
return this;
}
#else
/**
* Collects a Stream to a CustomHashSet
* @return a set with the contents of the Stream
*/
public static CUSTOM_HASH_SET toSet(JAVA_STREAM stream, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
return stream.collect(() -> new CUSTOM_HASH_SET(strategy), CUSTOM_HASH_SET::add, CUSTOM_HASH_SET::addAll);
}
#endif
#endif #endif
/** /**
* Helper getter function to get the current strategy * Helper getter function to get the current strategy
@@ -635,7 +683,7 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -652,18 +700,18 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
} }
state = operator.APPLY_VALUE(state, keys[i]); state = operator.APPLY_VALUE(state, keys[i]);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_VALUE; if(size() <= 0) return OPTIONAL.empty();
if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; if(containsNull && filter.test(keys[nullIndex])) return OPTIONAL.GET_OPTIONAL(keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.test(keys[i])) return keys[i]; if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && filter.test(keys[i])) return OPTIONAL.GET_OPTIONAL(keys[i]);
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -6,6 +6,15 @@ import java.util.ConcurrentModificationException;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
#if JDK_TYPE
import java.util.OPTIONAL;
#if !TYPE_OBJECT
import java.util.stream.JAVA_STREAM;
#else
import java.util.stream.Stream;
import java.util.stream.Collector;
#endif
#endif
#if TYPE_OBJECT #if TYPE_OBJECT
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
@@ -20,6 +29,9 @@ import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.utils.ITERATORS; import speiger.src.collections.PACKAGE.utils.ITERATORS;
import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif #endif
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER; import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION #if !JDK_FUNCTION
@@ -218,6 +230,42 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
while(iterator.hasNext()) add(iterator.NEXT()); while(iterator.hasNext()) add(iterator.NEXT());
} }
#endif
#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
/**
* Creates a Collector for a HashSet
* @Type(T)
* @return a collector
*/
public static <T> Collector<T, HASH_SET<T>, HASH_SET<T>> toSet() {
return Collector.of(HASH_SET::new, HASH_SET::add, HASH_SET::merge);
}
/**
* Collects a Stream to a HashSet
* @Type(T)
* @return a set with the contents of the Stream
*/
public static <T> HASH_SET KEY_GENERIC_TYPE toSet(Stream<T> stream) {
return stream.collect(HASH_SET::new, HASH_SET::add, HASH_SET::merge);
}
private HASH_SET<T> merge(HASH_SET<T> a) {
addAll(a);
return this;
}
#else
/**
* Collects a Stream to a HashSet
* @return a set with the contents of the Stream
*/
public static HASH_SET toSet(JAVA_STREAM stream) {
return stream.collect(HASH_SET::new, HASH_SET::add, HASH_SET::addAll);
}
#endif
#endif #endif
@Override @Override
public boolean add(KEY_TYPE o) { public boolean add(KEY_TYPE o) {
@@ -498,7 +546,7 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -515,18 +563,18 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
} }
state = operator.APPLY_VALUE(state, keys[i]); state = operator.APPLY_VALUE(state, keys[i]);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return EMPTY_VALUE; if(size() <= 0) return OPTIONAL.empty();
if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; if(containsNull && filter.test(keys[nullIndex])) return OPTIONAL.GET_OPTIONAL(keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.test(keys[i])) return keys[i]; if(KEY_EQUALS_NOT_NULL(keys[i]) && filter.test(keys[i])) return OPTIONAL.GET_OPTIONAL(keys[i]);
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -5,6 +5,7 @@ import java.util.SequencedSet;
#endif #endif
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
import speiger.src.collections.PACKAGE.collections.ORDERED_COLLECTION;
#if SPLIT_ITERATOR_FEATURE #if SPLIT_ITERATOR_FEATURE
import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR; import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR;
import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS; import speiger.src.collections.PACKAGE.utils.SPLIT_ITERATORS;
@@ -23,9 +24,9 @@ import speiger.src.collections.PACKAGE.utils.SETS;
* @Type(T) * @Type(T)
*/ */
#if JAVA_VERSION>=21 #if JAVA_VERSION>=21
public interface ORDERED_SET KEY_GENERIC_TYPE extends SET KEY_GENERIC_TYPE, SequencedSet<CLASS_TYPE> public interface ORDERED_SET KEY_GENERIC_TYPE extends SET KEY_GENERIC_TYPE, ORDERED_COLLECTION KEY_GENERIC_TYPE, SequencedSet<CLASS_TYPE>
#else #else
public interface ORDERED_SET KEY_GENERIC_TYPE extends SET KEY_GENERIC_TYPE public interface ORDERED_SET KEY_GENERIC_TYPE extends SET KEY_GENERIC_TYPE, ORDERED_COLLECTION KEY_GENERIC_TYPE
#endif #endif
{ {
/** /**
@@ -2,6 +2,15 @@ package speiger.src.collections.PACKAGE.sets;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
#if JDK_TYPE
import java.util.OPTIONAL;
#if !TYPE_OBJECT
import java.util.stream.JAVA_STREAM;
#else
import java.util.stream.Stream;
import java.util.stream.Collector;
#endif
#endif
#if TYPE_OBJECT #if TYPE_OBJECT
import java.util.Comparator; import java.util.Comparator;
import java.util.function.Consumer; import java.util.function.Consumer;
@@ -19,6 +28,9 @@ import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif #endif
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER; import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION #if !JDK_FUNCTION
@@ -208,6 +220,42 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
while(iterator.hasNext()) add(iterator.NEXT()); while(iterator.hasNext()) add(iterator.NEXT());
} }
#if JDK_TYPE && SPLIT_ITERATOR_FEATURE && STREAM_FEATURE
#if TYPE_OBJECT
/**
* Creates a Collector for a RBTreeSet
* @Type(T)
* @return a collector
*/
public static <T> Collector<T, RB_TREE_SET<T>, RB_TREE_SET<T>> toSet() {
return Collector.of(RB_TREE_SET::new, RB_TREE_SET::add, RB_TREE_SET::merge);
}
/**
* Collects a Stream to a RBTreeSet
* @Type(T)
* @return a set with the contents of the Stream
*/
public static <T> RB_TREE_SET KEY_GENERIC_TYPE toSet(Stream<T> stream) {
return stream.collect(RB_TREE_SET::new, RB_TREE_SET::add, RB_TREE_SET::merge);
}
private RB_TREE_SET<T> merge(RB_TREE_SET<T> a) {
addAll(a);
return this;
}
#else
/**
* Collects a Stream to a RBTreeSet
* @return a set with the contents of the Stream
*/
public static RB_TREE_SET toSet(JAVA_STREAM stream) {
return stream.collect(RB_TREE_SET::new, RB_TREE_SET::add, RB_TREE_SET::addAll);
}
#endif
#endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
@Override @Override
public void setDefaultMaxValue(KEY_TYPE value) { defaultMaxNotFound = value; } public void setDefaultMaxValue(KEY_TYPE value) { defaultMaxNotFound = value; }
@@ -427,7 +475,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -439,16 +487,16 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
} }
state = operator.APPLY_VALUE(state, entry.key); state = operator.APPLY_VALUE(state, entry.key);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) {
if(filter.test(entry.key)) return entry.key; if(filter.test(entry.key)) return OPTIONAL.GET_OPTIONAL(entry.key);
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -1374,7 +1422,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
KEY_TYPE state = EMPTY_VALUE; KEY_TYPE state = EMPTY_VALUE;
boolean empty = true; boolean empty = true;
@@ -1386,16 +1434,16 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
} }
state = operator.APPLY_VALUE(state, entry.key); state = operator.APPLY_VALUE(state, entry.key);
} }
return state; return empty ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(state);
} }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(Entry KEY_GENERIC_TYPE entry = start();entry != null && inRange(entry.key);entry = next(entry)) { for(Entry KEY_GENERIC_TYPE entry = start();entry != null && inRange(entry.key);entry = next(entry)) {
if(filter.test(entry.key)) return entry.key; if(filter.test(entry.key)) return OPTIONAL.GET_OPTIONAL(entry.key);
} }
return EMPTY_VALUE; return OPTIONAL.empty();
} }
@Override @Override
@@ -17,6 +17,7 @@ import java.util.function.IntFunction;
#endif #endif
import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.utils.SanityChecks; import speiger.src.collections.utils.SanityChecks;
import speiger.src.collections.utils.Swapper;
/** /**
* A Helper class for Arrays * A Helper class for Arrays
@@ -337,6 +338,69 @@ public class ARRAYS
return array; return array;
} }
/**
* Simple Shuffle method for Arrays.
* With a Callback for indirect shuffling
* @param array the elements that should be shuffled
* @param swapper the callback on the swaps
* @ArrayType(T)
* @note This uses the SanityChecks#getRandom
* @return the provided sorted array
*/
public static GENERIC_KEY_BRACES KEY_TYPE[] indirectShuffle(KEY_TYPE[] array, Swapper swapper) {
return indirectShuffle(array, SanityChecks.getRandom(), swapper);
}
/**
* Simple Shuffle method for Arrays.
* With a Callback for indirect shuffling
* @param array the elements that should be shuffled
* @param random the Random Number Generator that should be used for the shuffling
* @param swapper the callback on the swaps
* @ArrayType(T)
* @return the provided sorted array
*/
public static GENERIC_KEY_BRACES KEY_TYPE[] indirectShuffle(KEY_TYPE[] array, RANDOM random, Swapper swapper) {
return indirectShuffle(array, 0, array.length, random, swapper);
}
/**
* Simple Shuffle method for Arrays.
* With a Callback for indirect shuffling
* @param array the elements that should be shuffled
* @param length the length of the array
* @param random the Random Number Generator that should be used for the shuffling
* @param swapper the callback on the swaps
* @ArrayType(T)
* @return the provided sorted array
*/
public static GENERIC_KEY_BRACES KEY_TYPE[] indirectShuffle(KEY_TYPE[] array, int length, RANDOM random, Swapper swapper) {
return indirectShuffle(array, 0, length, random, swapper);
}
/**
* Simple Shuffle method for Arrays.
* With a Callback for indirect shuffling
* @param array the elements that should be shuffled
* @param offset the start array
* @param length the length of the array
* @param random the Random Number Generator that should be used for the shuffling
* @param swapper the callback on the swaps
* @ArrayType(T)
* @return the provided sorted array
*/
public static GENERIC_KEY_BRACES KEY_TYPE[] indirectShuffle(KEY_TYPE[] array, int offset, int length, RANDOM random, Swapper swapper) {
for(int i = length-1; i>=0;i--) {
int j = offset + i;
int p = offset + random.nextInt(i + 1);
swapper.swap(j, p);
KEY_TYPE t = array[j];
array[j] = array[p];
array[p] = t;
}
return array;
}
/** /**
* Simple Array Reversal method * Simple Array Reversal method
* @param array the Array that should flip * @param array the Array that should flip
@@ -575,6 +639,55 @@ public class ARRAYS
} }
} }
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Insertion Sort,
* On top of that allows to sort other things along with it.
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @ArrayType(T)
* @return input array
*/
public static GENERIC_KEY_BRACES KEY_TYPE[] indirectInsertionSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
indirectInsertionSort(array, 0, array.length, comp, swapper);
return array;
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Insertion Sort,
* On top of that allows to sort other things along with it.
* @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @ArrayType(T)
*/
public static GENERIC_KEY_BRACES void indirectInsertionSort(KEY_TYPE[] array, int length, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
indirectInsertionSort(array, 0, length, comp, swapper);
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Insertion Sort,
* On top of that allows to sort other things along with it.
* @param array the array that needs to be sorted
* @param from where the array should be sorted from
* @param to where the array should be sorted to
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @ArrayType(T)
*/
public static GENERIC_KEY_BRACES void indirectInsertionSort(KEY_TYPE[] array, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
for (int i = from+1;i<to; i++) {
KEY_TYPE current = array[i];
int j = i - 1;
while(j >= from && comp.compare(current, array[j]) < 0) {
swapper.swap(j+1, j);
array[j+1] = array[j--];
}
array[j+1] = current;
}
}
/** /**
* Sorts an array according to the natural ascending order using InsertionSort, * Sorts an array according to the natural ascending order using InsertionSort,
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
@@ -661,6 +774,60 @@ public class ARRAYS
} }
} }
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Selection Sort,
* On top of that allows to sort other things along with it.
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @ArrayType(T)
* @return input array
*/
public static GENERIC_KEY_BRACES KEY_TYPE[] indirectSelectionSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
indirectSelectionSort(array, 0, array.length, comp, swapper);
return array;
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Selection Sort,
* On top of that allows to sort other things along with it.
* @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @ArrayType(T)
*/
public static GENERIC_KEY_BRACES void indirectSelectionSort(KEY_TYPE[] array, int length, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
indirectSelectionSort(array, 0, length, comp, swapper);
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Selection Sort,
* On top of that allows to sort other things along with it.
* @param array the array that needs to be sorted
* @param from where the array should be sorted from
* @param to where the array should be sorted to
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @ArrayType(T)
*/
public static GENERIC_KEY_BRACES void indirectSelectionSort(KEY_TYPE[] array, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
for (int i = from; i < to; i++) {
KEY_TYPE min = array[i];
int minId = i;
for(int j = i+1; j < to; j++) {
if(comp.compare(array[j], min) < 0) {
min = array[j];
minId = j;
}
}
swapper.swap(i, minId);
KEY_TYPE temp = array[i];
array[i] = min;
array[minId] = temp;
}
}
/** /**
* Sorts an array according to the natural ascending order using Selection Sort, * Sorts an array according to the natural ascending order using Selection Sort,
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
@@ -760,6 +927,72 @@ public class ARRAYS
} }
} }
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Merge Sort,
* On top of that allows to sort other things along with it.
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @ArrayType(T)
* @return input array
*/
public static GENERIC_KEY_BRACES KEY_TYPE[] indirectMergeSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
indirectMergeSort(array, null, 0, array.length, comp, swapper);
return array;
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Merge Sort,
* On top of that allows to sort other things along with it.
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
* @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @ArrayType(T)
*/
public static GENERIC_KEY_BRACES void indirectMergeSort(KEY_TYPE[] array, int length, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
indirectMergeSort(array, null, 0, length, comp, swapper);
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Merge Sort,
* On top of that allows to sort other things along with it.
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
* @param array the array that needs to be sorted
* @param supp the auxillary array that is used to simplify the sorting
* @param from where the array should be sorted from
* @param to where the array should be sorted to
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @ArrayType(T)
*/
public static GENERIC_KEY_BRACES void indirectMergeSort(KEY_TYPE[] array, KEY_TYPE[] supp, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
if(to - from < BASE_THRESHOLD) {
indirectInsertionSort(array, from, to, comp, swapper);
return;
}
if(supp == null) supp = Arrays.copyOf(array, to);
int mid = (from + to) >>> 1;
indirectMergeSort(supp, array, from, mid, comp, swapper);
indirectMergeSort(supp, array, mid, to, comp, swapper);
if(comp.compare(supp[mid - 1], supp[mid]) <= 0)
{
System.arraycopy(supp, from, array, from, to - from);
return;
}
for(int p = from, q = mid;from < to;from++) {
if(q >= to || p < mid && comp.compare(supp[p], supp[q]) < 0) {
swapper.swap(from, p);
array[from] = supp[p++];
continue;
}
swapper.swap(from, q);
array[from] = supp[q++];
}
}
/** /**
* Sorts an array according to the natural ascending order using Merge Sort, * Sorts an array according to the natural ascending order using Merge Sort,
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations * This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
@@ -856,6 +1089,56 @@ public class ARRAYS
mergeSort(array, supp, from, to, comp); mergeSort(array, supp, from, to, comp);
} }
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using a Parallel Merge Sort,
* On top of that allows to sort other things along with it.
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
* @ArrayType(T)
*/
public static GENERIC_KEY_BRACES void indirectParallelMergeSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
indirectParallelMergeSort(array, null, 0, array.length, comp, swapper);
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Parallel Merge Sort,
* On top of that allows to sort other things along with it.
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
* @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted
* @param swapper the callback which elements were swapped
* @param comp the Comparator that decides the sorting order
* @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
* @ArrayType(T)
*/
public static GENERIC_KEY_BRACES void indirectParallelMergeSort(KEY_TYPE[] array, int length, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
indirectParallelMergeSort(array, null, 0, length, comp, swapper);
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Parallel Merge Sort,
* On top of that allows to sort other things along with it.
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
* @param array the array that needs to be sorted
* @param supp the auxillary array that is used to simplify the sorting
* @param from where the array should be sorted from
* @param to where the array should be sorted to
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
* @ArrayType(T)
*/
public static GENERIC_KEY_BRACES void indirectParallelMergeSort(KEY_TYPE[] array, KEY_TYPE[] supp, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
if(SanityChecks.canParallelTask() && to - from >= PARALLEL_THRESHOLD) {
SanityChecks.invokeTask(new MergeSortActionCompSwapBRACES(array, supp, from, to, comp, swapper));
return;
}
indirectMergeSort(array, supp, from, to, comp, swapper);
}
/** /**
* Sorts an array according to the natural ascending order using Parallel Merge Sort, * Sorts an array according to the natural ascending order using Parallel Merge Sort,
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations * This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
@@ -1209,6 +1492,73 @@ public class ARRAYS
if((length = d - c) > 1) quickSort(array, to - length, to, comp); if((length = d - c) > 1) quickSort(array, to - length, to, comp);
} }
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Quick Sort,
* On top of that allows to sort other things along with it.
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @ArrayType(T)
* @return input array
*/
public static GENERIC_KEY_BRACES KEY_TYPE[] indirectQuickSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
indirectQuickSort(array, 0, array.length, comp, swapper);
return array;
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Quick Sort,
* On top of that allows to sort other things along with it.
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @ArrayType(T)
*/
public static GENERIC_KEY_BRACES void indirectQuickSort(KEY_TYPE[] array, int length, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
indirectQuickSort(array, 0, length, comp, swapper);
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Quick Sort,
* On top of that allows to sort other things along with it.
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted
* @param from where the array should be sorted from
* @param to where the array should be sorted to
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @ArrayType(T)
*/
public static GENERIC_KEY_BRACES void indirectQuickSort(KEY_TYPE[] array, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
int length = to - from;
if(length <= 0) return;
if(length < BASE_THRESHOLD) {
indirectSelectionSort(array, from, to, comp, swapper);
return;
}
KEY_TYPE pivot = array[length > 128 ? subMedium(array, from, from + (length / 2), to - 1, length / 8, comp) : medium(array, from, from + (length / 2), to - 1, comp)];
int a = from, b = a, c = to - 1, d = c;
for(int compare;;swap(array, b++, c--, swapper)) {
for(;b<=c && (compare = comp.compare(array[b], pivot)) <= 0;b++) {
if(compare == 0) swap(array, a++, b, swapper);
}
for(;c>=b && (compare = comp.compare(array[c], pivot)) >= 0;c--) {
if(compare == 0) swap(array, c, d--, swapper);
}
if(b>c) break;
}
swap(array, from, b, Math.min(a - from, b - a), swapper);
swap(array, b, to, Math.min(d - c, to - d - 1), swapper);
if((length = b - a) > 1) indirectQuickSort(array, from, from + length, comp, swapper);
if((length = d - c) > 1) indirectQuickSort(array, to - length, to, comp, swapper);
}
/** /**
* Sorts an array according to the natural ascending order using Quick Sort, * Sorts an array according to the natural ascending order using Quick Sort,
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure, * This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
@@ -1313,6 +1663,58 @@ public class ARRAYS
quickSort(array, from, to, comp); quickSort(array, from, to, comp);
} }
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Parallel Quick Sort,
* On top of that allows to sort other things along with it.
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @ArrayType(T)
* @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/
public static GENERIC_KEY_BRACES void indirectParallelQuickSort(KEY_TYPE[] array, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
indirectParallelQuickSort(array, 0, array.length, comp, swapper);
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Parallel Quick Sort,
* On top of that allows to sort other things along with it.
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @ArrayType(T)
* @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/
public static GENERIC_KEY_BRACES void indirectParallelQuickSort(KEY_TYPE[] array, int length, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
indirectParallelQuickSort(array, 0, length, comp, swapper);
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Parallel Quick Sort,
* On top of that allows to sort other things along with it.
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted
* @param from where the array should be sorted from
* @param to where the array should be sorted to
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @ArrayType(T)
* @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/
public static GENERIC_KEY_BRACES void indirectParallelQuickSort(KEY_TYPE[] array, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
if(SanityChecks.canParallelTask() && to - from >= PARALLEL_THRESHOLD) {
SanityChecks.invokeTask(new QuickSortActionCompSwapBRACES(array, from, to, comp, swapper));
return;
}
indirectQuickSort(array, from, to, comp, swapper);
}
/** /**
* Sorts an array according to the natural ascending order using Parallel Quick Sort, * Sorts an array according to the natural ascending order using Parallel Quick Sort,
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure, * This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
@@ -1367,6 +1769,18 @@ public class ARRAYS
for(int i = 0;i<length;i++,swap(a, from++, to++)); for(int i = 0;i<length;i++,swap(a, from++, to++));
} }
static GENERIC_KEY_BRACES void swap(KEY_TYPE[] a, int from, int to, Swapper swapper) {
swapper.swap(from, to);
KEY_TYPE t = a[from];
a[from] = a[to];
a[to] = t;
}
static GENERIC_KEY_BRACES void swap(KEY_TYPE[] a, int from, int to, int length, Swapper swapper) {
to -= length;
for(int i = 0;i<length;i++,swap(a, from++, to++, swapper));
}
static GENERIC_KEY_BRACES int subMedium(KEY_TYPE[] data, int a, int b, int c, int length, COMPARATOR KEY_GENERIC_TYPE comp) { static GENERIC_KEY_BRACES int subMedium(KEY_TYPE[] data, int a, int b, int c, int length, COMPARATOR KEY_GENERIC_TYPE comp) {
return medium(data, medium(data, a, a + length, a + (length * 2), comp), medium(data, b - length, b, b + length, comp), medium(data, c - (length * 2), c - length, c, comp), comp); return medium(data, medium(data, a, a + length, a + (length * 2), comp), medium(data, b - length, b, b + length, comp), medium(data, c - (length * 2), c - length, c, comp), comp);
} }
@@ -1389,16 +1803,14 @@ public class ARRAYS
int from; int from;
int to; int to;
QuickSortAction(KEY_TYPE[] array, int from, int to) QuickSortAction(KEY_TYPE[] array, int from, int to) {
{
this.array = array; this.array = array;
this.from = from; this.from = from;
this.to = to; this.to = to;
} }
@Override @Override
protected void compute() protected void compute() {
{
int length = to - from; int length = to - from;
if(length <= 0) return; if(length <= 0) return;
if(length < BASE_THRESHOLD) { if(length < BASE_THRESHOLD) {
@@ -1431,8 +1843,7 @@ public class ARRAYS
int to; int to;
COMPARATOR KEY_GENERIC_TYPE comp; COMPARATOR KEY_GENERIC_TYPE comp;
QuickSortActionComp(KEY_TYPE[] array, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp) QuickSortActionComp(KEY_TYPE[] array, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp) {
{
this.array = array; this.array = array;
this.from = from; this.from = from;
this.to = to; this.to = to;
@@ -1440,8 +1851,7 @@ public class ARRAYS
} }
@Override @Override
protected void compute() protected void compute() {
{
int length = to - from; int length = to - from;
if(length <= 0) return; if(length <= 0) return;
if(length < BASE_THRESHOLD) { if(length < BASE_THRESHOLD) {
@@ -1467,6 +1877,49 @@ public class ARRAYS
} }
} }
static class QuickSortActionCompSwap KEY_GENERIC_TYPE extends RecursiveAction {
private static final long serialVersionUID = 0L;
KEY_TYPE[] array;
int from;
int to;
COMPARATOR KEY_GENERIC_TYPE comp;
Swapper swapper;
QuickSortActionCompSwap(KEY_TYPE[] array, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
this.array = array;
this.from = from;
this.to = to;
this.comp = comp;
this.swapper = swapper;
}
@Override
protected void compute() {
int length = to - from;
if(length <= 0) return;
if(length < BASE_THRESHOLD) {
indirectSelectionSort(array, from, to, comp, swapper);
return;
}
KEY_TYPE pivot = array[length > 128 ? subMedium(array, from, from + (length / 2), to - 1, length / 8, comp) : medium(array, from, from + (length / 2), to - 1, comp)];
int a = from, b = a, c = to - 1, d = c;
for(int compare;;swap(array, b++, c--, swapper)) {
for(;b<=c && (compare = comp.compare(array[b], pivot)) <= 0;b++) {
if(compare == 0) swap(array, a++, b, swapper);
}
for(;c>=b && (compare = comp.compare(array[c], pivot)) >= 0;c--) {
if(compare == 0) swap(array, c, d--, swapper);
}
if(b>c) break;
}
swap(array, from, b, Math.min(a - from, b - a), swapper);
swap(array, b, to, Math.min(d - c, to - d - 1), swapper);
if(b - a > 1 && d - c > 1) invokeAll(new QuickSortActionCompSwapBRACES(array, from, from + (b - a), comp, swapper), new QuickSortActionCompSwapBRACES(array, to - (d - c), to, comp, swapper));
else if(b - a > 1) new QuickSortActionCompSwapBRACES(array, from, from + (b - a), comp, swapper).invoke();
else if(d - c > 1) new QuickSortActionCompSwapBRACES(array, to - (d - c), to, comp, swapper).invoke();
}
}
static class MergeSortAction KEY_GENERIC_TYPE extends RecursiveAction { static class MergeSortAction KEY_GENERIC_TYPE extends RecursiveAction {
private static final long serialVersionUID = 0L; private static final long serialVersionUID = 0L;
KEY_TYPE[] array; KEY_TYPE[] array;
@@ -1474,8 +1927,7 @@ public class ARRAYS
int from; int from;
int to; int to;
MergeSortAction(KEY_TYPE[] array, KEY_TYPE[] supp, int from, int to) MergeSortAction(KEY_TYPE[] array, KEY_TYPE[] supp, int from, int to) {
{
this.array = array; this.array = array;
this.supp = supp; this.supp = supp;
this.from = from; this.from = from;
@@ -1483,8 +1935,7 @@ public class ARRAYS
} }
@Override @Override
protected void compute() protected void compute() {
{
if(to - from < BASE_THRESHOLD) { if(to - from < BASE_THRESHOLD) {
insertionSort(array, from, to); insertionSort(array, from, to);
return; return;
@@ -1512,8 +1963,7 @@ public class ARRAYS
int to; int to;
COMPARATOR KEY_GENERIC_TYPE comp; COMPARATOR KEY_GENERIC_TYPE comp;
MergeSortActionComp(KEY_TYPE[] array, KEY_TYPE[] supp, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp) MergeSortActionComp(KEY_TYPE[] array, KEY_TYPE[] supp, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp) {
{
this.array = array; this.array = array;
this.supp = supp; this.supp = supp;
this.from = from; this.from = from;
@@ -1522,8 +1972,7 @@ public class ARRAYS
} }
@Override @Override
protected void compute() protected void compute() {
{
if(to - from < BASE_THRESHOLD) { if(to - from < BASE_THRESHOLD) {
insertionSort(array, from, to, comp); insertionSort(array, from, to, comp);
return; return;
@@ -1543,22 +1992,64 @@ public class ARRAYS
} }
} }
static class MergeSortActionCompSwap KEY_GENERIC_TYPE extends RecursiveAction {
private static final long serialVersionUID = 0L;
KEY_TYPE[] array;
KEY_TYPE[] supp;
int from;
int to;
COMPARATOR KEY_GENERIC_TYPE comp;
Swapper swapper;
MergeSortActionCompSwap(KEY_TYPE[] array, KEY_TYPE[] supp, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp, Swapper swapper) {
this.array = array;
this.supp = supp;
this.from = from;
this.to = to;
this.comp = comp;
this.swapper = swapper;
}
@Override
protected void compute() {
if(to - from < BASE_THRESHOLD) {
indirectInsertionSort(array, from, to, comp, swapper);
return;
}
if(supp == null) supp = Arrays.copyOf(array, to);
int mid = (from + to) >>> 1;
invokeAll(new MergeSortActionCompSwapBRACES(supp, array, from, mid, comp, swapper), new MergeSortActionCompSwapBRACES(supp, array, mid, to, comp, swapper));
if(comp.compare(supp[mid - 1], supp[mid]) <= 0)
{
System.arraycopy(supp, from, array, from, to - from);
return;
}
for(int p = from, q = mid;from < to;from++) {
if(q >= to || p < mid && comp.compare(supp[p], supp[q]) < 0) {
swapper.swap(from, p);
array[from] = supp[p++];
continue;
}
swapper.swap(from, q);
array[from] = supp[q++];
}
}
}
static class MemFreeMergeSortAction KEY_GENERIC_TYPE extends RecursiveAction { static class MemFreeMergeSortAction KEY_GENERIC_TYPE extends RecursiveAction {
private static final long serialVersionUID = 0L; private static final long serialVersionUID = 0L;
KEY_TYPE[] array; KEY_TYPE[] array;
int from; int from;
int to; int to;
MemFreeMergeSortAction(KEY_TYPE[] array, int from, int to) MemFreeMergeSortAction(KEY_TYPE[] array, int from, int to) {
{
this.array = array; this.array = array;
this.from = from; this.from = from;
this.to = to; this.to = to;
} }
@Override @Override
protected void compute() protected void compute() {
{
if(to - from < BASE_THRESHOLD) { if(to - from < BASE_THRESHOLD) {
insertionSort(array, from, to); insertionSort(array, from, to);
return; return;
@@ -1604,8 +2095,7 @@ public class ARRAYS
int to; int to;
COMPARATOR KEY_GENERIC_TYPE comp; COMPARATOR KEY_GENERIC_TYPE comp;
MemFreeMergeSortActionComp(KEY_TYPE[] array, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp) MemFreeMergeSortActionComp(KEY_TYPE[] array, int from, int to, COMPARATOR KEY_GENERIC_TYPE comp) {
{
this.array = array; this.array = array;
this.from = from; this.from = from;
this.to = to; this.to = to;
@@ -1613,8 +2103,7 @@ public class ARRAYS
} }
@Override @Override
protected void compute() protected void compute() {
{
if(to - from < BASE_THRESHOLD) { if(to - from < BASE_THRESHOLD) {
insertionSort(array, from, to, comp); insertionSort(array, from, to, comp);
return; return;
@@ -7,6 +7,11 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.LockSupport; import java.util.concurrent.locks.LockSupport;
import java.util.function.Consumer; import java.util.function.Consumer;
#if JDK_TYPE
import java.util.OPTIONAL;
#else
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.CONSUMER;
@@ -299,9 +304,8 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE
* @param operator that reduces the elements. * @param operator that reduces the elements.
* @return self with the reduce action applied * @return self with the reduce action applied
*/ */
public ASYNC_BUILDER KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public ObjectAsyncBuilder<OPTIONAL KEY_GENERIC_TYPE> reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
task = new SimpleReduceTaskBRACES(iterable.iterator(), operator); return new ObjectAsyncBuilder<>(new SimpleReduceTaskBRACES(iterable.iterator(), operator));
return this;
} }
#if TYPE_OBJECT #if TYPE_OBJECT
@@ -435,9 +439,8 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE
* @param filter that decides the desired elements * @param filter that decides the desired elements
* @return self with the findFirst function applied * @return self with the findFirst function applied
*/ */
public ASYNC_BUILDER KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { public ObjectAsyncBuilder<OPTIONAL KEY_GENERIC_TYPE> findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
task = new FindFirstTaskBRACES(iterable.iterator(), filter); return new ObjectAsyncBuilder<>(new FindFirstTaskBRACES(iterable.iterator(), filter));
return this;
} }
#if INT_ASYNC_MODULE #if INT_ASYNC_MODULE
@@ -590,7 +593,7 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE
} }
#endif #endif
private static class SimpleReduceTask KEY_GENERIC_TYPE extends BASE_TASK KEY_GENERIC_TYPE private static class SimpleReduceTask KEY_GENERIC_TYPE extends BaseObjectTask<OPTIONAL KEY_GENERIC_TYPE>
{ {
ITERATOR KEY_GENERIC_TYPE iter; ITERATOR KEY_GENERIC_TYPE iter;
UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator; UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator;
@@ -600,6 +603,7 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE
public SimpleReduceTask(ITERATOR KEY_GENERIC_TYPE iter, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { public SimpleReduceTask(ITERATOR KEY_GENERIC_TYPE iter, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
this.iter = iter; this.iter = iter;
this.operator = operator; this.operator = operator;
this.setResult(OPTIONAL.empty());
} }
@Override @Override
@@ -614,7 +618,7 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE
} }
} }
if(!iter.hasNext()) { if(!iter.hasNext()) {
setResult(value); setResult(OPTIONAL.GET_OPTIONAL(value));
return true; return true;
} }
return false; return false;
@@ -689,7 +693,7 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE
} }
#endif #endif
private static class FindFirstTask KEY_GENERIC_TYPE extends BASE_TASK KEY_GENERIC_TYPE private static class FindFirstTask KEY_GENERIC_TYPE extends BaseObjectTask<OPTIONAL KEY_GENERIC_TYPE>
{ {
ITERATOR KEY_GENERIC_TYPE iter; ITERATOR KEY_GENERIC_TYPE iter;
PREDICATE KEY_GENERIC_TYPE filter; PREDICATE KEY_GENERIC_TYPE filter;
@@ -697,6 +701,7 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE
public FindFirstTask(ITERATOR KEY_GENERIC_TYPE iter, PREDICATE KEY_GENERIC_TYPE filter) { public FindFirstTask(ITERATOR KEY_GENERIC_TYPE iter, PREDICATE KEY_GENERIC_TYPE filter) {
this.iter = iter; this.iter = iter;
this.filter = filter; this.filter = filter;
this.setResult(OPTIONAL.empty());
} }
@Override @Override
@@ -704,7 +709,7 @@ public class ASYNC_BUILDER KEY_GENERIC_TYPE
while(shouldRun() && iter.hasNext()) { while(shouldRun() && iter.hasNext()) {
KEY_TYPE entry = iter.NEXT(); KEY_TYPE entry = iter.NEXT();
if(filter.test(iter.NEXT())) { if(filter.test(iter.NEXT())) {
setResult(entry); setResult(OPTIONAL.GET_OPTIONAL(entry));
return true; return true;
} }
} }
@@ -8,6 +8,9 @@ import java.util.ConcurrentModificationException;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
#if JDK_TYPE
import java.util.OPTIONAL;
#endif
#if TYPE_OBJECT #if TYPE_OBJECT
import java.util.Comparator; import java.util.Comparator;
import java.util.function.BiFunction; import java.util.function.BiFunction;
@@ -30,6 +33,9 @@ import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif #endif
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.objects.utils.ObjectArrays; import speiger.src.collections.objects.utils.ObjectArrays;
import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.CONSUMER;
@@ -141,7 +147,10 @@ public class COLLECTIONS
return new SingletonCollectionBRACES(element); return new SingletonCollectionBRACES(element);
} }
protected static GENERIC_KEY_BRACES CollectionWrapper KEY_GENERIC_TYPE wrapper() { /**
* Internal Use mainly. Its a collection wrapper with 0 dependencies for those actions where a collector is needed.
*/
public static GENERIC_KEY_BRACES CollectionWrapper KEY_GENERIC_TYPE wrapper() {
return new CollectionWrapperBRACES(); return new CollectionWrapperBRACES();
} }
@@ -869,9 +878,9 @@ public class COLLECTIONS
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return c.reduce(identity, operator); } } public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return c.reduce(identity, operator); } }
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return c.reduce(operator); } } public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return c.reduce(operator); } }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.findFirst(filter); } } public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.findFirst(filter); } }
@Override @Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.count(filter); } } public int count(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.count(filter); } }
} }
@@ -1021,9 +1030,9 @@ public class COLLECTIONS
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { return c.reduce(identity, operator); } public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { return c.reduce(identity, operator); }
#endif #endif
@Override @Override
public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { return c.reduce(operator); } public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { return c.reduce(operator); }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { return c.findFirst(filter); } public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { return c.findFirst(filter); }
@Override @Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) { return c.count(filter); } public int count(PREDICATE KEY_GENERIC_TYPE filter) { return c.count(filter); }
} }
@@ -4,16 +4,22 @@ package speiger.src.collections.PACKAGE.utils;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction;
#endif #endif
#if JDK_FUNCTION #if JDK_FUNCTION
import java.util.function.PREDICATE; import java.util.function.PREDICATE;
#endif #endif
#if JDK_TYPE
import java.util.OPTIONAL;
#endif
import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.collections.COLLECTION; import speiger.src.collections.PACKAGE.collections.COLLECTION;
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#endif #endif
#if !JDK_TYPE
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
#if DEQUEUE_FEATURE #if DEQUEUE_FEATURE
import speiger.src.collections.PACKAGE.queues.PRIORITY_DEQUEUE; import speiger.src.collections.PACKAGE.queues.PRIORITY_DEQUEUE;
#endif #endif
@@ -24,6 +30,7 @@ import speiger.src.collections.PACKAGE.functions.CONSUMER;
#if !JDK_FUNCTION #if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif #endif
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
/** /**
@@ -140,7 +147,15 @@ public class PRIORITY_QUEUES
@Override @Override
public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.matchesAll(filter); } } public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.matchesAll(filter); } }
@Override @Override
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.findFirst(filter); } } public OPTIONAL KEY_GENERIC_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.findFirst(filter); } }
#if TYPE_OBJECT
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) { synchronized(mutex) { return queue.reduce(identity, operator); } }
#else
@Override
public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return queue.reduce(identity, operator); } }
#endif
@Override
public OPTIONAL KEY_GENERIC_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return queue.reduce(operator); } }
@Override @Override
public int count(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.count(filter); } } public int count(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.count(filter); } }
} }
@@ -360,6 +360,10 @@ public class SETS
s = c; s = c;
} }
@Override
public void addFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -651,6 +655,10 @@ public class SETS
s = c; s = c;
} }
@Override
public void addFirst(KEY_TYPE o) { synchronized(mutex) { s.addFirst(o); } }
@Override
public void addLast(KEY_TYPE o) { synchronized(mutex) { s.addLast(o); } }
@Override @Override
public boolean addAndMoveToFirst(KEY_TYPE o) { synchronized(mutex) { return s.addAndMoveToFirst(o); } } public boolean addAndMoveToFirst(KEY_TYPE o) { synchronized(mutex) { return s.addAndMoveToFirst(o); } }
@Override @Override
@@ -755,6 +755,10 @@ public class MAPS
set = c; set = c;
} }
@Override
public void addFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -20,6 +20,7 @@ import speiger.src.testers.PACKAGE.generators.maps.TEST_ORDERED_MAP_GENERATOR;
import speiger.src.testers.PACKAGE.impl.maps.DERIVED_MAP_GENERATORS; import speiger.src.testers.PACKAGE.impl.maps.DERIVED_MAP_GENERATORS;
import speiger.src.testers.PACKAGE.tests.maps.FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapMoveTester; import speiger.src.testers.PACKAGE.tests.maps.FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapMoveTester;
import speiger.src.testers.PACKAGE.tests.maps.FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapNavigationTester; import speiger.src.testers.PACKAGE.tests.maps.FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapNavigationTester;
import speiger.src.testers.PACKAGE.tests.maps.FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapPutTester;
#if !TYPE_OBJECT #if !TYPE_OBJECT
import speiger.src.testers.objects.builder.ObjectSetTestSuiteBuilder; import speiger.src.testers.objects.builder.ObjectSetTestSuiteBuilder;
import speiger.src.testers.objects.generators.TestObjectSetGenerator; import speiger.src.testers.objects.generators.TestObjectSetGenerator;
@@ -41,6 +42,7 @@ public class ORDERED_MAP_TEST_BUILDER KEY_VALUE_GENERIC_TYPE extends MAP_TEST_BU
List<Class<? extends AbstractTester>> testers = super.getTesters(); List<Class<? extends AbstractTester>> testers = super.getTesters();
testers.add(FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapMoveTester.class); testers.add(FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapMoveTester.class);
testers.add(FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapNavigationTester.class); testers.add(FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapNavigationTester.class);
testers.add(FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapPutTester.class);
return testers; return testers;
} }
@@ -77,7 +79,7 @@ public class ORDERED_MAP_TEST_BUILDER KEY_VALUE_GENERIC_TYPE extends MAP_TEST_BU
features.addAll(parentBuilder.getFeatures()); features.addAll(parentBuilder.getFeatures());
features.remove(SpecialFeature.COPYING); features.remove(SpecialFeature.COPYING);
features.remove(SpecialFeature.CHILDREN_COPY); features.remove(SpecialFeature.CHILDREN_COPY);
return ORDERED_MAP_TEST_BUILDER.using(new DERIVED_MAP_GENERATORS.ReverseTestOrderedMapGenerator(delegate)) return ORDERED_MAP_TEST_BUILDER.using(new DERIVED_MAP_GENERATORS.ReverseTestOrderedMapGeneratorKV_BRACES(delegate))
.named(parentBuilder.getName() + " reversed").withFeatures(features) .named(parentBuilder.getName() + " reversed").withFeatures(features)
.suppressing(parentBuilder.getSuppressedTests()).createTestSuite(); .suppressing(parentBuilder.getSuppressedTests()).createTestSuite();
} }
@@ -27,6 +27,8 @@ import speiger.src.collections.PACKAGE.maps.impl.concurrent.CONCURRENT_HASH_MAP;
#if TYPE_OBJECT #if TYPE_OBJECT
import speiger.src.collections.PACKAGE.maps.impl.misc.ENUM_MAP; import speiger.src.collections.PACKAGE.maps.impl.misc.ENUM_MAP;
import speiger.src.collections.PACKAGE.maps.impl.misc.LINKED_ENUM_MAP; import speiger.src.collections.PACKAGE.maps.impl.misc.LINKED_ENUM_MAP;
import speiger.src.collections.PACKAGE.maps.impl.reference.REF_MAP;
import speiger.src.collections.PACKAGE.maps.impl.reference.LINKED_REF_MAP;
#endif #endif
import speiger.src.collections.PACKAGE.maps.impl.tree.RB_TREE_MAP; import speiger.src.collections.PACKAGE.maps.impl.tree.RB_TREE_MAP;
@@ -134,6 +136,97 @@ public class MAP_CONSTRUCTOR_TESTS
} }
#if TYPE_OBJECT #if TYPE_OBJECT
public static class RefMap extends FILE_KEY_TYPE2FILE_VALUE_TYPEMapConstructorTester KEY_VALUE_STRING_GENERIC_TYPE
{
public RefMap() {
setSimpleConstructor(REF_MAP::new);
setSizeConstructor(REF_MAP::new);
setPArrayConstructor(REF_MAP::new);
#if !TYPE_OBJECT || !VALUE_OBJECT
setArrayConstructor(REF_MAP::new);
#endif
setPMapConstructor(REF_MAP::new);
setMapConstructor(REF_MAP::new);
}
@Test
public void testWrongLoadFactorSize() {
setSizeConstructor(T -> new REF_MAP KEY_VALUE_STRING_GENERIC_TYPE(T, 0F));
try {
testSizeConstructor_smallSize();
fail("A Constructor using a 0 LoadFactor should error");
} catch(IllegalStateException e) {}
setSizeConstructor(T -> new REF_MAP KEY_VALUE_STRING_GENERIC_TYPE(T, 1F));
try {
testSizeConstructor_smallSize();
fail("A Constructor using a 1 LoadFactor should error");
} catch(IllegalStateException e) {}
}
#if TYPE_OBJECT
@Override
protected String[] createKeyElements() {
return Arrays.copyOfRange(StringSortTest.NAMES, 0, 100);
}
#endif
#if VALUE_OBJECT
@Override
protected String[] createValueElements() {
return Arrays.copyOfRange(StringSortTest.NAMES, 100, 200);
}
#endif
}
public static class LinkedRefMap extends FILE_KEY_TYPE2FILE_VALUE_TYPEMapConstructorTester KEY_VALUE_STRING_GENERIC_TYPE
{
public LinkedRefMap() {
setSimpleConstructor(LINKED_REF_MAP::new);
setSizeConstructor(LINKED_REF_MAP::new);
setPArrayConstructor(LINKED_REF_MAP::new);
#if !TYPE_OBJECT || !VALUE_OBJECT
setArrayConstructor(LINKED_REF_MAP::new);
#endif
setPMapConstructor(LINKED_REF_MAP::new);
setMapConstructor(LINKED_REF_MAP::new);
}
@Test
public void testWrongLoadFactorSize() {
setSizeConstructor(T -> new LINKED_REF_MAP KEY_VALUE_STRING_GENERIC_TYPE(T, 0F));
try {
testSizeConstructor_smallSize();
fail("A Constructor using a 0 LoadFactor should error");
} catch(IllegalStateException e) {
}
setSizeConstructor(T -> new LINKED_REF_MAP KEY_VALUE_STRING_GENERIC_TYPE(T, 1F));
try {
testSizeConstructor_smallSize();
fail("A Constructor using a 1 LoadFactor should error");
} catch(IllegalStateException e) {
}
}
#if TYPE_OBJECT
@Override
protected String[] createKeyElements() {
return Arrays.copyOfRange(StringSortTest.NAMES, 0, 100);
}
#endif
#if VALUE_OBJECT
@Override
protected String[] createValueElements() {
return Arrays.copyOfRange(StringSortTest.NAMES, 100, 200);
}
#endif
}
#if VALUE_OBJECT #if VALUE_OBJECT
public static class EnumMap extends FILE_KEY_TYPE2FILE_VALUE_TYPEMapConstructorTester<AnEnum, String> public static class EnumMap extends FILE_KEY_TYPE2FILE_VALUE_TYPEMapConstructorTester<AnEnum, String>
#else #else
@@ -26,6 +26,15 @@ public class FILE_KEY_TYPECollectionForEachTester KEY_GENERIC_TYPE extends ABSTR
HELPERS.assertContentsAnyOrder(elements, createSamplesArray()); HELPERS.assertContentsAnyOrder(elements, createSamplesArray());
} }
#ignore
@CollectionFeature.Require(absent = KNOWN_ORDER)
public void testIndexedForEachUnknownOrder() {
#endignore
LIST KEY_GENERIC_TYPE elements = new ARRAY_LISTBRACES();
collection.forEachIndexed((K, V) -> elements.add(V));
HELPERS.assertContentsAnyOrder(elements, createSamplesArray());
}
#ignore #ignore
@CollectionFeature.Require(absent = KNOWN_ORDER) @CollectionFeature.Require(absent = KNOWN_ORDER)
public void testForEachExtraUnknownOrder() { public void testForEachExtraUnknownOrder() {
@@ -44,6 +53,15 @@ public class FILE_KEY_TYPECollectionForEachTester KEY_GENERIC_TYPE extends ABSTR
assertEquals("Different ordered iteration", HELPERS.copyToList(getOrderedElements()), elements); assertEquals("Different ordered iteration", HELPERS.copyToList(getOrderedElements()), elements);
} }
#ignore
@CollectionFeature.Require(KNOWN_ORDER)
public void testIndexedForEachKnownOrder() {
#endignore
LIST KEY_GENERIC_TYPE elements = new ARRAY_LISTBRACES();
collection.forEachIndexed((K, V) -> elements.add(V));
assertEquals("Different ordered iteration", HELPERS.copyToList(getOrderedElements()), elements);
}
#ignore #ignore
@CollectionFeature.Require(KNOWN_ORDER) @CollectionFeature.Require(KNOWN_ORDER)
public void testForEachExtraKnownOrder() { public void testForEachExtraKnownOrder() {
@@ -4,6 +4,12 @@ package speiger.src.testers.PACKAGE.tests.iterable;
import java.util.Objects; import java.util.Objects;
#endif #endif
#if JDK_TYPE
import java.util.OPTIONAL;
#else
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif
import org.junit.Ignore; import org.junit.Ignore;
import com.google.common.collect.testing.features.CollectionSize; import com.google.common.collect.testing.features.CollectionSize;
@@ -27,17 +33,17 @@ public class FILE_KEY_TYPEIterableFindFirstTester KEY_GENERIC_TYPE extends ABSTR
@CollectionSize.Require(absent = CollectionSize.ZERO) @CollectionSize.Require(absent = CollectionSize.ZERO)
#endignore #endignore
public void testIterableFindFirst_FindFirstElements() { public void testIterableFindFirst_FindFirstElements() {
assertEquals("First Element should be found", e0(), container.findFirst(T -> KEY_EQUALS(T, e0()))); assertEquals("First Element should be found", e0(), container.findFirst(T -> KEY_EQUALS(T, e0())).SUPPLY_GET());
} }
public void testIterableFindFirst_FindNothing() { public void testIterableFindFirst_FindNothing() {
assertEquals("No element should be found", EMPTY_KEY_VALUE, container.findFirst(T -> KEY_EQUALS(T, e4()))); assertEquals("No element should be found", OPTIONAL.empty(), container.findFirst(T -> KEY_EQUALS(T, e4())));
} }
#ignore #ignore
@CollectionSize.Require(CollectionSize.SEVERAL) @CollectionSize.Require(CollectionSize.SEVERAL)
#endignore #endignore
public void testIterableFindFirst_FindLastElement() { public void testIterableFindFirst_FindLastElement() {
assertEquals("Last Element should be found", e2(), container.findFirst(T -> KEY_EQUALS(T, e2()))); assertEquals("Last Element should be found", e2(), container.findFirst(T -> KEY_EQUALS(T, e2())).SUPPLY_GET());
} }
} }
@@ -3,9 +3,16 @@ package speiger.src.testers.PACKAGE.tests.iterable;
#if TYPE_OBJECT #if TYPE_OBJECT
import java.util.Objects; import java.util.Objects;
#endif
#if JDK_TYPE
import java.util.OPTIONAL;
#else
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif #endif
import org.junit.Ignore; import org.junit.Ignore;
import com.google.common.collect.testing.features.CollectionSize;
import speiger.src.testers.PACKAGE.tests.base.ABSTRACT_COLLECTION_TESTER; import speiger.src.testers.PACKAGE.tests.base.ABSTRACT_COLLECTION_TESTER;
@Ignore @Ignore
@@ -29,7 +36,7 @@ public class FILE_KEY_TYPEIterableReduceTester KEY_GENERIC_TYPE extends ABSTRACT
} }
public void testIterableReduce() { public void testIterableReduce() {
assertEquals("The sum of the collection should match", getSum(), collection.reduce(this::sum)); assertEquals("The sum of the collection should match", size == CollectionSize.ZERO ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(getSum()), collection.reduce(this::sum));
} }
public void testIterableExtraReduce() { public void testIterableExtraReduce() {
@@ -27,6 +27,7 @@ public class FILE_KEY_TYPE2FILE_VALUE_TYPEMapContainsTester KEY_VALUE_GENERIC_TY
#ignore #ignore
@CollectionSize.Require(absent = ZERO) @CollectionSize.Require(absent = ZERO)
@SuppressWarnings("unlikely-arg-type")
#endignore #endignore
public void testContainsObject_yes() { public void testContainsObject_yes() {
assertTrue("contains(present) should return true", getMap().ENTRY_SET().contains(new AbstractMap.SimpleEntry<>(e0()))); assertTrue("contains(present) should return true", getMap().ENTRY_SET().contains(new AbstractMap.SimpleEntry<>(e0())));
@@ -36,6 +37,7 @@ public class FILE_KEY_TYPE2FILE_VALUE_TYPEMapContainsTester KEY_VALUE_GENERIC_TY
assertFalse("contains(notPresent) should return false", getMap().ENTRY_SET().contains(e3())); assertFalse("contains(notPresent) should return false", getMap().ENTRY_SET().contains(e3()));
} }
@SuppressWarnings("unlikely-arg-type")
public void testContainsObject_no() { public void testContainsObject_no() {
assertFalse("contains(notPresent) should return false", getMap().ENTRY_SET().contains(new AbstractMap.SimpleEntry<>(e3()))); assertFalse("contains(notPresent) should return false", getMap().ENTRY_SET().contains(new AbstractMap.SimpleEntry<>(e3())));
} }
@@ -136,6 +136,7 @@ public class FILE_KEY_TYPE2FILE_VALUE_TYPEMapRemoveEntryTester KEY_VALUE_GENERIC
#ignore #ignore
@CollectionSize.Require(ONE) @CollectionSize.Require(ONE)
@MapFeature.Require(SUPPORTS_REMOVE) @MapFeature.Require(SUPPORTS_REMOVE)
@SuppressWarnings("unlikely-arg-type")
#endignore #endignore
public void testRemove_supportedObjectEntryPresent() { public void testRemove_supportedObjectEntryPresent() {
assertTrue(getMap().ENTRY_SET().remove(new AbstractMap.SimpleEntry<>(e0()))); assertTrue(getMap().ENTRY_SET().remove(new AbstractMap.SimpleEntry<>(e0())));
@@ -154,6 +155,7 @@ public class FILE_KEY_TYPE2FILE_VALUE_TYPEMapRemoveEntryTester KEY_VALUE_GENERIC
#ignore #ignore
@CollectionSize.Require(ONE) @CollectionSize.Require(ONE)
@MapFeature.Require(SUPPORTS_REMOVE) @MapFeature.Require(SUPPORTS_REMOVE)
@SuppressWarnings("unlikely-arg-type")
#endignore #endignore
public void testRemove_supportedObjectEntryMissing() { public void testRemove_supportedObjectEntryMissing() {
assertFalse(getMap().ENTRY_SET().remove(new AbstractMap.SimpleEntry<>(e3()))); assertFalse(getMap().ENTRY_SET().remove(new AbstractMap.SimpleEntry<>(e3())));
@@ -0,0 +1,118 @@
package speiger.src.testers.PACKAGE.tests.maps;
#ignore
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
import static org.junit.Assert.assertNotEquals;
#endignore
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.MapFeature;
import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.ORDERED_MAP;
import speiger.src.collections.objects.lists.ObjectList;
import speiger.src.testers.PACKAGE.tests.base.maps.ABSTRACT_MAP_TESTER;
import speiger.src.testers.objects.utils.ObjectHelpers;
@SuppressWarnings("javadoc")
public class FILE_KEY_TYPE2FILE_VALUE_TYPEOrderedMapPutTester KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP_TESTER KEY_VALUE_GENERIC_TYPE
{
private ORDERED_MAP KEY_VALUE_GENERIC_TYPE orderedMap;
private ObjectList<MAP.Entry KEY_VALUE_GENERIC_TYPE> values;
private KEY_TYPE a;
private VALUE_TYPE aValue;
@Override
public void setUp() throws Exception {
super.setUp();
orderedMap = (ORDERED_MAP KEY_VALUE_GENERIC_TYPE)getMap();
values = ObjectHelpers.copyToList(getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements()));
if (values.size() >= 1) {
a = values.get(0).ENTRY_KEY();
aValue = values.get(0).ENTRY_VALUE();
}
}
#ignore
@MapFeature.Require(SUPPORTS_PUT)
@CollectionSize.Require(absent = ZERO)
#endignore
public void testPutFirst() {
assertEquals(aValue, orderedMap.putFirst(a, v3()));
assertNotEquals(v3(), orderedMap.FIRST_ENTRY_VALUE());
assertEquals(orderedMap.getDefaultReturnValue(), orderedMap.putFirst(k4(), v4()));
assertEquals(v4(), orderedMap.FIRST_ENTRY_VALUE());
assertEquals(e4(), orderedMap.firstEntry());
}
#ignore
@MapFeature.Require(SUPPORTS_PUT)
@CollectionSize.Require(absent = ZERO)
#endignore
public void testPutLast() {
assertEquals(aValue, orderedMap.putLast(a, v3()));
assertNotEquals(v3(), orderedMap.LAST_ENTRY_VALUE());
assertEquals(orderedMap.getDefaultReturnValue(), orderedMap.putLast(k4(), v4()));
assertEquals(v4(), orderedMap.LAST_ENTRY_VALUE());
assertEquals(e4(), orderedMap.lastEntry());
}
#ignore
@MapFeature.Require(SUPPORTS_REMOVE)
@CollectionSize.Require(CollectionSize.ONE)
#endignore
public void testPollFirst() {
assertFalse(orderedMap.isEmpty());
assertEquals(e0(), orderedMap.pollFirstEntry());
assertTrue(orderedMap.isEmpty());
}
#ignore
@MapFeature.Require(SUPPORTS_REMOVE)
@CollectionSize.Require(CollectionSize.ONE)
#endignore
public void testPollLast() {
assertFalse(orderedMap.isEmpty());
assertEquals(e0(), orderedMap.pollLastEntry());
assertTrue(orderedMap.isEmpty());
}
#ignore
@MapFeature.Require(absent = SUPPORTS_PUT)
#endignore
public void testUnsupportedPutFirst() {
try { orderedMap.putFirst(a, aValue); }
catch (UnsupportedOperationException tolerated) {}
expectUnchanged();
}
#ignore
@MapFeature.Require(absent = SUPPORTS_PUT)
#endignore
public void testUnsupportedPutLast() {
try { orderedMap.putLast(a, aValue); }
catch (UnsupportedOperationException tolerated) {}
expectUnchanged();
}
#ignore
@MapFeature.Require(absent = SUPPORTS_REMOVE)
#endignore
public void testUnsupportedPollFirst() {
try { orderedMap.pollFirstEntry(); }
catch (UnsupportedOperationException tolerated) {}
expectUnchanged();
}
#ignore
@MapFeature.Require(absent = SUPPORTS_REMOVE)
#endignore
public void testUnsupportedPollLast() {
try { orderedMap.pollFirstEntry(); }
catch (UnsupportedOperationException tolerated) {}
expectUnchanged();
}
}
@@ -2,6 +2,11 @@ package speiger.src.testers.PACKAGE.tests.queue.iterators;
#if TYPE_OBJECT #if TYPE_OBJECT
import java.util.Objects; import java.util.Objects;
#endif
#if JDK_TYPE
import java.util.OPTIONAL;
#else
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif #endif
import org.junit.Ignore; import org.junit.Ignore;
@@ -27,17 +32,17 @@ public class FILE_KEY_TYPEQueueFindFirstTester KEY_GENERIC_TYPE extends ABSTRACT
@CollectionSize.Require(absent = CollectionSize.ZERO) @CollectionSize.Require(absent = CollectionSize.ZERO)
#endignore #endignore
public void testQueueFindFirst_FindFirstElements() { public void testQueueFindFirst_FindFirstElements() {
assertEquals("First Element should be found", e0(), queue.findFirst(T -> KEY_EQUALS(T, e0()))); assertEquals("First Element should be found", e0(), queue.findFirst(T -> KEY_EQUALS(T, e0())).SUPPLY_GET());
} }
public void testQueueFindFirst_FindNothing() { public void testQueueFindFirst_FindNothing() {
assertEquals("No element should be found", EMPTY_KEY_VALUE, queue.findFirst(T -> KEY_EQUALS(T, e4()))); assertEquals("No element should be found", OPTIONAL.empty(), queue.findFirst(T -> KEY_EQUALS(T, e4())));
} }
#ignore #ignore
@CollectionSize.Require(CollectionSize.SEVERAL) @CollectionSize.Require(CollectionSize.SEVERAL)
#endignore #endignore
public void testQueueFindFirst_FindLastElement() { public void testQueueFindFirst_FindLastElement() {
assertEquals("Last Element should be found", e2(), queue.findFirst(T -> KEY_EQUALS(T, e2()))); assertEquals("Last Element should be found", e2(), queue.findFirst(T -> KEY_EQUALS(T, e2())).SUPPLY_GET());
} }
} }
@@ -3,10 +3,16 @@ package speiger.src.testers.PACKAGE.tests.queue.iterators;
#if TYPE_OBJECT #if TYPE_OBJECT
import java.util.Objects; import java.util.Objects;
#endif
#if JDK_TYPE
import java.util.OPTIONAL;
#else
import speiger.src.collections.PACKAGE.functions.OPTIONAL;
#endif #endif
import org.junit.Ignore; import org.junit.Ignore;
import speiger.src.testers.PACKAGE.tests.base.ABSTRACT_QUEUE_TESTER; import speiger.src.testers.PACKAGE.tests.base.ABSTRACT_QUEUE_TESTER;
import com.google.common.collect.testing.features.CollectionSize;
@Ignore @Ignore
@SuppressWarnings("javadoc") @SuppressWarnings("javadoc")
@@ -29,7 +35,7 @@ public class FILE_KEY_TYPEQueueReduceTester KEY_GENERIC_TYPE extends ABSTRACT_QU
} }
public void testQueueReduce() { public void testQueueReduce() {
assertEquals("The sum of the queue should match", getSum(), queue.reduce(this::sum)); assertEquals("The sum of the queue should match", size == CollectionSize.ZERO ? OPTIONAL.empty() : OPTIONAL.GET_OPTIONAL(getSum()), queue.reduce(this::sum));
} }
public void testQueueExtraReduce() { public void testQueueExtraReduce() {
@@ -12,6 +12,9 @@ import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.features.SetFeature; import com.google.common.collect.testing.features.SetFeature;
import com.google.common.collect.testing.features.Feature; import com.google.common.collect.testing.features.Feature;
#if TYPE_OBJECT #if TYPE_OBJECT
import com.google.common.collect.testing.testers.CollectionAddTester;
import com.google.common.collect.testing.testers.CollectionAddAllTester;
import com.google.common.collect.testing.testers.CollectionCreationTester;
import com.google.common.collect.testing.features.CollectionFeature; import com.google.common.collect.testing.features.CollectionFeature;
#endif #endif
import junit.framework.Test; import junit.framework.Test;
@@ -122,6 +125,9 @@ public class SET_TESTS extends TestCase
#ignore #ignore
.withFeatures(getSizes(size)).withFeatures(features); .withFeatures(getSizes(size)).withFeatures(features);
if(nullValues) builder.withFeatures(CollectionFeature.ALLOWS_NULL_VALUES); if(nullValues) builder.withFeatures(CollectionFeature.ALLOWS_NULL_VALUES);
builder.suppressing(CollectionAddTester.getAddNullUnsupportedMethod(), CollectionAddTester.getAddNullSupportedMethod());
builder.suppressing(CollectionAddAllTester.getAddAllNullUnsupportedMethod());
builder.suppressing(CollectionCreationTester.getCreateWithNullUnsupportedMethod());
#endignore #endignore
return builder.createTestSuite(); return builder.createTestSuite();
} }
@@ -41,6 +41,8 @@ import speiger.src.collections.PACKAGE.maps.impl.misc.ARRAY_MAP;
#if TYPE_OBJECT #if TYPE_OBJECT
import speiger.src.collections.PACKAGE.maps.impl.misc.ENUM_MAP; import speiger.src.collections.PACKAGE.maps.impl.misc.ENUM_MAP;
import speiger.src.collections.PACKAGE.maps.impl.misc.LINKED_ENUM_MAP; import speiger.src.collections.PACKAGE.maps.impl.misc.LINKED_ENUM_MAP;
import speiger.src.collections.PACKAGE.maps.impl.reference.REF_MAP;
import speiger.src.collections.PACKAGE.maps.impl.reference.LINKED_REF_MAP;
#endif #endif
import speiger.src.collections.PACKAGE.maps.impl.tree.AVL_TREE_MAP; import speiger.src.collections.PACKAGE.maps.impl.tree.AVL_TREE_MAP;
import speiger.src.collections.PACKAGE.maps.impl.tree.RB_TREE_MAP; import speiger.src.collections.PACKAGE.maps.impl.tree.RB_TREE_MAP;
@@ -104,6 +106,7 @@ public class MAP_TESTS extends TestCase
#if TYPE_OBJECT #if TYPE_OBJECT
constructors.addTest(new TestSuite(MAP_CONSTRUCTOR_TESTS.EnumMap.class)); constructors.addTest(new TestSuite(MAP_CONSTRUCTOR_TESTS.EnumMap.class));
constructors.addTest(new TestSuite(MAP_CONSTRUCTOR_TESTS.LinkedEnumMap.class)); constructors.addTest(new TestSuite(MAP_CONSTRUCTOR_TESTS.LinkedEnumMap.class));
constructors.addTest(new TestSuite(MAP_CONSTRUCTOR_TESTS.RefMap.class));
#endif #endif
constructors.addTest(new TestSuite(MAP_CONSTRUCTOR_TESTS.CustomHashMap.class)); constructors.addTest(new TestSuite(MAP_CONSTRUCTOR_TESTS.CustomHashMap.class));
constructors.addTest(new TestSuite(MAP_CONSTRUCTOR_TESTS.LinkedCustomHashMap.class)); constructors.addTest(new TestSuite(MAP_CONSTRUCTOR_TESTS.LinkedCustomHashMap.class));
@@ -130,27 +133,29 @@ public class MAP_TESTS extends TestCase
} }
public static void suite(TestSuite suite) { public static void suite(TestSuite suite) {
suite.addTest(mapSuite("HASH_MAP", HASH_MAP::new, getFeatures(), -1, true)); suite.addTest(mapSuite("HASH_MAP", HASH_MAP::new, getFeatures(), -1, true, true));
suite.addTest(orderedMapSuite("LINKED_HASH_MAP", LINKED_HASH_MAP::new, getFeatures(), -1)); suite.addTest(orderedMapSuite("LINKED_HASH_MAP", LINKED_HASH_MAP::new, getFeatures(), -1, true));
suite.addTest(orderedMapSuite("IMMUTABLE_HASH_MAP", IMMUTABLE_HASH_MAP::new, getImmutableFeatures(), -1)); suite.addTest(orderedMapSuite("IMMUTABLE_HASH_MAP", IMMUTABLE_HASH_MAP::new, getImmutableFeatures(), -1, true));
#if TYPE_OBJECT #if TYPE_OBJECT
suite.addTest(enumMapSuite("ENUM_MAP", ENUM_MAP::new, getFeatures(), 5)); suite.addTest(enumMapSuite("ENUM_MAP", ENUM_MAP::new, getFeatures(), 5));
suite.addTest(enumOrderedMapSuite("LINKED_ENUM_MAP", (K, V) -> K.length <= 0 ? new LINKED_ENUM_MAP<>(AnEnum.class) : new LINKED_ENUM_MAP<>(K, V), getFeatures(), 5)); suite.addTest(enumOrderedMapSuite("LINKED_ENUM_MAP", (K, V) -> K.length <= 0 ? new LINKED_ENUM_MAP<>(AnEnum.class) : new LINKED_ENUM_MAP<>(K, V), getFeatures(), 5));
suite.addTest(mapSuite("REF_MAP", REF_MAP::new, getFeatures(), - 1, true, false));
suite.addTest(orderedMapSuite("LINKED_REF_MAP", LINKED_REF_MAP::new, getFeatures(), - 1, false));
#endif #endif
suite.addTest(mapSuite("CUSTOM_HASH_MAP", (K, V) -> new CUSTOM_HASH_MAPKV_BRACES(K, V, HashStrategy.INSTANCE), getFeatures(), -1, true)); suite.addTest(mapSuite("CUSTOM_HASH_MAP", (K, V) -> new CUSTOM_HASH_MAPKV_BRACES(K, V, HashStrategy.INSTANCE), getFeatures(), -1, true, true));
suite.addTest(orderedMapSuite("LINKED_CUSTOM_HASH_MAP", (K, V) -> new LINKED_CUSTOM_HASH_MAPKV_BRACES(K, V, HashStrategy.INSTANCE), getFeatures(), -1)); suite.addTest(orderedMapSuite("LINKED_CUSTOM_HASH_MAP", (K, V) -> new LINKED_CUSTOM_HASH_MAPKV_BRACES(K, V, HashStrategy.INSTANCE), getFeatures(), -1, true));
suite.addTest(orderedMapSuite("ARRAY_MAP", ARRAY_MAP::new, getFeatures(), -1)); suite.addTest(orderedMapSuite("ARRAY_MAP", ARRAY_MAP::new, getFeatures(), -1, true));
suite.addTest(concurrentMapSuite("CONCURRENT_HASH_MAP", CONCURRENT_HASH_MAP::new, getFeatures(), 2)); suite.addTest(concurrentMapSuite("CONCURRENT_HASH_MAP", CONCURRENT_HASH_MAP::new, getFeatures(), 2));
suite.addTest(concurrentMapSuite("CONCURRENT_HASH_MAP", CONCURRENT_HASH_MAP::new, getFeatures(), 3)); suite.addTest(concurrentMapSuite("CONCURRENT_HASH_MAP", CONCURRENT_HASH_MAP::new, getFeatures(), 3));
suite.addTest(navigableMapSuite("RB_TREE_MAP", RB_TREE_MAP::new, getFeatures(), -1)); suite.addTest(navigableMapSuite("RB_TREE_MAP", RB_TREE_MAP::new, getFeatures(), -1));
suite.addTest(navigableMapSuite("AVL_TREE_MAP", AVL_TREE_MAP::new, getFeatures(), -1)); suite.addTest(navigableMapSuite("AVL_TREE_MAP", AVL_TREE_MAP::new, getFeatures(), -1));
suite.addTest(navigableMapSuite("SynchronizedRB_TREE_MAP", (K, V) -> new RB_TREE_MAPKV_BRACES(K, V).synchronize(), getLimitedFeatures(), -1)); suite.addTest(navigableMapSuite("SynchronizedRB_TREE_MAP", (K, V) -> new RB_TREE_MAPKV_BRACES(K, V).synchronize(), getLimitedFeatures(), -1));
suite.addTest(navigableMapSuite("UnmodifiableRB_TREE_MAP", (K, V) -> new RB_TREE_MAPKV_BRACES(K, V).unmodifiable(), getLimitedImmutableFeatures(), -1)); suite.addTest(navigableMapSuite("UnmodifiableRB_TREE_MAP", (K, V) -> new RB_TREE_MAPKV_BRACES(K, V).unmodifiable(), getLimitedImmutableFeatures(), -1));
suite.addTest(orderedMapSuite("SynchronizedORDERED_MAP", (K, V) -> new LINKED_HASH_MAPKV_BRACES(K, V).synchronize(), getFeatures(), -1)); suite.addTest(orderedMapSuite("SynchronizedORDERED_MAP", (K, V) -> new LINKED_HASH_MAPKV_BRACES(K, V).synchronize(), getFeatures(), -1, true));
suite.addTest(orderedMapSuite("UnmodifiableORDERED_MAP", (K, V) -> new LINKED_HASH_MAPKV_BRACES(K, V).unmodifiable(), getImmutableFeatures(), -1)); suite.addTest(orderedMapSuite("UnmodifiableORDERED_MAP", (K, V) -> new LINKED_HASH_MAPKV_BRACES(K, V).unmodifiable(), getImmutableFeatures(), -1, true));
suite.addTest(mapSuite("EmptyMAP", (K, V) -> MAPS.empty(), getEmptyFeatures(), 0, false)); suite.addTest(mapSuite("EmptyMAP", (K, V) -> MAPS.empty(), getEmptyFeatures(), 0, false, true));
suite.addTest(mapSuite("SingletonMAP", (K, V) -> MAPS.singleton(K[0], V[0]), getEmptyFeatures(), 1, false)); suite.addTest(mapSuite("SingletonMAP", (K, V) -> MAPS.singleton(K[0], V[0]), getEmptyFeatures(), 1, false, true));
suite.addTest(mapSuite("AbstractMap", SIMPLE_TEST_MAP::new, getTestFeatures(), -1, false)); suite.addTest(mapSuite("AbstractMap", SIMPLE_TEST_MAP::new, getTestFeatures(), -1, false, true));
} }
#if TYPE_OBJECT #if TYPE_OBJECT
@@ -209,14 +214,14 @@ public class MAP_TESTS extends TestCase
#endif #endif
private static Test mapSuite(String name, BiFunction<KEY_STRING_TYPE[], VALUE_STRING_TYPE[], MAP KEY_VALUE_STRING_GENERIC_TYPE> factory, Collection<Feature<?>> features, int size, boolean sorted) { private static Test mapSuite(String name, BiFunction<KEY_STRING_TYPE[], VALUE_STRING_TYPE[], MAP KEY_VALUE_STRING_GENERIC_TYPE> factory, Collection<Feature<?>> features, int size, boolean sorted, boolean allowNullKeys) {
SIMPLE_MAP_TEST_GENERATOR.Maps KEY_VALUE_STRING_GENERIC_TYPE generator = new SIMPLE_MAP_TEST_GENERATOR.Maps KEY_VALUE_STRING_GENERIC_TYPE(factory); SIMPLE_MAP_TEST_GENERATOR.Maps KEY_VALUE_STRING_GENERIC_TYPE generator = new SIMPLE_MAP_TEST_GENERATOR.Maps KEY_VALUE_STRING_GENERIC_TYPE(factory);
MAP_TEST_BUILDER KEY_VALUE_STRING_GENERIC_TYPE builder = MAP_TEST_BUILDER.using(generator); MAP_TEST_BUILDER KEY_VALUE_STRING_GENERIC_TYPE builder = MAP_TEST_BUILDER.using(generator);
builder.shouldBlockKeys(sorted); builder.shouldBlockKeys(sorted);
#if TYPE_OBJECT #if TYPE_OBJECT
generator.setKeys(createKeys()); generator.setKeys(createKeys());
#ignore #ignore
builder.withFeatures(MapFeature.ALLOWS_NULL_KEYS); if(allowNullKeys) builder.withFeatures(MapFeature.ALLOWS_NULL_KEYS);
#endignore #endignore
#endif #endif
#if VALUE_OBJECT #if VALUE_OBJECT
@@ -265,13 +270,13 @@ public class MAP_TESTS extends TestCase
return builder.named(name).createTestSuite(); return builder.named(name).createTestSuite();
} }
private static Test orderedMapSuite(String name, BiFunction<KEY_STRING_TYPE[], VALUE_STRING_TYPE[], ORDERED_MAP KEY_VALUE_STRING_GENERIC_TYPE> factory, Collection<Feature<?>> features, int size) { private static Test orderedMapSuite(String name, BiFunction<KEY_STRING_TYPE[], VALUE_STRING_TYPE[], ORDERED_MAP KEY_VALUE_STRING_GENERIC_TYPE> factory, Collection<Feature<?>> features, int size, boolean allowNullKeys) {
SIMPLE_MAP_TEST_GENERATOR.OrderedMaps KEY_VALUE_STRING_GENERIC_TYPE generator = new SIMPLE_MAP_TEST_GENERATOR.OrderedMaps KEY_VALUE_STRING_GENERIC_TYPE(factory); SIMPLE_MAP_TEST_GENERATOR.OrderedMaps KEY_VALUE_STRING_GENERIC_TYPE generator = new SIMPLE_MAP_TEST_GENERATOR.OrderedMaps KEY_VALUE_STRING_GENERIC_TYPE(factory);
ORDERED_MAP_TEST_BUILDER KEY_VALUE_STRING_GENERIC_TYPE builder = ORDERED_MAP_TEST_BUILDER.using(generator); ORDERED_MAP_TEST_BUILDER KEY_VALUE_STRING_GENERIC_TYPE builder = ORDERED_MAP_TEST_BUILDER.using(generator);
#if TYPE_OBJECT #if TYPE_OBJECT
generator.setKeys(createKeys()); generator.setKeys(createKeys());
#ignore #ignore
builder.withFeatures(MapFeature.ALLOWS_NULL_KEYS); if(allowNullKeys) builder.withFeatures(MapFeature.ALLOWS_NULL_KEYS);
#endignore #endignore
#endif #endif
#if VALUE_OBJECT #if VALUE_OBJECT
@@ -7,6 +7,7 @@ import java.util.function.Consumer;
import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.booleans.functions.BooleanConsumer;
import speiger.src.collections.booleans.functions.BooleanComparator; import speiger.src.collections.booleans.functions.BooleanComparator;
import speiger.src.collections.objects.collections.ObjectIterable; import speiger.src.collections.objects.collections.ObjectIterable;
import speiger.src.collections.booleans.functions.OptionalBoolean;
import speiger.src.collections.booleans.functions.function.BooleanFunction; import speiger.src.collections.booleans.functions.function.BooleanFunction;
import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
@@ -271,13 +272,13 @@ public interface BooleanIterable extends Iterable<Boolean>
* @param filter that should be applied * @param filter that should be applied
* @return the found value or the null equivalent variant. * @return the found value or the null equivalent variant.
*/ */
default boolean findFirst(BooleanPredicate filter) { default OptionalBoolean findFirst(BooleanPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(BooleanIterator iter = iterator();iter.hasNext();) { for(BooleanIterator iter = iterator();iter.hasNext();) {
boolean entry = iter.nextBoolean(); boolean entry = iter.nextBoolean();
if(filter.test(entry)) return entry; if(filter.test(entry)) return OptionalBoolean.of(entry);
} }
return false; return OptionalBoolean.empty();
} }
/** /**
@@ -302,7 +303,7 @@ public interface BooleanIterable extends Iterable<Boolean>
* @param operator the operation that should be applied * @param operator the operation that should be applied
* @return the reduction result, returns null value if nothing was found * @return the reduction result, returns null value if nothing was found
*/ */
default boolean reduce(BooleanBooleanUnaryOperator operator) { default OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
boolean state = false; boolean state = false;
boolean empty = true; boolean empty = true;
@@ -314,7 +315,7 @@ public interface BooleanIterable extends Iterable<Boolean>
} }
state = operator.applyAsBoolean(state, iter.nextBoolean()); state = operator.applyAsBoolean(state, iter.nextBoolean());
} }
return state; return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state);
} }
/** /**
@@ -0,0 +1,89 @@
package speiger.src.collections.booleans.functions;
import java.util.NoSuchElementException;
import java.util.function.Supplier;
public final class OptionalBoolean {
private static final OptionalBoolean EMPTY = new OptionalBoolean();
private final boolean isPresent;
private final boolean value;
private OptionalBoolean() {
this.isPresent = false;
this.value = false;
}
private OptionalBoolean(boolean value) {
this.isPresent = true;
this.value = value;
}
public static OptionalBoolean empty() {
return EMPTY;
}
public static OptionalBoolean of(boolean value) {
return new OptionalBoolean(value);
}
public boolean getAsBoolean() {
if(!isPresent) throw new NoSuchElementException("No value present");
return value;
}
public boolean isPresent() {
return isPresent;
}
public boolean isEmpty() {
return !isPresent;
}
public void ifPresent(BooleanConsumer consumer) {
if(isPresent) consumer.accept(value);
}
public void ifPresentOrElse(BooleanConsumer action, Runnable emptyAction) {
if (isPresent) action.accept(value);
else emptyAction.run();
}
public boolean orElse(boolean other) {
return isPresent ? value : other;
}
public boolean orElseGet(BooleanSupplier other) {
return isPresent ? value : other.getAsBoolean();
}
public boolean orElseThrow() {
if (!isPresent) throw new NoSuchElementException("No value present");
return value;
}
public <X extends Throwable> boolean orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if(isPresent) return value;
else throw exceptionSupplier.get();
}
@Override
public boolean equals(Object obj) {
if(obj == this) return true;
if(obj instanceof OptionalBoolean) {
OptionalBoolean other = (OptionalBoolean)obj;
return (isPresent && other.isPresent ? value == other.value : isPresent == other.isPresent);
}
return false;
}
@Override
public int hashCode() {
return isPresent ? Boolean.hashCode(value) : 0;
}
@Override
public String toString() {
return isPresent ? "OptionalBoolean["+value+"]" : "OptionalBoolean.empty";
}
}
@@ -10,6 +10,7 @@ import java.util.function.UnaryOperator;
import speiger.src.collections.booleans.collections.BooleanCollection; import speiger.src.collections.booleans.collections.BooleanCollection;
import speiger.src.collections.booleans.collections.BooleanStack; import speiger.src.collections.booleans.collections.BooleanStack;
import speiger.src.collections.booleans.collections.BooleanIterator; import speiger.src.collections.booleans.collections.BooleanIterator;
import speiger.src.collections.booleans.functions.OptionalBoolean;
import speiger.src.collections.booleans.functions.BooleanComparator; import speiger.src.collections.booleans.functions.BooleanComparator;
import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.booleans.functions.BooleanConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
@@ -510,12 +511,12 @@ public class BooleanArrayList extends AbstractBooleanList implements IBooleanArr
} }
@Override @Override
public boolean findFirst(BooleanPredicate filter) { public OptionalBoolean findFirst(BooleanPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) { for(int i = 0;i<size;i++) {
if(filter.test(data[i])) return data[i]; if(filter.test(data[i])) return OptionalBoolean.of(data[i]);
} }
return false; return OptionalBoolean.empty();
} }
@Override @Override
@@ -529,7 +530,7 @@ public class BooleanArrayList extends AbstractBooleanList implements IBooleanArr
} }
@Override @Override
public boolean reduce(BooleanBooleanUnaryOperator operator) { public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
boolean state = false; boolean state = false;
boolean empty = true; boolean empty = true;
@@ -541,7 +542,7 @@ public class BooleanArrayList extends AbstractBooleanList implements IBooleanArr
} }
state = operator.applyAsBoolean(state, data[i]); state = operator.applyAsBoolean(state, data[i]);
} }
return state; return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state);
} }
@Override @Override
@@ -8,6 +8,7 @@ import java.util.Spliterator;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
import speiger.src.collections.booleans.functions.OptionalBoolean;
import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
import speiger.src.collections.booleans.functions.function.BooleanPredicate; import speiger.src.collections.booleans.functions.function.BooleanPredicate;
@@ -415,12 +416,12 @@ public class BooleanLinkedList extends AbstractBooleanList implements BooleanPri
} }
@Override @Override
public boolean findFirst(BooleanPredicate filter) { public OptionalBoolean findFirst(BooleanPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(Entry entry = first;entry != null;entry = entry.next) { for(Entry entry = first;entry != null;entry = entry.next) {
if(filter.test(entry.value)) return entry.value; if(filter.test(entry.value)) return OptionalBoolean.of(entry.value);
} }
return false; return OptionalBoolean.empty();
} }
@Override @Override
@@ -434,7 +435,7 @@ public class BooleanLinkedList extends AbstractBooleanList implements BooleanPri
} }
@Override @Override
public boolean reduce(BooleanBooleanUnaryOperator operator) { public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
boolean state = false; boolean state = false;
boolean empty = true; boolean empty = true;
@@ -446,7 +447,7 @@ public class BooleanLinkedList extends AbstractBooleanList implements BooleanPri
} }
state = operator.applyAsBoolean(state, entry.value); state = operator.applyAsBoolean(state, entry.value);
} }
return state; return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state);
} }
@Override @Override
@@ -14,6 +14,7 @@ import java.util.function.UnaryOperator;
import speiger.src.collections.booleans.collections.BooleanCollection; import speiger.src.collections.booleans.collections.BooleanCollection;
import speiger.src.collections.booleans.collections.BooleanStack; import speiger.src.collections.booleans.collections.BooleanStack;
import speiger.src.collections.booleans.collections.BooleanIterator; import speiger.src.collections.booleans.collections.BooleanIterator;
import speiger.src.collections.booleans.functions.OptionalBoolean;
import speiger.src.collections.booleans.functions.BooleanComparator; import speiger.src.collections.booleans.functions.BooleanComparator;
import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.booleans.functions.BooleanConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
@@ -106,7 +107,6 @@ public class CopyOnWriteBooleanArrayList extends AbstractBooleanList implements
System.arraycopy(a, offset, data, 0, length); System.arraycopy(a, offset, data, 0, length);
} }
private void setArray(boolean[] data) { private void setArray(boolean[] data) {
this.data = data; this.data = data;
} }
@@ -645,13 +645,13 @@ public class CopyOnWriteBooleanArrayList extends AbstractBooleanList implements
} }
@Override @Override
public boolean findFirst(BooleanPredicate filter) { public OptionalBoolean findFirst(BooleanPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
boolean[] data = this.data; boolean[] data = this.data;
for(int i = 0,m=data.length;i<m;i++) { for(int i = 0,m=data.length;i<m;i++) {
if(filter.test(data[i])) return data[i]; if(filter.test(data[i])) return OptionalBoolean.of(data[i]);
} }
return false; return OptionalBoolean.empty();
} }
@Override @Override
@@ -666,7 +666,7 @@ public class CopyOnWriteBooleanArrayList extends AbstractBooleanList implements
} }
@Override @Override
public boolean reduce(BooleanBooleanUnaryOperator operator) { public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
boolean[] data = this.data; boolean[] data = this.data;
boolean state = false; boolean state = false;
@@ -679,7 +679,7 @@ public class CopyOnWriteBooleanArrayList extends AbstractBooleanList implements
} }
state = operator.applyAsBoolean(state, data[i]); state = operator.applyAsBoolean(state, data[i]);
} }
return state; return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state);
} }
@Override @Override
@@ -10,6 +10,7 @@ import java.util.function.UnaryOperator;
import speiger.src.collections.booleans.collections.BooleanCollection; import speiger.src.collections.booleans.collections.BooleanCollection;
import speiger.src.collections.booleans.functions.BooleanComparator; import speiger.src.collections.booleans.functions.BooleanComparator;
import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.booleans.functions.BooleanConsumer;
import speiger.src.collections.booleans.functions.OptionalBoolean;
import speiger.src.collections.booleans.utils.BooleanArrays; import speiger.src.collections.booleans.utils.BooleanArrays;
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
import speiger.src.collections.booleans.functions.function.BooleanPredicate; import speiger.src.collections.booleans.functions.function.BooleanPredicate;
@@ -270,12 +271,12 @@ public class ImmutableBooleanList extends AbstractBooleanList
} }
@Override @Override
public boolean findFirst(BooleanPredicate filter) { public OptionalBoolean findFirst(BooleanPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0,m=data.length;i<m;i++) { for(int i = 0,m=data.length;i<m;i++) {
if(filter.test(data[i])) return data[i]; if(filter.test(data[i])) return OptionalBoolean.of(data[i]);
} }
return false; return OptionalBoolean.empty();
} }
@Override @Override
@@ -289,7 +290,7 @@ public class ImmutableBooleanList extends AbstractBooleanList
} }
@Override @Override
public boolean reduce(BooleanBooleanUnaryOperator operator) { public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
boolean state = false; boolean state = false;
boolean empty = true; boolean empty = true;
@@ -301,7 +302,7 @@ public class ImmutableBooleanList extends AbstractBooleanList
} }
state = operator.applyAsBoolean(state, data[i]); state = operator.applyAsBoolean(state, data[i]);
} }
return state; return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state);
} }
@Override @Override
@@ -5,6 +5,7 @@ import java.util.Objects;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import speiger.src.collections.booleans.collections.BooleanIterator; import speiger.src.collections.booleans.collections.BooleanIterator;
import speiger.src.collections.booleans.functions.OptionalBoolean;
import speiger.src.collections.booleans.functions.BooleanComparator; import speiger.src.collections.booleans.functions.BooleanComparator;
import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.booleans.functions.BooleanConsumer;
import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer;
@@ -278,17 +279,17 @@ public class BooleanArrayFIFOQueue extends AbstractBooleanPriorityQueue implemen
} }
@Override @Override
public boolean findFirst(BooleanPredicate filter) { public OptionalBoolean findFirst(BooleanPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0,m=size();i<m;i++) { for(int i = 0,m=size();i<m;i++) {
int index = (first + i) % array.length; int index = (first + i) % array.length;
if(filter.test(array[index])) { if(filter.test(array[index])) {
boolean data = array[index]; boolean data = array[index];
removeIndex(index); removeIndex(index);
return data; return OptionalBoolean.of(data);
} }
} }
return false; return OptionalBoolean.empty();
} }
@Override @Override
@@ -302,7 +303,7 @@ public class BooleanArrayFIFOQueue extends AbstractBooleanPriorityQueue implemen
} }
@Override @Override
public boolean reduce(BooleanBooleanUnaryOperator operator) { public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
boolean state = false; boolean state = false;
boolean empty = true; boolean empty = true;
@@ -314,7 +315,7 @@ public class BooleanArrayFIFOQueue extends AbstractBooleanPriorityQueue implemen
} }
state = operator.applyAsBoolean(state, array[(first + i) % array.length]); state = operator.applyAsBoolean(state, array[(first + i) % array.length]);
} }
return state; return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state);
} }
@Override @Override
@@ -6,6 +6,7 @@ import java.util.Objects;
import speiger.src.collections.booleans.collections.BooleanCollection; import speiger.src.collections.booleans.collections.BooleanCollection;
import speiger.src.collections.booleans.collections.BooleanIterator; import speiger.src.collections.booleans.collections.BooleanIterator;
import speiger.src.collections.booleans.functions.OptionalBoolean;
import speiger.src.collections.booleans.functions.BooleanComparator; import speiger.src.collections.booleans.functions.BooleanComparator;
import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.booleans.functions.BooleanConsumer;
import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer;
@@ -173,7 +174,6 @@ public class BooleanArrayPriorityQueue extends AbstractBooleanPriorityQueue
queue.size = size; queue.size = size;
return queue; return queue;
} }
@Override @Override
public void enqueue(boolean e) { public void enqueue(boolean e) {
if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (long)(array.length >> 1), (long)SanityChecks.MAX_ARRAY_SIZE), size+1)); if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (long)(array.length >> 1), (long)SanityChecks.MAX_ARRAY_SIZE), size+1));
@@ -307,7 +307,7 @@ public class BooleanArrayPriorityQueue extends AbstractBooleanPriorityQueue
} }
@Override @Override
public boolean reduce(BooleanBooleanUnaryOperator operator) { public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
boolean state = false; boolean state = false;
boolean empty = true; boolean empty = true;
@@ -319,20 +319,20 @@ public class BooleanArrayPriorityQueue extends AbstractBooleanPriorityQueue
} }
state = operator.applyAsBoolean(state, array[i]); state = operator.applyAsBoolean(state, array[i]);
} }
return state; return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state);
} }
@Override @Override
public boolean findFirst(BooleanPredicate filter) { public OptionalBoolean findFirst(BooleanPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) { for(int i = 0;i<size;i++) {
if(filter.test(array[i])) { if(filter.test(array[i])) {
boolean data = array[i]; boolean data = array[i];
removeIndex(i); removeIndex(i);
return data; return OptionalBoolean.of(data);
} }
} }
return false; return OptionalBoolean.empty();
} }
@Override @Override
@@ -6,6 +6,7 @@ import java.util.Objects;
import speiger.src.collections.booleans.collections.BooleanCollection; import speiger.src.collections.booleans.collections.BooleanCollection;
import speiger.src.collections.booleans.collections.BooleanIterator; import speiger.src.collections.booleans.collections.BooleanIterator;
import speiger.src.collections.booleans.functions.OptionalBoolean;
import speiger.src.collections.booleans.functions.BooleanComparator; import speiger.src.collections.booleans.functions.BooleanComparator;
import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.booleans.functions.BooleanConsumer;
import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer;
@@ -290,7 +291,7 @@ public class BooleanHeapPriorityQueue extends AbstractBooleanPriorityQueue
} }
@Override @Override
public boolean reduce(BooleanBooleanUnaryOperator operator) { public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
boolean state = false; boolean state = false;
boolean empty = true; boolean empty = true;
@@ -302,20 +303,20 @@ public class BooleanHeapPriorityQueue extends AbstractBooleanPriorityQueue
} }
state = operator.applyAsBoolean(state, array[i]); state = operator.applyAsBoolean(state, array[i]);
} }
return state; return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state);
} }
@Override @Override
public boolean findFirst(BooleanPredicate filter) { public OptionalBoolean findFirst(BooleanPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) { for(int i = 0;i<size;i++) {
if(filter.test(array[i])) { if(filter.test(array[i])) {
boolean data = array[i]; boolean data = array[i];
removeIndex(i); removeIndex(i);
return data; return OptionalBoolean.of(data);
} }
} }
return false; return OptionalBoolean.empty();
} }
@Override @Override
@@ -7,6 +7,7 @@ import java.util.concurrent.RecursiveAction;
import speiger.src.collections.booleans.functions.BooleanComparator; import speiger.src.collections.booleans.functions.BooleanComparator;
import speiger.src.collections.booleans.collections.BooleanIterator; import speiger.src.collections.booleans.collections.BooleanIterator;
import speiger.src.collections.utils.SanityChecks; import speiger.src.collections.utils.SanityChecks;
import speiger.src.collections.utils.Swapper;
/** /**
* A Helper class for Arrays * A Helper class for Arrays
@@ -269,6 +270,65 @@ public class BooleanArrays
return array; return array;
} }
/**
* Simple Shuffle method for Arrays.
* With a Callback for indirect shuffling
* @param array the elements that should be shuffled
* @param swapper the callback on the swaps
* @note This uses the SanityChecks#getRandom
* @return the provided sorted array
*/
public static boolean[] indirectShuffle(boolean[] array, Swapper swapper) {
return indirectShuffle(array, SanityChecks.getRandom(), swapper);
}
/**
* Simple Shuffle method for Arrays.
* With a Callback for indirect shuffling
* @param array the elements that should be shuffled
* @param random the Random Number Generator that should be used for the shuffling
* @param swapper the callback on the swaps
* @return the provided sorted array
*/
public static boolean[] indirectShuffle(boolean[] array, Random random, Swapper swapper) {
return indirectShuffle(array, 0, array.length, random, swapper);
}
/**
* Simple Shuffle method for Arrays.
* With a Callback for indirect shuffling
* @param array the elements that should be shuffled
* @param length the length of the array
* @param random the Random Number Generator that should be used for the shuffling
* @param swapper the callback on the swaps
* @return the provided sorted array
*/
public static boolean[] indirectShuffle(boolean[] array, int length, Random random, Swapper swapper) {
return indirectShuffle(array, 0, length, random, swapper);
}
/**
* Simple Shuffle method for Arrays.
* With a Callback for indirect shuffling
* @param array the elements that should be shuffled
* @param offset the start array
* @param length the length of the array
* @param random the Random Number Generator that should be used for the shuffling
* @param swapper the callback on the swaps
* @return the provided sorted array
*/
public static boolean[] indirectShuffle(boolean[] array, int offset, int length, Random random, Swapper swapper) {
for(int i = length-1; i>=0;i--) {
int j = offset + i;
int p = offset + random.nextInt(i + 1);
swapper.swap(j, p);
boolean t = array[j];
array[j] = array[p];
array[p] = t;
}
return array;
}
/** /**
* Simple Array Reversal method * Simple Array Reversal method
* @param array the Array that should flip * @param array the Array that should flip
@@ -489,6 +549,52 @@ public class BooleanArrays
} }
} }
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Insertion Sort,
* On top of that allows to sort other things along with it.
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @return input array
*/
public static boolean[] indirectInsertionSort(boolean[] array, BooleanComparator comp, Swapper swapper) {
indirectInsertionSort(array, 0, array.length, comp, swapper);
return array;
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Insertion Sort,
* On top of that allows to sort other things along with it.
* @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
*/
public static void indirectInsertionSort(boolean[] array, int length, BooleanComparator comp, Swapper swapper) {
indirectInsertionSort(array, 0, length, comp, swapper);
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Insertion Sort,
* On top of that allows to sort other things along with it.
* @param array the array that needs to be sorted
* @param from where the array should be sorted from
* @param to where the array should be sorted to
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
*/
public static void indirectInsertionSort(boolean[] array, int from, int to, BooleanComparator comp, Swapper swapper) {
for (int i = from+1;i<to; i++) {
boolean current = array[i];
int j = i - 1;
while(j >= from && comp.compare(current, array[j]) < 0) {
swapper.swap(j+1, j);
array[j+1] = array[j--];
}
array[j+1] = current;
}
}
/** /**
* Sorts an array according to the natural ascending order using InsertionSort, * Sorts an array according to the natural ascending order using InsertionSort,
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
@@ -569,6 +675,57 @@ public class BooleanArrays
} }
} }
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Selection Sort,
* On top of that allows to sort other things along with it.
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @return input array
*/
public static boolean[] indirectSelectionSort(boolean[] array, BooleanComparator comp, Swapper swapper) {
indirectSelectionSort(array, 0, array.length, comp, swapper);
return array;
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Selection Sort,
* On top of that allows to sort other things along with it.
* @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
*/
public static void indirectSelectionSort(boolean[] array, int length, BooleanComparator comp, Swapper swapper) {
indirectSelectionSort(array, 0, length, comp, swapper);
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Selection Sort,
* On top of that allows to sort other things along with it.
* @param array the array that needs to be sorted
* @param from where the array should be sorted from
* @param to where the array should be sorted to
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
*/
public static void indirectSelectionSort(boolean[] array, int from, int to, BooleanComparator comp, Swapper swapper) {
for (int i = from; i < to; i++) {
boolean min = array[i];
int minId = i;
for(int j = i+1; j < to; j++) {
if(comp.compare(array[j], min) < 0) {
min = array[j];
minId = j;
}
}
swapper.swap(i, minId);
boolean temp = array[i];
array[i] = min;
array[minId] = temp;
}
}
/** /**
* Sorts an array according to the natural ascending order using Selection Sort, * Sorts an array according to the natural ascending order using Selection Sort,
* @param array the array that needs to be sorted * @param array the array that needs to be sorted
@@ -662,6 +819,69 @@ public class BooleanArrays
} }
} }
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Merge Sort,
* On top of that allows to sort other things along with it.
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @return input array
*/
public static boolean[] indirectMergeSort(boolean[] array, BooleanComparator comp, Swapper swapper) {
indirectMergeSort(array, null, 0, array.length, comp, swapper);
return array;
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Merge Sort,
* On top of that allows to sort other things along with it.
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
* @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
*/
public static void indirectMergeSort(boolean[] array, int length, BooleanComparator comp, Swapper swapper) {
indirectMergeSort(array, null, 0, length, comp, swapper);
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Merge Sort,
* On top of that allows to sort other things along with it.
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
* @param array the array that needs to be sorted
* @param supp the auxillary array that is used to simplify the sorting
* @param from where the array should be sorted from
* @param to where the array should be sorted to
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
*/
public static void indirectMergeSort(boolean[] array, boolean[] supp, int from, int to, BooleanComparator comp, Swapper swapper) {
if(to - from < BASE_THRESHOLD) {
indirectInsertionSort(array, from, to, comp, swapper);
return;
}
if(supp == null) supp = Arrays.copyOf(array, to);
int mid = (from + to) >>> 1;
indirectMergeSort(supp, array, from, mid, comp, swapper);
indirectMergeSort(supp, array, mid, to, comp, swapper);
if(comp.compare(supp[mid - 1], supp[mid]) <= 0)
{
System.arraycopy(supp, from, array, from, to - from);
return;
}
for(int p = from, q = mid;from < to;from++) {
if(q >= to || p < mid && comp.compare(supp[p], supp[q]) < 0) {
swapper.swap(from, p);
array[from] = supp[p++];
continue;
}
swapper.swap(from, q);
array[from] = supp[q++];
}
}
/** /**
* Sorts an array according to the natural ascending order using Merge Sort, * Sorts an array according to the natural ascending order using Merge Sort,
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations * This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
@@ -752,6 +972,53 @@ public class BooleanArrays
mergeSort(array, supp, from, to, comp); mergeSort(array, supp, from, to, comp);
} }
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using a Parallel Merge Sort,
* On top of that allows to sort other things along with it.
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/
public static void indirectParallelMergeSort(boolean[] array, BooleanComparator comp, Swapper swapper) {
indirectParallelMergeSort(array, null, 0, array.length, comp, swapper);
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Parallel Merge Sort,
* On top of that allows to sort other things along with it.
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
* @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted
* @param swapper the callback which elements were swapped
* @param comp the Comparator that decides the sorting order
* @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/
public static void indirectParallelMergeSort(boolean[] array, int length, BooleanComparator comp, Swapper swapper) {
indirectParallelMergeSort(array, null, 0, length, comp, swapper);
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Parallel Merge Sort,
* On top of that allows to sort other things along with it.
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
* @param array the array that needs to be sorted
* @param supp the auxillary array that is used to simplify the sorting
* @param from where the array should be sorted from
* @param to where the array should be sorted to
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/
public static void indirectParallelMergeSort(boolean[] array, boolean[] supp, int from, int to, BooleanComparator comp, Swapper swapper) {
if(SanityChecks.canParallelTask() && to - from >= PARALLEL_THRESHOLD) {
SanityChecks.invokeTask(new MergeSortActionCompSwap(array, supp, from, to, comp, swapper));
return;
}
indirectMergeSort(array, supp, from, to, comp, swapper);
}
/** /**
* Sorts an array according to the natural ascending order using Parallel Merge Sort, * Sorts an array according to the natural ascending order using Parallel Merge Sort,
* This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations * This implementation was copied from <a href="https://github.com/vigna/fastutil">FastUtil</a> with a couple custom optimizations
@@ -1087,6 +1354,70 @@ public class BooleanArrays
if((length = d - c) > 1) quickSort(array, to - length, to, comp); if((length = d - c) > 1) quickSort(array, to - length, to, comp);
} }
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Quick Sort,
* On top of that allows to sort other things along with it.
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @return input array
*/
public static boolean[] indirectQuickSort(boolean[] array, BooleanComparator comp, Swapper swapper) {
indirectQuickSort(array, 0, array.length, comp, swapper);
return array;
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Quick Sort,
* On top of that allows to sort other things along with it.
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
*/
public static void indirectQuickSort(boolean[] array, int length, BooleanComparator comp, Swapper swapper) {
indirectQuickSort(array, 0, length, comp, swapper);
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Quick Sort,
* On top of that allows to sort other things along with it.
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted
* @param from where the array should be sorted from
* @param to where the array should be sorted to
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
*/
public static void indirectQuickSort(boolean[] array, int from, int to, BooleanComparator comp, Swapper swapper) {
int length = to - from;
if(length <= 0) return;
if(length < BASE_THRESHOLD) {
indirectSelectionSort(array, from, to, comp, swapper);
return;
}
boolean pivot = array[length > 128 ? subMedium(array, from, from + (length / 2), to - 1, length / 8, comp) : medium(array, from, from + (length / 2), to - 1, comp)];
int a = from, b = a, c = to - 1, d = c;
for(int compare;;swap(array, b++, c--, swapper)) {
for(;b<=c && (compare = comp.compare(array[b], pivot)) <= 0;b++) {
if(compare == 0) swap(array, a++, b, swapper);
}
for(;c>=b && (compare = comp.compare(array[c], pivot)) >= 0;c--) {
if(compare == 0) swap(array, c, d--, swapper);
}
if(b>c) break;
}
swap(array, from, b, Math.min(a - from, b - a), swapper);
swap(array, b, to, Math.min(d - c, to - d - 1), swapper);
if((length = b - a) > 1) indirectQuickSort(array, from, from + length, comp, swapper);
if((length = d - c) > 1) indirectQuickSort(array, to - length, to, comp, swapper);
}
/** /**
* Sorts an array according to the natural ascending order using Quick Sort, * Sorts an array according to the natural ascending order using Quick Sort,
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure, * This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
@@ -1185,6 +1516,55 @@ public class BooleanArrays
quickSort(array, from, to, comp); quickSort(array, from, to, comp);
} }
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Parallel Quick Sort,
* On top of that allows to sort other things along with it.
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/
public static void indirectParallelQuickSort(boolean[] array, BooleanComparator comp, Swapper swapper) {
indirectParallelQuickSort(array, 0, array.length, comp, swapper);
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Parallel Quick Sort,
* On top of that allows to sort other things along with it.
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted
* @param length the maxmium size of the array to be sorted
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/
public static void indirectParallelQuickSort(boolean[] array, int length, BooleanComparator comp, Swapper swapper) {
indirectParallelQuickSort(array, 0, length, comp, swapper);
}
/**
* Sorts the specified range of elements according to the order induced by the specified comparator using Parallel Quick Sort,
* On top of that allows to sort other things along with it.
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
* and that sorting Algorithm is based on the tuned quicksort adapted from Jon L. Bentley and M. DouglasMcIlroy, "Engineering a Sort Function", Software: Practice and Experience, 23(11), pages12491265, 1993.
* @param array the array that needs to be sorted
* @param from where the array should be sorted from
* @param to where the array should be sorted to
* @param comp the Comparator that decides the sorting order
* @param swapper the callback which elements were swapped
* @note This parallelization is invoked through {@link SanityChecks#invokeTask} which the threadpool can be changed as needed
*/
public static void indirectParallelQuickSort(boolean[] array, int from, int to, BooleanComparator comp, Swapper swapper) {
if(SanityChecks.canParallelTask() && to - from >= PARALLEL_THRESHOLD) {
SanityChecks.invokeTask(new QuickSortActionCompSwap(array, from, to, comp, swapper));
return;
}
indirectQuickSort(array, from, to, comp, swapper);
}
/** /**
* Sorts an array according to the natural ascending order using Parallel Quick Sort, * Sorts an array according to the natural ascending order using Parallel Quick Sort,
* This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure, * This implementation is a custom of <a href="https://github.com/vigna/fastutil">FastUtil</a> quicksort but with a different code structure,
@@ -1236,6 +1616,18 @@ public class BooleanArrays
for(int i = 0;i<length;i++,swap(a, from++, to++)); for(int i = 0;i<length;i++,swap(a, from++, to++));
} }
static void swap(boolean[] a, int from, int to, Swapper swapper) {
swapper.swap(from, to);
boolean t = a[from];
a[from] = a[to];
a[to] = t;
}
static void swap(boolean[] a, int from, int to, int length, Swapper swapper) {
to -= length;
for(int i = 0;i<length;i++,swap(a, from++, to++, swapper));
}
static int subMedium(boolean[] data, int a, int b, int c, int length, BooleanComparator comp) { static int subMedium(boolean[] data, int a, int b, int c, int length, BooleanComparator comp) {
return medium(data, medium(data, a, a + length, a + (length * 2), comp), medium(data, b - length, b, b + length, comp), medium(data, c - (length * 2), c - length, c, comp), comp); return medium(data, medium(data, a, a + length, a + (length * 2), comp), medium(data, b - length, b, b + length, comp), medium(data, c - (length * 2), c - length, c, comp), comp);
} }
@@ -1258,16 +1650,14 @@ public class BooleanArrays
int from; int from;
int to; int to;
QuickSortAction(boolean[] array, int from, int to) QuickSortAction(boolean[] array, int from, int to) {
{
this.array = array; this.array = array;
this.from = from; this.from = from;
this.to = to; this.to = to;
} }
@Override @Override
protected void compute() protected void compute() {
{
int length = to - from; int length = to - from;
if(length <= 0) return; if(length <= 0) return;
if(length < BASE_THRESHOLD) { if(length < BASE_THRESHOLD) {
@@ -1300,8 +1690,7 @@ public class BooleanArrays
int to; int to;
BooleanComparator comp; BooleanComparator comp;
QuickSortActionComp(boolean[] array, int from, int to, BooleanComparator comp) QuickSortActionComp(boolean[] array, int from, int to, BooleanComparator comp) {
{
this.array = array; this.array = array;
this.from = from; this.from = from;
this.to = to; this.to = to;
@@ -1309,8 +1698,7 @@ public class BooleanArrays
} }
@Override @Override
protected void compute() protected void compute() {
{
int length = to - from; int length = to - from;
if(length <= 0) return; if(length <= 0) return;
if(length < BASE_THRESHOLD) { if(length < BASE_THRESHOLD) {
@@ -1336,6 +1724,49 @@ public class BooleanArrays
} }
} }
static class QuickSortActionCompSwap extends RecursiveAction {
private static final long serialVersionUID = 0L;
boolean[] array;
int from;
int to;
BooleanComparator comp;
Swapper swapper;
QuickSortActionCompSwap(boolean[] array, int from, int to, BooleanComparator comp, Swapper swapper) {
this.array = array;
this.from = from;
this.to = to;
this.comp = comp;
this.swapper = swapper;
}
@Override
protected void compute() {
int length = to - from;
if(length <= 0) return;
if(length < BASE_THRESHOLD) {
indirectSelectionSort(array, from, to, comp, swapper);
return;
}
boolean pivot = array[length > 128 ? subMedium(array, from, from + (length / 2), to - 1, length / 8, comp) : medium(array, from, from + (length / 2), to - 1, comp)];
int a = from, b = a, c = to - 1, d = c;
for(int compare;;swap(array, b++, c--, swapper)) {
for(;b<=c && (compare = comp.compare(array[b], pivot)) <= 0;b++) {
if(compare == 0) swap(array, a++, b, swapper);
}
for(;c>=b && (compare = comp.compare(array[c], pivot)) >= 0;c--) {
if(compare == 0) swap(array, c, d--, swapper);
}
if(b>c) break;
}
swap(array, from, b, Math.min(a - from, b - a), swapper);
swap(array, b, to, Math.min(d - c, to - d - 1), swapper);
if(b - a > 1 && d - c > 1) invokeAll(new QuickSortActionCompSwap(array, from, from + (b - a), comp, swapper), new QuickSortActionCompSwap(array, to - (d - c), to, comp, swapper));
else if(b - a > 1) new QuickSortActionCompSwap(array, from, from + (b - a), comp, swapper).invoke();
else if(d - c > 1) new QuickSortActionCompSwap(array, to - (d - c), to, comp, swapper).invoke();
}
}
static class MergeSortAction extends RecursiveAction { static class MergeSortAction extends RecursiveAction {
private static final long serialVersionUID = 0L; private static final long serialVersionUID = 0L;
boolean[] array; boolean[] array;
@@ -1343,8 +1774,7 @@ public class BooleanArrays
int from; int from;
int to; int to;
MergeSortAction(boolean[] array, boolean[] supp, int from, int to) MergeSortAction(boolean[] array, boolean[] supp, int from, int to) {
{
this.array = array; this.array = array;
this.supp = supp; this.supp = supp;
this.from = from; this.from = from;
@@ -1352,8 +1782,7 @@ public class BooleanArrays
} }
@Override @Override
protected void compute() protected void compute() {
{
if(to - from < BASE_THRESHOLD) { if(to - from < BASE_THRESHOLD) {
insertionSort(array, from, to); insertionSort(array, from, to);
return; return;
@@ -1381,8 +1810,7 @@ public class BooleanArrays
int to; int to;
BooleanComparator comp; BooleanComparator comp;
MergeSortActionComp(boolean[] array, boolean[] supp, int from, int to, BooleanComparator comp) MergeSortActionComp(boolean[] array, boolean[] supp, int from, int to, BooleanComparator comp) {
{
this.array = array; this.array = array;
this.supp = supp; this.supp = supp;
this.from = from; this.from = from;
@@ -1391,8 +1819,7 @@ public class BooleanArrays
} }
@Override @Override
protected void compute() protected void compute() {
{
if(to - from < BASE_THRESHOLD) { if(to - from < BASE_THRESHOLD) {
insertionSort(array, from, to, comp); insertionSort(array, from, to, comp);
return; return;
@@ -1412,22 +1839,64 @@ public class BooleanArrays
} }
} }
static class MergeSortActionCompSwap extends RecursiveAction {
private static final long serialVersionUID = 0L;
boolean[] array;
boolean[] supp;
int from;
int to;
BooleanComparator comp;
Swapper swapper;
MergeSortActionCompSwap(boolean[] array, boolean[] supp, int from, int to, BooleanComparator comp, Swapper swapper) {
this.array = array;
this.supp = supp;
this.from = from;
this.to = to;
this.comp = comp;
this.swapper = swapper;
}
@Override
protected void compute() {
if(to - from < BASE_THRESHOLD) {
indirectInsertionSort(array, from, to, comp, swapper);
return;
}
if(supp == null) supp = Arrays.copyOf(array, to);
int mid = (from + to) >>> 1;
invokeAll(new MergeSortActionCompSwap(supp, array, from, mid, comp, swapper), new MergeSortActionCompSwap(supp, array, mid, to, comp, swapper));
if(comp.compare(supp[mid - 1], supp[mid]) <= 0)
{
System.arraycopy(supp, from, array, from, to - from);
return;
}
for(int p = from, q = mid;from < to;from++) {
if(q >= to || p < mid && comp.compare(supp[p], supp[q]) < 0) {
swapper.swap(from, p);
array[from] = supp[p++];
continue;
}
swapper.swap(from, q);
array[from] = supp[q++];
}
}
}
static class MemFreeMergeSortAction extends RecursiveAction { static class MemFreeMergeSortAction extends RecursiveAction {
private static final long serialVersionUID = 0L; private static final long serialVersionUID = 0L;
boolean[] array; boolean[] array;
int from; int from;
int to; int to;
MemFreeMergeSortAction(boolean[] array, int from, int to) MemFreeMergeSortAction(boolean[] array, int from, int to) {
{
this.array = array; this.array = array;
this.from = from; this.from = from;
this.to = to; this.to = to;
} }
@Override @Override
protected void compute() protected void compute() {
{
if(to - from < BASE_THRESHOLD) { if(to - from < BASE_THRESHOLD) {
insertionSort(array, from, to); insertionSort(array, from, to);
return; return;
@@ -1473,8 +1942,7 @@ public class BooleanArrays
int to; int to;
BooleanComparator comp; BooleanComparator comp;
MemFreeMergeSortActionComp(boolean[] array, int from, int to, BooleanComparator comp) MemFreeMergeSortActionComp(boolean[] array, int from, int to, BooleanComparator comp) {
{
this.array = array; this.array = array;
this.from = from; this.from = from;
this.to = to; this.to = to;
@@ -1482,8 +1950,7 @@ public class BooleanArrays
} }
@Override @Override
protected void compute() protected void compute() {
{
if(to - from < BASE_THRESHOLD) { if(to - from < BASE_THRESHOLD) {
insertionSort(array, from, to, comp); insertionSort(array, from, to, comp);
return; return;
@@ -7,6 +7,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.LockSupport; import java.util.concurrent.locks.LockSupport;
import java.util.function.Consumer; import java.util.function.Consumer;
import speiger.src.collections.booleans.functions.OptionalBoolean;
import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.booleans.functions.BooleanConsumer;
import speiger.src.collections.booleans.functions.BooleanComparator; import speiger.src.collections.booleans.functions.BooleanComparator;
@@ -207,9 +208,8 @@ public class BooleanAsyncBuilder
* @param operator that reduces the elements. * @param operator that reduces the elements.
* @return self with the reduce action applied * @return self with the reduce action applied
*/ */
public BooleanAsyncBuilder reduce(BooleanBooleanUnaryOperator operator) { public ObjectAsyncBuilder<OptionalBoolean> reduce(BooleanBooleanUnaryOperator operator) {
task = new SimpleReduceTask(iterable.iterator(), operator); return new ObjectAsyncBuilder<>(new SimpleReduceTask(iterable.iterator(), operator));
return this;
} }
/** /**
@@ -281,9 +281,8 @@ public class BooleanAsyncBuilder
* @param filter that decides the desired elements * @param filter that decides the desired elements
* @return self with the findFirst function applied * @return self with the findFirst function applied
*/ */
public BooleanAsyncBuilder findFirst(BooleanPredicate filter) { public ObjectAsyncBuilder<OptionalBoolean> findFirst(BooleanPredicate filter) {
task = new FindFirstTask(iterable.iterator(), filter); return new ObjectAsyncBuilder<>(new FindFirstTask(iterable.iterator(), filter));
return this;
} }
/** /**
@@ -393,7 +392,7 @@ public class BooleanAsyncBuilder
} }
} }
private static class SimpleReduceTask extends BaseBooleanTask private static class SimpleReduceTask extends BaseObjectTask<OptionalBoolean>
{ {
BooleanIterator iter; BooleanIterator iter;
BooleanBooleanUnaryOperator operator; BooleanBooleanUnaryOperator operator;
@@ -403,6 +402,7 @@ public class BooleanAsyncBuilder
public SimpleReduceTask(BooleanIterator iter, BooleanBooleanUnaryOperator operator) { public SimpleReduceTask(BooleanIterator iter, BooleanBooleanUnaryOperator operator) {
this.iter = iter; this.iter = iter;
this.operator = operator; this.operator = operator;
this.setResult(OptionalBoolean.empty());
} }
@Override @Override
@@ -417,7 +417,7 @@ public class BooleanAsyncBuilder
} }
} }
if(!iter.hasNext()) { if(!iter.hasNext()) {
setResult(value); setResult(OptionalBoolean.of(value));
return true; return true;
} }
return false; return false;
@@ -487,7 +487,7 @@ public class BooleanAsyncBuilder
} }
} }
private static class FindFirstTask extends BaseBooleanTask private static class FindFirstTask extends BaseObjectTask<OptionalBoolean>
{ {
BooleanIterator iter; BooleanIterator iter;
BooleanPredicate filter; BooleanPredicate filter;
@@ -495,6 +495,7 @@ public class BooleanAsyncBuilder
public FindFirstTask(BooleanIterator iter, BooleanPredicate filter) { public FindFirstTask(BooleanIterator iter, BooleanPredicate filter) {
this.iter = iter; this.iter = iter;
this.filter = filter; this.filter = filter;
this.setResult(OptionalBoolean.empty());
} }
@Override @Override
@@ -502,7 +503,7 @@ public class BooleanAsyncBuilder
while(shouldRun() && iter.hasNext()) { while(shouldRun() && iter.hasNext()) {
boolean entry = iter.nextBoolean(); boolean entry = iter.nextBoolean();
if(filter.test(iter.nextBoolean())) { if(filter.test(iter.nextBoolean())) {
setResult(entry); setResult(OptionalBoolean.of(entry));
return true; return true;
} }
} }
@@ -13,6 +13,7 @@ import speiger.src.collections.booleans.collections.BooleanCollection;
import speiger.src.collections.booleans.collections.BooleanOrderedCollection; import speiger.src.collections.booleans.collections.BooleanOrderedCollection;
import speiger.src.collections.booleans.collections.BooleanIterator; import speiger.src.collections.booleans.collections.BooleanIterator;
import speiger.src.collections.booleans.functions.BooleanComparator; import speiger.src.collections.booleans.functions.BooleanComparator;
import speiger.src.collections.booleans.functions.OptionalBoolean;
import speiger.src.collections.objects.utils.ObjectArrays; import speiger.src.collections.objects.utils.ObjectArrays;
import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.booleans.functions.BooleanConsumer;
import speiger.src.collections.booleans.functions.function.BooleanPredicate; import speiger.src.collections.booleans.functions.function.BooleanPredicate;
@@ -105,7 +106,10 @@ public class BooleanCollections
return new SingletonCollection(element); return new SingletonCollection(element);
} }
protected static CollectionWrapper wrapper() { /**
* Internal Use mainly. Its a collection wrapper with 0 dependencies for those actions where a collector is needed.
*/
public static CollectionWrapper wrapper() {
return new CollectionWrapper(); return new CollectionWrapper();
} }
@@ -460,9 +464,9 @@ public class BooleanCollections
@Override @Override
public boolean reduce(boolean identity, BooleanBooleanUnaryOperator operator) { synchronized(mutex) { return c.reduce(identity, operator); } } public boolean reduce(boolean identity, BooleanBooleanUnaryOperator operator) { synchronized(mutex) { return c.reduce(identity, operator); } }
@Override @Override
public boolean reduce(BooleanBooleanUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } } public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { synchronized(mutex) { return c.reduce(operator); } }
@Override @Override
public boolean findFirst(BooleanPredicate filter) { synchronized(mutex) { return c.findFirst(filter); } } public OptionalBoolean findFirst(BooleanPredicate filter) { synchronized(mutex) { return c.findFirst(filter); } }
@Override @Override
public int count(BooleanPredicate filter) { synchronized(mutex) { return c.count(filter); } } public int count(BooleanPredicate filter) { synchronized(mutex) { return c.count(filter); } }
} }
@@ -588,9 +592,9 @@ public class BooleanCollections
@Override @Override
public boolean reduce(boolean identity, BooleanBooleanUnaryOperator operator) { return c.reduce(identity, operator); } public boolean reduce(boolean identity, BooleanBooleanUnaryOperator operator) { return c.reduce(identity, operator); }
@Override @Override
public boolean reduce(BooleanBooleanUnaryOperator operator) { return c.reduce(operator); } public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { return c.reduce(operator); }
@Override @Override
public boolean findFirst(BooleanPredicate filter) { return c.findFirst(filter); } public OptionalBoolean findFirst(BooleanPredicate filter) { return c.findFirst(filter); }
@Override @Override
public int count(BooleanPredicate filter) { return c.count(filter); } public int count(BooleanPredicate filter) { return c.count(filter); }
} }
@@ -1,13 +1,14 @@
package speiger.src.collections.booleans.utils; package speiger.src.collections.booleans.utils;
import speiger.src.collections.booleans.collections.BooleanIterator; import speiger.src.collections.booleans.collections.BooleanIterator;
import speiger.src.collections.booleans.collections.BooleanCollection; import speiger.src.collections.booleans.collections.BooleanCollection;
import speiger.src.collections.booleans.functions.BooleanComparator; import speiger.src.collections.booleans.functions.BooleanComparator;
import speiger.src.collections.booleans.functions.OptionalBoolean;
import speiger.src.collections.booleans.queues.BooleanPriorityDequeue; import speiger.src.collections.booleans.queues.BooleanPriorityDequeue;
import speiger.src.collections.booleans.queues.BooleanPriorityQueue; import speiger.src.collections.booleans.queues.BooleanPriorityQueue;
import speiger.src.collections.booleans.functions.BooleanConsumer; import speiger.src.collections.booleans.functions.BooleanConsumer;
import speiger.src.collections.booleans.functions.function.BooleanPredicate; import speiger.src.collections.booleans.functions.function.BooleanPredicate;
import speiger.src.collections.booleans.functions.function.BooleanBooleanUnaryOperator;
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
/** /**
@@ -113,7 +114,11 @@ public class BooleanPriorityQueues
@Override @Override
public boolean matchesAll(BooleanPredicate filter) { synchronized(mutex) { return queue.matchesAll(filter); } } public boolean matchesAll(BooleanPredicate filter) { synchronized(mutex) { return queue.matchesAll(filter); } }
@Override @Override
public boolean findFirst(BooleanPredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } } public OptionalBoolean findFirst(BooleanPredicate filter) { synchronized(mutex) { return queue.findFirst(filter); } }
@Override
public boolean reduce(boolean identity, BooleanBooleanUnaryOperator operator) { synchronized(mutex) { return queue.reduce(identity, operator); } }
@Override
public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) { synchronized(mutex) { return queue.reduce(operator); } }
@Override @Override
public int count(BooleanPredicate filter) { synchronized(mutex) { return queue.count(filter); } } public int count(BooleanPredicate filter) { synchronized(mutex) { return queue.count(filter); } }
} }
@@ -7,6 +7,7 @@ import java.util.function.Consumer;
import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.bytes.functions.ByteConsumer;
import speiger.src.collections.bytes.functions.ByteComparator; import speiger.src.collections.bytes.functions.ByteComparator;
import speiger.src.collections.objects.collections.ObjectIterable; import speiger.src.collections.objects.collections.ObjectIterable;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.ByteFunction; import speiger.src.collections.bytes.functions.function.ByteFunction;
import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer;
@@ -281,13 +282,13 @@ public interface ByteIterable extends Iterable<Byte>
* @param filter that should be applied * @param filter that should be applied
* @return the found value or the null equivalent variant. * @return the found value or the null equivalent variant.
*/ */
default byte findFirst(BytePredicate filter) { default OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(ByteIterator iter = iterator();iter.hasNext();) { for(ByteIterator iter = iterator();iter.hasNext();) {
byte entry = iter.nextByte(); byte entry = iter.nextByte();
if(filter.test(entry)) return entry; if(filter.test(entry)) return OptionalByte.of(entry);
} }
return (byte)0; return OptionalByte.empty();
} }
/** /**
@@ -312,7 +313,7 @@ public interface ByteIterable extends Iterable<Byte>
* @param operator the operation that should be applied * @param operator the operation that should be applied
* @return the reduction result, returns null value if nothing was found * @return the reduction result, returns null value if nothing was found
*/ */
default byte reduce(ByteByteUnaryOperator operator) { default OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -324,7 +325,7 @@ public interface ByteIterable extends Iterable<Byte>
} }
state = operator.applyAsByte(state, iter.nextByte()); state = operator.applyAsByte(state, iter.nextByte());
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
/** /**
@@ -0,0 +1,89 @@
package speiger.src.collections.bytes.functions;
import java.util.NoSuchElementException;
import java.util.function.Supplier;
public final class OptionalByte {
private static final OptionalByte EMPTY = new OptionalByte();
private final boolean isPresent;
private final byte value;
private OptionalByte() {
this.isPresent = false;
this.value = (byte)0;
}
private OptionalByte(byte value) {
this.isPresent = true;
this.value = value;
}
public static OptionalByte empty() {
return EMPTY;
}
public static OptionalByte of(byte value) {
return new OptionalByte(value);
}
public byte getAsByte() {
if(!isPresent) throw new NoSuchElementException("No value present");
return value;
}
public boolean isPresent() {
return isPresent;
}
public boolean isEmpty() {
return !isPresent;
}
public void ifPresent(ByteConsumer consumer) {
if(isPresent) consumer.accept(value);
}
public void ifPresentOrElse(ByteConsumer action, Runnable emptyAction) {
if (isPresent) action.accept(value);
else emptyAction.run();
}
public byte orElse(byte other) {
return isPresent ? value : other;
}
public byte orElseGet(ByteSupplier other) {
return isPresent ? value : other.getAsByte();
}
public byte orElseThrow() {
if (!isPresent) throw new NoSuchElementException("No value present");
return value;
}
public <X extends Throwable> byte orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
if(isPresent) return value;
else throw exceptionSupplier.get();
}
@Override
public boolean equals(Object obj) {
if(obj == this) return true;
if(obj instanceof OptionalByte) {
OptionalByte other = (OptionalByte)obj;
return (isPresent && other.isPresent ? value == other.value : isPresent == other.isPresent);
}
return false;
}
@Override
public int hashCode() {
return isPresent ? Byte.hashCode(value) : 0;
}
@Override
public String toString() {
return isPresent ? "OptionalByte["+value+"]" : "OptionalByte.empty";
}
}
@@ -12,6 +12,7 @@ import java.nio.ByteBuffer;
import speiger.src.collections.bytes.collections.ByteCollection; import speiger.src.collections.bytes.collections.ByteCollection;
import speiger.src.collections.bytes.collections.ByteStack; import speiger.src.collections.bytes.collections.ByteStack;
import speiger.src.collections.bytes.collections.ByteIterator; import speiger.src.collections.bytes.collections.ByteIterator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.ByteComparator; import speiger.src.collections.bytes.functions.ByteComparator;
import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.bytes.functions.ByteConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer;
@@ -519,12 +520,12 @@ public class ByteArrayList extends AbstractByteList implements IByteArray, ByteS
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0;i<size;i++) { for(int i = 0;i<size;i++) {
if(filter.test(data[i])) return data[i]; if(filter.test(data[i])) return OptionalByte.of(data[i]);
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -538,7 +539,7 @@ public class ByteArrayList extends AbstractByteList implements IByteArray, ByteS
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -550,7 +551,7 @@ public class ByteArrayList extends AbstractByteList implements IByteArray, ByteS
} }
state = operator.applyAsByte(state, data[i]); state = operator.applyAsByte(state, data[i]);
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
@@ -11,6 +11,7 @@ import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
import java.util.function.IntPredicate;import java.util.function.IntUnaryOperator; import java.util.function.IntPredicate;import java.util.function.IntUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
@@ -429,12 +430,12 @@ public class ByteLinkedList extends AbstractByteList implements BytePriorityDequ
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(Entry entry = first;entry != null;entry = entry.next) { for(Entry entry = first;entry != null;entry = entry.next) {
if(filter.test(entry.value)) return entry.value; if(filter.test(entry.value)) return OptionalByte.of(entry.value);
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -448,7 +449,7 @@ public class ByteLinkedList extends AbstractByteList implements BytePriorityDequ
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -460,7 +461,7 @@ public class ByteLinkedList extends AbstractByteList implements BytePriorityDequ
} }
state = operator.applyAsByte(state, entry.value); state = operator.applyAsByte(state, entry.value);
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
@@ -16,6 +16,7 @@ import java.nio.ByteBuffer;
import speiger.src.collections.bytes.collections.ByteCollection; import speiger.src.collections.bytes.collections.ByteCollection;
import speiger.src.collections.bytes.collections.ByteStack; import speiger.src.collections.bytes.collections.ByteStack;
import speiger.src.collections.bytes.collections.ByteIterator; import speiger.src.collections.bytes.collections.ByteIterator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.ByteComparator; import speiger.src.collections.bytes.functions.ByteComparator;
import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.bytes.functions.ByteConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer;
@@ -110,7 +111,6 @@ public class CopyOnWriteByteArrayList extends AbstractByteList implements ITrimm
System.arraycopy(a, offset, data, 0, length); System.arraycopy(a, offset, data, 0, length);
} }
private void setArray(byte[] data) { private void setArray(byte[] data) {
this.data = data; this.data = data;
} }
@@ -654,13 +654,13 @@ public class CopyOnWriteByteArrayList extends AbstractByteList implements ITrimm
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
byte[] data = this.data; byte[] data = this.data;
for(int i = 0,m=data.length;i<m;i++) { for(int i = 0,m=data.length;i<m;i++) {
if(filter.test(data[i])) return data[i]; if(filter.test(data[i])) return OptionalByte.of(data[i]);
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -675,7 +675,7 @@ public class CopyOnWriteByteArrayList extends AbstractByteList implements ITrimm
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte[] data = this.data; byte[] data = this.data;
byte state = (byte)0; byte state = (byte)0;
@@ -688,7 +688,7 @@ public class CopyOnWriteByteArrayList extends AbstractByteList implements ITrimm
} }
state = operator.applyAsByte(state, data[i]); state = operator.applyAsByte(state, data[i]);
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
@@ -1,24 +1,26 @@
package speiger.src.collections.bytes.lists; package speiger.src.collections.bytes.lists;
import java.util.Arrays; import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;
import java.util.Collection; import java.util.Collection;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
import java.util.function.IntPredicate;import java.util.function.IntUnaryOperator; import java.util.function.IntPredicate;
import java.util.function.IntUnaryOperator;
import speiger.src.collections.bytes.collections.ByteCollection; import speiger.src.collections.bytes.collections.ByteCollection;
import speiger.src.collections.bytes.functions.ByteComparator; import speiger.src.collections.bytes.functions.ByteComparator;
import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.bytes.functions.ByteConsumer;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.utils.ByteArrays; import speiger.src.collections.bytes.utils.ByteArrays;
import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.objects.utils.ObjectArrays; import speiger.src.collections.objects.utils.ObjectArrays;
import speiger.src.collections.bytes.utils.ByteIterators; import speiger.src.collections.bytes.utils.ByteIterators;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;
import speiger.src.collections.bytes.collections.ByteSplititerator; import speiger.src.collections.bytes.collections.ByteSplititerator;
import speiger.src.collections.bytes.utils.ByteSplititerators; import speiger.src.collections.bytes.utils.ByteSplititerators;
import speiger.src.collections.utils.SanityChecks; import speiger.src.collections.utils.SanityChecks;
@@ -273,12 +275,12 @@ public class ImmutableByteList extends AbstractByteList
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0,m=data.length;i<m;i++) { for(int i = 0,m=data.length;i<m;i++) {
if(filter.test(data[i])) return data[i]; if(filter.test(data[i])) return OptionalByte.of(data[i]);
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -292,7 +294,7 @@ public class ImmutableByteList extends AbstractByteList
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -304,7 +306,7 @@ public class ImmutableByteList extends AbstractByteList
} }
state = operator.applyAsByte(state, data[i]); state = operator.applyAsByte(state, data[i]);
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
@@ -4,6 +4,7 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.locks.StampedLock; import java.util.concurrent.locks.StampedLock;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteBooleanConsumer;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.functions.function.ByteBooleanUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteBooleanUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2BooleanMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2BooleanMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2BooleanMap; import speiger.src.collections.bytes.maps.interfaces.Byte2BooleanMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2BooleanConcurrentMap; import speiger.src.collections.bytes.maps.interfaces.Byte2BooleanConcurrentMap;
@@ -36,6 +38,7 @@ import speiger.src.collections.booleans.functions.BooleanConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
import speiger.src.collections.booleans.functions.function.BooleanPredicate; import speiger.src.collections.booleans.functions.function.BooleanPredicate;
import speiger.src.collections.booleans.functions.OptionalBoolean;
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSet; import speiger.src.collections.objects.sets.ObjectSet;
@@ -755,7 +758,7 @@ public class Byte2BooleanConcurrentOpenHashMap extends AbstractByte2BooleanMap i
} }
@Override @Override
public Byte2BooleanMap.Entry reduce(ObjectObjectUnaryOperator<Byte2BooleanMap.Entry, Byte2BooleanMap.Entry> operator) { public Optional<Byte2BooleanMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2BooleanMap.Entry, Byte2BooleanMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2BooleanMap.Entry state = null; Byte2BooleanMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -779,11 +782,11 @@ public class Byte2BooleanConcurrentOpenHashMap extends AbstractByte2BooleanMap i
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2BooleanMap.Entry findFirst(Predicate<Byte2BooleanMap.Entry> filter) { public Optional<Byte2BooleanMap.Entry> findFirst(Predicate<Byte2BooleanMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
@@ -793,7 +796,7 @@ public class Byte2BooleanConcurrentOpenHashMap extends AbstractByte2BooleanMap i
try { try {
while(index != -1) { while(index != -1) {
entry.set(index, i); entry.set(index, i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -801,7 +804,7 @@ public class Byte2BooleanConcurrentOpenHashMap extends AbstractByte2BooleanMap i
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1062,7 +1065,7 @@ public class Byte2BooleanConcurrentOpenHashMap extends AbstractByte2BooleanMap i
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1086,11 +1089,11 @@ public class Byte2BooleanConcurrentOpenHashMap extends AbstractByte2BooleanMap i
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment seg = segments[i]; Segment seg = segments[i];
@@ -1098,7 +1101,7 @@ public class Byte2BooleanConcurrentOpenHashMap extends AbstractByte2BooleanMap i
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.keys[index])) return seg.keys[index]; if(filter.test(seg.keys[index])) return OptionalByte.of(seg.keys[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1106,7 +1109,7 @@ public class Byte2BooleanConcurrentOpenHashMap extends AbstractByte2BooleanMap i
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1297,7 +1300,7 @@ public class Byte2BooleanConcurrentOpenHashMap extends AbstractByte2BooleanMap i
} }
@Override @Override
public boolean reduce(BooleanBooleanUnaryOperator operator) { public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
boolean state = false; boolean state = false;
boolean empty = true; boolean empty = true;
@@ -1321,20 +1324,20 @@ public class Byte2BooleanConcurrentOpenHashMap extends AbstractByte2BooleanMap i
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state);
} }
@Override @Override
public boolean findFirst(BooleanPredicate filter) { public OptionalBoolean findFirst(BooleanPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return false; if(size() <= 0) return OptionalBoolean.empty();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment seg = segments[i]; Segment seg = segments[i];
long stamp = seg.readLock(); long stamp = seg.readLock();
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.values[index])) return seg.values[index]; if(filter.test(seg.values[index])) return OptionalBoolean.of(seg.values[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1342,7 +1345,7 @@ public class Byte2BooleanConcurrentOpenHashMap extends AbstractByte2BooleanMap i
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return false; return OptionalBoolean.empty();
} }
@Override @Override
@@ -4,6 +4,7 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.locks.StampedLock; import java.util.concurrent.locks.StampedLock;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -18,6 +19,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
import speiger.src.collections.bytes.functions.consumer.ByteByteConsumer; import speiger.src.collections.bytes.functions.consumer.ByteByteConsumer;
import speiger.src.collections.bytes.functions.function.ByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ByteMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ByteMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2ByteMap; import speiger.src.collections.bytes.maps.interfaces.Byte2ByteMap;
@@ -761,7 +763,7 @@ public class Byte2ByteConcurrentOpenHashMap extends AbstractByte2ByteMap impleme
} }
@Override @Override
public Byte2ByteMap.Entry reduce(ObjectObjectUnaryOperator<Byte2ByteMap.Entry, Byte2ByteMap.Entry> operator) { public Optional<Byte2ByteMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2ByteMap.Entry, Byte2ByteMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2ByteMap.Entry state = null; Byte2ByteMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -785,11 +787,11 @@ public class Byte2ByteConcurrentOpenHashMap extends AbstractByte2ByteMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2ByteMap.Entry findFirst(Predicate<Byte2ByteMap.Entry> filter) { public Optional<Byte2ByteMap.Entry> findFirst(Predicate<Byte2ByteMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
@@ -799,7 +801,7 @@ public class Byte2ByteConcurrentOpenHashMap extends AbstractByte2ByteMap impleme
try { try {
while(index != -1) { while(index != -1) {
entry.set(index, i); entry.set(index, i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -807,7 +809,7 @@ public class Byte2ByteConcurrentOpenHashMap extends AbstractByte2ByteMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1068,7 +1070,7 @@ public class Byte2ByteConcurrentOpenHashMap extends AbstractByte2ByteMap impleme
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1092,11 +1094,11 @@ public class Byte2ByteConcurrentOpenHashMap extends AbstractByte2ByteMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment seg = segments[i]; Segment seg = segments[i];
@@ -1104,7 +1106,7 @@ public class Byte2ByteConcurrentOpenHashMap extends AbstractByte2ByteMap impleme
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.keys[index])) return seg.keys[index]; if(filter.test(seg.keys[index])) return OptionalByte.of(seg.keys[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1112,7 +1114,7 @@ public class Byte2ByteConcurrentOpenHashMap extends AbstractByte2ByteMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1303,7 +1305,7 @@ public class Byte2ByteConcurrentOpenHashMap extends AbstractByte2ByteMap impleme
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1327,20 +1329,20 @@ public class Byte2ByteConcurrentOpenHashMap extends AbstractByte2ByteMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment seg = segments[i]; Segment seg = segments[i];
long stamp = seg.readLock(); long stamp = seg.readLock();
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.values[index])) return seg.values[index]; if(filter.test(seg.values[index])) return OptionalByte.of(seg.values[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1348,7 +1350,7 @@ public class Byte2ByteConcurrentOpenHashMap extends AbstractByte2ByteMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -4,6 +4,7 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.locks.StampedLock; import java.util.concurrent.locks.StampedLock;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteCharConsumer;
import speiger.src.collections.bytes.functions.function.Byte2CharFunction; import speiger.src.collections.bytes.functions.function.Byte2CharFunction;
import speiger.src.collections.bytes.functions.function.ByteCharUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteCharUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2CharMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2CharMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2CharMap; import speiger.src.collections.bytes.maps.interfaces.Byte2CharMap;
@@ -37,6 +39,7 @@ import speiger.src.collections.chars.functions.CharConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer;
import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharPredicate;
import speiger.src.collections.chars.functions.OptionalChar;
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSet; import speiger.src.collections.objects.sets.ObjectSet;
@@ -768,7 +771,7 @@ public class Byte2CharConcurrentOpenHashMap extends AbstractByte2CharMap impleme
} }
@Override @Override
public Byte2CharMap.Entry reduce(ObjectObjectUnaryOperator<Byte2CharMap.Entry, Byte2CharMap.Entry> operator) { public Optional<Byte2CharMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2CharMap.Entry, Byte2CharMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2CharMap.Entry state = null; Byte2CharMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -792,11 +795,11 @@ public class Byte2CharConcurrentOpenHashMap extends AbstractByte2CharMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2CharMap.Entry findFirst(Predicate<Byte2CharMap.Entry> filter) { public Optional<Byte2CharMap.Entry> findFirst(Predicate<Byte2CharMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
@@ -806,7 +809,7 @@ public class Byte2CharConcurrentOpenHashMap extends AbstractByte2CharMap impleme
try { try {
while(index != -1) { while(index != -1) {
entry.set(index, i); entry.set(index, i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -814,7 +817,7 @@ public class Byte2CharConcurrentOpenHashMap extends AbstractByte2CharMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1075,7 +1078,7 @@ public class Byte2CharConcurrentOpenHashMap extends AbstractByte2CharMap impleme
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1099,11 +1102,11 @@ public class Byte2CharConcurrentOpenHashMap extends AbstractByte2CharMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment seg = segments[i]; Segment seg = segments[i];
@@ -1111,7 +1114,7 @@ public class Byte2CharConcurrentOpenHashMap extends AbstractByte2CharMap impleme
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.keys[index])) return seg.keys[index]; if(filter.test(seg.keys[index])) return OptionalByte.of(seg.keys[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1119,7 +1122,7 @@ public class Byte2CharConcurrentOpenHashMap extends AbstractByte2CharMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1310,7 +1313,7 @@ public class Byte2CharConcurrentOpenHashMap extends AbstractByte2CharMap impleme
} }
@Override @Override
public char reduce(CharCharUnaryOperator operator) { public OptionalChar reduce(CharCharUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
char state = (char)0; char state = (char)0;
boolean empty = true; boolean empty = true;
@@ -1334,20 +1337,20 @@ public class Byte2CharConcurrentOpenHashMap extends AbstractByte2CharMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalChar.empty() : OptionalChar.of(state);
} }
@Override @Override
public char findFirst(CharPredicate filter) { public OptionalChar findFirst(CharPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (char)0; if(size() <= 0) return OptionalChar.empty();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment seg = segments[i]; Segment seg = segments[i];
long stamp = seg.readLock(); long stamp = seg.readLock();
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.values[index])) return seg.values[index]; if(filter.test(seg.values[index])) return OptionalChar.of(seg.values[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1355,7 +1358,7 @@ public class Byte2CharConcurrentOpenHashMap extends AbstractByte2CharMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return (char)0; return OptionalChar.empty();
} }
@Override @Override
@@ -4,11 +4,13 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.locks.StampedLock; import java.util.concurrent.locks.StampedLock;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.DoublePredicate; import java.util.function.DoublePredicate;
import java.util.OptionalDouble;
import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator;
import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.bytes.functions.ByteConsumer;
@@ -20,6 +22,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteDoubleConsumer;
import speiger.src.collections.bytes.functions.function.Byte2DoubleFunction; import speiger.src.collections.bytes.functions.function.Byte2DoubleFunction;
import speiger.src.collections.bytes.functions.function.ByteDoubleUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteDoubleUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2DoubleMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2DoubleMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2DoubleMap; import speiger.src.collections.bytes.maps.interfaces.Byte2DoubleMap;
@@ -768,7 +771,7 @@ public class Byte2DoubleConcurrentOpenHashMap extends AbstractByte2DoubleMap imp
} }
@Override @Override
public Byte2DoubleMap.Entry reduce(ObjectObjectUnaryOperator<Byte2DoubleMap.Entry, Byte2DoubleMap.Entry> operator) { public Optional<Byte2DoubleMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2DoubleMap.Entry, Byte2DoubleMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2DoubleMap.Entry state = null; Byte2DoubleMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -792,11 +795,11 @@ public class Byte2DoubleConcurrentOpenHashMap extends AbstractByte2DoubleMap imp
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2DoubleMap.Entry findFirst(Predicate<Byte2DoubleMap.Entry> filter) { public Optional<Byte2DoubleMap.Entry> findFirst(Predicate<Byte2DoubleMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
@@ -806,7 +809,7 @@ public class Byte2DoubleConcurrentOpenHashMap extends AbstractByte2DoubleMap imp
try { try {
while(index != -1) { while(index != -1) {
entry.set(index, i); entry.set(index, i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -814,7 +817,7 @@ public class Byte2DoubleConcurrentOpenHashMap extends AbstractByte2DoubleMap imp
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1075,7 +1078,7 @@ public class Byte2DoubleConcurrentOpenHashMap extends AbstractByte2DoubleMap imp
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1099,11 +1102,11 @@ public class Byte2DoubleConcurrentOpenHashMap extends AbstractByte2DoubleMap imp
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment seg = segments[i]; Segment seg = segments[i];
@@ -1111,7 +1114,7 @@ public class Byte2DoubleConcurrentOpenHashMap extends AbstractByte2DoubleMap imp
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.keys[index])) return seg.keys[index]; if(filter.test(seg.keys[index])) return OptionalByte.of(seg.keys[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1119,7 +1122,7 @@ public class Byte2DoubleConcurrentOpenHashMap extends AbstractByte2DoubleMap imp
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1310,7 +1313,7 @@ public class Byte2DoubleConcurrentOpenHashMap extends AbstractByte2DoubleMap imp
} }
@Override @Override
public double reduce(DoubleDoubleUnaryOperator operator) { public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
double state = 0D; double state = 0D;
boolean empty = true; boolean empty = true;
@@ -1334,20 +1337,20 @@ public class Byte2DoubleConcurrentOpenHashMap extends AbstractByte2DoubleMap imp
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalDouble.empty() : OptionalDouble.of(state);
} }
@Override @Override
public double findFirst(DoublePredicate filter) { public OptionalDouble findFirst(DoublePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return 0D; if(size() <= 0) return OptionalDouble.empty();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment seg = segments[i]; Segment seg = segments[i];
long stamp = seg.readLock(); long stamp = seg.readLock();
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.values[index])) return seg.values[index]; if(filter.test(seg.values[index])) return OptionalDouble.of(seg.values[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1355,7 +1358,7 @@ public class Byte2DoubleConcurrentOpenHashMap extends AbstractByte2DoubleMap imp
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return 0D; return OptionalDouble.empty();
} }
@Override @Override
@@ -4,6 +4,7 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.locks.StampedLock; import java.util.concurrent.locks.StampedLock;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteFloatConsumer;
import speiger.src.collections.bytes.functions.function.Byte2FloatFunction; import speiger.src.collections.bytes.functions.function.Byte2FloatFunction;
import speiger.src.collections.bytes.functions.function.ByteFloatUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteFloatUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2FloatMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2FloatMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2FloatMap; import speiger.src.collections.bytes.maps.interfaces.Byte2FloatMap;
@@ -37,6 +39,7 @@ import speiger.src.collections.floats.functions.FloatConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer;
import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatPredicate;
import speiger.src.collections.floats.functions.OptionalFloat;
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSet; import speiger.src.collections.objects.sets.ObjectSet;
@@ -768,7 +771,7 @@ public class Byte2FloatConcurrentOpenHashMap extends AbstractByte2FloatMap imple
} }
@Override @Override
public Byte2FloatMap.Entry reduce(ObjectObjectUnaryOperator<Byte2FloatMap.Entry, Byte2FloatMap.Entry> operator) { public Optional<Byte2FloatMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2FloatMap.Entry, Byte2FloatMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2FloatMap.Entry state = null; Byte2FloatMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -792,11 +795,11 @@ public class Byte2FloatConcurrentOpenHashMap extends AbstractByte2FloatMap imple
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2FloatMap.Entry findFirst(Predicate<Byte2FloatMap.Entry> filter) { public Optional<Byte2FloatMap.Entry> findFirst(Predicate<Byte2FloatMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
@@ -806,7 +809,7 @@ public class Byte2FloatConcurrentOpenHashMap extends AbstractByte2FloatMap imple
try { try {
while(index != -1) { while(index != -1) {
entry.set(index, i); entry.set(index, i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -814,7 +817,7 @@ public class Byte2FloatConcurrentOpenHashMap extends AbstractByte2FloatMap imple
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1075,7 +1078,7 @@ public class Byte2FloatConcurrentOpenHashMap extends AbstractByte2FloatMap imple
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1099,11 +1102,11 @@ public class Byte2FloatConcurrentOpenHashMap extends AbstractByte2FloatMap imple
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment seg = segments[i]; Segment seg = segments[i];
@@ -1111,7 +1114,7 @@ public class Byte2FloatConcurrentOpenHashMap extends AbstractByte2FloatMap imple
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.keys[index])) return seg.keys[index]; if(filter.test(seg.keys[index])) return OptionalByte.of(seg.keys[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1119,7 +1122,7 @@ public class Byte2FloatConcurrentOpenHashMap extends AbstractByte2FloatMap imple
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1310,7 +1313,7 @@ public class Byte2FloatConcurrentOpenHashMap extends AbstractByte2FloatMap imple
} }
@Override @Override
public float reduce(FloatFloatUnaryOperator operator) { public OptionalFloat reduce(FloatFloatUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
float state = 0F; float state = 0F;
boolean empty = true; boolean empty = true;
@@ -1334,20 +1337,20 @@ public class Byte2FloatConcurrentOpenHashMap extends AbstractByte2FloatMap imple
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalFloat.empty() : OptionalFloat.of(state);
} }
@Override @Override
public float findFirst(FloatPredicate filter) { public OptionalFloat findFirst(FloatPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return 0F; if(size() <= 0) return OptionalFloat.empty();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment seg = segments[i]; Segment seg = segments[i];
long stamp = seg.readLock(); long stamp = seg.readLock();
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.values[index])) return seg.values[index]; if(filter.test(seg.values[index])) return OptionalFloat.of(seg.values[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1355,7 +1358,7 @@ public class Byte2FloatConcurrentOpenHashMap extends AbstractByte2FloatMap imple
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return 0F; return OptionalFloat.empty();
} }
@Override @Override
@@ -4,11 +4,13 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.locks.StampedLock; import java.util.concurrent.locks.StampedLock;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.IntPredicate; import java.util.function.IntPredicate;
import java.util.OptionalInt;
import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator;
import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.bytes.functions.ByteConsumer;
@@ -20,6 +22,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteIntConsumer;
import speiger.src.collections.bytes.functions.function.Byte2IntFunction; import speiger.src.collections.bytes.functions.function.Byte2IntFunction;
import speiger.src.collections.bytes.functions.function.ByteIntUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteIntUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2IntMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2IntMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2IntMap; import speiger.src.collections.bytes.maps.interfaces.Byte2IntMap;
@@ -768,7 +771,7 @@ public class Byte2IntConcurrentOpenHashMap extends AbstractByte2IntMap implement
} }
@Override @Override
public Byte2IntMap.Entry reduce(ObjectObjectUnaryOperator<Byte2IntMap.Entry, Byte2IntMap.Entry> operator) { public Optional<Byte2IntMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2IntMap.Entry, Byte2IntMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2IntMap.Entry state = null; Byte2IntMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -792,11 +795,11 @@ public class Byte2IntConcurrentOpenHashMap extends AbstractByte2IntMap implement
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2IntMap.Entry findFirst(Predicate<Byte2IntMap.Entry> filter) { public Optional<Byte2IntMap.Entry> findFirst(Predicate<Byte2IntMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
@@ -806,7 +809,7 @@ public class Byte2IntConcurrentOpenHashMap extends AbstractByte2IntMap implement
try { try {
while(index != -1) { while(index != -1) {
entry.set(index, i); entry.set(index, i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -814,7 +817,7 @@ public class Byte2IntConcurrentOpenHashMap extends AbstractByte2IntMap implement
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1075,7 +1078,7 @@ public class Byte2IntConcurrentOpenHashMap extends AbstractByte2IntMap implement
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1099,11 +1102,11 @@ public class Byte2IntConcurrentOpenHashMap extends AbstractByte2IntMap implement
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment seg = segments[i]; Segment seg = segments[i];
@@ -1111,7 +1114,7 @@ public class Byte2IntConcurrentOpenHashMap extends AbstractByte2IntMap implement
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.keys[index])) return seg.keys[index]; if(filter.test(seg.keys[index])) return OptionalByte.of(seg.keys[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1119,7 +1122,7 @@ public class Byte2IntConcurrentOpenHashMap extends AbstractByte2IntMap implement
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1310,7 +1313,7 @@ public class Byte2IntConcurrentOpenHashMap extends AbstractByte2IntMap implement
} }
@Override @Override
public int reduce(IntIntUnaryOperator operator) { public OptionalInt reduce(IntIntUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
int state = 0; int state = 0;
boolean empty = true; boolean empty = true;
@@ -1334,20 +1337,20 @@ public class Byte2IntConcurrentOpenHashMap extends AbstractByte2IntMap implement
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalInt.empty() : OptionalInt.of(state);
} }
@Override @Override
public int findFirst(IntPredicate filter) { public OptionalInt findFirst(IntPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return 0; if(size() <= 0) return OptionalInt.empty();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment seg = segments[i]; Segment seg = segments[i];
long stamp = seg.readLock(); long stamp = seg.readLock();
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.values[index])) return seg.values[index]; if(filter.test(seg.values[index])) return OptionalInt.of(seg.values[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1355,7 +1358,7 @@ public class Byte2IntConcurrentOpenHashMap extends AbstractByte2IntMap implement
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return 0; return OptionalInt.empty();
} }
@Override @Override
@@ -4,11 +4,13 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.locks.StampedLock; import java.util.concurrent.locks.StampedLock;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.LongPredicate; import java.util.function.LongPredicate;
import java.util.OptionalLong;
import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator;
import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.bytes.functions.ByteConsumer;
@@ -20,6 +22,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteLongConsumer;
import speiger.src.collections.bytes.functions.function.Byte2LongFunction; import speiger.src.collections.bytes.functions.function.Byte2LongFunction;
import speiger.src.collections.bytes.functions.function.ByteLongUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteLongUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2LongMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2LongMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2LongMap; import speiger.src.collections.bytes.maps.interfaces.Byte2LongMap;
@@ -768,7 +771,7 @@ public class Byte2LongConcurrentOpenHashMap extends AbstractByte2LongMap impleme
} }
@Override @Override
public Byte2LongMap.Entry reduce(ObjectObjectUnaryOperator<Byte2LongMap.Entry, Byte2LongMap.Entry> operator) { public Optional<Byte2LongMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2LongMap.Entry, Byte2LongMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2LongMap.Entry state = null; Byte2LongMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -792,11 +795,11 @@ public class Byte2LongConcurrentOpenHashMap extends AbstractByte2LongMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2LongMap.Entry findFirst(Predicate<Byte2LongMap.Entry> filter) { public Optional<Byte2LongMap.Entry> findFirst(Predicate<Byte2LongMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
@@ -806,7 +809,7 @@ public class Byte2LongConcurrentOpenHashMap extends AbstractByte2LongMap impleme
try { try {
while(index != -1) { while(index != -1) {
entry.set(index, i); entry.set(index, i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -814,7 +817,7 @@ public class Byte2LongConcurrentOpenHashMap extends AbstractByte2LongMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1075,7 +1078,7 @@ public class Byte2LongConcurrentOpenHashMap extends AbstractByte2LongMap impleme
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1099,11 +1102,11 @@ public class Byte2LongConcurrentOpenHashMap extends AbstractByte2LongMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment seg = segments[i]; Segment seg = segments[i];
@@ -1111,7 +1114,7 @@ public class Byte2LongConcurrentOpenHashMap extends AbstractByte2LongMap impleme
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.keys[index])) return seg.keys[index]; if(filter.test(seg.keys[index])) return OptionalByte.of(seg.keys[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1119,7 +1122,7 @@ public class Byte2LongConcurrentOpenHashMap extends AbstractByte2LongMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1310,7 +1313,7 @@ public class Byte2LongConcurrentOpenHashMap extends AbstractByte2LongMap impleme
} }
@Override @Override
public long reduce(LongLongUnaryOperator operator) { public OptionalLong reduce(LongLongUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
long state = 0L; long state = 0L;
boolean empty = true; boolean empty = true;
@@ -1334,20 +1337,20 @@ public class Byte2LongConcurrentOpenHashMap extends AbstractByte2LongMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalLong.empty() : OptionalLong.of(state);
} }
@Override @Override
public long findFirst(LongPredicate filter) { public OptionalLong findFirst(LongPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return 0L; if(size() <= 0) return OptionalLong.empty();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment seg = segments[i]; Segment seg = segments[i];
long stamp = seg.readLock(); long stamp = seg.readLock();
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.values[index])) return seg.values[index]; if(filter.test(seg.values[index])) return OptionalLong.of(seg.values[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1355,7 +1358,7 @@ public class Byte2LongConcurrentOpenHashMap extends AbstractByte2LongMap impleme
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return 0L; return OptionalLong.empty();
} }
@Override @Override
@@ -4,6 +4,7 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.locks.StampedLock; import java.util.concurrent.locks.StampedLock;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -18,6 +19,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteObjectConsumer;
import speiger.src.collections.bytes.functions.function.ByteFunction; import speiger.src.collections.bytes.functions.function.ByteFunction;
import speiger.src.collections.bytes.functions.function.ByteObjectUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteObjectUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ObjectMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ObjectMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2ObjectMap; import speiger.src.collections.bytes.maps.interfaces.Byte2ObjectMap;
@@ -715,7 +717,7 @@ public class Byte2ObjectConcurrentOpenHashMap<V> extends AbstractByte2ObjectMap<
} }
@Override @Override
public Byte2ObjectMap.Entry<V> reduce(ObjectObjectUnaryOperator<Byte2ObjectMap.Entry<V>, Byte2ObjectMap.Entry<V>> operator) { public Optional<Byte2ObjectMap.Entry<V>> reduce(ObjectObjectUnaryOperator<Byte2ObjectMap.Entry<V>, Byte2ObjectMap.Entry<V>> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2ObjectMap.Entry<V> state = null; Byte2ObjectMap.Entry<V> state = null;
boolean empty = true; boolean empty = true;
@@ -739,11 +741,11 @@ public class Byte2ObjectConcurrentOpenHashMap<V> extends AbstractByte2ObjectMap<
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2ObjectMap.Entry<V> findFirst(Predicate<Byte2ObjectMap.Entry<V>> filter) { public Optional<Byte2ObjectMap.Entry<V>> findFirst(Predicate<Byte2ObjectMap.Entry<V>> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
@@ -753,7 +755,7 @@ public class Byte2ObjectConcurrentOpenHashMap<V> extends AbstractByte2ObjectMap<
try { try {
while(index != -1) { while(index != -1) {
entry.set(index, i); entry.set(index, i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -761,7 +763,7 @@ public class Byte2ObjectConcurrentOpenHashMap<V> extends AbstractByte2ObjectMap<
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1022,7 +1024,7 @@ public class Byte2ObjectConcurrentOpenHashMap<V> extends AbstractByte2ObjectMap<
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1046,11 +1048,11 @@ public class Byte2ObjectConcurrentOpenHashMap<V> extends AbstractByte2ObjectMap<
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment<V> seg = segments[i]; Segment<V> seg = segments[i];
@@ -1058,7 +1060,7 @@ public class Byte2ObjectConcurrentOpenHashMap<V> extends AbstractByte2ObjectMap<
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.keys[index])) return seg.keys[index]; if(filter.test(seg.keys[index])) return OptionalByte.of(seg.keys[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1066,7 +1068,7 @@ public class Byte2ObjectConcurrentOpenHashMap<V> extends AbstractByte2ObjectMap<
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1258,7 +1260,7 @@ public class Byte2ObjectConcurrentOpenHashMap<V> extends AbstractByte2ObjectMap<
} }
@Override @Override
public V reduce(ObjectObjectUnaryOperator<V, V> operator) { public Optional<V> reduce(ObjectObjectUnaryOperator<V, V> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
V state = null; V state = null;
boolean empty = true; boolean empty = true;
@@ -1282,20 +1284,20 @@ public class Byte2ObjectConcurrentOpenHashMap<V> extends AbstractByte2ObjectMap<
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public V findFirst(Predicate<V> filter) { public Optional<V> findFirst(Predicate<V> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment<V> seg = segments[i]; Segment<V> seg = segments[i];
long stamp = seg.readLock(); long stamp = seg.readLock();
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.values[index])) return seg.values[index]; if(filter.test(seg.values[index])) return Optional.ofNullable(seg.values[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1303,7 +1305,7 @@ public class Byte2ObjectConcurrentOpenHashMap<V> extends AbstractByte2ObjectMap<
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -4,6 +4,7 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.locks.StampedLock; import java.util.concurrent.locks.StampedLock;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteShortConsumer;
import speiger.src.collections.bytes.functions.function.Byte2ShortFunction; import speiger.src.collections.bytes.functions.function.Byte2ShortFunction;
import speiger.src.collections.bytes.functions.function.ByteShortUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteShortUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ShortMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ShortMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2ShortMap; import speiger.src.collections.bytes.maps.interfaces.Byte2ShortMap;
@@ -37,6 +39,7 @@ import speiger.src.collections.shorts.functions.ShortConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer;
import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortPredicate;
import speiger.src.collections.shorts.functions.OptionalShort;
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSet; import speiger.src.collections.objects.sets.ObjectSet;
@@ -768,7 +771,7 @@ public class Byte2ShortConcurrentOpenHashMap extends AbstractByte2ShortMap imple
} }
@Override @Override
public Byte2ShortMap.Entry reduce(ObjectObjectUnaryOperator<Byte2ShortMap.Entry, Byte2ShortMap.Entry> operator) { public Optional<Byte2ShortMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2ShortMap.Entry, Byte2ShortMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2ShortMap.Entry state = null; Byte2ShortMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -792,11 +795,11 @@ public class Byte2ShortConcurrentOpenHashMap extends AbstractByte2ShortMap imple
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2ShortMap.Entry findFirst(Predicate<Byte2ShortMap.Entry> filter) { public Optional<Byte2ShortMap.Entry> findFirst(Predicate<Byte2ShortMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
@@ -806,7 +809,7 @@ public class Byte2ShortConcurrentOpenHashMap extends AbstractByte2ShortMap imple
try { try {
while(index != -1) { while(index != -1) {
entry.set(index, i); entry.set(index, i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -814,7 +817,7 @@ public class Byte2ShortConcurrentOpenHashMap extends AbstractByte2ShortMap imple
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1075,7 +1078,7 @@ public class Byte2ShortConcurrentOpenHashMap extends AbstractByte2ShortMap imple
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1099,11 +1102,11 @@ public class Byte2ShortConcurrentOpenHashMap extends AbstractByte2ShortMap imple
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment seg = segments[i]; Segment seg = segments[i];
@@ -1111,7 +1114,7 @@ public class Byte2ShortConcurrentOpenHashMap extends AbstractByte2ShortMap imple
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.keys[index])) return seg.keys[index]; if(filter.test(seg.keys[index])) return OptionalByte.of(seg.keys[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1119,7 +1122,7 @@ public class Byte2ShortConcurrentOpenHashMap extends AbstractByte2ShortMap imple
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1310,7 +1313,7 @@ public class Byte2ShortConcurrentOpenHashMap extends AbstractByte2ShortMap imple
} }
@Override @Override
public short reduce(ShortShortUnaryOperator operator) { public OptionalShort reduce(ShortShortUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
short state = (short)0; short state = (short)0;
boolean empty = true; boolean empty = true;
@@ -1334,20 +1337,20 @@ public class Byte2ShortConcurrentOpenHashMap extends AbstractByte2ShortMap imple
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return state; return empty ? OptionalShort.empty() : OptionalShort.of(state);
} }
@Override @Override
public short findFirst(ShortPredicate filter) { public OptionalShort findFirst(ShortPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (short)0; if(size() <= 0) return OptionalShort.empty();
for(int i = 0,m=segments.length;i<m;i++) { for(int i = 0,m=segments.length;i<m;i++) {
Segment seg = segments[i]; Segment seg = segments[i];
long stamp = seg.readLock(); long stamp = seg.readLock();
try { try {
int index = seg.firstIndex; int index = seg.firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(seg.values[index])) return seg.values[index]; if(filter.test(seg.values[index])) return OptionalShort.of(seg.values[index]);
index = (int)seg.links[index]; index = (int)seg.links[index];
} }
} }
@@ -1355,7 +1358,7 @@ public class Byte2ShortConcurrentOpenHashMap extends AbstractByte2ShortMap imple
seg.unlockRead(stamp); seg.unlockRead(stamp);
} }
} }
return (short)0; return OptionalShort.empty();
} }
@Override @Override
@@ -4,6 +4,7 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer; import speiger.src.collections.ints.functions.consumer.IntBooleanConsumer;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.functions.consumer.ByteBooleanConsumer; import speiger.src.collections.bytes.functions.consumer.ByteBooleanConsumer;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
@@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
import speiger.src.collections.booleans.functions.function.BooleanPredicate; import speiger.src.collections.booleans.functions.function.BooleanPredicate;
import speiger.src.collections.booleans.functions.OptionalBoolean;
import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator;
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
import speiger.src.collections.objects.lists.ObjectListIterator; import speiger.src.collections.objects.lists.ObjectListIterator;
@@ -196,13 +199,13 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
if(containsNull) { if(containsNull) {
boolean lastValue = values[nullIndex]; boolean lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -210,7 +213,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
boolean lastValue = values[pos]; boolean lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -218,7 +221,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -230,13 +233,13 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
if(containsNull) { if(containsNull) {
boolean lastValue = values[nullIndex]; boolean lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -244,7 +247,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
boolean lastValue = values[pos]; boolean lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToLastIndex(pos); moveToLastIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -252,7 +255,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -265,7 +268,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -276,7 +279,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -289,7 +292,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -300,7 +303,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -311,7 +314,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
if(isEmpty() || strategy.equals(firstByteKey(), key)) return false; if(isEmpty() || strategy.equals(firstByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -319,7 +322,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -333,7 +336,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
if(isEmpty() || strategy.equals(lastByteKey(), key)) return false; if(isEmpty() || strategy.equals(lastByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -341,7 +344,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToLastIndex(pos); moveToLastIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -354,7 +357,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
public boolean getAndMoveToFirst(byte key) { public boolean getAndMoveToFirst(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToFirstIndex(index); moveToFirstIndex(index, false);
return values[index]; return values[index];
} }
@@ -362,7 +365,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
public boolean getAndMoveToLast(byte key) { public boolean getAndMoveToLast(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToLastIndex(index); moveToLastIndex(index, false);
return values[index]; return values[index];
} }
@@ -538,8 +541,8 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
containsNull = false; containsNull = false;
} }
protected void moveToFirstIndex(int startPos) { protected void moveToFirstIndex(int startPos, boolean adding) {
if(size == 1 || firstIndex == startPos) return; if(size == (adding ? 0 : 1) || firstIndex == startPos) return;
if(lastIndex == startPos) { if(lastIndex == startPos) {
lastIndex = (int)(links[startPos] >>> 32); lastIndex = (int)(links[startPos] >>> 32);
links[lastIndex] |= 0xFFFFFFFFL; links[lastIndex] |= 0xFFFFFFFFL;
@@ -556,8 +559,8 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
firstIndex = startPos; firstIndex = startPos;
} }
protected void moveToLastIndex(int startPos) { protected void moveToLastIndex(int startPos, boolean adding) {
if(size == 1 || lastIndex == startPos) return; if(size == (adding ? 0 : 1) || lastIndex == startPos) return;
if(firstIndex == startPos) { if(firstIndex == startPos) {
firstIndex = (int)links[startPos]; firstIndex = (int)links[startPos];
links[lastIndex] |= 0xFFFFFFFF00000000L; links[lastIndex] |= 0xFFFFFFFF00000000L;
@@ -671,6 +674,10 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
} }
private class MapEntrySet extends AbstractObjectSet<Byte2BooleanMap.Entry> implements Byte2BooleanOrderedMap.FastOrderedSet { private class MapEntrySet extends AbstractObjectSet<Byte2BooleanMap.Entry> implements Byte2BooleanOrderedMap.FastOrderedSet {
@Override
public void addFirst(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(Byte2BooleanMap.Entry o) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -836,7 +843,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
} }
@Override @Override
public Byte2BooleanMap.Entry reduce(ObjectObjectUnaryOperator<Byte2BooleanMap.Entry, Byte2BooleanMap.Entry> operator) { public Optional<Byte2BooleanMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2BooleanMap.Entry, Byte2BooleanMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2BooleanMap.Entry state = null; Byte2BooleanMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -851,21 +858,21 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
state = operator.apply(state, new ValueMapEntry(index)); state = operator.apply(state, new ValueMapEntry(index));
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2BooleanMap.Entry findFirst(Predicate<Byte2BooleanMap.Entry> filter) { public Optional<Byte2BooleanMap.Entry> findFirst(Predicate<Byte2BooleanMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
int index = firstIndex; int index = firstIndex;
while(index != -1) { while(index != -1) {
entry.set(index); entry.set(index);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)links[index]; index = (int)links[index];
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -945,9 +952,12 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void addFirst(byte o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); }
@@ -1090,7 +1100,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1105,19 +1115,19 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
state = operator.applyAsByte(state, keys[index]); state = operator.applyAsByte(state, keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(keys[index])) return keys[index]; if(filter.test(keys[index])) return OptionalByte.of(keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1252,7 +1262,7 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
} }
@Override @Override
public boolean reduce(BooleanBooleanUnaryOperator operator) { public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
boolean state = false; boolean state = false;
boolean empty = true; boolean empty = true;
@@ -1267,19 +1277,19 @@ public class Byte2BooleanLinkedOpenCustomHashMap extends Byte2BooleanOpenCustomH
state = operator.applyAsBoolean(state, values[index]); state = operator.applyAsBoolean(state, values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state);
} }
@Override @Override
public boolean findFirst(BooleanPredicate filter) { public OptionalBoolean findFirst(BooleanPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return false; if(size() <= 0) return OptionalBoolean.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(values[index])) return values[index]; if(filter.test(values[index])) return OptionalBoolean.of(values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return false; return OptionalBoolean.empty();
} }
@Override @Override
@@ -5,6 +5,7 @@ import java.util.ConcurrentModificationException;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteBooleanConsumer;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.functions.function.ByteBooleanUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteBooleanUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2BooleanMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2BooleanMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2BooleanMap; import speiger.src.collections.bytes.maps.interfaces.Byte2BooleanMap;
import speiger.src.collections.bytes.sets.AbstractByteSet; import speiger.src.collections.bytes.sets.AbstractByteSet;
@@ -36,6 +38,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer; import speiger.src.collections.objects.functions.consumer.ObjectBooleanConsumer;
import speiger.src.collections.booleans.functions.function.BooleanPredicate; import speiger.src.collections.booleans.functions.function.BooleanPredicate;
import speiger.src.collections.booleans.functions.OptionalBoolean;
import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.collections.ObjectIterator;
import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSet; import speiger.src.collections.objects.sets.ObjectSet;
@@ -930,7 +933,7 @@ public class Byte2BooleanOpenCustomHashMap extends AbstractByte2BooleanMap imple
} }
@Override @Override
public Byte2BooleanMap.Entry reduce(ObjectObjectUnaryOperator<Byte2BooleanMap.Entry, Byte2BooleanMap.Entry> operator) { public Optional<Byte2BooleanMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2BooleanMap.Entry, Byte2BooleanMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2BooleanMap.Entry state = null; Byte2BooleanMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -947,25 +950,25 @@ public class Byte2BooleanOpenCustomHashMap extends AbstractByte2BooleanMap imple
} }
state = operator.apply(state, new ValueMapEntry(i)); state = operator.apply(state, new ValueMapEntry(i));
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2BooleanMap.Entry findFirst(Predicate<Byte2BooleanMap.Entry> filter) { public Optional<Byte2BooleanMap.Entry> findFirst(Predicate<Byte2BooleanMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
if(containsNull) { if(containsNull) {
entry.set(nullIndex); entry.set(nullIndex);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0)) { if(!strategy.equals(keys[i], (byte)0)) {
entry.set(i); entry.set(i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1139,7 +1142,7 @@ public class Byte2BooleanOpenCustomHashMap extends AbstractByte2BooleanMap imple
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1156,18 +1159,18 @@ public class Byte2BooleanOpenCustomHashMap extends AbstractByte2BooleanMap imple
} }
state = operator.applyAsByte(state, keys[i]); state = operator.applyAsByte(state, keys[i]);
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]);
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1282,7 +1285,7 @@ public class Byte2BooleanOpenCustomHashMap extends AbstractByte2BooleanMap imple
} }
@Override @Override
public boolean reduce(BooleanBooleanUnaryOperator operator) { public OptionalBoolean reduce(BooleanBooleanUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
boolean state = false; boolean state = false;
boolean empty = true; boolean empty = true;
@@ -1299,18 +1302,18 @@ public class Byte2BooleanOpenCustomHashMap extends AbstractByte2BooleanMap imple
} }
state = operator.applyAsBoolean(state, values[i]); state = operator.applyAsBoolean(state, values[i]);
} }
return state; return empty ? OptionalBoolean.empty() : OptionalBoolean.of(state);
} }
@Override @Override
public boolean findFirst(BooleanPredicate filter) { public OptionalBoolean findFirst(BooleanPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return false; if(size() <= 0) return OptionalBoolean.empty();
if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; if(containsNull && filter.test(values[nullIndex])) return OptionalBoolean.of(values[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return OptionalBoolean.of(values[i]);
} }
return false; return OptionalBoolean.empty();
} }
@Override @Override
@@ -4,6 +4,7 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -13,6 +14,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.functions.consumer.ByteByteConsumer; import speiger.src.collections.bytes.functions.consumer.ByteByteConsumer;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
@@ -189,13 +191,13 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
if(containsNull) { if(containsNull) {
byte lastValue = values[nullIndex]; byte lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -203,7 +205,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
byte lastValue = values[pos]; byte lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -211,7 +213,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -223,13 +225,13 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
if(containsNull) { if(containsNull) {
byte lastValue = values[nullIndex]; byte lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -237,7 +239,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
byte lastValue = values[pos]; byte lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToLastIndex(pos); moveToLastIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -245,7 +247,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -258,7 +260,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -269,7 +271,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -282,7 +284,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -293,7 +295,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -304,7 +306,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
if(isEmpty() || strategy.equals(firstByteKey(), key)) return false; if(isEmpty() || strategy.equals(firstByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -312,7 +314,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -326,7 +328,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
if(isEmpty() || strategy.equals(lastByteKey(), key)) return false; if(isEmpty() || strategy.equals(lastByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -334,7 +336,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToLastIndex(pos); moveToLastIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -347,7 +349,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
public byte getAndMoveToFirst(byte key) { public byte getAndMoveToFirst(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToFirstIndex(index); moveToFirstIndex(index, false);
return values[index]; return values[index];
} }
@@ -355,7 +357,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
public byte getAndMoveToLast(byte key) { public byte getAndMoveToLast(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToLastIndex(index); moveToLastIndex(index, false);
return values[index]; return values[index];
} }
@@ -531,8 +533,8 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
containsNull = false; containsNull = false;
} }
protected void moveToFirstIndex(int startPos) { protected void moveToFirstIndex(int startPos, boolean adding) {
if(size == 1 || firstIndex == startPos) return; if(size == (adding ? 0 : 1) || firstIndex == startPos) return;
if(lastIndex == startPos) { if(lastIndex == startPos) {
lastIndex = (int)(links[startPos] >>> 32); lastIndex = (int)(links[startPos] >>> 32);
links[lastIndex] |= 0xFFFFFFFFL; links[lastIndex] |= 0xFFFFFFFFL;
@@ -549,8 +551,8 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
firstIndex = startPos; firstIndex = startPos;
} }
protected void moveToLastIndex(int startPos) { protected void moveToLastIndex(int startPos, boolean adding) {
if(size == 1 || lastIndex == startPos) return; if(size == (adding ? 0 : 1) || lastIndex == startPos) return;
if(firstIndex == startPos) { if(firstIndex == startPos) {
firstIndex = (int)links[startPos]; firstIndex = (int)links[startPos];
links[lastIndex] |= 0xFFFFFFFF00000000L; links[lastIndex] |= 0xFFFFFFFF00000000L;
@@ -664,6 +666,10 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
} }
private class MapEntrySet extends AbstractObjectSet<Byte2ByteMap.Entry> implements Byte2ByteOrderedMap.FastOrderedSet { private class MapEntrySet extends AbstractObjectSet<Byte2ByteMap.Entry> implements Byte2ByteOrderedMap.FastOrderedSet {
@Override
public void addFirst(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(Byte2ByteMap.Entry o) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -829,7 +835,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
} }
@Override @Override
public Byte2ByteMap.Entry reduce(ObjectObjectUnaryOperator<Byte2ByteMap.Entry, Byte2ByteMap.Entry> operator) { public Optional<Byte2ByteMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2ByteMap.Entry, Byte2ByteMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2ByteMap.Entry state = null; Byte2ByteMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -844,21 +850,21 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
state = operator.apply(state, new ValueMapEntry(index)); state = operator.apply(state, new ValueMapEntry(index));
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2ByteMap.Entry findFirst(Predicate<Byte2ByteMap.Entry> filter) { public Optional<Byte2ByteMap.Entry> findFirst(Predicate<Byte2ByteMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
int index = firstIndex; int index = firstIndex;
while(index != -1) { while(index != -1) {
entry.set(index); entry.set(index);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)links[index]; index = (int)links[index];
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -938,9 +944,12 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void addFirst(byte o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); }
@@ -1083,7 +1092,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1098,19 +1107,19 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
state = operator.applyAsByte(state, keys[index]); state = operator.applyAsByte(state, keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(keys[index])) return keys[index]; if(filter.test(keys[index])) return OptionalByte.of(keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1245,7 +1254,7 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1260,19 +1269,19 @@ public class Byte2ByteLinkedOpenCustomHashMap extends Byte2ByteOpenCustomHashMap
state = operator.applyAsByte(state, values[index]); state = operator.applyAsByte(state, values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(values[index])) return values[index]; if(filter.test(values[index])) return OptionalByte.of(values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -5,6 +5,7 @@ import java.util.ConcurrentModificationException;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -17,6 +18,7 @@ import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
import speiger.src.collections.bytes.functions.consumer.ByteByteConsumer; import speiger.src.collections.bytes.functions.consumer.ByteByteConsumer;
import speiger.src.collections.bytes.functions.function.ByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ByteMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ByteMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2ByteMap; import speiger.src.collections.bytes.maps.interfaces.Byte2ByteMap;
@@ -944,7 +946,7 @@ public class Byte2ByteOpenCustomHashMap extends AbstractByte2ByteMap implements
} }
@Override @Override
public Byte2ByteMap.Entry reduce(ObjectObjectUnaryOperator<Byte2ByteMap.Entry, Byte2ByteMap.Entry> operator) { public Optional<Byte2ByteMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2ByteMap.Entry, Byte2ByteMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2ByteMap.Entry state = null; Byte2ByteMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -961,25 +963,25 @@ public class Byte2ByteOpenCustomHashMap extends AbstractByte2ByteMap implements
} }
state = operator.apply(state, new ValueMapEntry(i)); state = operator.apply(state, new ValueMapEntry(i));
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2ByteMap.Entry findFirst(Predicate<Byte2ByteMap.Entry> filter) { public Optional<Byte2ByteMap.Entry> findFirst(Predicate<Byte2ByteMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
if(containsNull) { if(containsNull) {
entry.set(nullIndex); entry.set(nullIndex);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0)) { if(!strategy.equals(keys[i], (byte)0)) {
entry.set(i); entry.set(i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1153,7 +1155,7 @@ public class Byte2ByteOpenCustomHashMap extends AbstractByte2ByteMap implements
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1170,18 +1172,18 @@ public class Byte2ByteOpenCustomHashMap extends AbstractByte2ByteMap implements
} }
state = operator.applyAsByte(state, keys[i]); state = operator.applyAsByte(state, keys[i]);
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]);
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1296,7 +1298,7 @@ public class Byte2ByteOpenCustomHashMap extends AbstractByte2ByteMap implements
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1313,18 +1315,18 @@ public class Byte2ByteOpenCustomHashMap extends AbstractByte2ByteMap implements
} }
state = operator.applyAsByte(state, values[i]); state = operator.applyAsByte(state, values[i]);
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; if(containsNull && filter.test(values[nullIndex])) return OptionalByte.of(values[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return OptionalByte.of(values[i]);
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -4,6 +4,7 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
import speiger.src.collections.ints.functions.consumer.IntCharConsumer; import speiger.src.collections.ints.functions.consumer.IntCharConsumer;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.functions.consumer.ByteCharConsumer; import speiger.src.collections.bytes.functions.consumer.ByteCharConsumer;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
@@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer;
import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharPredicate;
import speiger.src.collections.chars.functions.OptionalChar;
import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator;
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
import speiger.src.collections.objects.lists.ObjectListIterator; import speiger.src.collections.objects.lists.ObjectListIterator;
@@ -196,13 +199,13 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
if(containsNull) { if(containsNull) {
char lastValue = values[nullIndex]; char lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -210,7 +213,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
char lastValue = values[pos]; char lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -218,7 +221,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -230,13 +233,13 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
if(containsNull) { if(containsNull) {
char lastValue = values[nullIndex]; char lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -244,7 +247,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
char lastValue = values[pos]; char lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToLastIndex(pos); moveToLastIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -252,7 +255,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -265,7 +268,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -276,7 +279,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -289,7 +292,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -300,7 +303,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -311,7 +314,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
if(isEmpty() || strategy.equals(firstByteKey(), key)) return false; if(isEmpty() || strategy.equals(firstByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -319,7 +322,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -333,7 +336,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
if(isEmpty() || strategy.equals(lastByteKey(), key)) return false; if(isEmpty() || strategy.equals(lastByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -341,7 +344,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToLastIndex(pos); moveToLastIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -354,7 +357,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
public char getAndMoveToFirst(byte key) { public char getAndMoveToFirst(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToFirstIndex(index); moveToFirstIndex(index, false);
return values[index]; return values[index];
} }
@@ -362,7 +365,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
public char getAndMoveToLast(byte key) { public char getAndMoveToLast(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToLastIndex(index); moveToLastIndex(index, false);
return values[index]; return values[index];
} }
@@ -538,8 +541,8 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
containsNull = false; containsNull = false;
} }
protected void moveToFirstIndex(int startPos) { protected void moveToFirstIndex(int startPos, boolean adding) {
if(size == 1 || firstIndex == startPos) return; if(size == (adding ? 0 : 1) || firstIndex == startPos) return;
if(lastIndex == startPos) { if(lastIndex == startPos) {
lastIndex = (int)(links[startPos] >>> 32); lastIndex = (int)(links[startPos] >>> 32);
links[lastIndex] |= 0xFFFFFFFFL; links[lastIndex] |= 0xFFFFFFFFL;
@@ -556,8 +559,8 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
firstIndex = startPos; firstIndex = startPos;
} }
protected void moveToLastIndex(int startPos) { protected void moveToLastIndex(int startPos, boolean adding) {
if(size == 1 || lastIndex == startPos) return; if(size == (adding ? 0 : 1) || lastIndex == startPos) return;
if(firstIndex == startPos) { if(firstIndex == startPos) {
firstIndex = (int)links[startPos]; firstIndex = (int)links[startPos];
links[lastIndex] |= 0xFFFFFFFF00000000L; links[lastIndex] |= 0xFFFFFFFF00000000L;
@@ -671,6 +674,10 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
} }
private class MapEntrySet extends AbstractObjectSet<Byte2CharMap.Entry> implements Byte2CharOrderedMap.FastOrderedSet { private class MapEntrySet extends AbstractObjectSet<Byte2CharMap.Entry> implements Byte2CharOrderedMap.FastOrderedSet {
@Override
public void addFirst(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(Byte2CharMap.Entry o) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -836,7 +843,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
} }
@Override @Override
public Byte2CharMap.Entry reduce(ObjectObjectUnaryOperator<Byte2CharMap.Entry, Byte2CharMap.Entry> operator) { public Optional<Byte2CharMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2CharMap.Entry, Byte2CharMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2CharMap.Entry state = null; Byte2CharMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -851,21 +858,21 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
state = operator.apply(state, new ValueMapEntry(index)); state = operator.apply(state, new ValueMapEntry(index));
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2CharMap.Entry findFirst(Predicate<Byte2CharMap.Entry> filter) { public Optional<Byte2CharMap.Entry> findFirst(Predicate<Byte2CharMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
int index = firstIndex; int index = firstIndex;
while(index != -1) { while(index != -1) {
entry.set(index); entry.set(index);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)links[index]; index = (int)links[index];
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -945,9 +952,12 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void addFirst(byte o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); }
@@ -1090,7 +1100,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1105,19 +1115,19 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
state = operator.applyAsByte(state, keys[index]); state = operator.applyAsByte(state, keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(keys[index])) return keys[index]; if(filter.test(keys[index])) return OptionalByte.of(keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1252,7 +1262,7 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
} }
@Override @Override
public char reduce(CharCharUnaryOperator operator) { public OptionalChar reduce(CharCharUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
char state = (char)0; char state = (char)0;
boolean empty = true; boolean empty = true;
@@ -1267,19 +1277,19 @@ public class Byte2CharLinkedOpenCustomHashMap extends Byte2CharOpenCustomHashMap
state = operator.applyAsChar(state, values[index]); state = operator.applyAsChar(state, values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalChar.empty() : OptionalChar.of(state);
} }
@Override @Override
public char findFirst(CharPredicate filter) { public OptionalChar findFirst(CharPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (char)0; if(size() <= 0) return OptionalChar.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(values[index])) return values[index]; if(filter.test(values[index])) return OptionalChar.of(values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return (char)0; return OptionalChar.empty();
} }
@Override @Override
@@ -5,6 +5,7 @@ import java.util.ConcurrentModificationException;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteCharConsumer;
import speiger.src.collections.bytes.functions.function.Byte2CharFunction; import speiger.src.collections.bytes.functions.function.Byte2CharFunction;
import speiger.src.collections.bytes.functions.function.ByteCharUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteCharUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2CharMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2CharMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2CharMap; import speiger.src.collections.bytes.maps.interfaces.Byte2CharMap;
@@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer; import speiger.src.collections.objects.functions.consumer.ObjectCharConsumer;
import speiger.src.collections.chars.functions.function.CharPredicate; import speiger.src.collections.chars.functions.function.CharPredicate;
import speiger.src.collections.chars.functions.OptionalChar;
import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.collections.ObjectIterator;
import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSet; import speiger.src.collections.objects.sets.ObjectSet;
@@ -952,7 +955,7 @@ public class Byte2CharOpenCustomHashMap extends AbstractByte2CharMap implements
} }
@Override @Override
public Byte2CharMap.Entry reduce(ObjectObjectUnaryOperator<Byte2CharMap.Entry, Byte2CharMap.Entry> operator) { public Optional<Byte2CharMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2CharMap.Entry, Byte2CharMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2CharMap.Entry state = null; Byte2CharMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -969,25 +972,25 @@ public class Byte2CharOpenCustomHashMap extends AbstractByte2CharMap implements
} }
state = operator.apply(state, new ValueMapEntry(i)); state = operator.apply(state, new ValueMapEntry(i));
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2CharMap.Entry findFirst(Predicate<Byte2CharMap.Entry> filter) { public Optional<Byte2CharMap.Entry> findFirst(Predicate<Byte2CharMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
if(containsNull) { if(containsNull) {
entry.set(nullIndex); entry.set(nullIndex);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0)) { if(!strategy.equals(keys[i], (byte)0)) {
entry.set(i); entry.set(i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1161,7 +1164,7 @@ public class Byte2CharOpenCustomHashMap extends AbstractByte2CharMap implements
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1178,18 +1181,18 @@ public class Byte2CharOpenCustomHashMap extends AbstractByte2CharMap implements
} }
state = operator.applyAsByte(state, keys[i]); state = operator.applyAsByte(state, keys[i]);
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]);
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1304,7 +1307,7 @@ public class Byte2CharOpenCustomHashMap extends AbstractByte2CharMap implements
} }
@Override @Override
public char reduce(CharCharUnaryOperator operator) { public OptionalChar reduce(CharCharUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
char state = (char)0; char state = (char)0;
boolean empty = true; boolean empty = true;
@@ -1321,18 +1324,18 @@ public class Byte2CharOpenCustomHashMap extends AbstractByte2CharMap implements
} }
state = operator.applyAsChar(state, values[i]); state = operator.applyAsChar(state, values[i]);
} }
return state; return empty ? OptionalChar.empty() : OptionalChar.of(state);
} }
@Override @Override
public char findFirst(CharPredicate filter) { public OptionalChar findFirst(CharPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (char)0; if(size() <= 0) return OptionalChar.empty();
if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; if(containsNull && filter.test(values[nullIndex])) return OptionalChar.of(values[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return OptionalChar.of(values[i]);
} }
return (char)0; return OptionalChar.empty();
} }
@Override @Override
@@ -4,10 +4,12 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.DoublePredicate; import java.util.function.DoublePredicate;
import java.util.OptionalDouble;
import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator;
import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.bytes.functions.ByteConsumer;
@@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer; import speiger.src.collections.ints.functions.consumer.IntDoubleConsumer;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.functions.consumer.ByteDoubleConsumer; import speiger.src.collections.bytes.functions.consumer.ByteDoubleConsumer;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
@@ -196,13 +199,13 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
if(containsNull) { if(containsNull) {
double lastValue = values[nullIndex]; double lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -210,7 +213,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
double lastValue = values[pos]; double lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -218,7 +221,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -230,13 +233,13 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
if(containsNull) { if(containsNull) {
double lastValue = values[nullIndex]; double lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -244,7 +247,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
double lastValue = values[pos]; double lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToLastIndex(pos); moveToLastIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -252,7 +255,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -265,7 +268,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -276,7 +279,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -289,7 +292,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -300,7 +303,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -311,7 +314,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
if(isEmpty() || strategy.equals(firstByteKey(), key)) return false; if(isEmpty() || strategy.equals(firstByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -319,7 +322,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -333,7 +336,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
if(isEmpty() || strategy.equals(lastByteKey(), key)) return false; if(isEmpty() || strategy.equals(lastByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -341,7 +344,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToLastIndex(pos); moveToLastIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -354,7 +357,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
public double getAndMoveToFirst(byte key) { public double getAndMoveToFirst(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToFirstIndex(index); moveToFirstIndex(index, false);
return values[index]; return values[index];
} }
@@ -362,7 +365,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
public double getAndMoveToLast(byte key) { public double getAndMoveToLast(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToLastIndex(index); moveToLastIndex(index, false);
return values[index]; return values[index];
} }
@@ -538,8 +541,8 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
containsNull = false; containsNull = false;
} }
protected void moveToFirstIndex(int startPos) { protected void moveToFirstIndex(int startPos, boolean adding) {
if(size == 1 || firstIndex == startPos) return; if(size == (adding ? 0 : 1) || firstIndex == startPos) return;
if(lastIndex == startPos) { if(lastIndex == startPos) {
lastIndex = (int)(links[startPos] >>> 32); lastIndex = (int)(links[startPos] >>> 32);
links[lastIndex] |= 0xFFFFFFFFL; links[lastIndex] |= 0xFFFFFFFFL;
@@ -556,8 +559,8 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
firstIndex = startPos; firstIndex = startPos;
} }
protected void moveToLastIndex(int startPos) { protected void moveToLastIndex(int startPos, boolean adding) {
if(size == 1 || lastIndex == startPos) return; if(size == (adding ? 0 : 1) || lastIndex == startPos) return;
if(firstIndex == startPos) { if(firstIndex == startPos) {
firstIndex = (int)links[startPos]; firstIndex = (int)links[startPos];
links[lastIndex] |= 0xFFFFFFFF00000000L; links[lastIndex] |= 0xFFFFFFFF00000000L;
@@ -671,6 +674,10 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
} }
private class MapEntrySet extends AbstractObjectSet<Byte2DoubleMap.Entry> implements Byte2DoubleOrderedMap.FastOrderedSet { private class MapEntrySet extends AbstractObjectSet<Byte2DoubleMap.Entry> implements Byte2DoubleOrderedMap.FastOrderedSet {
@Override
public void addFirst(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(Byte2DoubleMap.Entry o) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -836,7 +843,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
} }
@Override @Override
public Byte2DoubleMap.Entry reduce(ObjectObjectUnaryOperator<Byte2DoubleMap.Entry, Byte2DoubleMap.Entry> operator) { public Optional<Byte2DoubleMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2DoubleMap.Entry, Byte2DoubleMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2DoubleMap.Entry state = null; Byte2DoubleMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -851,21 +858,21 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
state = operator.apply(state, new ValueMapEntry(index)); state = operator.apply(state, new ValueMapEntry(index));
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2DoubleMap.Entry findFirst(Predicate<Byte2DoubleMap.Entry> filter) { public Optional<Byte2DoubleMap.Entry> findFirst(Predicate<Byte2DoubleMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
int index = firstIndex; int index = firstIndex;
while(index != -1) { while(index != -1) {
entry.set(index); entry.set(index);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)links[index]; index = (int)links[index];
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -945,9 +952,12 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void addFirst(byte o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); }
@@ -1090,7 +1100,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1105,19 +1115,19 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
state = operator.applyAsByte(state, keys[index]); state = operator.applyAsByte(state, keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(keys[index])) return keys[index]; if(filter.test(keys[index])) return OptionalByte.of(keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1252,7 +1262,7 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
} }
@Override @Override
public double reduce(DoubleDoubleUnaryOperator operator) { public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
double state = 0D; double state = 0D;
boolean empty = true; boolean empty = true;
@@ -1267,19 +1277,19 @@ public class Byte2DoubleLinkedOpenCustomHashMap extends Byte2DoubleOpenCustomHas
state = operator.applyAsDouble(state, values[index]); state = operator.applyAsDouble(state, values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalDouble.empty() : OptionalDouble.of(state);
} }
@Override @Override
public double findFirst(DoublePredicate filter) { public OptionalDouble findFirst(DoublePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return 0D; if(size() <= 0) return OptionalDouble.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(values[index])) return values[index]; if(filter.test(values[index])) return OptionalDouble.of(values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return 0D; return OptionalDouble.empty();
} }
@Override @Override
@@ -5,10 +5,12 @@ import java.util.ConcurrentModificationException;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.DoublePredicate; import java.util.function.DoublePredicate;
import java.util.OptionalDouble;
import speiger.src.collections.bytes.collections.ByteIterator; import speiger.src.collections.bytes.collections.ByteIterator;
import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.bytes.functions.ByteConsumer;
@@ -20,6 +22,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteDoubleConsumer;
import speiger.src.collections.bytes.functions.function.Byte2DoubleFunction; import speiger.src.collections.bytes.functions.function.Byte2DoubleFunction;
import speiger.src.collections.bytes.functions.function.ByteDoubleUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteDoubleUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2DoubleMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2DoubleMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2DoubleMap; import speiger.src.collections.bytes.maps.interfaces.Byte2DoubleMap;
@@ -952,7 +955,7 @@ public class Byte2DoubleOpenCustomHashMap extends AbstractByte2DoubleMap impleme
} }
@Override @Override
public Byte2DoubleMap.Entry reduce(ObjectObjectUnaryOperator<Byte2DoubleMap.Entry, Byte2DoubleMap.Entry> operator) { public Optional<Byte2DoubleMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2DoubleMap.Entry, Byte2DoubleMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2DoubleMap.Entry state = null; Byte2DoubleMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -969,25 +972,25 @@ public class Byte2DoubleOpenCustomHashMap extends AbstractByte2DoubleMap impleme
} }
state = operator.apply(state, new ValueMapEntry(i)); state = operator.apply(state, new ValueMapEntry(i));
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2DoubleMap.Entry findFirst(Predicate<Byte2DoubleMap.Entry> filter) { public Optional<Byte2DoubleMap.Entry> findFirst(Predicate<Byte2DoubleMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
if(containsNull) { if(containsNull) {
entry.set(nullIndex); entry.set(nullIndex);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0)) { if(!strategy.equals(keys[i], (byte)0)) {
entry.set(i); entry.set(i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1161,7 +1164,7 @@ public class Byte2DoubleOpenCustomHashMap extends AbstractByte2DoubleMap impleme
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1178,18 +1181,18 @@ public class Byte2DoubleOpenCustomHashMap extends AbstractByte2DoubleMap impleme
} }
state = operator.applyAsByte(state, keys[i]); state = operator.applyAsByte(state, keys[i]);
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]);
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1304,7 +1307,7 @@ public class Byte2DoubleOpenCustomHashMap extends AbstractByte2DoubleMap impleme
} }
@Override @Override
public double reduce(DoubleDoubleUnaryOperator operator) { public OptionalDouble reduce(DoubleDoubleUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
double state = 0D; double state = 0D;
boolean empty = true; boolean empty = true;
@@ -1321,18 +1324,18 @@ public class Byte2DoubleOpenCustomHashMap extends AbstractByte2DoubleMap impleme
} }
state = operator.applyAsDouble(state, values[i]); state = operator.applyAsDouble(state, values[i]);
} }
return state; return empty ? OptionalDouble.empty() : OptionalDouble.of(state);
} }
@Override @Override
public double findFirst(DoublePredicate filter) { public OptionalDouble findFirst(DoublePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return 0D; if(size() <= 0) return OptionalDouble.empty();
if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; if(containsNull && filter.test(values[nullIndex])) return OptionalDouble.of(values[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return OptionalDouble.of(values[i]);
} }
return 0D; return OptionalDouble.empty();
} }
@Override @Override
@@ -4,6 +4,7 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
import speiger.src.collections.ints.functions.consumer.IntFloatConsumer; import speiger.src.collections.ints.functions.consumer.IntFloatConsumer;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.functions.consumer.ByteFloatConsumer; import speiger.src.collections.bytes.functions.consumer.ByteFloatConsumer;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
@@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer;
import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatPredicate;
import speiger.src.collections.floats.functions.OptionalFloat;
import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator;
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
import speiger.src.collections.objects.lists.ObjectListIterator; import speiger.src.collections.objects.lists.ObjectListIterator;
@@ -196,13 +199,13 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
if(containsNull) { if(containsNull) {
float lastValue = values[nullIndex]; float lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -210,7 +213,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
float lastValue = values[pos]; float lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -218,7 +221,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -230,13 +233,13 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
if(containsNull) { if(containsNull) {
float lastValue = values[nullIndex]; float lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -244,7 +247,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
float lastValue = values[pos]; float lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToLastIndex(pos); moveToLastIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -252,7 +255,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -265,7 +268,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -276,7 +279,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -289,7 +292,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -300,7 +303,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -311,7 +314,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
if(isEmpty() || strategy.equals(firstByteKey(), key)) return false; if(isEmpty() || strategy.equals(firstByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -319,7 +322,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -333,7 +336,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
if(isEmpty() || strategy.equals(lastByteKey(), key)) return false; if(isEmpty() || strategy.equals(lastByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -341,7 +344,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToLastIndex(pos); moveToLastIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -354,7 +357,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
public float getAndMoveToFirst(byte key) { public float getAndMoveToFirst(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToFirstIndex(index); moveToFirstIndex(index, false);
return values[index]; return values[index];
} }
@@ -362,7 +365,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
public float getAndMoveToLast(byte key) { public float getAndMoveToLast(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToLastIndex(index); moveToLastIndex(index, false);
return values[index]; return values[index];
} }
@@ -538,8 +541,8 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
containsNull = false; containsNull = false;
} }
protected void moveToFirstIndex(int startPos) { protected void moveToFirstIndex(int startPos, boolean adding) {
if(size == 1 || firstIndex == startPos) return; if(size == (adding ? 0 : 1) || firstIndex == startPos) return;
if(lastIndex == startPos) { if(lastIndex == startPos) {
lastIndex = (int)(links[startPos] >>> 32); lastIndex = (int)(links[startPos] >>> 32);
links[lastIndex] |= 0xFFFFFFFFL; links[lastIndex] |= 0xFFFFFFFFL;
@@ -556,8 +559,8 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
firstIndex = startPos; firstIndex = startPos;
} }
protected void moveToLastIndex(int startPos) { protected void moveToLastIndex(int startPos, boolean adding) {
if(size == 1 || lastIndex == startPos) return; if(size == (adding ? 0 : 1) || lastIndex == startPos) return;
if(firstIndex == startPos) { if(firstIndex == startPos) {
firstIndex = (int)links[startPos]; firstIndex = (int)links[startPos];
links[lastIndex] |= 0xFFFFFFFF00000000L; links[lastIndex] |= 0xFFFFFFFF00000000L;
@@ -671,6 +674,10 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
} }
private class MapEntrySet extends AbstractObjectSet<Byte2FloatMap.Entry> implements Byte2FloatOrderedMap.FastOrderedSet { private class MapEntrySet extends AbstractObjectSet<Byte2FloatMap.Entry> implements Byte2FloatOrderedMap.FastOrderedSet {
@Override
public void addFirst(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(Byte2FloatMap.Entry o) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -836,7 +843,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
} }
@Override @Override
public Byte2FloatMap.Entry reduce(ObjectObjectUnaryOperator<Byte2FloatMap.Entry, Byte2FloatMap.Entry> operator) { public Optional<Byte2FloatMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2FloatMap.Entry, Byte2FloatMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2FloatMap.Entry state = null; Byte2FloatMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -851,21 +858,21 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
state = operator.apply(state, new ValueMapEntry(index)); state = operator.apply(state, new ValueMapEntry(index));
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2FloatMap.Entry findFirst(Predicate<Byte2FloatMap.Entry> filter) { public Optional<Byte2FloatMap.Entry> findFirst(Predicate<Byte2FloatMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
int index = firstIndex; int index = firstIndex;
while(index != -1) { while(index != -1) {
entry.set(index); entry.set(index);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)links[index]; index = (int)links[index];
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -945,9 +952,12 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void addFirst(byte o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); }
@@ -1090,7 +1100,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1105,19 +1115,19 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
state = operator.applyAsByte(state, keys[index]); state = operator.applyAsByte(state, keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(keys[index])) return keys[index]; if(filter.test(keys[index])) return OptionalByte.of(keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1252,7 +1262,7 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
} }
@Override @Override
public float reduce(FloatFloatUnaryOperator operator) { public OptionalFloat reduce(FloatFloatUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
float state = 0F; float state = 0F;
boolean empty = true; boolean empty = true;
@@ -1267,19 +1277,19 @@ public class Byte2FloatLinkedOpenCustomHashMap extends Byte2FloatOpenCustomHashM
state = operator.applyAsFloat(state, values[index]); state = operator.applyAsFloat(state, values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalFloat.empty() : OptionalFloat.of(state);
} }
@Override @Override
public float findFirst(FloatPredicate filter) { public OptionalFloat findFirst(FloatPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return 0F; if(size() <= 0) return OptionalFloat.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(values[index])) return values[index]; if(filter.test(values[index])) return OptionalFloat.of(values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return 0F; return OptionalFloat.empty();
} }
@Override @Override
@@ -5,6 +5,7 @@ import java.util.ConcurrentModificationException;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteFloatConsumer;
import speiger.src.collections.bytes.functions.function.Byte2FloatFunction; import speiger.src.collections.bytes.functions.function.Byte2FloatFunction;
import speiger.src.collections.bytes.functions.function.ByteFloatUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteFloatUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2FloatMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2FloatMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2FloatMap; import speiger.src.collections.bytes.maps.interfaces.Byte2FloatMap;
@@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer; import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer;
import speiger.src.collections.floats.functions.function.FloatPredicate; import speiger.src.collections.floats.functions.function.FloatPredicate;
import speiger.src.collections.floats.functions.OptionalFloat;
import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.collections.ObjectIterator;
import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSet; import speiger.src.collections.objects.sets.ObjectSet;
@@ -952,7 +955,7 @@ public class Byte2FloatOpenCustomHashMap extends AbstractByte2FloatMap implement
} }
@Override @Override
public Byte2FloatMap.Entry reduce(ObjectObjectUnaryOperator<Byte2FloatMap.Entry, Byte2FloatMap.Entry> operator) { public Optional<Byte2FloatMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2FloatMap.Entry, Byte2FloatMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2FloatMap.Entry state = null; Byte2FloatMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -969,25 +972,25 @@ public class Byte2FloatOpenCustomHashMap extends AbstractByte2FloatMap implement
} }
state = operator.apply(state, new ValueMapEntry(i)); state = operator.apply(state, new ValueMapEntry(i));
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2FloatMap.Entry findFirst(Predicate<Byte2FloatMap.Entry> filter) { public Optional<Byte2FloatMap.Entry> findFirst(Predicate<Byte2FloatMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
if(containsNull) { if(containsNull) {
entry.set(nullIndex); entry.set(nullIndex);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0)) { if(!strategy.equals(keys[i], (byte)0)) {
entry.set(i); entry.set(i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1161,7 +1164,7 @@ public class Byte2FloatOpenCustomHashMap extends AbstractByte2FloatMap implement
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1178,18 +1181,18 @@ public class Byte2FloatOpenCustomHashMap extends AbstractByte2FloatMap implement
} }
state = operator.applyAsByte(state, keys[i]); state = operator.applyAsByte(state, keys[i]);
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]);
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1304,7 +1307,7 @@ public class Byte2FloatOpenCustomHashMap extends AbstractByte2FloatMap implement
} }
@Override @Override
public float reduce(FloatFloatUnaryOperator operator) { public OptionalFloat reduce(FloatFloatUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
float state = 0F; float state = 0F;
boolean empty = true; boolean empty = true;
@@ -1321,18 +1324,18 @@ public class Byte2FloatOpenCustomHashMap extends AbstractByte2FloatMap implement
} }
state = operator.applyAsFloat(state, values[i]); state = operator.applyAsFloat(state, values[i]);
} }
return state; return empty ? OptionalFloat.empty() : OptionalFloat.of(state);
} }
@Override @Override
public float findFirst(FloatPredicate filter) { public OptionalFloat findFirst(FloatPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return 0F; if(size() <= 0) return OptionalFloat.empty();
if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; if(containsNull && filter.test(values[nullIndex])) return OptionalFloat.of(values[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return OptionalFloat.of(values[i]);
} }
return 0F; return OptionalFloat.empty();
} }
@Override @Override
@@ -4,10 +4,12 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.IntPredicate; import java.util.function.IntPredicate;
import java.util.OptionalInt;
import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator;
import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.bytes.functions.ByteConsumer;
@@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
import speiger.src.collections.ints.functions.consumer.IntIntConsumer; import speiger.src.collections.ints.functions.consumer.IntIntConsumer;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.functions.consumer.ByteIntConsumer; import speiger.src.collections.bytes.functions.consumer.ByteIntConsumer;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
@@ -196,13 +199,13 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
if(containsNull) { if(containsNull) {
int lastValue = values[nullIndex]; int lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -210,7 +213,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
int lastValue = values[pos]; int lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -218,7 +221,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -230,13 +233,13 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
if(containsNull) { if(containsNull) {
int lastValue = values[nullIndex]; int lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -244,7 +247,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
int lastValue = values[pos]; int lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToLastIndex(pos); moveToLastIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -252,7 +255,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -265,7 +268,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -276,7 +279,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -289,7 +292,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -300,7 +303,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -311,7 +314,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
if(isEmpty() || strategy.equals(firstByteKey(), key)) return false; if(isEmpty() || strategy.equals(firstByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -319,7 +322,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -333,7 +336,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
if(isEmpty() || strategy.equals(lastByteKey(), key)) return false; if(isEmpty() || strategy.equals(lastByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -341,7 +344,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToLastIndex(pos); moveToLastIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -354,7 +357,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
public int getAndMoveToFirst(byte key) { public int getAndMoveToFirst(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToFirstIndex(index); moveToFirstIndex(index, false);
return values[index]; return values[index];
} }
@@ -362,7 +365,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
public int getAndMoveToLast(byte key) { public int getAndMoveToLast(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToLastIndex(index); moveToLastIndex(index, false);
return values[index]; return values[index];
} }
@@ -538,8 +541,8 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
containsNull = false; containsNull = false;
} }
protected void moveToFirstIndex(int startPos) { protected void moveToFirstIndex(int startPos, boolean adding) {
if(size == 1 || firstIndex == startPos) return; if(size == (adding ? 0 : 1) || firstIndex == startPos) return;
if(lastIndex == startPos) { if(lastIndex == startPos) {
lastIndex = (int)(links[startPos] >>> 32); lastIndex = (int)(links[startPos] >>> 32);
links[lastIndex] |= 0xFFFFFFFFL; links[lastIndex] |= 0xFFFFFFFFL;
@@ -556,8 +559,8 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
firstIndex = startPos; firstIndex = startPos;
} }
protected void moveToLastIndex(int startPos) { protected void moveToLastIndex(int startPos, boolean adding) {
if(size == 1 || lastIndex == startPos) return; if(size == (adding ? 0 : 1) || lastIndex == startPos) return;
if(firstIndex == startPos) { if(firstIndex == startPos) {
firstIndex = (int)links[startPos]; firstIndex = (int)links[startPos];
links[lastIndex] |= 0xFFFFFFFF00000000L; links[lastIndex] |= 0xFFFFFFFF00000000L;
@@ -671,6 +674,10 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
} }
private class MapEntrySet extends AbstractObjectSet<Byte2IntMap.Entry> implements Byte2IntOrderedMap.FastOrderedSet { private class MapEntrySet extends AbstractObjectSet<Byte2IntMap.Entry> implements Byte2IntOrderedMap.FastOrderedSet {
@Override
public void addFirst(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(Byte2IntMap.Entry o) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -836,7 +843,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
} }
@Override @Override
public Byte2IntMap.Entry reduce(ObjectObjectUnaryOperator<Byte2IntMap.Entry, Byte2IntMap.Entry> operator) { public Optional<Byte2IntMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2IntMap.Entry, Byte2IntMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2IntMap.Entry state = null; Byte2IntMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -851,21 +858,21 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
state = operator.apply(state, new ValueMapEntry(index)); state = operator.apply(state, new ValueMapEntry(index));
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2IntMap.Entry findFirst(Predicate<Byte2IntMap.Entry> filter) { public Optional<Byte2IntMap.Entry> findFirst(Predicate<Byte2IntMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
int index = firstIndex; int index = firstIndex;
while(index != -1) { while(index != -1) {
entry.set(index); entry.set(index);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)links[index]; index = (int)links[index];
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -945,9 +952,12 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void addFirst(byte o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); }
@@ -1090,7 +1100,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1105,19 +1115,19 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
state = operator.applyAsByte(state, keys[index]); state = operator.applyAsByte(state, keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(keys[index])) return keys[index]; if(filter.test(keys[index])) return OptionalByte.of(keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1252,7 +1262,7 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
} }
@Override @Override
public int reduce(IntIntUnaryOperator operator) { public OptionalInt reduce(IntIntUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
int state = 0; int state = 0;
boolean empty = true; boolean empty = true;
@@ -1267,19 +1277,19 @@ public class Byte2IntLinkedOpenCustomHashMap extends Byte2IntOpenCustomHashMap i
state = operator.applyAsInt(state, values[index]); state = operator.applyAsInt(state, values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalInt.empty() : OptionalInt.of(state);
} }
@Override @Override
public int findFirst(IntPredicate filter) { public OptionalInt findFirst(IntPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return 0; if(size() <= 0) return OptionalInt.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(values[index])) return values[index]; if(filter.test(values[index])) return OptionalInt.of(values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return 0; return OptionalInt.empty();
} }
@Override @Override
@@ -5,10 +5,12 @@ import java.util.ConcurrentModificationException;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.IntPredicate; import java.util.function.IntPredicate;
import java.util.OptionalInt;
import speiger.src.collections.bytes.collections.ByteIterator; import speiger.src.collections.bytes.collections.ByteIterator;
import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.bytes.functions.ByteConsumer;
@@ -20,6 +22,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteIntConsumer;
import speiger.src.collections.bytes.functions.function.Byte2IntFunction; import speiger.src.collections.bytes.functions.function.Byte2IntFunction;
import speiger.src.collections.bytes.functions.function.ByteIntUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteIntUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2IntMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2IntMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2IntMap; import speiger.src.collections.bytes.maps.interfaces.Byte2IntMap;
@@ -952,7 +955,7 @@ public class Byte2IntOpenCustomHashMap extends AbstractByte2IntMap implements IT
} }
@Override @Override
public Byte2IntMap.Entry reduce(ObjectObjectUnaryOperator<Byte2IntMap.Entry, Byte2IntMap.Entry> operator) { public Optional<Byte2IntMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2IntMap.Entry, Byte2IntMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2IntMap.Entry state = null; Byte2IntMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -969,25 +972,25 @@ public class Byte2IntOpenCustomHashMap extends AbstractByte2IntMap implements IT
} }
state = operator.apply(state, new ValueMapEntry(i)); state = operator.apply(state, new ValueMapEntry(i));
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2IntMap.Entry findFirst(Predicate<Byte2IntMap.Entry> filter) { public Optional<Byte2IntMap.Entry> findFirst(Predicate<Byte2IntMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
if(containsNull) { if(containsNull) {
entry.set(nullIndex); entry.set(nullIndex);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0)) { if(!strategy.equals(keys[i], (byte)0)) {
entry.set(i); entry.set(i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1161,7 +1164,7 @@ public class Byte2IntOpenCustomHashMap extends AbstractByte2IntMap implements IT
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1178,18 +1181,18 @@ public class Byte2IntOpenCustomHashMap extends AbstractByte2IntMap implements IT
} }
state = operator.applyAsByte(state, keys[i]); state = operator.applyAsByte(state, keys[i]);
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]);
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1304,7 +1307,7 @@ public class Byte2IntOpenCustomHashMap extends AbstractByte2IntMap implements IT
} }
@Override @Override
public int reduce(IntIntUnaryOperator operator) { public OptionalInt reduce(IntIntUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
int state = 0; int state = 0;
boolean empty = true; boolean empty = true;
@@ -1321,18 +1324,18 @@ public class Byte2IntOpenCustomHashMap extends AbstractByte2IntMap implements IT
} }
state = operator.applyAsInt(state, values[i]); state = operator.applyAsInt(state, values[i]);
} }
return state; return empty ? OptionalInt.empty() : OptionalInt.of(state);
} }
@Override @Override
public int findFirst(IntPredicate filter) { public OptionalInt findFirst(IntPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return 0; if(size() <= 0) return OptionalInt.empty();
if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; if(containsNull && filter.test(values[nullIndex])) return OptionalInt.of(values[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return OptionalInt.of(values[i]);
} }
return 0; return OptionalInt.empty();
} }
@Override @Override
@@ -4,10 +4,12 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.LongPredicate; import java.util.function.LongPredicate;
import java.util.OptionalLong;
import speiger.src.collections.bytes.collections.ByteBidirectionalIterator; import speiger.src.collections.bytes.collections.ByteBidirectionalIterator;
import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.bytes.functions.ByteConsumer;
@@ -15,6 +17,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
import speiger.src.collections.ints.functions.consumer.IntLongConsumer; import speiger.src.collections.ints.functions.consumer.IntLongConsumer;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.functions.consumer.ByteLongConsumer; import speiger.src.collections.bytes.functions.consumer.ByteLongConsumer;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
@@ -196,13 +199,13 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
if(containsNull) { if(containsNull) {
long lastValue = values[nullIndex]; long lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -210,7 +213,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
long lastValue = values[pos]; long lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -218,7 +221,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -230,13 +233,13 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
if(containsNull) { if(containsNull) {
long lastValue = values[nullIndex]; long lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -244,7 +247,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
long lastValue = values[pos]; long lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToLastIndex(pos); moveToLastIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -252,7 +255,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -265,7 +268,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -276,7 +279,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -289,7 +292,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -300,7 +303,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -311,7 +314,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
if(isEmpty() || strategy.equals(firstByteKey(), key)) return false; if(isEmpty() || strategy.equals(firstByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -319,7 +322,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -333,7 +336,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
if(isEmpty() || strategy.equals(lastByteKey(), key)) return false; if(isEmpty() || strategy.equals(lastByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -341,7 +344,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToLastIndex(pos); moveToLastIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -354,7 +357,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
public long getAndMoveToFirst(byte key) { public long getAndMoveToFirst(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToFirstIndex(index); moveToFirstIndex(index, false);
return values[index]; return values[index];
} }
@@ -362,7 +365,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
public long getAndMoveToLast(byte key) { public long getAndMoveToLast(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToLastIndex(index); moveToLastIndex(index, false);
return values[index]; return values[index];
} }
@@ -538,8 +541,8 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
containsNull = false; containsNull = false;
} }
protected void moveToFirstIndex(int startPos) { protected void moveToFirstIndex(int startPos, boolean adding) {
if(size == 1 || firstIndex == startPos) return; if(size == (adding ? 0 : 1) || firstIndex == startPos) return;
if(lastIndex == startPos) { if(lastIndex == startPos) {
lastIndex = (int)(links[startPos] >>> 32); lastIndex = (int)(links[startPos] >>> 32);
links[lastIndex] |= 0xFFFFFFFFL; links[lastIndex] |= 0xFFFFFFFFL;
@@ -556,8 +559,8 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
firstIndex = startPos; firstIndex = startPos;
} }
protected void moveToLastIndex(int startPos) { protected void moveToLastIndex(int startPos, boolean adding) {
if(size == 1 || lastIndex == startPos) return; if(size == (adding ? 0 : 1) || lastIndex == startPos) return;
if(firstIndex == startPos) { if(firstIndex == startPos) {
firstIndex = (int)links[startPos]; firstIndex = (int)links[startPos];
links[lastIndex] |= 0xFFFFFFFF00000000L; links[lastIndex] |= 0xFFFFFFFF00000000L;
@@ -671,6 +674,10 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
} }
private class MapEntrySet extends AbstractObjectSet<Byte2LongMap.Entry> implements Byte2LongOrderedMap.FastOrderedSet { private class MapEntrySet extends AbstractObjectSet<Byte2LongMap.Entry> implements Byte2LongOrderedMap.FastOrderedSet {
@Override
public void addFirst(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(Byte2LongMap.Entry o) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -836,7 +843,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
} }
@Override @Override
public Byte2LongMap.Entry reduce(ObjectObjectUnaryOperator<Byte2LongMap.Entry, Byte2LongMap.Entry> operator) { public Optional<Byte2LongMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2LongMap.Entry, Byte2LongMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2LongMap.Entry state = null; Byte2LongMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -851,21 +858,21 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
state = operator.apply(state, new ValueMapEntry(index)); state = operator.apply(state, new ValueMapEntry(index));
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2LongMap.Entry findFirst(Predicate<Byte2LongMap.Entry> filter) { public Optional<Byte2LongMap.Entry> findFirst(Predicate<Byte2LongMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
int index = firstIndex; int index = firstIndex;
while(index != -1) { while(index != -1) {
entry.set(index); entry.set(index);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)links[index]; index = (int)links[index];
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -945,9 +952,12 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void addFirst(byte o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); }
@@ -1090,7 +1100,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1105,19 +1115,19 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
state = operator.applyAsByte(state, keys[index]); state = operator.applyAsByte(state, keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(keys[index])) return keys[index]; if(filter.test(keys[index])) return OptionalByte.of(keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1252,7 +1262,7 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
} }
@Override @Override
public long reduce(LongLongUnaryOperator operator) { public OptionalLong reduce(LongLongUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
long state = 0L; long state = 0L;
boolean empty = true; boolean empty = true;
@@ -1267,19 +1277,19 @@ public class Byte2LongLinkedOpenCustomHashMap extends Byte2LongOpenCustomHashMap
state = operator.applyAsLong(state, values[index]); state = operator.applyAsLong(state, values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalLong.empty() : OptionalLong.of(state);
} }
@Override @Override
public long findFirst(LongPredicate filter) { public OptionalLong findFirst(LongPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return 0L; if(size() <= 0) return OptionalLong.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(values[index])) return values[index]; if(filter.test(values[index])) return OptionalLong.of(values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return 0L; return OptionalLong.empty();
} }
@Override @Override
@@ -5,10 +5,12 @@ import java.util.ConcurrentModificationException;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.LongPredicate; import java.util.function.LongPredicate;
import java.util.OptionalLong;
import speiger.src.collections.bytes.collections.ByteIterator; import speiger.src.collections.bytes.collections.ByteIterator;
import speiger.src.collections.bytes.functions.ByteConsumer; import speiger.src.collections.bytes.functions.ByteConsumer;
@@ -20,6 +22,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteLongConsumer;
import speiger.src.collections.bytes.functions.function.Byte2LongFunction; import speiger.src.collections.bytes.functions.function.Byte2LongFunction;
import speiger.src.collections.bytes.functions.function.ByteLongUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteLongUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2LongMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2LongMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2LongMap; import speiger.src.collections.bytes.maps.interfaces.Byte2LongMap;
@@ -952,7 +955,7 @@ public class Byte2LongOpenCustomHashMap extends AbstractByte2LongMap implements
} }
@Override @Override
public Byte2LongMap.Entry reduce(ObjectObjectUnaryOperator<Byte2LongMap.Entry, Byte2LongMap.Entry> operator) { public Optional<Byte2LongMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2LongMap.Entry, Byte2LongMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2LongMap.Entry state = null; Byte2LongMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -969,25 +972,25 @@ public class Byte2LongOpenCustomHashMap extends AbstractByte2LongMap implements
} }
state = operator.apply(state, new ValueMapEntry(i)); state = operator.apply(state, new ValueMapEntry(i));
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2LongMap.Entry findFirst(Predicate<Byte2LongMap.Entry> filter) { public Optional<Byte2LongMap.Entry> findFirst(Predicate<Byte2LongMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
if(containsNull) { if(containsNull) {
entry.set(nullIndex); entry.set(nullIndex);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0)) { if(!strategy.equals(keys[i], (byte)0)) {
entry.set(i); entry.set(i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1161,7 +1164,7 @@ public class Byte2LongOpenCustomHashMap extends AbstractByte2LongMap implements
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1178,18 +1181,18 @@ public class Byte2LongOpenCustomHashMap extends AbstractByte2LongMap implements
} }
state = operator.applyAsByte(state, keys[i]); state = operator.applyAsByte(state, keys[i]);
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]);
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1304,7 +1307,7 @@ public class Byte2LongOpenCustomHashMap extends AbstractByte2LongMap implements
} }
@Override @Override
public long reduce(LongLongUnaryOperator operator) { public OptionalLong reduce(LongLongUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
long state = 0L; long state = 0L;
boolean empty = true; boolean empty = true;
@@ -1321,18 +1324,18 @@ public class Byte2LongOpenCustomHashMap extends AbstractByte2LongMap implements
} }
state = operator.applyAsLong(state, values[i]); state = operator.applyAsLong(state, values[i]);
} }
return state; return empty ? OptionalLong.empty() : OptionalLong.of(state);
} }
@Override @Override
public long findFirst(LongPredicate filter) { public OptionalLong findFirst(LongPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return 0L; if(size() <= 0) return OptionalLong.empty();
if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; if(containsNull && filter.test(values[nullIndex])) return OptionalLong.of(values[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return OptionalLong.of(values[i]);
} }
return 0L; return OptionalLong.empty();
} }
@Override @Override
@@ -4,6 +4,7 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -13,6 +14,7 @@ import speiger.src.collections.bytes.functions.ByteConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer; import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.functions.consumer.ByteObjectConsumer; import speiger.src.collections.bytes.functions.consumer.ByteObjectConsumer;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
@@ -191,13 +193,13 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
if(containsNull) { if(containsNull) {
V lastValue = values[nullIndex]; V lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -205,7 +207,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
V lastValue = values[pos]; V lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -213,7 +215,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -225,13 +227,13 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
if(containsNull) { if(containsNull) {
V lastValue = values[nullIndex]; V lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -239,7 +241,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
V lastValue = values[pos]; V lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToLastIndex(pos); moveToLastIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -247,7 +249,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -260,7 +262,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -271,7 +273,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -284,7 +286,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -295,7 +297,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -306,7 +308,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
if(isEmpty() || strategy.equals(firstByteKey(), key)) return false; if(isEmpty() || strategy.equals(firstByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -314,7 +316,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -328,7 +330,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
if(isEmpty() || strategy.equals(lastByteKey(), key)) return false; if(isEmpty() || strategy.equals(lastByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -336,7 +338,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToLastIndex(pos); moveToLastIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -349,7 +351,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
public V getAndMoveToFirst(byte key) { public V getAndMoveToFirst(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToFirstIndex(index); moveToFirstIndex(index, false);
return values[index]; return values[index];
} }
@@ -357,7 +359,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
public V getAndMoveToLast(byte key) { public V getAndMoveToLast(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToLastIndex(index); moveToLastIndex(index, false);
return values[index]; return values[index];
} }
@@ -533,8 +535,8 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
containsNull = false; containsNull = false;
} }
protected void moveToFirstIndex(int startPos) { protected void moveToFirstIndex(int startPos, boolean adding) {
if(size == 1 || firstIndex == startPos) return; if(size == (adding ? 0 : 1) || firstIndex == startPos) return;
if(lastIndex == startPos) { if(lastIndex == startPos) {
lastIndex = (int)(links[startPos] >>> 32); lastIndex = (int)(links[startPos] >>> 32);
links[lastIndex] |= 0xFFFFFFFFL; links[lastIndex] |= 0xFFFFFFFFL;
@@ -551,8 +553,8 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
firstIndex = startPos; firstIndex = startPos;
} }
protected void moveToLastIndex(int startPos) { protected void moveToLastIndex(int startPos, boolean adding) {
if(size == 1 || lastIndex == startPos) return; if(size == (adding ? 0 : 1) || lastIndex == startPos) return;
if(firstIndex == startPos) { if(firstIndex == startPos) {
firstIndex = (int)links[startPos]; firstIndex = (int)links[startPos];
links[lastIndex] |= 0xFFFFFFFF00000000L; links[lastIndex] |= 0xFFFFFFFF00000000L;
@@ -666,6 +668,10 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
} }
private class MapEntrySet extends AbstractObjectSet<Byte2ObjectMap.Entry<V>> implements Byte2ObjectOrderedMap.FastOrderedSet<V> { private class MapEntrySet extends AbstractObjectSet<Byte2ObjectMap.Entry<V>> implements Byte2ObjectOrderedMap.FastOrderedSet<V> {
@Override
public void addFirst(Byte2ObjectMap.Entry<V> o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(Byte2ObjectMap.Entry<V> o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(Byte2ObjectMap.Entry<V> o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(Byte2ObjectMap.Entry<V> o) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -831,7 +837,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
} }
@Override @Override
public Byte2ObjectMap.Entry<V> reduce(ObjectObjectUnaryOperator<Byte2ObjectMap.Entry<V>, Byte2ObjectMap.Entry<V>> operator) { public Optional<Byte2ObjectMap.Entry<V>> reduce(ObjectObjectUnaryOperator<Byte2ObjectMap.Entry<V>, Byte2ObjectMap.Entry<V>> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2ObjectMap.Entry<V> state = null; Byte2ObjectMap.Entry<V> state = null;
boolean empty = true; boolean empty = true;
@@ -846,21 +852,21 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
state = operator.apply(state, new ValueMapEntry(index)); state = operator.apply(state, new ValueMapEntry(index));
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2ObjectMap.Entry<V> findFirst(Predicate<Byte2ObjectMap.Entry<V>> filter) { public Optional<Byte2ObjectMap.Entry<V>> findFirst(Predicate<Byte2ObjectMap.Entry<V>> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
int index = firstIndex; int index = firstIndex;
while(index != -1) { while(index != -1) {
entry.set(index); entry.set(index);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)links[index]; index = (int)links[index];
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -940,9 +946,12 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void addFirst(byte o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); }
@@ -1085,7 +1094,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1100,19 +1109,19 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
state = operator.applyAsByte(state, keys[index]); state = operator.applyAsByte(state, keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(keys[index])) return keys[index]; if(filter.test(keys[index])) return OptionalByte.of(keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1248,7 +1257,7 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
} }
@Override @Override
public V reduce(ObjectObjectUnaryOperator<V, V> operator) { public Optional<V> reduce(ObjectObjectUnaryOperator<V, V> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
V state = null; V state = null;
boolean empty = true; boolean empty = true;
@@ -1263,19 +1272,19 @@ public class Byte2ObjectLinkedOpenCustomHashMap<V> extends Byte2ObjectOpenCustom
state = operator.apply(state, values[index]); state = operator.apply(state, values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public V findFirst(Predicate<V> filter) { public Optional<V> findFirst(Predicate<V> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(values[index])) return values[index]; if(filter.test(values[index])) return Optional.ofNullable(values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -5,6 +5,7 @@ import java.util.ConcurrentModificationException;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -18,6 +19,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteObjectConsumer;
import speiger.src.collections.bytes.functions.function.ByteFunction; import speiger.src.collections.bytes.functions.function.ByteFunction;
import speiger.src.collections.bytes.functions.function.ByteObjectUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteObjectUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ObjectMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ObjectMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2ObjectMap; import speiger.src.collections.bytes.maps.interfaces.Byte2ObjectMap;
@@ -868,7 +870,7 @@ public class Byte2ObjectOpenCustomHashMap<V> extends AbstractByte2ObjectMap<V> i
} }
@Override @Override
public Byte2ObjectMap.Entry<V> reduce(ObjectObjectUnaryOperator<Byte2ObjectMap.Entry<V>, Byte2ObjectMap.Entry<V>> operator) { public Optional<Byte2ObjectMap.Entry<V>> reduce(ObjectObjectUnaryOperator<Byte2ObjectMap.Entry<V>, Byte2ObjectMap.Entry<V>> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2ObjectMap.Entry<V> state = null; Byte2ObjectMap.Entry<V> state = null;
boolean empty = true; boolean empty = true;
@@ -885,25 +887,25 @@ public class Byte2ObjectOpenCustomHashMap<V> extends AbstractByte2ObjectMap<V> i
} }
state = operator.apply(state, new ValueMapEntry(i)); state = operator.apply(state, new ValueMapEntry(i));
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2ObjectMap.Entry<V> findFirst(Predicate<Byte2ObjectMap.Entry<V>> filter) { public Optional<Byte2ObjectMap.Entry<V>> findFirst(Predicate<Byte2ObjectMap.Entry<V>> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
if(containsNull) { if(containsNull) {
entry.set(nullIndex); entry.set(nullIndex);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0)) { if(!strategy.equals(keys[i], (byte)0)) {
entry.set(i); entry.set(i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1077,7 +1079,7 @@ public class Byte2ObjectOpenCustomHashMap<V> extends AbstractByte2ObjectMap<V> i
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1094,18 +1096,18 @@ public class Byte2ObjectOpenCustomHashMap<V> extends AbstractByte2ObjectMap<V> i
} }
state = operator.applyAsByte(state, keys[i]); state = operator.applyAsByte(state, keys[i]);
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]);
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1220,7 +1222,7 @@ public class Byte2ObjectOpenCustomHashMap<V> extends AbstractByte2ObjectMap<V> i
} }
@Override @Override
public V reduce(ObjectObjectUnaryOperator<V, V> operator) { public Optional<V> reduce(ObjectObjectUnaryOperator<V, V> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
V state = null; V state = null;
boolean empty = true; boolean empty = true;
@@ -1237,18 +1239,18 @@ public class Byte2ObjectOpenCustomHashMap<V> extends AbstractByte2ObjectMap<V> i
} }
state = operator.apply(state, values[i]); state = operator.apply(state, values[i]);
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public V findFirst(Predicate<V> filter) { public Optional<V> findFirst(Predicate<V> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; if(containsNull && filter.test(values[nullIndex])) return Optional.ofNullable(values[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return Optional.ofNullable(values[i]);
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -4,6 +4,7 @@ import java.util.Arrays;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -14,6 +15,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntByteConsumer; import speiger.src.collections.ints.functions.consumer.IntByteConsumer;
import speiger.src.collections.ints.functions.consumer.IntObjectConsumer; import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
import speiger.src.collections.ints.functions.consumer.IntShortConsumer; import speiger.src.collections.ints.functions.consumer.IntShortConsumer;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.functions.consumer.ByteShortConsumer; import speiger.src.collections.bytes.functions.consumer.ByteShortConsumer;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
@@ -33,6 +35,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer;
import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortPredicate;
import speiger.src.collections.shorts.functions.OptionalShort;
import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator;
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
import speiger.src.collections.objects.lists.ObjectListIterator; import speiger.src.collections.objects.lists.ObjectListIterator;
@@ -196,13 +199,13 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
if(containsNull) { if(containsNull) {
short lastValue = values[nullIndex]; short lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -210,7 +213,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
short lastValue = values[pos]; short lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -218,7 +221,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -230,13 +233,13 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
if(containsNull) { if(containsNull) {
short lastValue = values[nullIndex]; short lastValue = values[nullIndex];
values[nullIndex] = value; values[nullIndex] = value;
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return lastValue; return lastValue;
} }
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -244,7 +247,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
short lastValue = values[pos]; short lastValue = values[pos];
values[pos] = value; values[pos] = value;
moveToLastIndex(pos); moveToLastIndex(pos, false);
return lastValue; return lastValue;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -252,7 +255,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -265,7 +268,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -276,7 +279,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToFirstIndex(pos); moveToFirstIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -289,7 +292,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
values[nullIndex] = value; values[nullIndex] = value;
containsNull = true; containsNull = true;
onNodeAdded(nullIndex); onNodeAdded(nullIndex);
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, true);
} }
else { else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
@@ -300,7 +303,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
keys[pos] = key; keys[pos] = key;
values[pos] = value; values[pos] = value;
onNodeAdded(pos); onNodeAdded(pos);
moveToLastIndex(pos); moveToLastIndex(pos, true);
} }
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor)); if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue(); return getDefaultReturnValue();
@@ -311,7 +314,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
if(isEmpty() || strategy.equals(firstByteKey(), key)) return false; if(isEmpty() || strategy.equals(firstByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToFirstIndex(nullIndex); moveToFirstIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -319,7 +322,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToFirstIndex(pos); moveToFirstIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -333,7 +336,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
if(isEmpty() || strategy.equals(lastByteKey(), key)) return false; if(isEmpty() || strategy.equals(lastByteKey(), key)) return false;
if(strategy.equals(key, (byte)0)) { if(strategy.equals(key, (byte)0)) {
if(containsNull) { if(containsNull) {
moveToLastIndex(nullIndex); moveToLastIndex(nullIndex, false);
return true; return true;
} }
} }
@@ -341,7 +344,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
int pos = HashUtil.mix(strategy.hashCode(key)) & mask; int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], (byte)0)) { while(!strategy.equals(keys[pos], (byte)0)) {
if(strategy.equals(keys[pos], key)) { if(strategy.equals(keys[pos], key)) {
moveToLastIndex(pos); moveToLastIndex(pos, false);
return true; return true;
} }
pos = ++pos & mask; pos = ++pos & mask;
@@ -354,7 +357,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
public short getAndMoveToFirst(byte key) { public short getAndMoveToFirst(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToFirstIndex(index); moveToFirstIndex(index, false);
return values[index]; return values[index];
} }
@@ -362,7 +365,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
public short getAndMoveToLast(byte key) { public short getAndMoveToLast(byte key) {
int index = findIndex(key); int index = findIndex(key);
if(index < 0) return getDefaultReturnValue(); if(index < 0) return getDefaultReturnValue();
moveToLastIndex(index); moveToLastIndex(index, false);
return values[index]; return values[index];
} }
@@ -538,8 +541,8 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
containsNull = false; containsNull = false;
} }
protected void moveToFirstIndex(int startPos) { protected void moveToFirstIndex(int startPos, boolean adding) {
if(size == 1 || firstIndex == startPos) return; if(size == (adding ? 0 : 1) || firstIndex == startPos) return;
if(lastIndex == startPos) { if(lastIndex == startPos) {
lastIndex = (int)(links[startPos] >>> 32); lastIndex = (int)(links[startPos] >>> 32);
links[lastIndex] |= 0xFFFFFFFFL; links[lastIndex] |= 0xFFFFFFFFL;
@@ -556,8 +559,8 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
firstIndex = startPos; firstIndex = startPos;
} }
protected void moveToLastIndex(int startPos) { protected void moveToLastIndex(int startPos, boolean adding) {
if(size == 1 || lastIndex == startPos) return; if(size == (adding ? 0 : 1) || lastIndex == startPos) return;
if(firstIndex == startPos) { if(firstIndex == startPos) {
firstIndex = (int)links[startPos]; firstIndex = (int)links[startPos];
links[lastIndex] |= 0xFFFFFFFF00000000L; links[lastIndex] |= 0xFFFFFFFF00000000L;
@@ -671,6 +674,10 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
} }
private class MapEntrySet extends AbstractObjectSet<Byte2ShortMap.Entry> implements Byte2ShortOrderedMap.FastOrderedSet { private class MapEntrySet extends AbstractObjectSet<Byte2ShortMap.Entry> implements Byte2ShortOrderedMap.FastOrderedSet {
@Override
public void addFirst(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(Byte2ShortMap.Entry o) { throw new UnsupportedOperationException(); }
@Override @Override
@@ -836,7 +843,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
} }
@Override @Override
public Byte2ShortMap.Entry reduce(ObjectObjectUnaryOperator<Byte2ShortMap.Entry, Byte2ShortMap.Entry> operator) { public Optional<Byte2ShortMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2ShortMap.Entry, Byte2ShortMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2ShortMap.Entry state = null; Byte2ShortMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -851,21 +858,21 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
state = operator.apply(state, new ValueMapEntry(index)); state = operator.apply(state, new ValueMapEntry(index));
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2ShortMap.Entry findFirst(Predicate<Byte2ShortMap.Entry> filter) { public Optional<Byte2ShortMap.Entry> findFirst(Predicate<Byte2ShortMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
int index = firstIndex; int index = firstIndex;
while(index != -1) { while(index != -1) {
entry.set(index); entry.set(index);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
index = (int)links[index]; index = (int)links[index];
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -945,9 +952,12 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} }
@Override
public void addFirst(byte o) { throw new UnsupportedOperationException(); }
@Override
public void addLast(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToFirst(byte o) { throw new UnsupportedOperationException(); }
@Override @Override
public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); } public boolean addAndMoveToLast(byte o) { throw new UnsupportedOperationException(); }
@@ -1090,7 +1100,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1105,19 +1115,19 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
state = operator.applyAsByte(state, keys[index]); state = operator.applyAsByte(state, keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(keys[index])) return keys[index]; if(filter.test(keys[index])) return OptionalByte.of(keys[index]);
index = (int)links[index]; index = (int)links[index];
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1252,7 +1262,7 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
} }
@Override @Override
public short reduce(ShortShortUnaryOperator operator) { public OptionalShort reduce(ShortShortUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
short state = (short)0; short state = (short)0;
boolean empty = true; boolean empty = true;
@@ -1267,19 +1277,19 @@ public class Byte2ShortLinkedOpenCustomHashMap extends Byte2ShortOpenCustomHashM
state = operator.applyAsShort(state, values[index]); state = operator.applyAsShort(state, values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return state; return empty ? OptionalShort.empty() : OptionalShort.of(state);
} }
@Override @Override
public short findFirst(ShortPredicate filter) { public OptionalShort findFirst(ShortPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (short)0; if(size() <= 0) return OptionalShort.empty();
int index = firstIndex; int index = firstIndex;
while(index != -1){ while(index != -1){
if(filter.test(values[index])) return values[index]; if(filter.test(values[index])) return OptionalShort.of(values[index]);
index = (int)links[index]; index = (int)links[index];
} }
return (short)0; return OptionalShort.empty();
} }
@Override @Override
@@ -5,6 +5,7 @@ import java.util.ConcurrentModificationException;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Predicate; import java.util.function.Predicate;
@@ -19,6 +20,7 @@ import speiger.src.collections.bytes.functions.consumer.ByteShortConsumer;
import speiger.src.collections.bytes.functions.function.Byte2ShortFunction; import speiger.src.collections.bytes.functions.function.Byte2ShortFunction;
import speiger.src.collections.bytes.functions.function.ByteShortUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteShortUnaryOperator;
import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator; import speiger.src.collections.bytes.functions.function.ByteByteUnaryOperator;
import speiger.src.collections.bytes.functions.OptionalByte;
import speiger.src.collections.bytes.functions.function.BytePredicate; import speiger.src.collections.bytes.functions.function.BytePredicate;
import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ShortMap; import speiger.src.collections.bytes.maps.abstracts.AbstractByte2ShortMap;
import speiger.src.collections.bytes.maps.interfaces.Byte2ShortMap; import speiger.src.collections.bytes.maps.interfaces.Byte2ShortMap;
@@ -37,6 +39,7 @@ import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer; import speiger.src.collections.objects.functions.consumer.ObjectShortConsumer;
import speiger.src.collections.shorts.functions.function.ShortPredicate; import speiger.src.collections.shorts.functions.function.ShortPredicate;
import speiger.src.collections.shorts.functions.OptionalShort;
import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.collections.ObjectIterator;
import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSet; import speiger.src.collections.objects.sets.ObjectSet;
@@ -952,7 +955,7 @@ public class Byte2ShortOpenCustomHashMap extends AbstractByte2ShortMap implement
} }
@Override @Override
public Byte2ShortMap.Entry reduce(ObjectObjectUnaryOperator<Byte2ShortMap.Entry, Byte2ShortMap.Entry> operator) { public Optional<Byte2ShortMap.Entry> reduce(ObjectObjectUnaryOperator<Byte2ShortMap.Entry, Byte2ShortMap.Entry> operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
Byte2ShortMap.Entry state = null; Byte2ShortMap.Entry state = null;
boolean empty = true; boolean empty = true;
@@ -969,25 +972,25 @@ public class Byte2ShortOpenCustomHashMap extends AbstractByte2ShortMap implement
} }
state = operator.apply(state, new ValueMapEntry(i)); state = operator.apply(state, new ValueMapEntry(i));
} }
return state; return empty ? Optional.empty() : Optional.ofNullable(state);
} }
@Override @Override
public Byte2ShortMap.Entry findFirst(Predicate<Byte2ShortMap.Entry> filter) { public Optional<Byte2ShortMap.Entry> findFirst(Predicate<Byte2ShortMap.Entry> filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return null; if(size() <= 0) return Optional.empty();
MapEntry entry = new MapEntry(); MapEntry entry = new MapEntry();
if(containsNull) { if(containsNull) {
entry.set(nullIndex); entry.set(nullIndex);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0)) { if(!strategy.equals(keys[i], (byte)0)) {
entry.set(i); entry.set(i);
if(filter.test(entry)) return entry; if(filter.test(entry)) return Optional.ofNullable(entry);
} }
} }
return null; return Optional.empty();
} }
@Override @Override
@@ -1161,7 +1164,7 @@ public class Byte2ShortOpenCustomHashMap extends AbstractByte2ShortMap implement
} }
@Override @Override
public byte reduce(ByteByteUnaryOperator operator) { public OptionalByte reduce(ByteByteUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
byte state = (byte)0; byte state = (byte)0;
boolean empty = true; boolean empty = true;
@@ -1178,18 +1181,18 @@ public class Byte2ShortOpenCustomHashMap extends AbstractByte2ShortMap implement
} }
state = operator.applyAsByte(state, keys[i]); state = operator.applyAsByte(state, keys[i]);
} }
return state; return empty ? OptionalByte.empty() : OptionalByte.of(state);
} }
@Override @Override
public byte findFirst(BytePredicate filter) { public OptionalByte findFirst(BytePredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (byte)0; if(size() <= 0) return OptionalByte.empty();
if(containsNull && filter.test(keys[nullIndex])) return keys[nullIndex]; if(containsNull && filter.test(keys[nullIndex])) return OptionalByte.of(keys[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return keys[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(keys[i])) return OptionalByte.of(keys[i]);
} }
return (byte)0; return OptionalByte.empty();
} }
@Override @Override
@@ -1304,7 +1307,7 @@ public class Byte2ShortOpenCustomHashMap extends AbstractByte2ShortMap implement
} }
@Override @Override
public short reduce(ShortShortUnaryOperator operator) { public OptionalShort reduce(ShortShortUnaryOperator operator) {
Objects.requireNonNull(operator); Objects.requireNonNull(operator);
short state = (short)0; short state = (short)0;
boolean empty = true; boolean empty = true;
@@ -1321,18 +1324,18 @@ public class Byte2ShortOpenCustomHashMap extends AbstractByte2ShortMap implement
} }
state = operator.applyAsShort(state, values[i]); state = operator.applyAsShort(state, values[i]);
} }
return state; return empty ? OptionalShort.empty() : OptionalShort.of(state);
} }
@Override @Override
public short findFirst(ShortPredicate filter) { public OptionalShort findFirst(ShortPredicate filter) {
Objects.requireNonNull(filter); Objects.requireNonNull(filter);
if(size() <= 0) return (short)0; if(size() <= 0) return OptionalShort.empty();
if(containsNull && filter.test(values[nullIndex])) return values[nullIndex]; if(containsNull && filter.test(values[nullIndex])) return OptionalShort.of(values[nullIndex]);
for(int i = nullIndex-1;i>=0;i--) { for(int i = nullIndex-1;i>=0;i--) {
if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return values[i]; if(!strategy.equals(keys[i], (byte)0) && filter.test(values[i])) return OptionalShort.of(values[i]);
} }
return (short)0; return OptionalShort.empty();
} }
@Override @Override

Some files were not shown because too many files have changed in this diff Show More