1579 lines
46 KiB
Plaintext
1579 lines
46 KiB
Plaintext
package speiger.src.collections.PACKAGE.maps.impl.misc;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.Map;
|
|
import java.util.NoSuchElementException;
|
|
import java.util.Objects;
|
|
import java.util.function.Consumer;
|
|
import java.util.function.BiFunction;
|
|
import java.util.function.Predicate;
|
|
#if !TYPE_OBJECT && JDK_TYPE
|
|
import java.util.function.PREDICATE;
|
|
#endif
|
|
#if !SAME_TYPE && JDK_VALUE && !VALUE_OBJECT
|
|
import java.util.function.VALUE_PREDICATE;
|
|
#endif
|
|
|
|
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
|
|
#if !TYPE_OBJECT
|
|
import speiger.src.collections.PACKAGE.functions.CONSUMER;
|
|
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
|
|
#endif
|
|
import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER;
|
|
#if !TYPE_OBJECT && !VALUE_OBJECT
|
|
import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
|
|
#endif
|
|
#if !SAME_TYPE && !TYPE_INT
|
|
import speiger.src.collections.ints.functions.consumer.VALUE_BI_FROM_INT_CONSUMER;
|
|
#endif
|
|
#if !TYPE_OBJECT && !VALUE_BOOLEAN && !JDK_TYPE
|
|
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
|
|
#endif
|
|
#if !TYPE_INT || !SAME_TYPE
|
|
import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER;
|
|
#endif
|
|
#if !VALUE_BOOLEAN || !JDK_TYPE
|
|
import speiger.src.collections.PACKAGE.functions.function.FUNCTION;
|
|
#endif
|
|
import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR;
|
|
#if !SAME_TYPE
|
|
import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR;
|
|
#endif
|
|
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
|
|
import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP;
|
|
import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
|
|
import speiger.src.collections.PACKAGE.maps.interfaces.ORDERED_MAP;
|
|
import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET;
|
|
import speiger.src.collections.PACKAGE.sets.ORDERED_SET;
|
|
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION;
|
|
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION;
|
|
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR;
|
|
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_SUPPLIER;
|
|
#if !SAME_TYPE
|
|
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR;
|
|
#if !VALUE_OBJECT
|
|
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_CONSUMER;
|
|
import speiger.src.collections.VALUE_PACKAGE.lists.VALUE_LIST_ITERATOR;
|
|
#endif
|
|
#endif
|
|
#if !TYPE_OBJECT && !VALUE_OBJECT || !VALUE_OBJECT
|
|
import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;
|
|
#endif
|
|
#if !SAME_TYPE
|
|
#if VALUE_OBJECT
|
|
import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_CONSUMER;
|
|
|
|
#endif
|
|
#if !JDK_VALUE
|
|
import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE;
|
|
#endif
|
|
#endif
|
|
#if !VALUE_OBJECT
|
|
import speiger.src.collections.objects.collections.ObjectIterator;
|
|
#endif
|
|
#if !TYPE_OBJECT
|
|
#if !VALUE_OBJECT
|
|
import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator;
|
|
|
|
#endif
|
|
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
|
|
import speiger.src.collections.objects.lists.ObjectListIterator;
|
|
import speiger.src.collections.objects.sets.AbstractObjectSet;
|
|
import speiger.src.collections.objects.sets.ObjectOrderedSet;
|
|
#endif
|
|
import speiger.src.collections.utils.HashUtil;
|
|
|
|
/**
|
|
* A Very Specific Type Specific implementation of a ArrayMap.
|
|
* This type of map is for very specific use cases that usaully would have lead to Tupled Lists otherwise.
|
|
* It also does not allow duplication (except for array constructors) and checks from last to first.
|
|
* It is not designed to be used as a HashMap replacement due to the poor performance it would cause.
|
|
* @note in this implementation SubMaps do NOT keep track of parent changes fully. For performance reasons it will just have a start/end index and not values
|
|
* Anything within that range will be updated appropiatly a shrink/growth of elements will break SubMaps in some ways. This can be useful but be careful
|
|
* @note this implementation does not shrink and only grows.
|
|
* @Type(T)
|
|
* @ValueType(V)
|
|
*/
|
|
public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements ORDERED_MAP KEY_VALUE_GENERIC_TYPE
|
|
{
|
|
/** The Backing keys array */
|
|
protected transient KEY_TYPE[] keys;
|
|
/** The Backing values array */
|
|
protected transient VALUE_TYPE[] values;
|
|
/** Amount of Elements stored in the ArrayMap */
|
|
protected int size = 0;
|
|
/** KeySet cache */
|
|
protected ORDERED_SET KEY_GENERIC_TYPE keySet;
|
|
/** Values cache */
|
|
protected VALUE_COLLECTION VALUE_GENERIC_TYPE valuesC;
|
|
/** EntrySet cache */
|
|
protected FastOrderedSet KEY_VALUE_GENERIC_TYPE entrySet;
|
|
|
|
/**
|
|
* Default Constructor
|
|
*/
|
|
public ARRAY_MAP() {
|
|
this(HashUtil.DEFAULT_MIN_CAPACITY);
|
|
}
|
|
|
|
/**
|
|
* Constructor that defines the minimum capacity
|
|
* @param minCapacity the minimum capacity the HashMap is allowed to be.
|
|
* @throws IllegalStateException if the minimum capacity is negative
|
|
*/
|
|
public ARRAY_MAP(int minCapacity) {
|
|
if(minCapacity < 0) throw new IllegalStateException("Minimum Capacity is negative. This is not allowed");
|
|
keys = NEW_KEY_ARRAY(minCapacity);
|
|
values = NEW_VALUE_ARRAY(minCapacity);
|
|
}
|
|
|
|
#if !TYPE_OBJECT || !VALUE_OBJECT
|
|
/**
|
|
* Helper constructor that allow to create a map from boxed values (it will unbox them)
|
|
* @param keys the keys that should be put into the map
|
|
* @param values the values that should be put into the map.
|
|
* @throws IllegalStateException if the keys and values do not match in length
|
|
*/
|
|
public ARRAY_MAP(CLASS_TYPE[] keys, CLASS_VALUE_TYPE[] values) {
|
|
this(keys, values, keys.length);
|
|
}
|
|
|
|
/**
|
|
* Helper constructor that allow to create a map from boxed values (it will unbox them) with a custom length
|
|
* @param keys the keys that should be put into the map
|
|
* @param values the values that should be put into the map.
|
|
* @param length the amount of values that should be pulled from the array
|
|
* @throws IllegalStateException if the keys and values do not match in length
|
|
*/
|
|
public ARRAY_MAP(CLASS_TYPE[] keys, CLASS_VALUE_TYPE[] values, int length) {
|
|
this(length);
|
|
if(keys.length != values.length) throw new IllegalStateException("Input Arrays are not equal size");
|
|
putAll(keys, values, 0, length);
|
|
}
|
|
|
|
#endif
|
|
/**
|
|
* Helper constructor that allow to create a map from unboxed values
|
|
* @param keys the keys that should be put into the map
|
|
* @param values the values that should be put into the map.
|
|
* @throws IllegalStateException if the keys and values do not match in lenght
|
|
*/
|
|
public ARRAY_MAP(KEY_TYPE[] keys, VALUE_TYPE[] values) {
|
|
this(keys, values, keys.length);
|
|
}
|
|
|
|
/**
|
|
* Helper constructor that allow to create a map from unboxed values
|
|
* @param keys the keys that should be put into the map
|
|
* @param values the values that should be put into the map.
|
|
* @param length the amount of values that should be pulled from the array
|
|
* @throws IllegalStateException if the keys and values do not match in lenght
|
|
*/
|
|
public ARRAY_MAP(KEY_TYPE[] keys, VALUE_TYPE[] values, int length) {
|
|
this(length);
|
|
if(keys.length != values.length) throw new IllegalStateException("Input Arrays are not equal size");
|
|
putAll(keys, values, 0, length);
|
|
}
|
|
|
|
/**
|
|
* A Helper constructor that allows to create a Map with exactly the same values as the provided map.
|
|
* @param map the values that should be present in the map
|
|
*/
|
|
public ARRAY_MAP(Map<? extends CLASS_TYPE, ? extends CLASS_VALUE_TYPE> map) {
|
|
this(map.size());
|
|
putAll(map);
|
|
}
|
|
|
|
/**
|
|
* A Type Specific Helper function that allows to create a new Map with exactly the same values as the provided map.
|
|
* @param map the values that should be present in the map
|
|
*/
|
|
public ARRAY_MAP(MAP KEY_VALUE_GENERIC_TYPE map) {
|
|
this(map.size());
|
|
for(ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iter = getFastIterator(map);iter.hasNext();size++) {
|
|
MAP.Entry KEY_VALUE_GENERIC_TYPE entry = iter.next();
|
|
keys[size] = entry.ENTRY_KEY();
|
|
values[size] = entry.ENTRY_VALUE();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) {
|
|
int index = findIndex(key);
|
|
if(index < 0) {
|
|
insertIndex(size++, key, value);
|
|
return getDefaultReturnValue();
|
|
}
|
|
VALUE_TYPE oldValue = values[index];
|
|
values[index] = value;
|
|
return oldValue;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value) {
|
|
int index = findIndex(key);
|
|
if(index < 0) {
|
|
insertIndex(size++, key, value);
|
|
return getDefaultReturnValue();
|
|
}
|
|
else if(VALUE_EQUALS(values[index], getDefaultReturnValue())) {
|
|
VALUE_TYPE oldValue = values[index];
|
|
values[index] = value;
|
|
return oldValue;
|
|
}
|
|
return values[index];
|
|
}
|
|
|
|
#if VALUE_PRIMITIVES
|
|
@Override
|
|
public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) {
|
|
int index = findIndex(key);
|
|
if(index < 0) {
|
|
insertIndex(size++, key, value);
|
|
return getDefaultReturnValue();
|
|
}
|
|
VALUE_TYPE oldValue = values[index];
|
|
values[index] += value;
|
|
return oldValue;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) {
|
|
int slot = findIndex(key);
|
|
if(slot < 0) return getDefaultReturnValue();
|
|
VALUE_TYPE oldValue = values[slot];
|
|
values[slot] -= value;
|
|
if(value < 0 ? (values[slot] >= getDefaultReturnValue()) : (values[slot] <= getDefaultReturnValue())) removeIndex(slot);
|
|
return oldValue;
|
|
}
|
|
|
|
#endif
|
|
@Override
|
|
public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) {
|
|
int index = findIndex(key);
|
|
if(index < 0) {
|
|
insertIndex(0, key, value);
|
|
size++;
|
|
return getDefaultReturnValue();
|
|
}
|
|
VALUE_TYPE lastValue = values[index];
|
|
values[index] = value;
|
|
moveIndexToFirst(index);
|
|
return lastValue;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE putAndMoveToLast(KEY_TYPE key, VALUE_TYPE value) {
|
|
int index = findIndex(key);
|
|
if(index < 0) {
|
|
insertIndex(size++, key, value);
|
|
return getDefaultReturnValue();
|
|
}
|
|
VALUE_TYPE lastValue = values[index];
|
|
values[index] = value;
|
|
moveIndexToLast(index);
|
|
return lastValue;
|
|
}
|
|
|
|
@Override
|
|
public boolean moveToFirst(KEY_TYPE key) {
|
|
int index = findIndex(key);
|
|
if(index > 0) {
|
|
moveIndexToFirst(index);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean moveToLast(KEY_TYPE key) {
|
|
int index = findIndex(key);
|
|
if(index >= 0 && index < size-1) {
|
|
moveIndexToLast(index);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
#if !TYPE_OBJECT
|
|
@Override
|
|
public boolean containsKey(KEY_TYPE key) {
|
|
return findIndex(key) >= 0;
|
|
}
|
|
|
|
#endif
|
|
#if !VALUE_OBJECT
|
|
@Override
|
|
public boolean containsValue(VALUE_TYPE value) {
|
|
return findValue(value) >= 0;
|
|
}
|
|
|
|
#endif
|
|
@Override
|
|
public boolean containsKey(Object key) {
|
|
return findIndex(key) >= 0;
|
|
}
|
|
|
|
@Override
|
|
public boolean containsValue(Object value) {
|
|
return findValue(value) >= 0;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE GET_VALUE(KEY_TYPE key) {
|
|
int index = findIndex(key);
|
|
return index < 0 ? getDefaultReturnValue() : values[index];
|
|
}
|
|
|
|
#if TYPE_OBJECT
|
|
@Override
|
|
public VALUE_TYPE getOrDefault(Object key, VALUE_TYPE defaultValue) {
|
|
int index = findIndex(key);
|
|
return index < 0 ? defaultValue : values[index];
|
|
}
|
|
|
|
#else
|
|
@Override
|
|
public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) {
|
|
int index = findIndex(key);
|
|
return index < 0 ? defaultValue : values[index];
|
|
}
|
|
|
|
#endif
|
|
@Override
|
|
public VALUE_TYPE getAndMoveToFirst(KEY_TYPE key) {
|
|
int index = findIndex(key);
|
|
if(index >= 0) {
|
|
VALUE_TYPE value = values[index];
|
|
moveIndexToFirst(index);
|
|
return value;
|
|
}
|
|
return getDefaultReturnValue();
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) {
|
|
int index = findIndex(key);
|
|
if(index >= 0) {
|
|
VALUE_TYPE value = values[index];
|
|
moveIndexToLast(index);
|
|
return value;
|
|
}
|
|
return getDefaultReturnValue();
|
|
}
|
|
|
|
@Override
|
|
public KEY_TYPE FIRST_ENTRY_KEY() {
|
|
if(size <= 0) throw new NoSuchElementException();
|
|
return keys[0];
|
|
}
|
|
|
|
@Override
|
|
public KEY_TYPE LAST_ENTRY_KEY() {
|
|
if(size <= 0) throw new NoSuchElementException();
|
|
return keys[size-1];
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE FIRST_ENTRY_VALUE() {
|
|
if(size <= 0) throw new NoSuchElementException();
|
|
return values[0];
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE LAST_ENTRY_VALUE() {
|
|
if(size <= 0) throw new NoSuchElementException();
|
|
return values[size-1];
|
|
}
|
|
|
|
@Override
|
|
public KEY_TYPE POLL_FIRST_ENTRY_KEY() {
|
|
if(size == 0) throw new NoSuchElementException();
|
|
KEY_TYPE result = keys[0];
|
|
removeIndex(0);
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
public KEY_TYPE POLL_LAST_ENTRY_KEY() {
|
|
if(size == 0) throw new NoSuchElementException();
|
|
KEY_TYPE result = keys[size-1];
|
|
removeIndex(size-1);
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) {
|
|
int index = findIndex(key);
|
|
if(index < 0) return getDefaultReturnValue();
|
|
VALUE_TYPE value = values[index];
|
|
removeIndex(index);
|
|
return value;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE REMOVE_VALUEOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) {
|
|
int index = findIndex(key);
|
|
if(index < 0) return defaultValue;
|
|
VALUE_TYPE value = values[index];
|
|
removeIndex(index);
|
|
return value;
|
|
}
|
|
|
|
#if !TYPE_OBJECT || !VALUE_OBJECT
|
|
@Override
|
|
public boolean remove(KEY_TYPE key, VALUE_TYPE value) {
|
|
int index = findIndex(key, value);
|
|
if(index < 0) return false;
|
|
removeIndex(index);
|
|
return true;
|
|
}
|
|
|
|
#endif
|
|
@Override
|
|
public CLASS_VALUE_TYPE remove(Object key) {
|
|
int index = findIndex(key);
|
|
if(index < 0) return VALUE_TO_OBJ(getDefaultReturnValue());
|
|
VALUE_TYPE value = values[index];
|
|
removeIndex(index);
|
|
return VALUE_TO_OBJ(value);
|
|
}
|
|
|
|
@Override
|
|
public boolean remove(Object key, Object value) {
|
|
int index = findIndex(key, value);
|
|
if(index < 0) return false;
|
|
removeIndex(index);
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public void forEach(BI_CONSUMER KEY_VALUE_GENERIC_TYPE action) {
|
|
if(size() <= 0) return;
|
|
for(int i = 0;i<size;i++)
|
|
action.accept(keys[i], values[i]);
|
|
}
|
|
|
|
@Override
|
|
public ORDERED_SET KEY_GENERIC_TYPE keySet() {
|
|
if(keySet == null) keySet = new KeySet();
|
|
return keySet;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_COLLECTION VALUE_GENERIC_TYPE values() {
|
|
if(valuesC == null) valuesC = new Values();
|
|
return valuesC;
|
|
}
|
|
|
|
@Override
|
|
public ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
|
|
if(entrySet == null) entrySet = new MapEntrySet();
|
|
return entrySet;
|
|
}
|
|
|
|
@Override
|
|
public boolean replace(KEY_TYPE key, VALUE_TYPE oldValue, VALUE_TYPE newValue) {
|
|
int index = findIndex(key);
|
|
if(index < 0 || values[index] != oldValue) return false;
|
|
values[index] = newValue;
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE replace(KEY_TYPE key, VALUE_TYPE value) {
|
|
int index = findIndex(key);
|
|
if(index < 0) return getDefaultReturnValue();
|
|
VALUE_TYPE oldValue = values[index];
|
|
values[index] = value;
|
|
return oldValue;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE COMPUTE(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
|
|
Objects.requireNonNull(mappingFunction);
|
|
int index = findIndex(key);
|
|
if(index == -1) {
|
|
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue());
|
|
#if VALUE_OBJECT
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
|
|
#endif
|
|
insertIndex(size++, key, newValue);
|
|
return newValue;
|
|
}
|
|
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]);
|
|
#if VALUE_OBJECT
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
|
|
removeIndex(index);
|
|
return newValue;
|
|
}
|
|
#endif
|
|
values[index] = newValue;
|
|
return newValue;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) {
|
|
Objects.requireNonNull(mappingFunction);
|
|
int index = findIndex(key);
|
|
if(index == -1) {
|
|
VALUE_TYPE newValue = mappingFunction.APPLY(key);
|
|
#if VALUE_OBJECT
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
|
|
#endif
|
|
insertIndex(size++, key, newValue);
|
|
return newValue;
|
|
}
|
|
VALUE_TYPE newValue = values[index];
|
|
#if VALUE_OBJECT
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
|
|
newValue = mappingFunction.APPLY(key);
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
|
|
values[index] = newValue;
|
|
}
|
|
#endif
|
|
return newValue;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE SUPPLY_IF_ABSENT(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) {
|
|
Objects.requireNonNull(valueProvider);
|
|
int index = findIndex(key);
|
|
if(index == -1) {
|
|
VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET();
|
|
#if VALUE_OBJECT
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
|
|
#endif
|
|
insertIndex(size++, key, newValue);
|
|
return newValue;
|
|
}
|
|
VALUE_TYPE newValue = values[index];
|
|
#if VALUE_OBJECT
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
|
|
newValue = valueProvider.VALUE_SUPPLY_GET();
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
|
|
values[index] = newValue;
|
|
}
|
|
#endif
|
|
return newValue;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
|
|
Objects.requireNonNull(mappingFunction);
|
|
int index = findIndex(key);
|
|
#if !VALUE_OBJECT
|
|
if(index == -1) return getDefaultReturnValue();
|
|
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]);
|
|
#else
|
|
if(index == -1 || VALUE_EQUALS(values[index], getDefaultReturnValue())) return getDefaultReturnValue();
|
|
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]);
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
|
|
removeIndex(index);
|
|
return newValue;
|
|
}
|
|
#endif
|
|
values[index] = newValue;
|
|
return newValue;
|
|
}
|
|
|
|
#if !VALUE_OBJECT
|
|
@Override
|
|
public VALUE_TYPE COMPUTENonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
|
|
Objects.requireNonNull(mappingFunction);
|
|
int index = findIndex(key);
|
|
if(index == -1) {
|
|
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue());
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
|
|
insertIndex(size++, key, newValue);
|
|
return newValue;
|
|
}
|
|
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]);
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
|
|
removeIndex(index);
|
|
return newValue;
|
|
}
|
|
values[index] = newValue;
|
|
return newValue;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE COMPUTE_IF_ABSENTNonDefault(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) {
|
|
Objects.requireNonNull(mappingFunction);
|
|
int index = findIndex(key);
|
|
if(index == -1) {
|
|
VALUE_TYPE newValue = mappingFunction.APPLY(key);
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
|
|
insertIndex(size++, key, newValue);
|
|
return newValue;
|
|
}
|
|
VALUE_TYPE newValue = values[index];
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
|
|
newValue = mappingFunction.APPLY(key);
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
|
|
values[index] = newValue;
|
|
}
|
|
return newValue;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE SUPPLY_IF_ABSENTNonDefault(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) {
|
|
Objects.requireNonNull(valueProvider);
|
|
int index = findIndex(key);
|
|
if(index == -1) {
|
|
VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET();
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
|
|
insertIndex(size++, key, newValue);
|
|
return newValue;
|
|
}
|
|
VALUE_TYPE newValue = values[index];
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
|
|
newValue = valueProvider.VALUE_SUPPLY_GET();
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue;
|
|
values[index] = newValue;
|
|
}
|
|
return newValue;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE COMPUTE_IF_PRESENTNonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
|
|
Objects.requireNonNull(mappingFunction);
|
|
int index = findIndex(key);
|
|
if(index == -1 || VALUE_EQUALS(values[index], getDefaultReturnValue())) return getDefaultReturnValue();
|
|
VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]);
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
|
|
removeIndex(index);
|
|
return newValue;
|
|
}
|
|
values[index] = newValue;
|
|
return newValue;
|
|
}
|
|
|
|
#endif
|
|
@Override
|
|
public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) {
|
|
Objects.requireNonNull(mappingFunction);
|
|
#if VALUE_OBJECT
|
|
Objects.requireNonNull(value);
|
|
#endif
|
|
int index = findIndex(key);
|
|
VALUE_TYPE newValue = index == -1 || VALUE_EQUALS(values[index], getDefaultReturnValue()) ? value : mappingFunction.APPLY_VALUE(values[index], value);
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
|
|
if(index >= 0)
|
|
removeIndex(index);
|
|
}
|
|
else if(index == -1) insertIndex(size++, key, newValue);
|
|
else values[index] = newValue;
|
|
return newValue;
|
|
}
|
|
|
|
@Override
|
|
public void BULK_MERGE(MAP KEY_VALUE_GENERIC_TYPE m, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) {
|
|
Objects.requireNonNull(mappingFunction);
|
|
for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : getFastIterable(m)) {
|
|
KEY_TYPE key = entry.ENTRY_KEY();
|
|
int index = findIndex(key);
|
|
VALUE_TYPE newValue = index == -1 || VALUE_EQUALS(values[index], getDefaultReturnValue()) ? entry.ENTRY_VALUE() : mappingFunction.APPLY_VALUE(values[index], entry.ENTRY_VALUE());
|
|
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) {
|
|
if(index >= 0)
|
|
removeIndex(index);
|
|
}
|
|
else if(index == -1) insertIndex(size++, key, newValue);
|
|
else values[index] = newValue;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public int size() {
|
|
return size;
|
|
}
|
|
|
|
@Override
|
|
public void clear() {
|
|
Arrays.fill(keys, 0, size, EMPTY_KEY_VALUE);
|
|
Arrays.fill(values, 0, size, EMPTY_VALUE);
|
|
size = 0;
|
|
}
|
|
|
|
public ARRAY_MAP KEY_VALUE_GENERIC_TYPE copy() {
|
|
ARRAY_MAP KEY_VALUE_GENERIC_TYPE map = new ARRAY_MAPKV_BRACES();
|
|
map.size = size;
|
|
map.keys = Arrays.copyOf(keys, keys.length);
|
|
map.values = Arrays.copyOf(values, keys.length);
|
|
return map;
|
|
}
|
|
|
|
protected void moveIndexToFirst(int index) {
|
|
if(index == 0) return;
|
|
KEY_TYPE key = keys[index];
|
|
VALUE_TYPE value = values[index];
|
|
System.arraycopy(keys, 0, keys, 1, index);
|
|
System.arraycopy(values, 0, values, 1, index);
|
|
keys[0] = key;
|
|
values[0] = value;
|
|
}
|
|
|
|
protected void moveIndexToLast(int index) {
|
|
if(index == size-1) return;
|
|
KEY_TYPE key = keys[index];
|
|
VALUE_TYPE value = values[index];
|
|
System.arraycopy(keys, index+1, keys, index, size-index-1);
|
|
System.arraycopy(values, index+1, values, index, size-index-1);
|
|
keys[size-1] = key;
|
|
values[size-1] = value;
|
|
}
|
|
|
|
protected void grow(int newSize) {
|
|
if(newSize < keys.length) return;
|
|
newSize = Math.max(newSize, keys.length == 0 ? 2 : keys.length * 2);
|
|
keys = Arrays.copyOf(keys, newSize);
|
|
values = Arrays.copyOf(values, newSize);
|
|
}
|
|
|
|
protected void insertIndex(int index, KEY_TYPE key, VALUE_TYPE value) {
|
|
grow(size+1);
|
|
if(index != size) {
|
|
System.arraycopy(keys, index, keys, index+1, size-index);
|
|
System.arraycopy(values, index, values, index+1, size-index);
|
|
}
|
|
keys[index] = key;
|
|
values[index] = value;
|
|
}
|
|
|
|
protected void removeRange(int from, int to) {
|
|
if(from < 0 || from >= size) throw new IllegalStateException("From Element ");
|
|
int length = to - from;
|
|
if(length <= 0) return;
|
|
if(to != size) {
|
|
System.arraycopy(keys, to, keys, from, size - to);
|
|
System.arraycopy(values, to, values, from, size - to);
|
|
}
|
|
for(int i = 0;i<length;i++) {
|
|
keys[i+to] = EMPTY_KEY_VALUE;
|
|
values[i+to] = EMPTY_VALUE;
|
|
}
|
|
size -= length;
|
|
}
|
|
|
|
protected void removeIndex(int index) {
|
|
if(index == size-1) {
|
|
size--;
|
|
keys[size] = EMPTY_KEY_VALUE;
|
|
values[size] = EMPTY_VALUE;
|
|
return;
|
|
}
|
|
System.arraycopy(keys, index+1, keys, index, size-index-1);
|
|
System.arraycopy(values, index+1, values, index, size-index-1);
|
|
size--;
|
|
keys[size] = EMPTY_KEY_VALUE;
|
|
values[size] = EMPTY_VALUE;
|
|
}
|
|
|
|
#if !TYPE_OBJECT || !VALUE_OBJECT
|
|
protected int findIndex(KEY_TYPE key, VALUE_TYPE value) {
|
|
for(int i = size-1;i>=0;i--)
|
|
if(KEY_EQUALS(keys[i], key) && VALUE_EQUALS(values[i], value)) return i;
|
|
return -1;
|
|
}
|
|
|
|
#endif
|
|
#if !TYPE_OBJECT
|
|
protected int findIndex(KEY_TYPE key) {
|
|
for(int i = size-1;i>=0;i--)
|
|
if(KEY_EQUALS(keys[i], key)) return i;
|
|
return -1;
|
|
}
|
|
|
|
#endif
|
|
#if !VALUE_OBJECT
|
|
protected int findValue(VALUE_TYPE value) {
|
|
for(int i = size-1;i>=0;i--)
|
|
if(VALUE_EQUALS(values[i], value)) return i;
|
|
return -1;
|
|
}
|
|
|
|
#endif
|
|
protected int findIndex(Object key, Object value) {
|
|
for(int i = size-1;i>=0;i--)
|
|
if(EQUALS_KEY_TYPE(keys[i], key) && EQUALS_VALUE_TYPE(values[i], value)) return i;
|
|
return -1;
|
|
}
|
|
|
|
protected int findIndex(Object key) {
|
|
for(int i = size-1;i>=0;i--)
|
|
if(EQUALS_KEY_TYPE(keys[i], key)) return i;
|
|
return -1;
|
|
}
|
|
|
|
protected int findValue(Object value) {
|
|
for(int i = size-1;i>=0;i--)
|
|
if(EQUALS_VALUE_TYPE(values[i], value)) return i;
|
|
return -1;
|
|
}
|
|
|
|
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements ORDERED_MAP.FastOrderedSet KEY_VALUE_GENERIC_TYPE {
|
|
@Override
|
|
public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
|
|
@Override
|
|
public boolean addAndMoveToLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); }
|
|
|
|
@Override
|
|
public boolean moveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) {
|
|
return ARRAY_MAP.this.moveToFirst(o.ENTRY_KEY());
|
|
}
|
|
|
|
@Override
|
|
public boolean moveToLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) {
|
|
return ARRAY_MAP.this.moveToLast(o.ENTRY_KEY());
|
|
}
|
|
|
|
@Override
|
|
public MAP.Entry KEY_VALUE_GENERIC_TYPE first() {
|
|
return new BasicEntryKV_BRACES(FIRST_ENTRY_KEY(), FIRST_ENTRY_VALUE());
|
|
}
|
|
|
|
@Override
|
|
public MAP.Entry KEY_VALUE_GENERIC_TYPE last() {
|
|
return new BasicEntryKV_BRACES(LAST_ENTRY_KEY(), LAST_ENTRY_VALUE());
|
|
}
|
|
|
|
@Override
|
|
public MAP.Entry KEY_VALUE_GENERIC_TYPE pollFirst() {
|
|
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(FIRST_ENTRY_KEY(), FIRST_ENTRY_VALUE());
|
|
POLL_FIRST_ENTRY_KEY();
|
|
return entry;
|
|
}
|
|
|
|
@Override
|
|
public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLast() {
|
|
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(LAST_ENTRY_KEY(), LAST_ENTRY_VALUE());
|
|
POLL_LAST_ENTRY_KEY();
|
|
return entry;
|
|
}
|
|
|
|
@Override
|
|
public ObjectBidirectionalIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iterator() {
|
|
return new EntryIterator();
|
|
}
|
|
|
|
@Override
|
|
public ObjectBidirectionalIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iterator(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement) {
|
|
return new EntryIterator(fromElement.ENTRY_KEY());
|
|
}
|
|
|
|
@Override
|
|
public ObjectBidirectionalIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> fastIterator() {
|
|
return new FastEntryIterator();
|
|
}
|
|
|
|
@Override
|
|
public ObjectBidirectionalIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> fastIterator(KEY_TYPE fromElement) {
|
|
return new FastEntryIterator(fromElement);
|
|
}
|
|
|
|
@Override
|
|
public MapEntrySet copy() { throw new UnsupportedOperationException(); }
|
|
|
|
@Override
|
|
public void forEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
|
|
Objects.requireNonNull(action);
|
|
for(int i = 0;i<size;i++)
|
|
action.accept(new ValueMapEntry(i));
|
|
}
|
|
|
|
@Override
|
|
public void fastForEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
|
|
Objects.requireNonNull(action);
|
|
if(size() <= 0) return;
|
|
MapEntry entry = new MapEntry();
|
|
for(int i = 0;i<size;i++) {
|
|
entry.set(i);
|
|
action.accept(entry);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void forEachIndexed(IntObjectConsumer<MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
|
|
Objects.requireNonNull(action);
|
|
for(int i = 0;i<size;i++) {
|
|
action.accept(i, new ValueMapEntry(i));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public <E> void forEach(E input, ObjectObjectConsumer<E, MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
|
|
Objects.requireNonNull(action);
|
|
for(int i = 0;i<size;i++) {
|
|
action.accept(input, new ValueMapEntry(i));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean matchesAny(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
|
|
Objects.requireNonNull(filter);
|
|
if(size() <= 0) return false;
|
|
MapEntry entry = new MapEntry();
|
|
for(int i = 0;i<size;i++) {
|
|
entry.set(i);
|
|
if(filter.test(entry)) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean matchesNone(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
|
|
Objects.requireNonNull(filter);
|
|
if(size() <= 0) return true;
|
|
MapEntry entry = new MapEntry();
|
|
for(int i = 0;i<size;i++) {
|
|
entry.set(i);
|
|
if(filter.test(entry)) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean matchesAll(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
|
|
Objects.requireNonNull(filter);
|
|
if(size() <= 0) return true;
|
|
MapEntry entry = new MapEntry();
|
|
for(int i = 0;i<size;i++) {
|
|
entry.set(i);
|
|
if(!filter.test(entry)) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public <E> E reduce(E identity, BiFunction<E, MAP.Entry KEY_VALUE_GENERIC_TYPE, E> operator) {
|
|
Objects.requireNonNull(operator);
|
|
E state = identity;
|
|
for(int i = 0;i<size;i++) {
|
|
state = operator.apply(state, new ValueMapEntry(i));
|
|
}
|
|
return state;
|
|
}
|
|
|
|
@Override
|
|
public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator<MAP.Entry KEY_VALUE_GENERIC_TYPE, MAP.Entry KEY_VALUE_GENERIC_TYPE> operator) {
|
|
Objects.requireNonNull(operator);
|
|
MAP.Entry KEY_VALUE_GENERIC_TYPE state = null;
|
|
boolean empty = true;
|
|
for(int i = 0;i<size;i++) {
|
|
if(empty) {
|
|
empty = false;
|
|
state = new ValueMapEntry(i);
|
|
continue;
|
|
}
|
|
state = operator.apply(state, new ValueMapEntry(i));
|
|
}
|
|
return state;
|
|
}
|
|
|
|
@Override
|
|
public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
|
|
Objects.requireNonNull(filter);
|
|
if(size() <= 0) return null;
|
|
MapEntry entry = new MapEntry();
|
|
for(int i = 0;i<size;i++) {
|
|
entry.set(i);
|
|
if(filter.test(entry)) return entry;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public int count(Predicate<MAP.Entry KEY_VALUE_GENERIC_TYPE> filter) {
|
|
Objects.requireNonNull(filter);
|
|
if(size() <= 0) return 0;
|
|
int result = 0;
|
|
MapEntry entry = new MapEntry();
|
|
for(int i = 0;i<size;i++) {
|
|
entry.set(i);
|
|
if(filter.test(entry)) result++;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
@Deprecated
|
|
public boolean contains(Object o) {
|
|
if(o instanceof Map.Entry) {
|
|
if(o instanceof MAP.Entry) {
|
|
MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o;
|
|
int index = ARRAY_MAP.this.findIndex(entry.ENTRY_KEY());
|
|
if(index >= 0) return VALUE_EQUALS(entry.ENTRY_VALUE(), ARRAY_MAP.this.values[index]);
|
|
}
|
|
else {
|
|
Map.Entry<?, ?> entry = (Map.Entry<?, ?>)o;
|
|
int index = ARRAY_MAP.this.findIndex(entry.getKey());
|
|
if(index >= 0) return Objects.equals(entry.getValue(), VALUE_TO_OBJ(ARRAY_MAP.this.values[index]));
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
@Deprecated
|
|
public boolean remove(Object o) {
|
|
if(o instanceof Map.Entry) {
|
|
if(o instanceof MAP.Entry) {
|
|
MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o;
|
|
return ARRAY_MAP.this.remove(entry.ENTRY_KEY(), entry.ENTRY_VALUE());
|
|
}
|
|
Map.Entry<?, ?> entry = (Map.Entry<?, ?>)o;
|
|
return ARRAY_MAP.this.remove(entry.getKey(), entry.getValue());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public int size() {
|
|
return ARRAY_MAP.this.size();
|
|
}
|
|
|
|
@Override
|
|
public void clear() {
|
|
ARRAY_MAP.this.clear();
|
|
}
|
|
}
|
|
|
|
private class KeySet extends ABSTRACT_SET KEY_GENERIC_TYPE implements ORDERED_SET KEY_GENERIC_TYPE {
|
|
#if TYPE_OBJECT
|
|
@Override
|
|
public boolean contains(Object e) { return containsKey(e); }
|
|
|
|
@Override
|
|
public boolean remove(Object o) {
|
|
int oldSize = size;
|
|
ARRAY_MAP.this.remove(o);
|
|
return size != oldSize;
|
|
}
|
|
|
|
#else
|
|
@Override
|
|
public boolean contains(KEY_TYPE e) { return containsKey(e); }
|
|
|
|
@Override
|
|
public boolean remove(KEY_TYPE o) {
|
|
int oldSize = size;
|
|
ARRAY_MAP.this.remove(o);
|
|
return size != oldSize;
|
|
}
|
|
|
|
#endif
|
|
@Override
|
|
public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); }
|
|
@Override
|
|
public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); }
|
|
@Override
|
|
public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); }
|
|
@Override
|
|
public boolean moveToFirst(KEY_TYPE o) { return ARRAY_MAP.this.moveToFirst(o); }
|
|
@Override
|
|
public boolean moveToLast(KEY_TYPE o) { return ARRAY_MAP.this.moveToLast(o); }
|
|
@Override
|
|
public LIST_ITERATOR KEY_GENERIC_TYPE iterator() { return new KeyIterator(); }
|
|
@Override
|
|
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { return new KeyIterator(fromElement); }
|
|
@Override
|
|
public int size() { return ARRAY_MAP.this.size(); }
|
|
@Override
|
|
public void clear() { ARRAY_MAP.this.clear(); }
|
|
@Override
|
|
public KEY_TYPE FIRST_KEY() { return FIRST_ENTRY_KEY(); }
|
|
@Override
|
|
public KEY_TYPE POLL_FIRST_KEY() { return POLL_FIRST_ENTRY_KEY(); }
|
|
@Override
|
|
public KEY_TYPE LAST_KEY() { return LAST_ENTRY_KEY(); }
|
|
@Override
|
|
public KEY_TYPE POLL_LAST_KEY() { return POLL_LAST_ENTRY_KEY(); }
|
|
|
|
@Override
|
|
public KeySet copy() { throw new UnsupportedOperationException(); }
|
|
|
|
@Override
|
|
public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) {
|
|
Objects.requireNonNull(action);
|
|
for(int i = 0;i<size;action.accept(keys[i++]));
|
|
}
|
|
|
|
@Override
|
|
public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) {
|
|
Objects.requireNonNull(action);
|
|
for(int i = 0;i<size;action.accept(i, keys[i++]));
|
|
}
|
|
|
|
@Override
|
|
public <E> void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) {
|
|
Objects.requireNonNull(action);
|
|
for(int i = 0;i<size;action.accept(input, keys[i++]));
|
|
}
|
|
|
|
@Override
|
|
public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) {
|
|
Objects.requireNonNull(filter);
|
|
if(size() <= 0) return false;
|
|
for(int i = 0;i<size;i++) {
|
|
if(filter.test(keys[i])) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) {
|
|
Objects.requireNonNull(filter);
|
|
for(int i = 0;i<size;i++) {
|
|
if(filter.test(keys[i])) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) {
|
|
Objects.requireNonNull(filter);
|
|
for(int i = 0;i<size;i++) {
|
|
if(!filter.test(keys[i])) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
#if !TYPE_OBJECT
|
|
@Override
|
|
public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
|
|
Objects.requireNonNull(operator);
|
|
KEY_TYPE state = identity;
|
|
for(int i = 0;i<size;i++) {
|
|
state = operator.APPLY_KEY_VALUE(state, keys[i]);
|
|
}
|
|
return state;
|
|
}
|
|
|
|
#else
|
|
@Override
|
|
public <KEY_SPECIAL_TYPE> KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction<KEY_SPECIAL_TYPE, KEY_TYPE, KEY_SPECIAL_TYPE> operator) {
|
|
Objects.requireNonNull(operator);
|
|
KEY_SPECIAL_TYPE state = identity;
|
|
for(int i = 0;i<size;i++) {
|
|
state = operator.apply(state, keys[i]);
|
|
}
|
|
return state;
|
|
}
|
|
|
|
#endif
|
|
@Override
|
|
public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) {
|
|
Objects.requireNonNull(operator);
|
|
KEY_TYPE state = EMPTY_KEY_VALUE;
|
|
boolean empty = true;
|
|
for(int i = 0;i<size;i++) {
|
|
if(empty) {
|
|
empty = false;
|
|
state = keys[i];
|
|
continue;
|
|
}
|
|
state = operator.APPLY_KEY_VALUE(state, keys[i]);
|
|
}
|
|
return state;
|
|
}
|
|
|
|
@Override
|
|
public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) {
|
|
Objects.requireNonNull(filter);
|
|
for(int i = 0;i<size;i++) {
|
|
if(filter.test(keys[i])) return keys[i];
|
|
}
|
|
return EMPTY_KEY_VALUE;
|
|
}
|
|
|
|
@Override
|
|
public int count(PREDICATE KEY_GENERIC_TYPE filter) {
|
|
Objects.requireNonNull(filter);
|
|
int result = 0;
|
|
for(int i = 0;i<size;i++) {
|
|
if(filter.test(keys[i])) result++;
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
private class Values extends VALUE_ABSTRACT_COLLECTION VALUE_GENERIC_TYPE {
|
|
#if VALUE_OBJECT
|
|
@Override
|
|
public boolean contains(Object e) {
|
|
return containsValue(e);
|
|
}
|
|
|
|
#else
|
|
@Override
|
|
public boolean contains(VALUE_TYPE e) {
|
|
return containsValue(e);
|
|
}
|
|
|
|
#endif
|
|
@Override
|
|
public boolean add(VALUE_TYPE o) {
|
|
throw new UnsupportedOperationException();
|
|
}
|
|
|
|
@Override
|
|
public VALUE_ITERATOR VALUE_GENERIC_TYPE iterator() {
|
|
return new ValueIterator();
|
|
}
|
|
|
|
@Override
|
|
public int size() {
|
|
return ARRAY_MAP.this.size();
|
|
}
|
|
|
|
@Override
|
|
public void clear() {
|
|
ARRAY_MAP.this.clear();
|
|
}
|
|
|
|
@Override
|
|
public void forEach(VALUE_CONSUMER VALUE_SUPER_GENERIC_TYPE action) {
|
|
Objects.requireNonNull(action);
|
|
for(int i = 0;i<size;action.accept(values[i++]));
|
|
}
|
|
|
|
@Override
|
|
public void forEachIndexed(VALUE_BI_FROM_INT_CONSUMER VALUE_GENERIC_TYPE action) {
|
|
Objects.requireNonNull(action);
|
|
for(int i = 0;i<size;action.accept(i, values[i++]));
|
|
}
|
|
|
|
@Override
|
|
public boolean matchesAny(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
|
|
Objects.requireNonNull(filter);
|
|
for(int i = 0;i<size;i++) {
|
|
if(filter.test(values[i])) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean matchesNone(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
|
|
Objects.requireNonNull(filter);
|
|
for(int i = 0;i<size;i++) {
|
|
if(filter.test(values[i])) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean matchesAll(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
|
|
Objects.requireNonNull(filter);
|
|
for(int i = 0;i<size;i++) {
|
|
if(!filter.test(values[i])) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
#if !VALUE_OBJECT
|
|
@Override
|
|
public VALUE_TYPE reduce(VALUE_TYPE identity, VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
|
|
Objects.requireNonNull(operator);
|
|
VALUE_TYPE state = identity;
|
|
for(int i = 0;i<size;i++) {
|
|
state = operator.APPLY_VALUE(state, values[i]);
|
|
}
|
|
return state;
|
|
}
|
|
|
|
#else
|
|
@Override
|
|
public <VALUE_SPECIAL_TYPE> VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction<VALUE_SPECIAL_TYPE, VALUE_TYPE, VALUE_SPECIAL_TYPE> operator) {
|
|
Objects.requireNonNull(operator);
|
|
VALUE_SPECIAL_TYPE state = identity;
|
|
for(int i = 0;i<size;i++) {
|
|
state = operator.apply(state, values[i]);
|
|
}
|
|
return state;
|
|
}
|
|
|
|
#endif
|
|
@Override
|
|
public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) {
|
|
Objects.requireNonNull(operator);
|
|
VALUE_TYPE state = EMPTY_VALUE;
|
|
boolean empty = true;
|
|
for(int i = 0;i<size;i++) {
|
|
if(empty) {
|
|
empty = false;
|
|
state = values[i];
|
|
continue;
|
|
}
|
|
state = operator.APPLY_VALUE(state, values[i]);
|
|
}
|
|
return state;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
|
|
Objects.requireNonNull(filter);
|
|
for(int i = 0;i<size;i++) {
|
|
if(filter.test(values[i])) return values[i];
|
|
}
|
|
return EMPTY_VALUE;
|
|
}
|
|
|
|
@Override
|
|
public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) {
|
|
Objects.requireNonNull(filter);
|
|
int result = 0;
|
|
for(int i = 0;i<size;i++) {
|
|
if(filter.test(values[i])) result++;
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
|
|
private class FastEntryIterator extends MapIterator implements ObjectListIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> {
|
|
MapEntry entry = new MapEntry();
|
|
|
|
public FastEntryIterator() {}
|
|
public FastEntryIterator(KEY_TYPE from) {
|
|
index = findIndex(from);
|
|
}
|
|
|
|
@Override
|
|
public MAP.Entry KEY_VALUE_GENERIC_TYPE next() {
|
|
entry.index = nextEntry();
|
|
return entry;
|
|
}
|
|
|
|
@Override
|
|
public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() {
|
|
entry.index = previousEntry();
|
|
return entry;
|
|
}
|
|
|
|
@Override
|
|
public void set(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); }
|
|
@Override
|
|
public void add(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); }
|
|
}
|
|
|
|
private class EntryIterator extends MapIterator implements ObjectListIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> {
|
|
MapEntry entry = null;
|
|
|
|
public EntryIterator() {}
|
|
public EntryIterator(KEY_TYPE from) {
|
|
index = findIndex(from);
|
|
if(index == -1) throw new NoSuchElementException();
|
|
}
|
|
|
|
@Override
|
|
public MAP.Entry KEY_VALUE_GENERIC_TYPE next() {
|
|
return entry = new ValueMapEntry(nextEntry());
|
|
}
|
|
|
|
@Override
|
|
public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() {
|
|
return entry = new ValueMapEntry(previousEntry());
|
|
}
|
|
|
|
@Override
|
|
public void remove() {
|
|
super.remove();
|
|
if(entry != null && entry.index != -1) {
|
|
entry.index = -1;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void set(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); }
|
|
@Override
|
|
public void add(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); }
|
|
}
|
|
|
|
private class KeyIterator extends MapIterator implements LIST_ITERATOR KEY_GENERIC_TYPE {
|
|
public KeyIterator() {}
|
|
public KeyIterator(KEY_TYPE element) {
|
|
index = findIndex(element);
|
|
if(index == -1) throw new NoSuchElementException();
|
|
}
|
|
|
|
@Override
|
|
public KEY_TYPE PREVIOUS() {
|
|
return keys[previousEntry()];
|
|
}
|
|
|
|
@Override
|
|
public KEY_TYPE NEXT() {
|
|
return keys[nextEntry()];
|
|
}
|
|
|
|
@Override
|
|
public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); }
|
|
|
|
@Override
|
|
public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); }
|
|
}
|
|
|
|
private class ValueIterator extends MapIterator implements VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE {
|
|
@Override
|
|
public VALUE_TYPE VALUE_PREVIOUS() {
|
|
return values[previousEntry()];
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE VALUE_NEXT() {
|
|
return values[nextEntry()];
|
|
}
|
|
|
|
@Override
|
|
public void set(VALUE_TYPE e) { throw new UnsupportedOperationException(); }
|
|
|
|
@Override
|
|
public void add(VALUE_TYPE e) { throw new UnsupportedOperationException(); }
|
|
}
|
|
|
|
private class MapIterator {
|
|
int index;
|
|
int lastReturned = -1;
|
|
|
|
public boolean hasNext() {
|
|
return index < size;
|
|
}
|
|
|
|
public boolean hasPrevious() {
|
|
return index > 0;
|
|
}
|
|
|
|
public int nextIndex() {
|
|
return index;
|
|
}
|
|
|
|
public int previousIndex() {
|
|
return index-1;
|
|
}
|
|
|
|
public void remove() {
|
|
if(lastReturned == -1) throw new IllegalStateException();
|
|
removeIndex(lastReturned);
|
|
if(lastReturned < index)
|
|
index--;
|
|
lastReturned = -1;
|
|
}
|
|
|
|
public int previousEntry() {
|
|
if(!hasPrevious()) throw new NoSuchElementException();
|
|
index--;
|
|
return (lastReturned = index);
|
|
}
|
|
|
|
public int nextEntry() {
|
|
if(!hasNext()) throw new NoSuchElementException();
|
|
lastReturned = index;
|
|
return index++;
|
|
}
|
|
|
|
public int skip(int amount) {
|
|
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
|
|
int steps = Math.min(amount, size() - index);
|
|
index += steps;
|
|
if(steps > 0) lastReturned = Math.min(index-1, size()-1);
|
|
return steps;
|
|
}
|
|
|
|
public int back(int amount) {
|
|
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
|
|
int steps = Math.min(amount, index);
|
|
index -= steps;
|
|
if(steps > 0) lastReturned = Math.min(index, size()-1);
|
|
return steps;
|
|
}
|
|
}
|
|
|
|
protected class ValueMapEntry extends MapEntry {
|
|
protected KEY_TYPE key;
|
|
protected VALUE_TYPE value;
|
|
|
|
public ValueMapEntry(int index) {
|
|
super(index);
|
|
key = keys[index];
|
|
value = values[index];
|
|
}
|
|
|
|
@Override
|
|
public KEY_TYPE ENTRY_KEY() {
|
|
return key;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE ENTRY_VALUE() {
|
|
return value;
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE setValue(VALUE_TYPE value) {
|
|
this.value = value;
|
|
return super.setValue(value);
|
|
}
|
|
}
|
|
|
|
private class MapEntry implements MAP.Entry KEY_VALUE_GENERIC_TYPE, Map.Entry<CLASS_TYPE, CLASS_VALUE_TYPE> {
|
|
int index = -1;
|
|
|
|
public MapEntry() {}
|
|
public MapEntry(int index) {
|
|
this.index = index;
|
|
}
|
|
|
|
void set(int index) {
|
|
this.index = index;
|
|
}
|
|
|
|
@Override
|
|
public KEY_TYPE ENTRY_KEY() {
|
|
return keys[index];
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE ENTRY_VALUE() {
|
|
return values[index];
|
|
}
|
|
|
|
@Override
|
|
public VALUE_TYPE setValue(VALUE_TYPE value) {
|
|
VALUE_TYPE oldValue = values[index];
|
|
values[index] = value;
|
|
return oldValue;
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object obj) {
|
|
if(obj instanceof Map.Entry) {
|
|
if(obj instanceof MAP.Entry) {
|
|
MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)obj;
|
|
return KEY_EQUALS(keys[index], entry.ENTRY_KEY()) && VALUE_EQUALS(ENTRY_VALUE(), entry.ENTRY_VALUE());
|
|
}
|
|
Map.Entry<?, ?> entry = (Map.Entry<?, ?>)obj;
|
|
Object key = entry.getKey();
|
|
Object value = entry.getValue();
|
|
#if TYPE_OBJECT && VALUE_OBJECT
|
|
return KEY_EQUALS(ENTRY_KEY(), key) && VALUE_EQUALS(ENTRY_VALUE(), value);
|
|
#else if TYPE_OBJECT
|
|
return value instanceof CLASS_VALUE_TYPE && KEY_EQUALS(ENTRY_KEY(), key) && VALUE_EQUALS(ENTRY_VALUE(), CLASS_TO_VALUE(value));
|
|
#else if VALUE_OBJECT
|
|
return key instanceof CLASS_TYPE && KEY_EQUALS(ENTRY_KEY(), CLASS_TO_KEY(key)) && VALUE_EQUALS(ENTRY_VALUE(), value);
|
|
#else
|
|
return key instanceof CLASS_TYPE && value instanceof CLASS_VALUE_TYPE && KEY_EQUALS(ENTRY_KEY(), CLASS_TO_KEY(key)) && VALUE_EQUALS(ENTRY_VALUE(), CLASS_TO_VALUE(value));
|
|
#endif
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return KEY_TO_HASH(ENTRY_KEY()) ^ VALUE_TO_HASH(ENTRY_VALUE());
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return KEY_TO_STRING(ENTRY_KEY()) + "=" + VALUE_TO_STRING(ENTRY_VALUE());
|
|
}
|
|
}
|
|
} |