Added New Utility methods.

-Added: bulk putIfAbsent & AddTo function
-Added: ArrayPut method simplify adding arrays into a map.
-Added: Bulk Replace function that uses a "Map" instead of a lambda
-Added: Bulk Merge function
This commit is contained in:
Speiger 2021-05-22 05:26:44 +02:00
parent b9d49aea65
commit a319e0136a
3 changed files with 90 additions and 0 deletions

View File

@ -211,6 +211,7 @@ public class GlobalVariables
addSimpleMapper("APPLY_VALUE", valueType.isObject() ? "apply" : "applyAs"+valueType.getNonFileType());
addSimpleMapper("APPLY_CAST", "applyAs"+type.getCustomJDKType().getNonFileType());
addSimpleMapper("APPLY", type.isObject() ? "apply" : "applyAs"+type.getNonFileType());
addFunctionValueMapper("BULK_MERGE", "mergeAll");
addFunctionValueMappers("COMPUTE_IF_ABSENT", "compute%sIfAbsent");
addFunctionValueMappers("COMPUTE_IF_PRESENT", "compute%sIfPresent");
addFunctionValueMapper("COMPUTE", "compute");

View File

@ -24,6 +24,7 @@ import speiger.src.collections.objects.collections.ObjectIterator;
#if !TYPE_OBJECT
import speiger.src.collections.objects.sets.ObjectSet;
#endif
import speiger.src.collections.utils.SanityChecks;
/**
* A Base Implementation of a Type Specific Map to reduce boxing/unboxing
@ -45,6 +46,14 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
return this;
}
#if VALUE_PRIMITIVES
@Override
public void addToAll(MAP KEY_VALUE_GENERIC_TYPE m) {
for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : MAPS.fastIterable(m))
addTo(entry.ENTRY_KEY(), entry.ENTRY_VALUE());
}
#endif
@Override
public void putAll(MAP KEY_VALUE_GENERIC_TYPE m) {
for(ObjectIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> iter = MAPS.fastIterator(m);iter.hasNext();) {
@ -60,6 +69,19 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
else super.putAll(m);
}
@Override
public void putAll(KEY_TYPE[] keys, VALUE_TYPE[] values, int offset, int size) {
SanityChecks.checkArrayCapacity(keys.length, offset, size);
SanityChecks.checkArrayCapacity(values.length, offset, size);
for(int i = 0;i<size;i++) put(keys[i], values[i]);
}
@Override
public void putAllIfAbsent(MAP KEY_VALUE_GENERIC_TYPE m) {
for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : MAPS.fastIterable(m))
putIfAbsent(entry.ENTRY_KEY(), entry.ENTRY_VALUE());
}
#if TYPE_OBJECT
@Override
@ -114,6 +136,12 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
return curValue;
}
@Override
public void REPLACE_VALUES(MAP KEY_VALUE_GENERIC_TYPE m) {
for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : MAPS.fastIterable(m))
replace(entry.ENTRY_KEY(), entry.ENTRY_VALUE());
}
@Override
public void REPLACE_VALUES(UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) {
Objects.requireNonNull(mappingFunction);
@ -178,6 +206,18 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
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 : MAPS.fastIterable(m)) {
KEY_TYPE key = entry.ENTRY_KEY();
VALUE_TYPE oldValue = GET_VALUE(key);
VALUE_TYPE newValue = VALUE_EQUALS(oldValue, getDefaultReturnValue()) ? entry.ENTRY_VALUE() : mappingFunction.APPLY_VALUE(oldValue, entry.ENTRY_VALUE());
if(VALUE_EQUALS(newValue, getDefaultReturnValue())) remove(key);
else put(key, newValue);
}
}
#if TYPE_OBJECT
@Override
public CLASS_VALUE_TYPE get(Object key) {

View File

@ -50,6 +50,30 @@ public interface MAP KEY_VALUE_GENERIC_TYPE extends Map<CLASS_TYPE, CLASS_VALUE_
* @see Map#put(Object, Object)
*/
public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value);
/**
* Type Specific array method to bulk add elements into a map without creating a wrapper and increasing performances
* @param keys the keys that should be added
* @param values the values that should be added
* @see Map#putAll(Map)
* @throws IllegalStateException if the arrays are not the same size
*/
public default void putAll(KEY_TYPE[] keys, VALUE_TYPE[] values) {
if(keys.length != values.length) throw new IllegalStateException("Array sizes do not match");
putAll(keys, values, 0, keys.length);
}
/**
* Type Specific array method to bulk add elements into a map without creating a wrapper and increasing performances
* @param keys the keys that should be added
* @param values the values that should be added
* @param offset where the to start in the array
* @param size how many elements should be added
* @see Map#putAll(Map)
* @throws IllegalStateException if the arrays are not within the range
*/
public void putAll(KEY_TYPE[] keys, VALUE_TYPE[] values, int offset, int size);
/**
* Type Specific method to reduce boxing/unboxing of values
* @param key the key that should be inserted,
@ -59,6 +83,12 @@ public interface MAP KEY_VALUE_GENERIC_TYPE extends Map<CLASS_TYPE, CLASS_VALUE_
*/
public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value);
/**
* Type-Specific bulk put method put elements into the map if not present.
* @param m elements that should be added if not present.
*/
public void putAllIfAbsent(MAP KEY_VALUE_GENERIC_TYPE m);
#if VALUE_PRIMITIVES
/**
* A Helper method to add a primitives together. If key is not present then this functions as a put.
@ -68,6 +98,12 @@ public interface MAP KEY_VALUE_GENERIC_TYPE extends Map<CLASS_TYPE, CLASS_VALUE_
*/
public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value);
/**
* A Helper method to bulk add primitives together.
* @param m the values that should be added/inserted
*/
public void addToAll(MAP KEY_VALUE_GENERIC_TYPE m);
#endif
/**
* Type Specific function for the bull putting of values
@ -182,6 +218,12 @@ public interface MAP KEY_VALUE_GENERIC_TYPE extends Map<CLASS_TYPE, CLASS_VALUE_
* @note this fails if the value is not present
*/
public VALUE_TYPE replace(KEY_TYPE key, VALUE_TYPE value);
/**
* Type-Specific bulk replace method. Could be seen as putAllIfPresent
* @param m elements that should be replaced.
*/
public void REPLACE_VALUES(MAP KEY_VALUE_GENERIC_TYPE m);
/**
* A Type Specific mass replace method to reduce boxing/unboxing
* @param mappingFunction operation to replace all values
@ -218,6 +260,13 @@ public interface MAP KEY_VALUE_GENERIC_TYPE extends Map<CLASS_TYPE, CLASS_VALUE_
* @note if the result matches the default return value then the key is removed from the map
*/
public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction);
/**
* A Bulk method for merging Maps.
* @param m the entries that should be bulk added
* @param mappingFunction the operator that should generate the new Value
* @note if the result matches the default return value then the key is removed from the map
*/
public void BULK_MERGE(MAP KEY_VALUE_GENERIC_TYPE m, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction);
#if !TYPE_OBJECT || !VALUE_OBJECT
@Override