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:
2021-05-22 05:26:44 +02:00
parent b9d49aea65
commit a319e0136a
3 changed files with 90 additions and 0 deletions
@@ -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) {