New Features.

-Added: removeOrDefault function
-Added: New functions into implementations
This commit is contained in:
Speiger 2021-05-22 05:50:40 +02:00
parent a319e0136a
commit 7fcfecf308
9 changed files with 98 additions and 2 deletions

View File

@ -291,6 +291,13 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
return removeIndex(slot);
}
@Override
public VALUE_TYPE REMOVE_KEYOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) {
int slot = findIndex(key);
if(slot < 0) return defaultValue;
return removeIndex(slot);
}
@Override
@Deprecated
public CLASS_VALUE_TYPE remove(Object key) {

View File

@ -262,6 +262,13 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE
return removeIndex(slot);
}
@Override
public VALUE_TYPE REMOVE_KEYOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) {
int slot = findIndex(key);
if(slot < 0) return defaultValue;
return removeIndex(slot);
}
@Override
@Deprecated
public CLASS_VALUE_TYPE remove(Object key) {

View File

@ -355,6 +355,15 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
return value;
}
@Override
public VALUE_TYPE REMOVE_KEYOrDefault(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) {
@ -777,6 +786,16 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
return value;
}
@Override
public VALUE_TYPE REMOVE_KEYOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) {
int index = findIndex(key);
if(index < 0) return defaultValue;
VALUE_TYPE value = values[index];
removeIndex(index);
length--;
return value;
}
#if !TYPE_OBJECT || !VALUE_OBJECT
@Override
public boolean remove(KEY_TYPE key, VALUE_TYPE value) {

View File

@ -124,6 +124,16 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
return result;
}
@Override
public VALUE_TYPE remOrDefault(T key, VALUE_TYPE defaultValue) {
int index = key.ordinal();
if(!isSet(index)) return defaultValue;
clear(index);
VALUE_TYPE result = values[index];
values[index] = EMPTY_VALUE;
return result;
}
#if VALUE_OBJECT
@Override
public boolean remove(Object key, Object value) {

View File

@ -425,6 +425,15 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
return value;
}
@Override
public VALUE_TYPE REMOVE_KEYOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) {
Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key);
if(entry == null) return defaultValue;
VALUE_TYPE value = entry.value;
removeNode(entry);
return value;
}
#if TYPE_OBJECT && VALUE_OBJECT
@Override
public boolean remove(Object key, Object value) {
@ -1117,6 +1126,11 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
return inRange(key) ? m.REMOVE_KEY(key) : getDefaultReturnValue();
}
@Override
public VALUE_TYPE REMOVE_KEYOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) {
return inRange(key) ? m.REMOVE_KEY(key) : defaultValue;
}
#if TYPE_OBJECT && VALUE_OBJECT
@Override
public boolean remove(Object key, Object value) {

View File

@ -424,6 +424,15 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
return value;
}
@Override
public VALUE_TYPE REMOVE_KEYOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) {
Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key);
if(entry == null) return defaultValue;
VALUE_TYPE value = entry.value;
removeNode(entry);
return value;
}
#if TYPE_OBJECT && VALUE_OBJECT
@Override
public boolean remove(Object key, Object value) {
@ -1174,6 +1183,11 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
return inRange(key) ? m.REMOVE_KEY(key) : getDefaultReturnValue();
}
@Override
public VALUE_TYPE REMOVE_KEYOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) {
return inRange(key) ? m.REMOVE_KEY(key) : defaultValue;
}
#if TYPE_OBJECT && VALUE_OBJECT
@Override
public boolean remove(Object key, Object value) {

View File

@ -199,8 +199,16 @@ public interface MAP KEY_VALUE_GENERIC_TYPE extends Map<CLASS_TYPE, CLASS_VALUE_
return key instanceof CLASS_TYPE && value instanceof CLASS_VALUE_TYPE && remove(CLASS_TO_KEY(key), CLASS_TO_VALUE(value));
#endif
}
#endif
#endif
/**
* Type-Specific Remove function with a default return value if wanted.
* @see Map#remove(Object, Object)
* @param key the element that should be removed
* @param defaultValue the value that should be returned if the entry doesn't exist
* @return the value that was removed or default value
*/
public VALUE_TYPE REMOVE_KEYOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue);
/**
* A Type Specific replace method to replace an existing value
* @param key the element that should be searched for

View File

@ -266,6 +266,8 @@ public class MAPS
#endif
@Override
public VALUE_TYPE REMOVE_KEY(KEY_TYPE key) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE REMOVE_KEYOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { throw new UnsupportedOperationException(); }
#if !TYPE_OBJECT || !VALUE_OBJECT
@Override
public boolean remove(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
@ -310,6 +312,8 @@ public class MAPS
#endif
@Override
public VALUE_TYPE REMOVE_KEY(KEY_TYPE key) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE REMOVE_KEYOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { throw new UnsupportedOperationException(); }
#if !TYPE_OBJECT || !VALUE_OBJECT
@Override
public boolean remove(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
@ -486,6 +490,8 @@ public class MAPS
#endif
@Override
public VALUE_TYPE REMOVE_KEY(KEY_TYPE key) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE REMOVE_KEYOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { throw new UnsupportedOperationException(); }
#if !TYPE_OBJECT || !VALUE_OBJECT
@Override
public boolean remove(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
@ -768,14 +774,19 @@ public class MAPS
public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.put(key, value); } }
@Override
public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.putIfAbsent(key, value); } }
public void putAllIfAbsent(MAP KEY_VALUE_GENERIC_TYPE m) { synchronized(mutex) { map.putAllIfAbsent(m); } }
#if VALUE_PRIMITIVES
@Override
public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.addTo(key, value); } }
public void addToAll(MAP KEY_VALUE_GENERIC_TYPE m) { synchronized(mutex) { map.addToAll(m); } }
#endif
@Override
public void putAll(MAP KEY_VALUE_GENERIC_TYPE m) { synchronized(mutex) { map.putAll(m); } }
@Override
public void putAll(Map<? extends CLASS_TYPE, ? extends CLASS_VALUE_TYPE> m) { synchronized(mutex) { map.putAll(m); } }
@Override
public void putAll(KEY_TYPE[] keys, VALUE_TYPE[] values, int offset, int size) { synchronized(mutex) { map.putAll(keys, values, offset, size); } }
#if !TYPE_OBJECT
@Override
public boolean containsKey(KEY_TYPE key) { synchronized(mutex) { return map.containsKey(key); } }
@ -788,6 +799,8 @@ public class MAPS
public VALUE_TYPE GET_VALUE(KEY_TYPE key) { synchronized(mutex) { return map.GET_VALUE(key); } }
@Override
public VALUE_TYPE REMOVE_KEY(KEY_TYPE key) { synchronized(mutex) { return map.REMOVE_KEY(key); } }
@Override
public VALUE_TYPE REMOVE_KEYOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { synchronized(mutex) { return map.REMOVE_KEYOrDefault(key, defaultValue); } }
#if !TYPE_OBJECT || !VALUE_OBJECT
@Override
public boolean remove(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.remove(key, value); } }
@ -797,6 +810,8 @@ public class MAPS
@Override
public VALUE_TYPE replace(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.replace(key, value); } }
@Override
public void REPLACE_VALUES(MAP KEY_VALUE_GENERIC_TYPE m) { synchronized(mutex) { map.REPLACE_VALUES(m); } }
@Override
public void REPLACE_VALUES(UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { map.REPLACE_VALUES(mappingFunction); } }
@Override
public VALUE_TYPE COMPUTE(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { return map.COMPUTE(key, mappingFunction); } }
@ -806,6 +821,8 @@ public class MAPS
public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { return map.COMPUTE_IF_PRESENT(key, mappingFunction); } }
@Override
public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { return map.MERGE(key, value, mappingFunction); } }
@Override
public void BULK_MERGE(MAP KEY_VALUE_GENERIC_TYPE m, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { synchronized(mutex) { map.BULK_MERGE(m, mappingFunction); } }
#if !TYPE_OBJECT || !VALUE_OBJECT
@Override
public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { synchronized(mutex) { return map.getOrDefault(key, defaultValue); } }