Added Maps

-Added: ArrayMap/(Custom/Linked/HashMap)/TreeMap(RB/AVL)/EnumMap
-Reworked: Some Variables and how Types are chosen.
This commit is contained in:
Speiger 2021-01-28 20:42:29 +01:00
parent c20945dad5
commit f60a287868
24 changed files with 9342 additions and 83 deletions

View File

@ -26,7 +26,7 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>

View File

@ -6,15 +6,9 @@ tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
apply plugin: 'java'
apply plugin: 'eclipse'
eclipse {
classpath {
downloadJavadoc = true
downloadSources = true
}
}
repositories {
jcenter()
flatDir {
@ -22,6 +16,28 @@ repositories {
}
}
sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8'
eclipse {
classpath {
downloadJavadoc = true
downloadSources = true
file {
whenMerged {
//Enforce a custom container and allowing access to the sun.misc package which is nessesary for EnumMaps
entries.find{ it.kind == 'con' && it.path.startsWith('org.eclipse.jdt')}.path = 'org.eclipse.jdt.launching.JRE_CONTAINER';
}
}
}
}
task generateSource(type: JavaExec) {
group = 'internal'
description = 'Builds the sourcecode'
classpath = sourceSets.main.runtimeClasspath
main = 'speiger.src.builder.example.TestBuilder'
}
dependencies {
compile 'SimpleCodeGenerator:Simple Code Generator:1.0.1'
testImplementation 'junit:junit:4.12'

Binary file not shown.

View File

@ -48,14 +48,16 @@ public class GlobalVariables
addSimpleMapper(" NO_GENERIC_TYPE", type.isObject() ? "<?>" : "");
addSimpleMapper(" KEY_COMPAREABLE_TYPE", type.isObject() ? "<"+type.getKeyType()+" extends Comparable<T>>" : "");
addSimpleMapper(" KEY_SUPER_GENERIC_TYPE", type.isObject() ? "<? super "+type.getKeyType()+">" : "");
addSimpleMapper(" VALUE_SUPER_GENERIC_TYPE", valueType.isObject() ? "<? super "+valueType.getKeyType()+">" : "");
addSimpleMapper(" VALUE_SUPER_GENERIC_TYPE", valueType.isObject() ? "<? super "+valueType.getValueType()+">" : "");
addSimpleMapper(" KEY_VALUE_SUPER_GENERIC_TYPE", type.isObject() ? (valueType.isObject() ? "<? super "+type.getKeyType()+", ? super "+valueType.getValueType()+">" : "<? super "+type.getKeyType()+">") : (valueType.isObject() ? "<? super "+valueType.getValueType()+">" : ""));
addSimpleMapper(" KEY_ENUM_VALUE_GENERIC_TYPE", type.isObject() ? (valueType.isObject() ? "<"+type.getKeyType()+" extends Enum<"+type.getKeyType()+">, "+valueType.getValueType()+">" : "<"+type.getKeyType()+" extends Enum<"+type.getKeyType()+">>") : (valueType.isObject() ? "<"+valueType.getValueType()+">" : ""));
addSimpleMapper(" KEY_VALUE_ENUM_GENERIC_TYPE", type.isObject() ? (valueType.isObject() ? "<"+type.getKeyType()+", "+valueType.getValueType()+" extends Enum<"+valueType.getValueType()+">>" : "<"+type.getKeyType()+">") : (valueType.isObject() ? "<"+valueType.getValueType()+" extends Enum<"+valueType.getValueType()+">>" : ""));
addSimpleMapper(" GENERIC_KEY_BRACES", type.isObject() ? " <"+type.getKeyType()+">" : "");
addSimpleMapper(" GENERIC_VALUE_BRACES", type.isObject() ? " <"+valueType.getValueType()+">" : "");
addSimpleMapper(" GENERIC_KEY_VALUE_BRACES", type.isObject() ? (valueType.isObject() ? " <"+type.getKeyType()+", "+valueType.getValueType()+">" : " <"+type.getKeyType()+">") : (valueType.isObject() ? " <"+valueType.getValueType()+">" : ""));
addSimpleMapper(" COMPAREABLE_KEY_BRACES", type.isObject() ? " <"+type.getKeyType()+" extends Comparable<T>>" : "");
addSimpleMapper("KV_BRACES", type.isObject() || valueType.isObject() ? "<>" : "");
addSimpleMapper("BRACES", type.isObject() ? "<>" : "");
if(type.needsCustomJDKType())
{
@ -92,7 +94,7 @@ public class GlobalVariables
addInjectMapper(fix+"_TO_OBJ", type.isObject() ? "%s" : type.getClassType(value)+".valueOf(%s)").removeBraces();
addInjectMapper("OBJ_TO_"+fix, type.isObject() ? "%s" : "%s."+type.getKeyType(value)+"Value()").removeBraces();
addInjectMapper("CLASS_TO_"+fix, "(("+type.getClassType(value)+")%s)."+type.getKeyType(value)+"Value()").removeBraces();
addInjectMapper("CLASS_TO_"+fix, type.isObject() ? "("+type.getKeyType(value)+")%s" : "(("+type.getClassType(value)+")%s)."+type.getKeyType(value)+"Value()").removeBraces();
addInjectMapper(fix+"_TO_HASH", type.isObject() ? "%s.hashCode()" : type.getClassType(value)+".hashCode(%s)").removeBraces();
addInjectMapper(fix+"_TO_STRING", type.isObject() ? "%s.toString()" : type.getClassType(value)+".toString(%s)").removeBraces();
@ -126,6 +128,14 @@ public class GlobalVariables
addClassMapper("LINKED_HASH_SET", "LinkedOpenHashSet");
addClassMapper("CUSTOM_HASH_SET", "OpenCustomHashSet");
addClassMapper("HASH_SET", "OpenHashSet");
addBiClassMapper("LINKED_CUSTOM_HASH_MAP", "LinkedOpenCustomHashMap", "2");
addBiClassMapper("LINKED_HASH_MAP", "LinkedOpenHashMap", "2");
addBiClassMapper("CUSTOM_HASH_MAP", "OpenCustomHashMap", "2");
addBiClassMapper("AVL_TREE_MAP", "AVLTreeMap", "2");
addBiClassMapper("RB_TREE_MAP", "RBTreeMap", "2");
addFunctionValueMappers("ENUM_MAP", valueType.isObject() ? "Enum2ObjectMap" : "Enum2%sMap");
addBiClassMapper("HASH_MAP", "OpenHashMap", "2");
addBiClassMapper("ARRAY_MAP", "ArrayMap", "2");
addClassMapper("RB_TREE_SET", "RBTreeSet");
addClassMapper("AVL_TREE_SET", "AVLTreeSet");
addClassMapper("ARRAY_SET", "ArraySet");
@ -155,6 +165,8 @@ public class GlobalVariables
addBiClassMapper("FUNCTION", "Function", "2");
addClassMapper("LIST_ITER", "ListIter");
addClassMapper("LIST", "List");
addBiClassMapper("NAVIGABLE_MAP", "NavigableMap", "2");
addBiClassMapper("SORTED_MAP", "SortedMap", "2");
addBiClassMapper("MAP", "Map", "2");
addClassMapper("NAVIGABLE_SET", "NavigableSet");
addClassMapper("PRIORITY_QUEUE", "PriorityQueue");
@ -166,6 +178,10 @@ public class GlobalVariables
addBiClassMapper("UNARY_OPERATOR", "UnaryOperator", "");
if(type.isObject())
{
if(!valueType.isObject())
{
addSimpleMapper("VALUE_CONSUMER", valueType.getFileType()+"Consumer");
}
addSimpleMapper("CONSUMER", "Consumer");
addSimpleMapper("COMPARATOR", "Comparator");
addSimpleMapper("IARRAY", "IObjectArray");
@ -191,13 +207,19 @@ public class GlobalVariables
addFunctionMapper("ENQUEUE", "enqueue");
addFunctionMapper("DEQUEUE_LAST", "dequeueLast");
addFunctionMapper("DEQUEUE", "dequeue");
addFunctionMappers("ENTRY_KEY", "get%sKey");
addFunctionValueMappers("ENTRY_VALUE", "get%sValue");
addFunctionMappers("POLL_FIRST_ENTRY_KEY", "pollFirst%sKey");
addFunctionMappers("POLL_LAST_ENTRY_KEY", "pollLast%sKey");
addFunctionMapper("POLL_FIRST_KEY", "pollFirst");
addFunctionMapper("POLL_LAST_KEY", "pollLast");
addFunctionMappers("FIRST_ENTRY_KEY", "first%sKey");
addFunctionValueMappers("FIRST_ENTRY_VALUE", "first%sValue");
addFunctionMapper("FIRST_KEY", "first");
addFunctionMappers("LAST_ENTRY_KEY", "last%sKey");
addFunctionValueMappers("LAST_ENTRY_VALUE", "last%sValue");
addFunctionMappers("ENTRY_KEY", "get%sKey");
addFunctionValueMappers("ENTRY_VALUE", "get%sValue");
addFunctionMapper("GET_KEY", "get");
addFunctionValueMapper("GET_VALUE", "get");
addFunctionValueMapper("GET_VALUE", valueType.isObject() ? "getObject" : "get");
addFunctionMapper("LAST_KEY", "last");
addFunctionValueMapper("MERGE", "merge");
addFunctionMapper("NEXT", "next");
@ -229,18 +251,9 @@ public class GlobalVariables
return this;
}
public TemplateProcess create(String fileName)
public TemplateProcess create(String fileName, String splitter, boolean valueOnly)
{
TemplateProcess process = new TemplateProcess(String.format(fileName+".java", type.getFileType()));
process.setPathBuilder(new PathBuilder(type.getPathType()));
process.addFlags(flags);
process.addMappers(operators);
return process;
}
public TemplateProcess createBi(String fileName, String splitter)
{
TemplateProcess process = new TemplateProcess(String.format(fileName+".java", type.getFileType()+splitter+valueType.getFileType()));
TemplateProcess process = new TemplateProcess(String.format(fileName+".java", (splitter != null ? type.getFileType()+splitter+valueType.getFileType() : (valueOnly ? valueType : type).getFileType())));
process.setPathBuilder(new PathBuilder(type.getPathType()));
process.addFlags(flags);
process.addMappers(operators);

View File

@ -6,8 +6,10 @@ import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import speiger.src.builder.processor.TemplateProcess;
@ -15,12 +17,14 @@ import speiger.src.builder.processor.TemplateProcessor;
public class TestBuilder extends TemplateProcessor
{
Map<String, EnumSet<ClassType>> blocked = new HashMap<String, EnumSet<ClassType>>();
Map<String, String> nameRemapper = new HashMap<String, String>();
Map<String, String> biRequired = new HashMap<String, String>();
Map<String, EnumSet<ClassType>> blocked = new HashMap<>();
Map<String, String> nameRemapper = new HashMap<>();
Map<String, String> biRequired = new HashMap<>();
Set<String> enumRequired = new HashSet<>();
public static final ClassType[] TYPE = ClassType.values();
List<GlobalVariables> variables = new ArrayList<GlobalVariables>();
List<GlobalVariables> variables = new ArrayList<>();
List<GlobalVariables> biVariables = new ArrayList<>();
List<GlobalVariables> enumVariables = new ArrayList<>();
public TestBuilder()
{
@ -61,21 +65,20 @@ public class TestBuilder extends TemplateProcessor
create(clzType, subType);
}
}
// blocked.put("AbstractMap", EnumSet.allOf(ClassType.class));
enumRequired.add("EnumMap");
biRequired.put("BiConsumer", "");
biRequired.put("Function", "2");
biRequired.put("UnaryOperator", "");
biRequired.put("Map", "2");
biRequired.put("Maps", "2");
biRequired.put("AbstractMap", "2");
addBiClass("Function", "Maps", "Map", "SortedMap", "NavigableMap", "AbstractMap", "OpenHashMap", "LinkedOpenHashMap", "OpenCustomHashMap", "LinkedOpenCustomHashMap", "ArrayMap", "RBTreeMap", "AVLTreeMap");
nameRemapper.put("BiConsumer", "%sConsumer");
nameRemapper.put("IArray", "I%sArray");
nameRemapper.put("AbstractMap", "Abstract%sMap");
nameRemapper.put("AbstractCollection", "Abstract%sCollection");
nameRemapper.put("AbstractSet", "Abstract%sSet");
nameRemapper.put("AbstractList", "Abstract%sList");
nameRemapper.put("EnumMap", "Enum2%sMap");
addBlockage(ClassType.OBJECT, "Consumer", "Comparator", "Stack");
addBlockage(ClassType.BOOLEAN, "Sets", "ArraySet", "AVLTreeSet", "RBTreeSet", "SortedSet", "NavigableSet", "OpenHashSet", "OpenCustomHashSet", "LinkedOpenHashSet", "LinkedOpenCustomHashSet");
addBlockage(ClassType.BOOLEAN, "SortedMap", "NavigableMap", "OpenHashMap", "LinkedOpenHashMap", "OpenCustomHashMap", "LinkedOpenCustomHashMap", "ArrayMap", "RBTreeMap", "AVLTreeMap");
}
protected void create(ClassType mainType, ClassType subType)
@ -89,12 +92,24 @@ public class TestBuilder extends TemplateProcessor
type.createFunctions();
if(mainType == subType) variables.add(type);
biVariables.add(type);
if(mainType.isObject()) enumVariables.add(type);
}
protected void addBlockage(ClassType type, String...args) {
for(String s : args) {
protected void addBiClass(String...classNames)
{
for(String s : classNames)
{
biRequired.put(s, "2");
}
}
protected void addBlockage(ClassType type, String...args)
{
for(String s : args)
{
EnumSet<ClassType> set = blocked.get(s);
if(set == null) {
if(set == null)
{
set = EnumSet.noneOf(ClassType.class);
blocked.put(s, set);
}
@ -105,30 +120,26 @@ public class TestBuilder extends TemplateProcessor
@Override
public void createProcesses(String name, Consumer<TemplateProcess> acceptor)
{
EnumSet<ClassType> types = blocked.get(name);
String splitter = biRequired.get(name);
if(splitter != null)
boolean valueRequired = enumRequired.contains(name);
List<GlobalVariables> vars = getVariablesByClass(name, splitter != null);
EnumSet<ClassType> types = blocked.get(name);
for(int i = 0,m=vars.size();i<m;i++)
{
for(int i = 0,m=biVariables.size();i<m;i++)
{
GlobalVariables type = biVariables.get(i);
if(types == null || !types.contains(type.getType()))
{
acceptor.accept(type.createBi(nameRemapper.getOrDefault(name, "%s"+name), splitter));
}
}
return;
}
for(int i = 0,m=variables.size();i<m;i++)
{
GlobalVariables type = variables.get(i);
GlobalVariables type = vars.get(i);
if(types == null || !types.contains(type.getType()))
{
acceptor.accept(type.create(nameRemapper.getOrDefault(name, "%s"+name)));
acceptor.accept(type.create(nameRemapper.getOrDefault(name, "%s"+name), splitter, valueRequired));
}
}
}
protected List<GlobalVariables> getVariablesByClass(String name, boolean bi) {
if(enumRequired.contains(name)) return enumVariables;
if(bi) return biVariables;
return variables;
}
public static void main(String...args)
{
try

View File

@ -27,7 +27,7 @@ import speiger.src.collections.objects.sets.ObjectSet;
public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CLASS_TYPE, CLASS_VALUE_TYPE> implements MAP KEY_VALUE_GENERIC_TYPE
{
VALUE_TYPE defaultReturnValue = EMPTY_VALUE;
protected VALUE_TYPE defaultReturnValue = EMPTY_VALUE;
@Override
public VALUE_TYPE getDefaultReturnValue() {
@ -174,6 +174,11 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
}
#if TYPE_OBJECT
@Override
public CLASS_VALUE_TYPE get(Object key) {
return VALUE_TO_OBJ(GET_VALUE((T)key));
}
@Override
public VALUE_TYPE getOrDefault(Object key, VALUE_TYPE defaultValue) {
VALUE_TYPE value = get(key);
@ -181,6 +186,11 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
}
#else
@Override
public CLASS_VALUE_TYPE get(Object key) {
return VALUE_TO_OBJ(key instanceof CLASS_TYPE ? GET_VALUE(CLASS_TO_KEY(key)) : getDefaultReturnValue());
}
@Override
public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) {
VALUE_TYPE value = get(key);
@ -334,6 +344,11 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
this.value = value;
}
public void set(KEY_TYPE key, VALUE_TYPE value) {
this.key = key;
this.value = value;
}
@Override
public KEY_TYPE ENTRY_KEY() {
return key;
@ -354,7 +369,7 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
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 == entry.ENTRY_KEY() && value == entry.ENTRY_VALUE();
return KEY_EQUALS(key, entry.ENTRY_KEY()) && VALUE_EQUALS(value, entry.ENTRY_VALUE());
}
Map.Entry<?, ?> entry = (Map.Entry<?, ?>)obj;
Object key = entry.getKey();

View File

@ -0,0 +1,999 @@
package speiger.src.collections.PACKAGE.maps.impl.customHash;
import java.util.Comparator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.function.Consumer;
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER;
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.SORTED_MAP;
import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET;
import speiger.src.collections.PACKAGE.sets.SORTED_SET;
import speiger.src.collections.PACKAGE.sets.SET;
import speiger.src.collections.PACKAGE.utils.STRATEGY;
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;
#if !VALUE_OBJECT && !SAME_TYPE
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_CONSUMER;
import speiger.src.collections.VALUE_PACKAGE.lists.VALUE_LIST_ITERATOR;
#endif
#if !TYPE_OBJECT
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.ObjectSortedSet;
import speiger.src.collections.objects.sets.ObjectSet;
#endif
import speiger.src.collections.utils.HashUtil;
public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE
{
protected transient long[] links;
protected int firstIndex = -1;
protected int lastIndex = -1;
public LINKED_CUSTOM_HASH_MAP(STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(HashUtil.DEFAULT_MIN_CAPACITY, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
public LINKED_CUSTOM_HASH_MAP(int minCapacity, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(minCapacity, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
public LINKED_CUSTOM_HASH_MAP(int minCapacity, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
super(minCapacity, loadFactor, strategy);
links = new long[nullIndex + 1];
}
#if !TYPE_OBJECT || !VALUE_OBJECT
public LINKED_CUSTOM_HASH_MAP(CLASS_TYPE[] keys, CLASS_VALUE_TYPE[] values, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(keys, values, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
public LINKED_CUSTOM_HASH_MAP(CLASS_TYPE[] keys, CLASS_VALUE_TYPE[] values, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(keys.length, loadFactor, strategy);
if(keys.length != values.length) throw new IllegalStateException("Input Arrays are not equal size");
for(int i = 0,m=keys.length;i<m;i++) put(OBJ_TO_KEY(keys[i]), OBJ_TO_VALUE(values[i]));
}
#endif
public LINKED_CUSTOM_HASH_MAP(KEY_TYPE[] keys, VALUE_TYPE[] values, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(keys, values, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
public LINKED_CUSTOM_HASH_MAP(KEY_TYPE[] keys, VALUE_TYPE[] values, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(keys.length, loadFactor, strategy);
if(keys.length != values.length) throw new IllegalStateException("Input Arrays are not equal size");
for(int i = 0,m=keys.length;i<m;i++) put(keys[i], values[i]);
}
public LINKED_CUSTOM_HASH_MAP(Map<? extends CLASS_TYPE, ? extends CLASS_VALUE_TYPE> map, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(map, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
public LINKED_CUSTOM_HASH_MAP(Map<? extends CLASS_TYPE, ? extends CLASS_VALUE_TYPE> map, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(map.size(), loadFactor, strategy);
putAll(map);
}
public LINKED_CUSTOM_HASH_MAP(MAP KEY_VALUE_GENERIC_TYPE map, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(map, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
public LINKED_CUSTOM_HASH_MAP(MAP KEY_VALUE_GENERIC_TYPE map, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(map.size(), loadFactor, strategy);
putAll(map);
}
@Override
public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) {
if(strategy.equals(key, EMPTY_KEY_VALUE)) {
if(containsNull) {
VALUE_TYPE lastValue = values[nullIndex];
values[nullIndex] = value;
moveToFirstIndex(nullIndex);
return lastValue;
}
values[nullIndex] = value;
containsNull = true;
onNodeAdded(nullIndex);
}
else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], EMPTY_KEY_VALUE)) {
if(strategy.equals(keys[pos], key)) {
VALUE_TYPE lastValue = values[pos];
values[pos] = value;
moveToFirstIndex(pos);
return lastValue;
}
pos = ++pos & mask;
}
keys[pos] = key;
values[pos] = value;
onNodeAdded(pos);
}
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue();
}
@Override
public VALUE_TYPE putAndMoveToLast(KEY_TYPE key, VALUE_TYPE value) {
if(strategy.equals(key, EMPTY_KEY_VALUE)) {
if(containsNull) {
VALUE_TYPE lastValue = values[nullIndex];
values[nullIndex] = value;
moveToLastIndex(nullIndex);
return lastValue;
}
values[nullIndex] = value;
containsNull = true;
onNodeAdded(nullIndex);
}
else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], EMPTY_KEY_VALUE)) {
if(strategy.equals(keys[pos], key)) {
VALUE_TYPE lastValue = values[pos];
values[pos] = value;
moveToLastIndex(pos);
return lastValue;
}
pos = ++pos & mask;
}
keys[pos] = key;
values[pos] = value;
onNodeAdded(pos);
}
if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
return getDefaultReturnValue();
}
@Override
public boolean moveToFirst(KEY_TYPE key) {
if(strategy.equals(FIRST_ENTRY_KEY(), key)) return false;
if(strategy.equals(key, EMPTY_KEY_VALUE)) {
if(containsNull) {
moveToFirstIndex(nullIndex);
return true;
}
}
else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], EMPTY_KEY_VALUE)) {
if(strategy.equals(keys[pos], key)) {
moveToFirstIndex(pos);
return true;
}
pos = ++pos & mask;
}
}
return false;
}
@Override
public boolean moveToLast(KEY_TYPE key) {
if(strategy.equals(LAST_ENTRY_KEY(), key)) return false;
if(strategy.equals(key, EMPTY_KEY_VALUE)) {
if(containsNull) {
moveToLastIndex(nullIndex);
return true;
}
}
else {
int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
while(!strategy.equals(keys[pos], EMPTY_KEY_VALUE)) {
if(strategy.equals(keys[pos], key)) {
moveToLastIndex(pos);
return true;
}
pos = ++pos & mask;
}
}
return false;
}
@Override
public VALUE_TYPE getAndMoveToFirst(KEY_TYPE key) {
int index = findIndex(key);
if(index < 0) return getDefaultReturnValue();
moveToFirstIndex(index);
return values[index];
}
@Override
public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) {
int index = findIndex(key);
if(index < 0) return getDefaultReturnValue();
moveToLastIndex(index);
return values[index];
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() {
return null;
}
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { throw new UnsupportedOperationException(); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { throw new UnsupportedOperationException(); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { throw new UnsupportedOperationException(); }
@Override
public KEY_TYPE FIRST_ENTRY_KEY() {
if(size == 0) throw new NoSuchElementException();
return keys[firstIndex];
}
@Override
public KEY_TYPE POLL_FIRST_ENTRY_KEY() {
if(size == 0) throw new NoSuchElementException();
int pos = firstIndex;
firstIndex = (int)links[pos];
if(0 <= firstIndex) links[firstIndex] |= 0xFFFFFFFF00000000L;
KEY_TYPE result = keys[pos];
size--;
if(strategy.equals(result, EMPTY_KEY_VALUE)) {
containsNull = false;
keys[nullIndex] = EMPTY_KEY_VALUE;
values[nullIndex] = EMPTY_VALUE;
}
else shiftKeys(pos);
if(nullIndex > minCapacity && size < maxFill / 4 && nullIndex > HashUtil.DEFAULT_MIN_CAPACITY) rehash(nullIndex / 2);
return result;
}
@Override
public KEY_TYPE LAST_ENTRY_KEY() {
if(size == 0) throw new NoSuchElementException();
return keys[lastIndex];
}
@Override
public KEY_TYPE POLL_LAST_ENTRY_KEY() {
if(size == 0) throw new NoSuchElementException();
int pos = lastIndex;
lastIndex = (int)(links[pos] >>> 32);
if(0 <= lastIndex) links[lastIndex] |= 0xFFFFFFFFL;
KEY_TYPE result = keys[pos];
size--;
if(strategy.equals(result, EMPTY_KEY_VALUE)) {
containsNull = false;
keys[nullIndex] = EMPTY_KEY_VALUE;
values[nullIndex] = EMPTY_VALUE;
}
else shiftKeys(pos);
if(nullIndex > minCapacity && size < maxFill / 4 && nullIndex > HashUtil.DEFAULT_MIN_CAPACITY) rehash(nullIndex / 2);
return result;
}
@Override
public VALUE_TYPE FIRST_ENTRY_VALUE() {
if(size == 0) throw new NoSuchElementException();
return values[firstIndex];
}
@Override
public VALUE_TYPE LAST_ENTRY_VALUE() {
if(size == 0) throw new NoSuchElementException();
return values[lastIndex];
}
@Override
public ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
if(entrySet == null) entrySet = new MapEntrySet();
return entrySet;
}
@Override
public SET KEY_GENERIC_TYPE keySet() {
if(keySet == null) keySet = new KeySet();
return keySet;
}
@Override
public VALUE_COLLECTION VALUE_GENERIC_TYPE values() {
if(values == null) valuesC = new Values();
return valuesC;
}
@Override
public void forEach(BI_CONSUMER KEY_VALUE_GENERIC_TYPE action) {
int index = firstIndex;
while(index != -1){
action.accept(keys[index], values[index]);
index = (int)links[index];
}
}
@Override
public void clear() {
super.clear();
firstIndex = lastIndex = -1;
}
protected void moveToFirstIndex(int startPos) {
if(size == 1 || firstIndex == startPos) return;
if(lastIndex == startPos) {
lastIndex = (int)(links[startPos] >>> 32);
links[lastIndex] |= 0xFFFFFFFFL;
}
else {
long link = links[startPos];
int prev = (int)(link >>> 32);
int next = (int)link;
links[prev] ^= ((links[prev] ^ (link & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
links[next] ^= ((links[next] ^ (link & 0xFFFFFFFF00000000L)) & 0xFFFFFFFF00000000L);
}
links[firstIndex] ^= ((links[firstIndex] ^ ((startPos & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
links[startPos] = 0xFFFFFFFF00000000L | (firstIndex & 0xFFFFFFFFL);
firstIndex = startPos;
}
protected void moveToLastIndex(int startPos) {
if(size == 1 || lastIndex == startPos) return;
if(firstIndex == startPos) {
firstIndex = (int)links[startPos];
links[lastIndex] |= 0xFFFFFFFF00000000L;
}
else {
long link = links[startPos];
int prev = (int)(link >>> 32);
int next = (int)link;
links[prev] ^= ((links[prev] ^ (link & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
links[next] ^= ((links[next] ^ (link & 0xFFFFFFFF00000000L)) & 0xFFFFFFFF00000000L);
}
links[lastIndex] ^= ((links[lastIndex] ^ (startPos & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
links[startPos] = ((lastIndex & 0xFFFFFFFFL) << 32) | 0xFFFFFFFFL;
lastIndex = startPos;
}
@Override
protected void onNodeAdded(int pos) {
if(size == 0) {
firstIndex = lastIndex = pos;
links[pos] = -1L;
}
else {
links[lastIndex] ^= ((links[lastIndex] ^ (pos & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
links[pos] = ((lastIndex & 0xFFFFFFFFL) << 32) | 0xFFFFFFFFL;
lastIndex = pos;
}
}
@Override
protected void onNodeRemoved(int pos) {
if(size == 0) firstIndex = lastIndex = -1;
else if(firstIndex == pos) {
firstIndex = (int)links[pos];
if(0 <= firstIndex) links[firstIndex] |= 0xFFFFFFFF00000000L;
}
else if(lastIndex == pos) {
lastIndex = (int)(links[pos] >>> 32);
if(0 <= lastIndex) links[pos] |= 0xFFFFFFFFL;
}
else {
long link = links[pos];
int prev = (int)(link >>> 32);
int next = (int)link;
links[prev] ^= ((links[prev] ^ (link & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
links[next] ^= ((links[next] ^ (link & 0xFFFFFFFF00000000L)) & 0xFFFFFFFF00000000L);
}
}
@Override
protected void onNodeMoved(int from, int to) {
if(size == 1) {
firstIndex = lastIndex = to;
links[to] = -1L;
}
else if(firstIndex == from) {
firstIndex = to;
links[(int)links[from]] ^= ((links[(int)links[from]] ^ ((to & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
links[to] = links[from];
}
else if(lastIndex == from) {
lastIndex = to;
links[(int)(links[from] >>> 32)] ^= ((links[(int)(links[from] >>> 32)] ^ (to & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
links[to] = links[from];
}
else {
long link = links[from];
int prev = (int)(link >>> 32);
int next = (int)link;
links[prev] ^= ((links[prev] ^ (to & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
links[next] ^= ((links[next] ^ ((to & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
links[to] = link;
}
}
@Override
protected void rehash(int newSize) {
int newMask = newSize - 1;
KEY_TYPE[] newKeys = NEW_KEY_ARRAY(newSize + 1);
VALUE_TYPE[] newValues = NEW_VALUE_ARRAY(newSize + 1);
long[] newLinks = new long[newSize + 1];
int newPrev = -1;
for(int j = size, i = firstIndex, pos = 0, prev = -1;j != 0;) {
if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) pos = newSize;
else {
pos = HashUtil.mix(strategy.hashCode(keys[i])) & newMask;
while(!strategy.equals(newKeys[pos], EMPTY_KEY_VALUE)) pos = ++pos & newMask;
}
newKeys[pos] = keys[i];
newValues[pos] = values[i];
if(prev != -1) {
newLinks[newPrev] ^= ((newLinks[newPrev] ^ (pos & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
newLinks[pos] ^= ((newLinks[pos] ^ ((newPrev & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
newPrev = pos;
}
else {
newPrev = firstIndex = pos;
newLinks[pos] = -1L;
}
i = (int)links[prev = i];
}
links = newLinks;
lastIndex = newPrev;
if(newPrev != -1) newLinks[newPrev] |= 0xFFFFFFFFL;
nullIndex = newSize;
mask = newMask;
maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
keys = newKeys;
}
private class MapEntrySet extends AbstractObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> implements SORTED_MAP.FastSortedSet 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 LINKED_CUSTOM_HASH_MAP.this.moveToFirst(o.ENTRY_KEY());
}
@Override
public boolean moveToLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) {
return LINKED_CUSTOM_HASH_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 void forEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
int index = firstIndex;
while(index != -1){
action.accept(new BasicEntryKV_BRACES(keys[index], values[index]));
index = (int)links[index];
}
}
@Override
public void fastForEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES();
int index = firstIndex;
while(index != -1){
entry.set(keys[index], values[index]);
action.accept(entry);
index = (int)links[index];
}
}
@Override
@Deprecated
public boolean contains(Object o) {
if(o instanceof Map.Entry) {
if(o instanceof MAP.Entry) return LINKED_CUSTOM_HASH_MAP.this.containsKey(((MAP.Entry KEY_VALUE_GENERIC_TYPE)o).ENTRY_KEY());
return LINKED_CUSTOM_HASH_MAP.this.containsKey(((Map.Entry<?, ?>)o).getKey());
}
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 LINKED_CUSTOM_HASH_MAP.this.remove(entry.ENTRY_KEY(), entry.ENTRY_VALUE());
}
Map.Entry<?, ?> entry = (Map.Entry<?, ?>)o;
return LINKED_CUSTOM_HASH_MAP.this.remove(entry.getKey(), entry.getValue());
}
return false;
}
@Override
public int size() {
return LINKED_CUSTOM_HASH_MAP.this.size();
}
@Override
public void clear() {
LINKED_CUSTOM_HASH_MAP.this.clear();
}
@Override
public Comparator<MAP.Entry KEY_VALUE_GENERIC_TYPE> comparator() {
return null;
}
@Override
public ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> subSet(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement, MAP.Entry KEY_VALUE_GENERIC_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> headSet(MAP.Entry KEY_VALUE_GENERIC_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public ObjectSortedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> tailSet(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement) { throw new UnsupportedOperationException(); }
}
private final class KeySet extends ABSTRACT_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE {
#if TYPE_OBJECT
@Override
@Deprecated
public boolean contains(Object e) {
return containsKey(e);
}
@Override
public boolean remove(Object o) {
int oldSize = size;
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;
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 LINKED_CUSTOM_HASH_MAP.this.moveToFirst(o);
}
@Override
public boolean moveToLast(KEY_TYPE o) {
return LINKED_CUSTOM_HASH_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 LINKED_CUSTOM_HASH_MAP.this.size();
}
@Override
public void clear() {
LINKED_CUSTOM_HASH_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();
}
#if TYPE_OBJECT
@Override
public void forEach(Consumer KEY_SUPER_GENERIC_TYPE action) {
int index = firstIndex;
while(index != -1){
action.accept(keys[index]);
index = (int)links[index];
}
}
#else
@Override
public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) {
int index = firstIndex;
while(index != -1){
action.accept(keys[index]);
index = (int)links[index];
}
}
#endif
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return null; }
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { throw new UnsupportedOperationException(); }
}
private class Values extends VALUE_ABSTRACT_COLLECTION VALUE_GENERIC_TYPE {
#if VALUE_OBJECT
@Override
@Deprecated
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 LINKED_CUSTOM_HASH_MAP.this.size();
}
@Override
public void clear() {
LINKED_CUSTOM_HASH_MAP.this.clear();
}
#if VALUE_OBJECT
@Override
public void forEach(Consumer VALUE_SUPER_GENERIC_TYPE action) {
int index = firstIndex;
while(index != -1){
action.accept(values[index]);
index = (int)links[index];
}
}
#else
@Override
public void forEach(VALUE_CONSUMER VALUE_SUPER_GENERIC_TYPE action) {
int index = firstIndex;
while(index != -1){
action.accept(values[index]);
index = (int)links[index];
}
}
#endif
}
private class FastEntryIterator extends MapIterator implements ObjectListIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> {
MapEntry entry = new MapEntry(nextEntry());
public FastEntryIterator() {}
public FastEntryIterator(KEY_TYPE from) {
super(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 entry) { throw new UnsupportedOperationException(); }
@Override
public void add(MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { throw new UnsupportedOperationException(); }
}
private class EntryIterator extends MapIterator implements ObjectListIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE> {
MapEntry entry;
public EntryIterator() {}
public EntryIterator(KEY_TYPE from) {
super(from);
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE next() {
return entry = new MapEntry(nextEntry());
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() {
return entry = new MapEntry(previousEntry());
}
@Override
public void remove() {
super.remove();
entry.index = -1;
}
@Override
public void set(MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { throw new UnsupportedOperationException(); }
@Override
public void add(MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { throw new UnsupportedOperationException(); }
}
private class KeyIterator extends MapIterator implements LIST_ITERATOR KEY_GENERIC_TYPE {
public KeyIterator() {}
public KeyIterator(KEY_TYPE from) {
super(from);
}
@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 {
public ValueIterator() {}
@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 previous = -1;
int next = -1;
int current = -1;
int index = 0;
MapIterator() {
next = firstIndex;
}
MapIterator(KEY_TYPE from) {
if(strategy.equals(from, EMPTY_KEY_VALUE)) {
if(containsNull) {
next = (int) links[nullIndex];
previous = nullIndex;
}
else throw new NoSuchElementException("The null element is not in the set");
}
else if(keys[lastIndex] == from) {
previous = lastIndex;
index = size;
}
else {
int pos = HashUtil.mix(strategy.hashCode(from)) & mask;
while(!strategy.equals(keys[pos], EMPTY_KEY_VALUE)) {
if(strategy.equals(keys[pos], from)) {
next = (int)links[pos];
previous = pos;
break;
}
pos = ++pos & mask;
}
if(previous == -1 && next == -1)
throw new NoSuchElementException("The element was not found");
}
}
public boolean hasNext() {
return next != -1;
}
public boolean hasPrevious() {
return previous != -1;
}
public int nextIndex() {
ensureIndexKnown();
return index;
}
public int previousIndex() {
ensureIndexKnown();
return index - 1;
}
public void remove() {
if(current == -1) throw new IllegalStateException();
ensureIndexKnown();
if(current == previous) {
index--;
previous = (int)(links[current] >>> 32);
}
else next = (int)links[current];
size--;
if(previous == -1) firstIndex = next;
else links[previous] ^= ((links[previous] ^ (next & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
if (next == -1) lastIndex = previous;
else links[next] ^= ((links[next] ^ ((previous & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
if(current == nullIndex) {
current = -1;
containsNull = false;
keys[nullIndex] = EMPTY_KEY_VALUE;
values[nullIndex] = EMPTY_VALUE;
}
else {
int slot, last, startPos = current;
current = -1;
KEY_TYPE current;
while(true) {
startPos = ((last = startPos) + 1) & mask;
while(true){
if(strategy.equals((current = keys[startPos]), EMPTY_KEY_VALUE)) {
keys[last] = EMPTY_KEY_VALUE;
values[last] = EMPTY_VALUE;
return;
}
slot = HashUtil.mix(strategy.hashCode(current)) & mask;
if(last <= startPos ? (last >= slot || slot > startPos) : (last >= slot && slot > startPos)) break;
startPos = ++startPos & mask;
}
keys[last] = current;
values[last] = values[startPos];
if(next == startPos) next = last;
if(previous == startPos) previous = last;
onNodeMoved(startPos, last);
}
}
}
public int previousEntry() {
if(!hasPrevious()) throw new NoSuchElementException();
current = previous;
previous = (int)(links[current] >> 32);
next = current;
if(index >= 0) index--;
return current;
}
public int nextEntry() {
if(!hasNext()) throw new NoSuchElementException();
current = next;
next = (int)(links[current]);
previous = current;
if(index >= 0) index++;
return current;
}
private void ensureIndexKnown() {
if(index == -1) {
if(previous == -1) {
index = 0;
}
else if(next == -1) {
index = size;
}
else {
index = 1;
for(int pos = firstIndex;pos != previous;pos = (int)links[pos], index++);
}
}
}
}
}

View File

@ -0,0 +1,781 @@
package speiger.src.collections.PACKAGE.maps.impl.customHash;
import java.util.Arrays;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Consumer;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
#endif
import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER;
import speiger.src.collections.PACKAGE.lists.ARRAY_LIST;
import speiger.src.collections.PACKAGE.lists.LIST;
import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET;
import speiger.src.collections.PACKAGE.sets.SET;
#endif
import speiger.src.collections.PACKAGE.utils.STRATEGY;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION;
#if !SAME_TYPE && !VALUE_OBJECT
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR;
import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_CONSUMER;
#endif
import speiger.src.collections.objects.collections.ObjectIterator;
import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSet;
import speiger.src.collections.utils.HashUtil;
public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE
{
protected transient KEY_TYPE[] keys;
protected transient VALUE_TYPE[] values;
protected transient boolean containsNull;
protected transient int minCapacity;
protected transient int nullIndex;
protected transient int maxFill;
protected transient int mask;
protected transient FastEntrySet KEY_VALUE_GENERIC_TYPE entrySet;
protected transient SET KEY_GENERIC_TYPE keySet;
protected transient VALUE_COLLECTION VALUE_GENERIC_TYPE valuesC;
protected int size;
protected final float loadFactor;
protected final STRATEGY KEY_SUPER_GENERIC_TYPE strategy;
public CUSTOM_HASH_MAP(STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(HashUtil.DEFAULT_MIN_CAPACITY, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
public CUSTOM_HASH_MAP(int minCapacity, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(minCapacity, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
public CUSTOM_HASH_MAP(int minCapacity, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
if(minCapacity < 0) throw new IllegalStateException("Minimum Capacity is negative. This is not allowed");
if(loadFactor <= 0 || loadFactor >= 1F) throw new IllegalStateException("Load Factor is not between 0 and 1");
this.loadFactor = loadFactor;
this.minCapacity = nullIndex = HashUtil.arraySize(minCapacity, loadFactor);
this.strategy = strategy;
mask = nullIndex - 1;
maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
keys = NEW_KEY_ARRAY(nullIndex + 1);
values = NEW_VALUE_ARRAY(nullIndex + 1);
}
#if !TYPE_OBJECT || !VALUE_OBJECT
public CUSTOM_HASH_MAP(CLASS_TYPE[] keys, CLASS_VALUE_TYPE[] values, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(keys, values, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
public CUSTOM_HASH_MAP(CLASS_TYPE[] keys, CLASS_VALUE_TYPE[] values, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(keys.length, loadFactor, strategy);
if(keys.length != values.length) throw new IllegalStateException("Input Arrays are not equal size");
for(int i = 0,m=keys.length;i<m;i++) put(OBJ_TO_KEY(keys[i]), OBJ_TO_VALUE(values[i]));
}
#endif
public CUSTOM_HASH_MAP(KEY_TYPE[] keys, VALUE_TYPE[] values, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(keys, values, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
public CUSTOM_HASH_MAP(KEY_TYPE[] keys, VALUE_TYPE[] values, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(keys.length, loadFactor, strategy);
if(keys.length != values.length) throw new IllegalStateException("Input Arrays are not equal size");
for(int i = 0,m=keys.length;i<m;i++) put(keys[i], values[i]);
}
public CUSTOM_HASH_MAP(Map<? extends CLASS_TYPE, ? extends CLASS_VALUE_TYPE> map, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(map, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
public CUSTOM_HASH_MAP(Map<? extends CLASS_TYPE, ? extends CLASS_VALUE_TYPE> map, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(map.size(), loadFactor, strategy);
putAll(map);
}
public CUSTOM_HASH_MAP(MAP KEY_VALUE_GENERIC_TYPE map, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(map, HashUtil.DEFAULT_LOAD_FACTOR, strategy);
}
public CUSTOM_HASH_MAP(MAP KEY_VALUE_GENERIC_TYPE map, float loadFactor, STRATEGY KEY_SUPER_GENERIC_TYPE strategy) {
this(map.size(), loadFactor, strategy);
putAll(map);
}
@Override
public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) {
int slot = findIndex(key);
if(slot < 0) {
insert(-slot-1, key, value);
return getDefaultReturnValue();
}
VALUE_TYPE oldValue = values[slot];
values[slot] = value;
return oldValue;
}
@Override
public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value) {
int slot = findIndex(key);
if(slot < 0) {
insert(-slot-1, key, value);
return getDefaultReturnValue();
}
return values[slot];
}
#if VALUE_PRIMITIVES
@Override
public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) {
int slot = findIndex(key);
if(slot < 0) {
insert(-slot-1, key, value);
return getDefaultReturnValue();
}
VALUE_TYPE oldValue = values[slot];
values[slot] += value;
return oldValue;
}
#endif
#if !TYPE_OBJECT
@Override
public boolean containsKey(KEY_TYPE key) {
return findIndex(key) >= 0;
}
#endif
@Override
@Deprecated
public boolean containsKey(Object key) {
return findIndex(key) >= 0;
}
#if !VALUE_OBJECT
@Override
public boolean containsValue(VALUE_TYPE value) {
if(VALUE_EQUALS(value, values[nullIndex])) return true;
for(int i = nullIndex-1;i >= 0;i--)
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && VALUE_EQUALS(values[i], value)) return true;
return false;
}
#endif
@Override
@Deprecated
public boolean containsValue(Object value) {
if((value == null && VALUE_EQUALS(values[nullIndex], getDefaultReturnValue())) || EQUALS_VALUE_TYPE(values[nullIndex], value)) return true;
for(int i = nullIndex-1;i >= 0;i--)
if(!strategy.equals(keys[i], EMPTY_KEY_VALUE) && ((value == null && values[i] == getDefaultReturnValue()) || EQUALS_VALUE_TYPE(values[i], value))) return true;
return false;
}
@Override
public VALUE_TYPE REMOVE_KEY(KEY_TYPE key) {
int slot = findIndex(key);
if(slot < 0) return getDefaultReturnValue();
return removeIndex(slot);
}
@Override
@Deprecated
public CLASS_VALUE_TYPE remove(Object key) {
int slot = findIndex(key);
if(slot < 0) return VALUE_TO_OBJ(getDefaultReturnValue());
return removeIndex(slot);
}
#if !TYPE_OBJECT || !VALUE_OBJECT
@Override
public boolean remove(KEY_TYPE key, VALUE_TYPE value) {
if(strategy.equals(key, EMPTY_KEY_VALUE)) {
if(containsNull && VALUE_EQUALS(value, values[nullIndex])) {
removeNullIndex();
return true;
}
return false;
}
int pos = HashUtil.mix(strategy.hashCode(key)) & mask;
KEY_TYPE current = keys[pos];
if(strategy.equals(current, EMPTY_KEY_VALUE)) return false;
if(strategy.equals(current, key) && VALUE_EQUALS(value, values[pos])) {
removeIndex(pos);
return true;
}
while(true) {
if(strategy.equals((current = keys[pos = (++pos & mask)]), EMPTY_KEY_VALUE)) return false;
else if(strategy.equals(current, key) && VALUE_EQUALS(value, values[pos])) {
removeIndex(pos);
return true;
}
}
}
#endif
@Override
public boolean remove(Object key, Object value) {
Objects.requireNonNull(value);
if(key == null) {
if(containsNull && EQUALS_VALUE_TYPE(values[nullIndex], value)) {
removeNullIndex();
return true;
}
return false;
}
KEY_TYPE keyType = CLASS_TO_KEY(key);
int pos = HashUtil.mix(strategy.hashCode(keyType)) & mask;
KEY_TYPE current = keys[pos];
if(strategy.equals(current, EMPTY_KEY_VALUE)) return false;
if(strategy.equals(current, keyType) && EQUALS_VALUE_TYPE(values[pos], value)) {
removeIndex(pos);
return true;
}
while(true) {
if(strategy.equals((current = keys[pos = (++pos & mask)]), EMPTY_KEY_VALUE)) return false;
else if(strategy.equals(current, keyType) && EQUALS_VALUE_TYPE(values[pos], value)){
removeIndex(pos);
return true;
}
}
}
@Override
public VALUE_TYPE GET_VALUE(KEY_TYPE key) {
int slot = findIndex(key);
return slot < 0 ? getDefaultReturnValue() : values[slot];
}