Added subFrom (which is the inverse of addTo)

This commit is contained in:
Speiger 2021-12-25 20:07:37 +01:00
parent 8f32b2d887
commit 7a4a0f05d1
12 changed files with 142 additions and 9 deletions

View File

@ -1,5 +1,10 @@
# Changelog of versions # Changelog of versions
### Version 0.5.3
- Added: OrderedMap/Set
- Added: Deprecation to Functions that are specific to Ordered interfaces in the SortedMap/Set
- Added: subFrom to Maps which is the counterpart of the addTo method
### Version 0.5.2 ### Version 0.5.2
- Fixed: Bugs with Queues starting with the wrong size - Fixed: Bugs with Queues starting with the wrong size
- Fixed: ArrayGrowth for Queues was +1 instead of +50% - Fixed: ArrayGrowth for Queues was +1 instead of +50%

View File

@ -18,7 +18,7 @@ repositories {
} }
archivesBaseName = 'Primitive Collections' archivesBaseName = 'Primitive Collections'
version = '0.5.2'; version = '0.5.3';
sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8' sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8'

View File

@ -1130,7 +1130,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
} }
protected void grow(int capacity) { protected void grow(int capacity) {
if(capacity < data.length) return; if(capacity <= data.length) return;
data = Arrays.copyOf(data, data == ARRAYS.EMPTY_ARRAY ? Math.max(DEFAULT_ARRAY_SIZE, capacity) : (int)Math.max(Math.min((long)data.length + (data.length >> 1), SanityChecks.MAX_ARRAY_SIZE), capacity)); data = Arrays.copyOf(data, data == ARRAYS.EMPTY_ARRAY ? Math.max(DEFAULT_ARRAY_SIZE, capacity) : (int)Math.max(Math.min((long)data.length + (data.length >> 1), SanityChecks.MAX_ARRAY_SIZE), capacity));
} }

View File

@ -286,6 +286,15 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
return oldValue; 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 #endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
@Override @Override

View File

@ -261,6 +261,15 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE
return oldValue; 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 #endif
#if !TYPE_OBJECT #if !TYPE_OBJECT
@Override @Override

View File

@ -25,6 +25,7 @@ import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR;
#endif #endif
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR; import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
import speiger.src.collections.PACKAGE.maps.interfaces.SORTED_MAP; import speiger.src.collections.PACKAGE.maps.interfaces.SORTED_MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.ORDERED_MAP;
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.sets.SORTED_SET; import speiger.src.collections.PACKAGE.sets.SORTED_SET;
@ -84,7 +85,7 @@ import speiger.src.collections.utils.SanityChecks;
* @Type(T) * @Type(T)
* @ValueType(V) * @ValueType(V)
*/ */
public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE, ORDERED_MAP KEY_VALUE_GENERIC_TYPE
{ {
/** The Backing keys array */ /** The Backing keys array */
protected transient KEY_TYPE[] keys; protected transient KEY_TYPE[] keys;
@ -297,11 +298,11 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
@Override @Override
public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
#if VALUE_PRIMITIVES #if VALUE_PRIMITIVES
@Override @Override
public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
#endif #endif
@Override @Override
public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }

View File

@ -222,6 +222,16 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
return oldValue; 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 #endif
@Override @Override
public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) {

View File

@ -180,6 +180,18 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
return getDefaultReturnValue(); return getDefaultReturnValue();
} }
@Override
public VALUE_TYPE subFrom(T key, VALUE_TYPE value) {
int index = key.ordinal();
if(!isSet(index)) return getDefaultReturnValue();
VALUE_TYPE oldValue = values[index];
values[index] -= value;
if(value < 0 ? (values[index] >= getDefaultReturnValue()) : (values[index] <= getDefaultReturnValue())) {
clear(index);
values[index] = EMPTY_VALUE;
}
return oldValue;
}
#endif #endif
@Override @Override
public boolean containsKey(Object key) { public boolean containsKey(Object key) {

View File

@ -306,9 +306,6 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
#if VALUE_PRIMITIVES #if VALUE_PRIMITIVES
@Override @Override
public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) {
#if TYPE_OBJECT
validate(key);
#endif
if(tree == null) { if(tree == null) {
tree = first = last = new NodeKV_BRACES(key, value, null); tree = first = last = new NodeKV_BRACES(key, value, null);
size++; size++;
@ -341,6 +338,30 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
return getDefaultReturnValue(); return getDefaultReturnValue();
} }
@Override
public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) {
if(tree == null) return getDefaultReturnValue();
int compare = 0;
Node KEY_VALUE_GENERIC_TYPE parent = tree;
while(true) {
if((compare = compare(key, parent.key)) == 0)
{
VALUE_TYPE oldValue = parent.subFrom(value);
if(value < 0 ? (parent.value >= getDefaultReturnValue()) : (parent.value <= getDefaultReturnValue())) removeNode(parent);
return oldValue;
}
if(compare < 0) {
if(parent.left == null) break;
parent = parent.left;
}
else if(compare > 0) {
if(parent.right == null) break;
parent = parent.right;
}
}
return getDefaultReturnValue();
}
#endif #endif
@Override @Override
public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
@ -1604,6 +1625,12 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
return map.addTo(key, value); return map.addTo(key, value);
} }
@Override
public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) {
if(!inRange(key)) throw new IllegalArgumentException("key out of range");
return map.subFrom(key, value);
}
#endif #endif
#if TYPE_OBJECT #if TYPE_OBJECT
@Override @Override
@ -2674,6 +2701,11 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
return oldValue; return oldValue;
} }
VALUE_TYPE subFrom(VALUE_TYPE value) {
VALUE_TYPE oldValue = this.value;
this.value -= value;
return oldValue;
}
#endif #endif
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {

View File

@ -341,6 +341,30 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
return getDefaultReturnValue(); return getDefaultReturnValue();
} }
@Override
public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) {
if(tree == null) return getDefaultReturnValue();
int compare = 0;
Node KEY_VALUE_GENERIC_TYPE parent = tree;
while(true) {
if((compare = compare(key, parent.key)) == 0)
{
VALUE_TYPE oldValue = parent.subFrom(value);
if(value < 0 ? (parent.value >= getDefaultReturnValue()) : (parent.value <= getDefaultReturnValue())) removeNode(parent);
return oldValue;
}
if(compare < 0) {
if(parent.left == null) break;
parent = parent.left;
}
else if(compare > 0) {
if(parent.right == null) break;
parent = parent.right;
}
}
return getDefaultReturnValue();
}
#endif #endif
@Override @Override
public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
@ -1659,6 +1683,12 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
return map.addTo(key, value); return map.addTo(key, value);
} }
@Override
public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) {
if(!inRange(key)) throw new IllegalArgumentException("key out of range");
return map.subFrom(key, value);
}
#endif #endif
#if TYPE_OBJECT #if TYPE_OBJECT
@Override @Override
@ -2731,6 +2761,12 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
return oldValue; return oldValue;
} }
VALUE_TYPE subFrom(VALUE_TYPE value) {
VALUE_TYPE oldValue = this.value;
this.value -= value;
return oldValue;
}
#endif #endif
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {

View File

@ -179,6 +179,17 @@ public interface MAP KEY_VALUE_GENERIC_TYPE extends Map<CLASS_TYPE, CLASS_VALUE_
*/ */
public void addToAll(MAP KEY_VALUE_GENERIC_TYPE m); public void addToAll(MAP KEY_VALUE_GENERIC_TYPE m);
/**
* A Helper method to subtract from primitive from each other. If the key is not present it will just return the defaultValue
* How the implementation works is that it will subtract from the current value (if not present it will do nothing) and fence it to the {@link #getDefaultReturnValue()}
* If the fence is reached the element will be automaticall removed
*
* @param key that should be subtract from
* @param value that should be subtract
* @return the last present or default return value
*/
public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value);
#endif #endif
/** /**
* Type Specific function for the bull putting of values * Type Specific function for the bull putting of values

View File

@ -298,6 +298,8 @@ public class MAPS
#if VALUE_PRIMITIVES #if VALUE_PRIMITIVES
@Override @Override
public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
#endif #endif
@Override @Override
public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { throw new UnsupportedOperationException(); } public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { throw new UnsupportedOperationException(); }
@ -346,6 +348,8 @@ public class MAPS
#if VALUE_PRIMITIVES #if VALUE_PRIMITIVES
@Override @Override
public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
#endif #endif
@Override @Override
public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { throw new UnsupportedOperationException(); } public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { throw new UnsupportedOperationException(); }
@ -577,6 +581,8 @@ public class MAPS
#if VALUE_PRIMITIVES #if VALUE_PRIMITIVES
@Override @Override
public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
@Override
public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); }
#endif #endif
@Override @Override
public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { throw new UnsupportedOperationException(); } public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { throw new UnsupportedOperationException(); }
@ -926,6 +932,8 @@ public class MAPS
#if VALUE_PRIMITIVES #if VALUE_PRIMITIVES
@Override @Override
public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.addTo(key, value); } } public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.addTo(key, value); } }
@Override
public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { synchronized(mutex) { return map.subFrom(key, value); } }
public void addToAll(MAP KEY_VALUE_GENERIC_TYPE m) { synchronized(mutex) { map.addToAll(m); } } public void addToAll(MAP KEY_VALUE_GENERIC_TYPE m) { synchronized(mutex) { map.addToAll(m); } }
#endif #endif
@Override @Override