package speiger.src.collections.floats.maps.impl.immutable;

import java.util.Arrays;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.BiFunction;
import java.util.function.Predicate;

import speiger.src.collections.floats.collections.FloatBidirectionalIterator;
import speiger.src.collections.floats.functions.FloatConsumer;
import speiger.src.collections.objects.functions.consumer.ObjectFloatConsumer;
import speiger.src.collections.ints.functions.consumer.IntFloatConsumer;
import speiger.src.collections.ints.functions.consumer.IntObjectConsumer;
import speiger.src.collections.floats.functions.OptionalFloat;
import speiger.src.collections.floats.functions.function.FloatPredicate;
import speiger.src.collections.floats.functions.consumer.FloatObjectConsumer;
import speiger.src.collections.floats.functions.function.FloatFunction;
import speiger.src.collections.floats.functions.function.FloatObjectUnaryOperator;
import speiger.src.collections.floats.functions.function.FloatFloatUnaryOperator;
import speiger.src.collections.floats.lists.FloatListIterator;
import speiger.src.collections.floats.maps.interfaces.Float2ObjectOrderedMap;
import speiger.src.collections.floats.maps.abstracts.AbstractFloat2ObjectMap;
import speiger.src.collections.floats.maps.interfaces.Float2ObjectMap;
import speiger.src.collections.floats.sets.FloatOrderedSet;
import speiger.src.collections.floats.utils.FloatArrays;
import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer;

import speiger.src.collections.floats.sets.AbstractFloatSet;
import speiger.src.collections.objects.collections.AbstractObjectCollection;
import speiger.src.collections.objects.collections.ObjectOrderedCollection;
import speiger.src.collections.objects.collections.ObjectIterator;
import speiger.src.collections.objects.functions.ObjectSupplier;
import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator;
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.ObjectOrderedSet;
import speiger.src.collections.utils.HashUtil;
import speiger.src.collections.utils.SanityChecks;

/**
 * A Type Specific Custom implementation of the HashMap
 * Instead of using Wrapper Object Arrays for storing keys and values there is dedicated arrays for storing keys and values.
 * Extra to that there is a couple quality of life functions provided
 * @param <V> the keyType of elements maintained by this Collection
 */
public class ImmutableFloat2ObjectOpenHashMap<V> extends AbstractFloat2ObjectMap<V> implements Float2ObjectOrderedMap<V>
{
	/** The Backing keys array */
	protected transient float[] keys;
	/** The Backing values array */
	protected transient V[] values;
	/** The Backing array for links between nodes. Left 32 Bits => Previous Entry, Right 32 Bits => Next Entry */
	protected transient long[] links;
	/** If a null value is present */
	protected transient boolean containsNull;
	/** Index of the Null Value */
	protected transient int nullIndex;
	/** Max Index that is allowed to be searched through nullIndex - 1 */
	protected transient int mask;
	/** The First Index in the Map */
	protected int firstIndex = -1;
	/** The Last Index in the Map */
	protected int lastIndex = -1;
	/** EntrySet cache */
	protected transient FastOrderedSet<V> entrySet;
	/** KeySet cache */
	protected transient FloatOrderedSet keySet;
	/** Values cache */
	protected transient ObjectOrderedCollection<V> valuesC;
	
	/** Amount of Elements stored in the HashMap */
	protected int size;
	
	/**
	 * Helper constructor for copying the Map
	 */
	protected ImmutableFloat2ObjectOpenHashMap() {}
	
	/**
	 * Helper constructor that allow to create a map from boxed values (it will unbox them)
	 * @param keys the keys that should be put into the map
	 * @param values the values that should be put into the map.
	 * @throws IllegalStateException if the keys and values do not match in lenght
	 */
	public ImmutableFloat2ObjectOpenHashMap(Float[] keys, V[] values) {
		this(keys, values, HashUtil.DEFAULT_LOAD_FACTOR);
	}
	
	/**
	 * Helper constructor that allow to create a map from boxed values (it will unbox them)
	 * @param keys the keys that should be put into the map
	 * @param values the values that should be put into the map.
	 * @param loadFactor the percentage of how full the backing array can be before they resize
	 * @throws IllegalStateException if the keys and values do not match in lenght
	 * @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
	 */
	public ImmutableFloat2ObjectOpenHashMap(Float[] keys, V[] values, float loadFactor) {
		if(keys.length != values.length) throw new IllegalStateException("Input Arrays are not equal size");
		init(FloatArrays.unwrap(keys), values, 0, keys.length, loadFactor);
	}
	
	/**
	 * Helper constructor that allow to create a map from unboxed values
	 * @param keys the keys that should be put into the map
	 * @param values the values that should be put into the map.
	 * @throws IllegalStateException if the keys and values do not match in lenght
	 */
	public ImmutableFloat2ObjectOpenHashMap(float[] keys, V[] values) {
		this(keys, values, HashUtil.DEFAULT_LOAD_FACTOR);
	}
	
	/**
	 * Helper constructor that allow to create a map from unboxed values
	 * @param keys the keys that should be put into the map
	 * @param values the values that should be put into the map.
	 * @param loadFactor the percentage of how full the backing array can be before they resize
	 * @throws IllegalStateException if the keys and values do not match in lenght
	 * @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
	 */
	public ImmutableFloat2ObjectOpenHashMap(float[] keys, V[] values, float loadFactor) {
		if(keys.length != values.length) throw new IllegalStateException("Input Arrays are not equal size");
		init(keys, values, 0, keys.length, loadFactor);
	}
	
	/**
	 * A Helper constructor that allows to create a Map with exactly the same values as the provided map.
	 * @param map the values that should be present in the map
	 */
	public ImmutableFloat2ObjectOpenHashMap(Map<? extends Float, ? extends V> map) {
		this(map, HashUtil.DEFAULT_LOAD_FACTOR);
	}
	
	/**
	 * A Helper constructor that allows to create a Map with exactly the same values as the provided map.
	 * @param map the values that should be present in the map
	 * @param loadFactor the percentage of how full the backing array can be before they resize
	 * @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
	 */
	public ImmutableFloat2ObjectOpenHashMap(Map<? extends Float, ? extends V> map, float loadFactor) {
		float[] keys = new float[map.size()];
		V[] values = (V[])new Object[keys.length];
		int index = 0;
		for(Map.Entry<? extends Float, ? extends V> entry : map.entrySet()) {
			keys[index] = entry.getKey().floatValue();
			values[index] = entry.getValue();
			index++;
		}
		init(keys, values, 0, index, loadFactor);
	}
	
	/**
	 * A Type Specific Helper function that allows to create a new Map with exactly the same values as the provided map.
	 * @param map the values that should be present in the map
 	 */
	public ImmutableFloat2ObjectOpenHashMap(Float2ObjectMap<V> map) {
		this(map, HashUtil.DEFAULT_LOAD_FACTOR);
	}
	
	/**
	 * A Type Specific Helper function that allows to create a new Map with exactly the same values as the provided map.
	 * @param map the values that should be present in the map
	 * @param loadFactor the percentage of how full the backing array can be before they resize
	 * @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
 	 */
	public ImmutableFloat2ObjectOpenHashMap(Float2ObjectMap<V> map, float loadFactor) {
		float[] keys = new float[map.size()];
		V[] values = (V[])new Object[keys.length];
		int index = 0;
		for(Float2ObjectMap.Entry<V> entry : getFastIterable(map)) {
			keys[index] = entry.getFloatKey();
			values[index] = entry.getValue();
			index++;
		}
		init(keys, values, 0, index, loadFactor);
	}
	
	protected void init(float[] a, V[] b, int offset, int length, float loadFactor) {
		SanityChecks.checkArrayCapacity(a.length, offset, length);
		int newSize = HashUtil.arraySize(length+1, loadFactor);
		int newMask = newSize - 1;
		float[] newKeys = new float[newSize + 1];
		V[] newValues = (V[])new Object[newSize + 1];
		long[] newLinks = new long[newSize + 1];
		int prev = -1;
		for(int i = offset,m=offset+length;i<m;i++)
		{
			float o = a[i];
			if(Float.floatToIntBits(o) == 0) {
				if(!containsNull) {
					size++;
					if(prev != -1) {
						newLinks[prev] ^= ((newLinks[prev] ^ (newSize & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
						newLinks[newSize] ^= ((newLinks[newSize] ^ ((prev & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
						prev = newSize;
					}
					else {
						prev = firstIndex = newSize;
						newLinks[newSize] = -1L;
					}
				}
				containsNull = true;
				newValues[newSize] = b[i];
				continue;
			}
			boolean found = true;
			int pos = HashUtil.mix(Float.hashCode(o)) & newMask;
			float current = newKeys[pos];
			if(Float.floatToIntBits(current) != 0) {
				if(Float.floatToIntBits(current) == Float.floatToIntBits(o)) {
					newValues[pos] = b[i];
					continue;
				}
				while(Float.floatToIntBits((current = newKeys[pos = (++pos & newMask)])) != 0) {
					if(Float.floatToIntBits(current) == Float.floatToIntBits(o)) {
						found = false;
						newValues[pos] = b[i];
						break;
					}
				}
			}
			if(found) {
				size++;
				newKeys[pos] = o;
				newValues[pos] = b[i];
				if(prev != -1) {
					newLinks[prev] ^= ((newLinks[prev] ^ (pos & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
					newLinks[pos] ^= ((newLinks[pos] ^ ((prev & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
					prev = pos;
				}
				else {
					prev = firstIndex = pos;
					newLinks[pos] = -1L;
				}
			}
		}
		nullIndex = newSize;
		mask = newMask;
		keys = newKeys;
		values = newValues;
		links = newLinks;
		lastIndex = prev;
		if(prev != -1) newLinks[prev] |= 0xFFFFFFFFL;
	}
	
	@Override
	public V put(float key, V value) { throw new UnsupportedOperationException(); }
	@Override
	public V putIfAbsent(float key, V value) { throw new UnsupportedOperationException(); }	
	@Override
	public V putAndMoveToFirst(float key, V value) { throw new UnsupportedOperationException(); }
	@Override
	public V putAndMoveToLast(float key, V value) { throw new UnsupportedOperationException(); }
	@Override
	public V putFirst(float key, V value) { throw new UnsupportedOperationException(); }
	@Override
	public V putLast(float key, V value) { throw new UnsupportedOperationException(); }
	@Override
	public boolean moveToFirst(float key) { throw new UnsupportedOperationException(); }
	@Override
	public boolean moveToLast(float key) { throw new UnsupportedOperationException(); }
	@Override
	public V getAndMoveToFirst(float key) { throw new UnsupportedOperationException(); }
	@Override
	public V getAndMoveToLast(float key) { throw new UnsupportedOperationException(); }
	
	@Override
	public boolean containsKey(float key) {
		return findIndex(key) >= 0;
	}
	
	@Override
	@Deprecated
	public boolean containsKey(Object key) {
		return findIndex(key) >= 0;
	}
	
	@Override
	public boolean containsValue(Object value) {
		int index = firstIndex;
		while(index != -1) {
			if(Objects.equals(values[index], value)) return true;
			index = (int)links[index];
		}
		return false;
	}
	
	@Override
	public V remove(float key) { throw new UnsupportedOperationException(); }
	
	@Override
	public V removeOrDefault(float key, V defaultValue) { throw new UnsupportedOperationException(); }
	
	@Override
	@Deprecated
	public V remove(Object key) { throw new UnsupportedOperationException(); }
	
	@Override
	public boolean remove(float key, V value) { throw new UnsupportedOperationException(); }
	
	@Override
	public boolean remove(Object key, Object value) { throw new UnsupportedOperationException(); }
	
	@Override
	public V get(float key) {
		int slot = findIndex(key);
		return slot < 0 ? getDefaultReturnValue() : values[slot];
	}
	
	@Override
	public V get(Object key) {
		int slot = findIndex(key);
		return slot < 0 ? getDefaultReturnValue() : values[slot];
	}
	
	@Override
	public V getOrDefault(float key, V defaultValue) {
		int slot = findIndex(key);
		return slot < 0 ? defaultValue : values[slot];
	}
	
	@Override
	public float firstFloatKey() {
		if(size == 0) throw new NoSuchElementException();
		return keys[firstIndex];
	}
	
	@Override
	public float pollFirstFloatKey() { throw new UnsupportedOperationException(); }
	
	@Override
	public float lastFloatKey() {
		if(size == 0) throw new NoSuchElementException();
		return keys[lastIndex];
	}
	
	@Override
	public float pollLastFloatKey() { throw new UnsupportedOperationException(); }
	
	@Override
	public V firstValue() {
		if(size == 0) throw new NoSuchElementException();
		return values[firstIndex];
	}
	
	@Override
	public V lastValue() {
		if(size == 0) throw new NoSuchElementException();
		return values[lastIndex];
	}
	
	@Override
	public Float2ObjectMap.Entry<V> firstEntry() {
		if(size == 0) throw new NoSuchElementException();
		return new BasicEntry<>(keys[firstIndex], values[firstIndex]);
	}
	
	@Override
	public Float2ObjectMap.Entry<V> lastEntry() {
		if(size == 0) throw new NoSuchElementException();
		return new BasicEntry<>(keys[lastIndex], values[lastIndex]);
	}
	
	@Override
	public Float2ObjectMap.Entry<V> pollFirstEntry() { throw new UnsupportedOperationException(); }
	@Override
	public Float2ObjectMap.Entry<V> pollLastEntry() { throw new UnsupportedOperationException(); }

	@Override
	public ObjectOrderedSet<Float2ObjectMap.Entry<V>> float2ObjectEntrySet() {
		if(entrySet == null) entrySet = new MapEntrySet();
		return entrySet;
	}
	
	@Override
	public FloatOrderedSet keySet() {
		if(keySet == null) keySet = new KeySet();
		return keySet;
	}
	
	@Override
	public ObjectOrderedCollection<V> values() {
		if(valuesC == null) valuesC = new Values();
		return valuesC;
	}
	
	@Override
	public ImmutableFloat2ObjectOpenHashMap<V> copy() {
		ImmutableFloat2ObjectOpenHashMap<V> map = new ImmutableFloat2ObjectOpenHashMap<>();
		map.mask = mask;
		map.nullIndex = nullIndex;
		map.containsNull = containsNull;
		map.size = size;
		map.keys = Arrays.copyOf(keys, keys.length);
		map.values = Arrays.copyOf(values, values.length);
		map.links = Arrays.copyOf(links, links.length);
		map.firstIndex = firstIndex;
		map.lastIndex = lastIndex;
		return map;
	}
	
	@Override
	public void forEach(FloatObjectConsumer<V> action) {
		int index = firstIndex;
		while(index != -1){
			action.accept(keys[index], values[index]);
			index = (int)links[index];
		}
	}
	
	@Override
	public boolean replace(float key, V oldValue, V newValue) { throw new UnsupportedOperationException(); }
	
	@Override
	public V replace(float key, V value) { throw new UnsupportedOperationException(); }
	
	@Override
	public V compute(float key, FloatObjectUnaryOperator<V> mappingFunction) { throw new UnsupportedOperationException(); }
	@Override
	public V computeIfAbsent(float key, FloatFunction<V> mappingFunction) { throw new UnsupportedOperationException(); }
	@Override
	public V supplyIfAbsent(float key, ObjectSupplier<V> valueProvider) { throw new UnsupportedOperationException(); }
	@Override
	public V computeIfPresent(float key, FloatObjectUnaryOperator<V> mappingFunction) { throw new UnsupportedOperationException(); }
	@Override
	public V merge(float key, V value, ObjectObjectUnaryOperator<V, V> mappingFunction) { throw new UnsupportedOperationException(); }
	
	@Override
	public void mergeAll(Float2ObjectMap<V> m, ObjectObjectUnaryOperator<V, V> mappingFunction) { throw new UnsupportedOperationException(); }
	
	@Override
	public int size() { return size; }
	
	@Override
	public void clear() { throw new UnsupportedOperationException(); }
	
	protected int findIndex(float key) {
		if(Float.floatToIntBits(key) == 0) return containsNull ? nullIndex : -(nullIndex + 1);
		int pos = HashUtil.mix(Float.hashCode(key)) & mask;
		float current = keys[pos];
		if(Float.floatToIntBits(current) != 0) {
			if(Float.floatToIntBits(current) == Float.floatToIntBits(key)) return pos;
			while(Float.floatToIntBits((current = keys[pos = (++pos & mask)])) != 0)
				if(Float.floatToIntBits(current) == Float.floatToIntBits(key)) return pos;
		}
		return -(pos + 1);
	}
	
	protected int findIndex(Object key) {
		if(key == null) return containsNull ? nullIndex : -(nullIndex + 1);
		if(Float.floatToIntBits(((Float)key).floatValue()) == 0) return containsNull ? nullIndex : -(nullIndex + 1);
		int pos = HashUtil.mix(key.hashCode()) & mask;
		float current = keys[pos];
		if(Float.floatToIntBits(current) != 0) {
			if(Objects.equals(key, Float.valueOf(current))) return pos;
			while(Float.floatToIntBits((current = keys[pos = (++pos & mask)])) != 0)
				if(Objects.equals(key, Float.valueOf(current))) return pos;
		}
		return -(pos + 1);
	}
	
	protected class MapEntry implements Float2ObjectMap.Entry<V>, Map.Entry<Float, V> {
		public int index = -1;
		
		public MapEntry() {}
		public MapEntry(int index) {
			this.index = index;
		}

		@Override
		public float getFloatKey() {
			return keys[index];
		}

		@Override
		public V getValue() {
			return values[index];
		}

		@Override
		public V setValue(V value) { throw new UnsupportedOperationException(); }
		
		@Override
		public boolean equals(Object obj) {
			if(obj instanceof Map.Entry) {
				if(obj instanceof Float2ObjectMap.Entry) {
					Float2ObjectMap.Entry<V> entry = (Float2ObjectMap.Entry<V>)obj;
					return Float.floatToIntBits(keys[index]) == Float.floatToIntBits(entry.getFloatKey()) && Objects.equals(values[index], entry.getValue());
				}
				Map.Entry<?, ?> entry = (Map.Entry<?, ?>)obj;
				Object key = entry.getKey();
				if(!(key instanceof Float)) return false;
				Object value = entry.getValue();
				return key instanceof Float && Float.floatToIntBits(keys[index]) == Float.floatToIntBits(((Float)key).floatValue()) && Objects.equals(values[index], value);
			}
			return false;
		}
		
		@Override
		public int hashCode() {
			return Float.hashCode(keys[index]) ^ Objects.hashCode(values[index]);
		}
		
		@Override
		public String toString() {
			return Float.toString(keys[index]) + "=" + Objects.toString(values[index]);
		}
	}
	
	private class MapEntrySet extends AbstractObjectSet<Float2ObjectMap.Entry<V>> implements Float2ObjectOrderedMap.FastOrderedSet<V> {
		@Override
		public void addFirst(Float2ObjectMap.Entry<V> o) { throw new UnsupportedOperationException(); }
		@Override
		public void addLast(Float2ObjectMap.Entry<V> o) { throw new UnsupportedOperationException(); }
		@Override
		public boolean addAndMoveToFirst(Float2ObjectMap.Entry<V> o) { throw new UnsupportedOperationException(); }
		@Override
		public boolean addAndMoveToLast(Float2ObjectMap.Entry<V> o) { throw new UnsupportedOperationException(); }
		
		@Override
		public boolean moveToFirst(Float2ObjectMap.Entry<V> o) { throw new UnsupportedOperationException(); }
		
		@Override
		public boolean moveToLast(Float2ObjectMap.Entry<V> o) { throw new UnsupportedOperationException(); }
		
		@Override
		public Float2ObjectMap.Entry<V> getFirst() {
			return new BasicEntry<>(firstFloatKey(), firstValue());
		}
		
		@Override
		public Float2ObjectMap.Entry<V> getLast() {
			return new BasicEntry<>(lastFloatKey(), lastValue());
		}
		
		@Override
		public Float2ObjectMap.Entry<V> removeFirst() { throw new UnsupportedOperationException(); }
		
		@Override
		public Float2ObjectMap.Entry<V> removeLast() { throw new UnsupportedOperationException(); }
		
		@Override
		public ObjectBidirectionalIterator<Float2ObjectMap.Entry<V>> iterator() {
			return new EntryIterator(true);
		}
		
		@Override
		public ObjectBidirectionalIterator<Float2ObjectMap.Entry<V>> reverseIterator() {
			return new EntryIterator(false);
		}
		
		@Override
		public ObjectBidirectionalIterator<Float2ObjectMap.Entry<V>> iterator(Float2ObjectMap.Entry<V> fromElement) {
			return new EntryIterator(fromElement.getFloatKey());
		}
		
		@Override
		public ObjectBidirectionalIterator<Float2ObjectMap.Entry<V>> fastIterator() {
			return new FastEntryIterator(true);
		}
		
		@Override
		public ObjectBidirectionalIterator<Float2ObjectMap.Entry<V>> fastIterator(float fromElement) {
			return new FastEntryIterator(fromElement);
		}
		
		@Override
		public MapEntrySet copy() { throw new UnsupportedOperationException(); }
		
		@Override
		public void forEach(Consumer<? super Float2ObjectMap.Entry<V>> action) {
			int index = firstIndex;
			while(index != -1){
				action.accept(new BasicEntry<>(keys[index], values[index]));
				index = (int)links[index];
			}
		}
		
		@Override
		public void fastForEach(Consumer<? super Float2ObjectMap.Entry<V>> action) {
			BasicEntry<V> entry = new BasicEntry<>();
			int index = firstIndex;
			while(index != -1){
				entry.set(keys[index], values[index]);
				action.accept(entry);
				index = (int)links[index];
			}
		}
		
		@Override
		public void forEachIndexed(IntObjectConsumer<Float2ObjectMap.Entry<V>> action) {
			Objects.requireNonNull(action);
			if(size() <= 0) return;
			int count = 0;
			int index = firstIndex;
			while(index != -1) {
				action.accept(count++, new BasicEntry<>(keys[index], values[index]));
				index = (int)links[index];
			}
		}
		
		@Override
		public <E> void forEach(E input, ObjectObjectConsumer<E, Float2ObjectMap.Entry<V>> action) {
			Objects.requireNonNull(action);
			if(size() <= 0) return;
			int index = firstIndex;
			while(index != -1) {
				action.accept(input, new BasicEntry<>(keys[index], values[index]));
				index = (int)links[index];
			}
		}
		
		@Override
		public boolean matchesAny(Predicate<Float2ObjectMap.Entry<V>> filter) {
			Objects.requireNonNull(filter);
			if(size() <= 0) return false;
			BasicEntry<V> entry = new BasicEntry<>();
			int index = firstIndex;
			while(index != -1) {
				entry.set(keys[index], values[index]);
				if(filter.test(entry)) return true;
				index = (int)links[index];
			}
			return false;
		}
		
		@Override
		public boolean matchesNone(Predicate<Float2ObjectMap.Entry<V>> filter) {
			Objects.requireNonNull(filter);
			if(size() <= 0) return true;
			BasicEntry<V> entry = new BasicEntry<>();
			int index = firstIndex;
			while(index != -1) {
				entry.set(keys[index], values[index]);
				if(filter.test(entry)) return false;
				index = (int)links[index];
			}
			return true;
		}
		
		@Override
		public boolean matchesAll(Predicate<Float2ObjectMap.Entry<V>> filter) {
			Objects.requireNonNull(filter);
			if(size() <= 0) return true;
			BasicEntry<V> entry = new BasicEntry<>();
			int index = firstIndex;
			while(index != -1) {
				entry.set(keys[index], values[index]);
				if(!filter.test(entry)) return false;
				index = (int)links[index];
			}
			return true;
		}
		
		@Override
		public <E> E reduce(E identity, BiFunction<E, Float2ObjectMap.Entry<V>, E> operator) {
			Objects.requireNonNull(operator);
			E state = identity;
			int index = firstIndex;
			while(index != -1) {
				state = operator.apply(state, new BasicEntry<>(keys[index], values[index]));
				index = (int)links[index];
			}
			return state;
		}
		
		@Override
		public Optional<Float2ObjectMap.Entry<V>> reduce(ObjectObjectUnaryOperator<Float2ObjectMap.Entry<V>, Float2ObjectMap.Entry<V>> operator) {
			Objects.requireNonNull(operator);
			Float2ObjectMap.Entry<V> state = null;
			boolean empty = true;
			int index = firstIndex;
			while(index != -1) {
				if(empty) {
					empty = false;
					state = new BasicEntry<>(keys[index], values[index]);
					index = (int)links[index];
					continue;
				}
				state = operator.apply(state, new BasicEntry<>(keys[index], values[index]));
				index = (int)links[index];
			}
			return empty ? Optional.empty() : Optional.ofNullable(state);
		}
		
		@Override
		public Optional<Float2ObjectMap.Entry<V>> findFirst(Predicate<Float2ObjectMap.Entry<V>> filter) {
			Objects.requireNonNull(filter);
			if(size() <= 0) return Optional.empty();
			BasicEntry<V> entry = new BasicEntry<>();
			int index = firstIndex;
			while(index != -1) {
				entry.set(keys[index], values[index]);
				if(filter.test(entry)) return Optional.ofNullable(entry);
				index = (int)links[index];
			}
			return Optional.empty();
		}
		
		@Override
		public int count(Predicate<Float2ObjectMap.Entry<V>> filter) {
			Objects.requireNonNull(filter);
			if(size() <= 0) return 0;
			BasicEntry<V> entry = new BasicEntry<>();
			int result = 0;
			int index = firstIndex;
			while(index != -1) {
				entry.set(keys[index], values[index]);
				if(filter.test(entry)) result++;
				index = (int)links[index];
			}
			return result;
		}
		
		@Override
		@Deprecated
		public boolean contains(Object o) {
			if(o instanceof Map.Entry) {
				if(o instanceof Float2ObjectMap.Entry) {
					Float2ObjectMap.Entry<V> entry = (Float2ObjectMap.Entry<V>)o;
					int index = ImmutableFloat2ObjectOpenHashMap.this.findIndex(entry.getFloatKey());
					if(index >= 0) return Objects.equals(entry.getValue(), ImmutableFloat2ObjectOpenHashMap.this.values[index]);
				}
				else {
					Map.Entry<?, ?> entry = (Map.Entry<?, ?>)o;
					int index = ImmutableFloat2ObjectOpenHashMap.this.findIndex(entry.getKey());
					if(index >= 0) return Objects.equals(entry.getValue(), ImmutableFloat2ObjectOpenHashMap.this.values[index]);
				}
			}
			return false;
		}
		
		@Override
		@Deprecated
		public boolean remove(Object o) { throw new UnsupportedOperationException(); }
		
		@Override
		public int size() {
			return ImmutableFloat2ObjectOpenHashMap.this.size();
		}
		
		@Override
		public void clear() { throw new UnsupportedOperationException(); }
	}
	
	private final class KeySet extends AbstractFloatSet implements FloatOrderedSet {
		@Override
		public boolean contains(float e) {
			return containsKey(e);
		}
		
		@Override
		public boolean remove(float o) { throw new UnsupportedOperationException(); }
		
		@Override
		public boolean add(float o) {
			throw new UnsupportedOperationException();
		}
		
		@Override
		public void addFirst(float o) { throw new UnsupportedOperationException(); }
		
		@Override
		public void addLast(float o) { throw new UnsupportedOperationException(); }
		
		@Override
		public boolean addAndMoveToFirst(float o) { throw new UnsupportedOperationException(); }

		@Override
		public boolean addAndMoveToLast(float o) { throw new UnsupportedOperationException(); }

		@Override
		public boolean moveToFirst(float o) { throw new UnsupportedOperationException(); }

		@Override
		public boolean moveToLast(float o) { throw new UnsupportedOperationException(); }
		
		@Override
		public FloatListIterator iterator() {
			return new KeyIterator(true);
		}
		
		@Override
		public FloatListIterator reverseIterator() {
			return new KeyIterator(false);
		}
		
		@Override
		public FloatBidirectionalIterator iterator(float fromElement) {
			return new KeyIterator(fromElement);
		}
		
		@Override
		public KeySet copy() { throw new UnsupportedOperationException(); }
		
		@Override
		public int size() {
			return ImmutableFloat2ObjectOpenHashMap.this.size();
		}
		
		@Override
		public void clear() { throw new UnsupportedOperationException(); }
		
		@Override
		public float getFirstFloat() {
			return firstFloatKey();
		}
		
		@Override
		public float removeFirstFloat() { throw new UnsupportedOperationException(); }

		@Override
		public float getLastFloat() {
			return lastFloatKey();
		}

		@Override
		public float removeLastFloat() { throw new UnsupportedOperationException(); }
		
		@Override
		public void forEach(FloatConsumer action) {
			int index = firstIndex;
			while(index != -1){
				action.accept(keys[index]);
				index = (int)links[index];
			}
		}
		
		@Override
		public void forEachIndexed(IntFloatConsumer action) {
			Objects.requireNonNull(action);
			if(size() <= 0) return;
			int count = 0;
			int index = firstIndex;
			while(index != -1){
				action.accept(count++, keys[index]);
				index = (int)links[index];
			}
		}
		
		@Override
		public <E> void forEach(E input, ObjectFloatConsumer<E> action) {
			Objects.requireNonNull(action);
			if(size() <= 0) return;
			int index = firstIndex;
			while(index != -1){
				action.accept(input, keys[index]);
				index = (int)links[index];
			}
		}
		
		@Override
		public boolean matchesAny(FloatPredicate filter) {
			Objects.requireNonNull(filter);
			if(size() <= 0) return false;
			int index = firstIndex;
			while(index != -1){
				if(filter.test(keys[index])) return true;
				index = (int)links[index];
			}
			return false;
		}
		
		@Override
		public boolean matchesNone(FloatPredicate filter) {
			Objects.requireNonNull(filter);
			if(size() <= 0) return true;
			int index = firstIndex;
			while(index != -1){
				if(filter.test(keys[index])) return false;
				index = (int)links[index];
			}
			return true;
		}
		
		@Override
		public boolean matchesAll(FloatPredicate filter) {
			Objects.requireNonNull(filter);
			if(size() <= 0) return true;
			int index = firstIndex;
			while(index != -1){
				if(!filter.test(keys[index])) return false;
				index = (int)links[index];
			}
			return true;
		}
		
		@Override
		public float reduce(float identity, FloatFloatUnaryOperator operator) {
			Objects.requireNonNull(operator);
			float state = identity;
			int index = firstIndex;
			while(index != -1) {
				state = operator.applyAsFloat(state, keys[index]);
				index = (int)links[index];
			}
			return state;
		}
		
		@Override
		public OptionalFloat reduce(FloatFloatUnaryOperator operator) {
			Objects.requireNonNull(operator);
			float state = 0F;
			boolean empty = true;
			int index = firstIndex;
			while(index != -1) {
				if(empty) {
					empty = false;
					state = keys[index];
					index = (int)links[index];
					continue;
				}
				state = operator.applyAsFloat(state, keys[index]);
				index = (int)links[index];
			}
			return empty ? OptionalFloat.empty() : OptionalFloat.of(state);
		}
		
		@Override
		public OptionalFloat findFirst(FloatPredicate filter) {
			Objects.requireNonNull(filter);
			if(size() <= 0) return OptionalFloat.empty();
			int index = firstIndex;
			while(index != -1){
				if(filter.test(keys[index])) return OptionalFloat.of(keys[index]);
				index = (int)links[index];
			}
			return OptionalFloat.empty();
		}
		
		@Override
		public int count(FloatPredicate filter) {
			Objects.requireNonNull(filter);
			if(size() <= 0) return 0;
			int index = firstIndex;
			int result = 0;
			while(index != -1){
				if(filter.test(keys[index])) result++;
				index = (int)links[index];
			}
			return result;
		}
	}
	
	private class Values extends AbstractObjectCollection<V> implements ObjectOrderedCollection<V> {
		@Override
		@Deprecated
		public boolean contains(Object e) { return containsValue(e); }
		
		@Override
		public boolean add(V o) { throw new UnsupportedOperationException(); }
		@Override
		public ObjectIterator<V> iterator() { return new ValueIterator(true); }
		@Override
		public int size() { return ImmutableFloat2ObjectOpenHashMap.this.size(); }
		@Override
		public void clear() { throw new UnsupportedOperationException(); }
		@Override
		public ObjectOrderedCollection<V> reversed() { return new AbstractObjectCollection.ReverseObjectOrderedCollection<>(this, this::reverseIterator); }
		private ObjectIterator<V> reverseIterator() {
			return new ValueIterator(false);
		}
		@Override
		public void addFirst(V e) { throw new UnsupportedOperationException(); }
		@Override
		public void addLast(V e) { throw new UnsupportedOperationException(); }
		@Override
		public V getFirst() { return firstValue(); }
		@Override
		public V removeFirst() { throw new UnsupportedOperationException(); }
		@Override
		public V getLast() { return lastValue(); }
		@Override
		public V removeLast() { throw new UnsupportedOperationException(); }
		@Override
		public void forEach(Consumer<? super V> action) {
			int index = firstIndex;
			while(index != -1){
				action.accept(values[index]);
				index = (int)links[index];
			}
		}
		
		@Override
		public void forEachIndexed(IntObjectConsumer<V> action) {
			Objects.requireNonNull(action);
			if(size() <= 0) return;
			int count = 0;
			int index = firstIndex;
			while(index != -1){
				action.accept(count++, values[index]);
				index = (int)links[index];
			}
		}
		
		@Override
		public <E> void forEach(E input, ObjectObjectConsumer<E, V> action) {
			Objects.requireNonNull(action);
			if(size() <= 0) return;
			int index = firstIndex;
			while(index != -1){
				action.accept(input, values[index]);
				index = (int)links[index];
			}
		}
		
		@Override
		public boolean matchesAny(Predicate<V> filter) {
			Objects.requireNonNull(filter);
			if(size() <= 0) return false;
			int index = firstIndex;
			while(index != -1){
				if(filter.test(values[index])) return true;
				index = (int)links[index];
			}
			return false;
		}
		
		@Override
		public boolean matchesNone(Predicate<V> filter) {
			Objects.requireNonNull(filter);
			if(size() <= 0) return true;
			int index = firstIndex;
			while(index != -1){
				if(filter.test(values[index])) return false;
				index = (int)links[index];
			}
			return true;
		}
		
		@Override
		public boolean matchesAll(Predicate<V> filter) {
			Objects.requireNonNull(filter);
			if(size() <= 0) return true;
			int index = firstIndex;
			while(index != -1){
				if(!filter.test(values[index])) return false;
				index = (int)links[index];
			}
			return true;
		}
		
		@Override
		public <E> E reduce(E identity, BiFunction<E, V, E> operator) {
			Objects.requireNonNull(operator);
			E state = identity;
			int index = firstIndex;
			while(index != -1) {
				state = operator.apply(state, values[index]);
				index = (int)links[index];
			}
			return state;
		}
		
		@Override
		public Optional<V> reduce(ObjectObjectUnaryOperator<V, V> operator) {
			Objects.requireNonNull(operator);
			V state = null;
			boolean empty = true;
			int index = firstIndex;
			while(index != -1) {
				if(empty) {
					empty = false;
					state = values[index];
					index = (int)links[index];
					continue;
				}
				state = operator.apply(state, values[index]);
				index = (int)links[index];
			}
			return empty ? Optional.empty() : Optional.ofNullable(state);
		}
		
		@Override
		public Optional<V> findFirst(Predicate<V> filter) {
			Objects.requireNonNull(filter);
			if(size() <= 0) return Optional.empty();
			int index = firstIndex;
			while(index != -1){
				if(filter.test(values[index])) return Optional.ofNullable(values[index]);
				index = (int)links[index];
			}
			return Optional.empty();
		}
		
		@Override
		public int count(Predicate<V> filter) {
			Objects.requireNonNull(filter);
			if(size() <= 0) return 0;
			int result = 0;
			int index = firstIndex;
			while(index != -1){
				if(filter.test(values[index])) result++;
				index = (int)links[index];
			}
			return result;
		}
	}
	
	private class FastEntryIterator extends MapIterator implements ObjectListIterator<Float2ObjectMap.Entry<V>> {
		MapEntry entry = new MapEntry();
		
		public FastEntryIterator(boolean start) { super(start); }
		public FastEntryIterator(float from) {
			super(from);
		}
		
		@Override
		public Float2ObjectMap.Entry<V> next() {
			entry.index = nextEntry();
			return entry;
		}
		
		@Override
		public Float2ObjectMap.Entry<V> previous() {
			entry.index = previousEntry();
			return entry;
		}
		
		@Override
		public void set(Float2ObjectMap.Entry<V> entry) { throw new UnsupportedOperationException(); }

		@Override
		public void add(Float2ObjectMap.Entry<V> entry) { throw new UnsupportedOperationException(); }
	}
	
	private class EntryIterator extends MapIterator implements ObjectListIterator<Float2ObjectMap.Entry<V>> {
		
		public EntryIterator(boolean start) { super(start); }
		public EntryIterator(float from) {
			super(from);
		}
		
		@Override
		public Float2ObjectMap.Entry<V> next() {
			return new MapEntry(nextEntry());
		}
		
		@Override
		public Float2ObjectMap.Entry<V> previous() {
			return new MapEntry(previousEntry());
		}
		
		@Override
		public void remove() { throw new UnsupportedOperationException(); }

		@Override
		public void set(Float2ObjectMap.Entry<V> entry) { throw new UnsupportedOperationException(); }

		@Override
		public void add(Float2ObjectMap.Entry<V> entry) { throw new UnsupportedOperationException(); }
	}
	
	private class KeyIterator extends MapIterator implements FloatListIterator {
		
		public KeyIterator(boolean start) { super(start); }
		public KeyIterator(float from) {
			super(from);
		}
		
		@Override
		public float previousFloat() {
			return keys[previousEntry()];
		}

		@Override
		public float nextFloat() {
			return keys[nextEntry()];
		}
		
		@Override
		public void set(float e) { throw new UnsupportedOperationException(); }
		@Override
		public void add(float e) { throw new UnsupportedOperationException(); }
	}
	
	private class ValueIterator extends MapIterator implements ObjectListIterator<V> {
		public ValueIterator(boolean start) { super(start); }
		
		@Override
		public V previous() {
			return values[previousEntry()];
		}

		@Override
		public V next() {
			return values[nextEntry()];
		}

		@Override
		public void set(V e) { throw new UnsupportedOperationException(); }

		@Override
		public void add(V e) { throw new UnsupportedOperationException(); }
		
	}
	
	private class MapIterator {
		boolean forward;
		int previous = -1;
		int next = -1;
		int current = -1;
		int index = 0;
		
		MapIterator(boolean start) {
			this.forward = start;
			if(start) next = firstIndex;
			else previous = lastIndex;
		}
		
		MapIterator(float from) {
			if(Float.floatToIntBits(from) == 0) {
				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(Float.hashCode(from)) & mask;
				while(Float.floatToIntBits(keys[pos]) != 0) {
					if(Float.floatToIntBits(keys[pos]) == Float.floatToIntBits(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 (forward ? next : previous) != -1;
		}

		public boolean hasPrevious() {
			return (forward ? previous : next) != -1;
		}
		
		public int nextIndex() {
			ensureIndexKnown();
			return index;
		}
		
		public int previousIndex() {
			ensureIndexKnown();
			return index - 1;
		}
		
		public void remove() { throw new UnsupportedOperationException(); }
		
		public int previousEntry() {
			if(!hasPrevious()) throw new NoSuchElementException();
			if(forward) moveBackwards();
			else moveForwards();
			if(index >= 0) index--;
			return current;
		}
		
		public int nextEntry() {
			if(!hasNext()) throw new NoSuchElementException();
			if(forward) moveForwards();
			else moveBackwards();
			if(index >= 0) index++;
			return current;
		}
		
		private void moveBackwards() {
			current = previous;
			previous = (int)(links[current] >> 32);
			next = current;
		}
		
		private void moveForwards() {
			current = next;
			next = (int)(links[current]);
			previous = 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++);
				}
			}
		}
	}
}