package speiger.src.collections.PACKAGE.sets;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;

import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
import speiger.src.collections.PACKAGE.lists.ARRAY_LIST;
import speiger.src.collections.PACKAGE.lists.LIST;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.utils.ITERATORS;
#endif
import speiger.src.collections.utils.HashUtil;
import speiger.src.collections.utils.ITrimmable;
import speiger.src.collections.utils.SanityChecks;

/**
 * A Type Specific Custom implementation of the HashSet
 * Instead of using Wrapper Object Arrays for storing keys and values there is dedicated arrays for storing keys.
 * Extra to that there is a couple quality of life functions provided
 * @Type(T)
 */
public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements ITrimmable
{
	/** The Backing keys array */
	protected transient KEY_TYPE[] keys;
	/** If a null value is present */
	protected transient boolean containsNull;
	/** Minimum array size the HashSet will be */
	protected transient int minCapacity;
	/** Index of the Null Value */
	protected transient int nullIndex;
	/** Maximum amount of Values that can be stored before the array gets expanded usually 75% */
	protected transient int maxFill;
	/** Max Index that is allowed to be searched through nullIndex - 1 */
	protected transient int mask;
	
	/** Amount of Elements stored in the HashSet */
	protected int size;
	/** How full the Array is allowed to get before resize */
	protected final float loadFactor;
	
	/**
	 * Default Constructor
	 */
	public HASH_SET() {
		this(HashUtil.DEFAULT_MIN_CAPACITY, HashUtil.DEFAULT_LOAD_FACTOR);
	}
	
	/**
	 * Constructor that defines the minimum capacity
	 * @param minCapacity the minimum capacity the HashSet is allowed to be.
	 * @throws IllegalStateException if the minimum capacity is negative
	 */
	public HASH_SET(int minCapacity) {
		this(minCapacity, HashUtil.DEFAULT_LOAD_FACTOR);
	}
	
	/**
	 * Constructor that defines the minimum capacity and load factor
	 * @param minCapacity the minimum capacity the HashSet is allowed to be.
	 * @param loadFactor the percentage of how full the backing array can be before they resize
	 * @throws IllegalStateException if the minimum capacity is negative
	 * @throws IllegalStateException if the loadfactor is either below/equal to 0 or above/equal to 1
	 */
	public HASH_SET(int minCapacity, float loadFactor) {
		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);
		mask = nullIndex - 1;
		maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
		keys = NEW_KEY_ARRAY(nullIndex + 1);
	}
	
	/**
	 * Helper constructor that allow to create a set from unboxed values
	 * @param array the elements that should be put into the set
	 */
	public HASH_SET(KEY_TYPE[] array) {
		this(array, 0, array.length, HashUtil.DEFAULT_LOAD_FACTOR);
	}
	
	/**
	 * Helper constructor that allow to create a set from unboxed values
	 * @param array the elements that should be put into the set
	 * @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 HASH_SET(KEY_TYPE[] array, float loadFactor) {
		this(array, 0, array.length, loadFactor);
	}
	
	/**
	 * Helper constructor that allow to create a set from unboxed values
	 * @param array the elements that should be put into the set
	 * @param offset the starting index within the array that should be used
	 * @param length the amount of elements used from the array
	 * @throws IllegalStateException if offset and length causes to step outside of the arrays range
	 */
	public HASH_SET(KEY_TYPE[] array, int offset, int length) {
		this(array, offset, length, HashUtil.DEFAULT_LOAD_FACTOR);
	}
	
	/**
	 * Helper constructor that allow to create a set from unboxed values
	 * @param array the elements that should be put into the set
	 * @param offset the starting index within the array that should be used
	 * @param length the amount of elements used from the array
	 * @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
	 * @throws IllegalStateException if offset and length causes to step outside of the arrays range
	 */
	public HASH_SET(KEY_TYPE[] array, int offset, int length, float loadFactor) {
		this(length < 0 ? 0 : length);
		SanityChecks.checkArrayCapacity(array.length, offset, length);
		for(int i = 0;i<length;i++) add(array[offset+i]);
	}
	
	/**
	 * A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
	 * @param collection the set the elements should be added to the Set
	 */
	@Deprecated
	public HASH_SET(Collection<? extends CLASS_TYPE> collection) {
		this(collection, HashUtil.DEFAULT_LOAD_FACTOR);
	}
	
	/**
	 * A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
	 * @param collection the set the elements should be added to the Set
	 * @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
	 */
	@Deprecated
	public HASH_SET(Collection<? extends CLASS_TYPE> collection, float loadFactor) {
		this(collection.size(), loadFactor);
		addAll(collection);
	}
	
	/**
	 * A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
	 * @param collection the set the elements should be added to the Set
	 */
	public HASH_SET(COLLECTION KEY_GENERIC_TYPE collection) {
		this(collection, HashUtil.DEFAULT_LOAD_FACTOR);
	}
	
	/**
	 * A Helper constructor that allows to create a Set with exactly the same values as the provided collection.
	 * @param collection the set the elements should be added to the Set
	 * @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 HASH_SET(COLLECTION KEY_GENERIC_TYPE collection, float loadFactor) {
		this(collection.size());
		addAll(collection);
	}
	
	/**
	 * A Helper constructor that allows to create a set from a iterator of an unknown size
	 * @param iterator the elements that should be added to the set
	 */
	public HASH_SET(Iterator<CLASS_TYPE> iterator) {
		this(iterator, HashUtil.DEFAULT_LOAD_FACTOR);
	}
	
	/**
	 * A Helper constructor that allows to create a set from a iterator of an unknown size
	 * @param iterator the elements that should be added to the set
	 * @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 HASH_SET(Iterator<CLASS_TYPE> iterator, float loadFactor) {
#if !TYPE_OBJECT
		this(ITERATORS.wrap(iterator), loadFactor);
#else
		this(HashUtil.DEFAULT_MIN_CAPACITY, loadFactor);
		while(iterator.hasNext()) add(iterator.next());
#endif
	}
	
#if !TYPE_OBJECT
	/**
	 * A Helper constructor that allows to create a set from a iterator of an unknown size
	 * @param iterator the elements that should be added to the set
	 */
	public HASH_SET(ITERATOR KEY_GENERIC_TYPE iterator) {
		this(iterator, HashUtil.DEFAULT_LOAD_FACTOR);
	}
	
	/**
	 * A Helper constructor that allows to create a set from a iterator of an unknown size
	 * @param iterator the elements that should be added to the set
	 * @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 HASH_SET(ITERATOR KEY_GENERIC_TYPE iterator, float loadFactor) {
		this(HashUtil.DEFAULT_MIN_CAPACITY, loadFactor);
		while(iterator.hasNext()) add(iterator.NEXT());
	}
	
#endif
	@Override
	public boolean add(KEY_TYPE o) {
		if(KEY_EQUALS_NULL(o)) {
			if(containsNull) return false;
			containsNull = true;
			onNodeAdded(nullIndex);
		}
		else {
			int pos = HashUtil.mix(KEY_TO_HASH(o)) & mask;
			KEY_TYPE current = keys[pos];
			if(KEY_EQUALS_NOT_NULL(current)) {
				if(KEY_EQUALS(current, o)) return false;
				while(KEY_EQUALS_NOT_NULL((current = keys[pos = (++pos & mask)])))
					if(KEY_EQUALS(current, o)) return false;
			}
			keys[pos] = o;
			onNodeAdded(pos);
		}
		if(size++ >= maxFill) rehash(HashUtil.arraySize(size+1, loadFactor));
		return true;
	}
	
	@Override
	@Deprecated
	public boolean addAll(Collection<? extends CLASS_TYPE> c) {
		if(loadFactor <= 0.5F) ensureCapacity(c.size());
		else ensureCapacity(c.size() + size());
		return super.addAll(c);
	}
	
	@Override
	public boolean addAll(COLLECTION KEY_GENERIC_TYPE c) {
		if(loadFactor <= 0.5F) ensureCapacity(c.size());
		else ensureCapacity(c.size() + size());		
		return super.addAll(c);
	}
	
	@Override
	public boolean contains(Object o) {
		if(o == null) return containsNull;
		int pos = HashUtil.mix(o.hashCode()) & mask;
		KEY_TYPE current = keys[pos];
		if(KEY_EQUALS_NULL(current)) return false;
		if(EQUALS_KEY_TYPE(current, o)) return true;
		while(true) {
			if(KEY_EQUALS_NULL((current = keys[pos = (++pos & mask)]))) return false;
			else if(EQUALS_KEY_TYPE(current, o)) return true;
		}
	}

	@Override
	public boolean remove(Object o) {
		if(o == null) return (containsNull ? removeNullIndex() : false);
		int pos = HashUtil.mix(o.hashCode()) & mask;
		KEY_TYPE current = keys[pos];
		if(KEY_EQUALS_NULL(current)) return false;
		if(EQUALS_KEY_TYPE(current, o)) return removeIndex(pos);
		while(true) {
			if(KEY_EQUALS_NULL((current = keys[pos = (++pos & mask)]))) return false;
			else if(EQUALS_KEY_TYPE(current, o)) return removeIndex(pos);
		}
	}

#if !TYPE_OBJECT
	@Override
	public boolean contains(KEY_TYPE o) {
		if(KEY_EQUALS_NULL(o)) return containsNull;
		int pos = HashUtil.mix(KEY_TO_HASH(o)) & mask;
		KEY_TYPE current = keys[pos];
		if(KEY_EQUALS_NULL(current)) return false;
		if(KEY_EQUALS(current, o)) return true;
		while(true) {
			if(KEY_EQUALS_NULL((current = keys[pos = (++pos & mask)]))) return false;
			else if(KEY_EQUALS(current, o)) return true;
		}
	}
	
	@Override
	public boolean remove(KEY_TYPE o) {
		if(KEY_EQUALS_NULL(o)) return containsNull ? removeNullIndex() : false;
		int pos = HashUtil.mix(KEY_TO_HASH(o)) & mask;
		KEY_TYPE current = keys[pos];
		if(KEY_EQUALS_NULL(current)) return false;
		if(KEY_EQUALS(current, o)) return removeIndex(pos);
		while(true) {
			if(KEY_EQUALS_NULL((current = keys[pos = (++pos & mask)]))) return false;
			else if(KEY_EQUALS(current, o)) return removeIndex(pos);
		}
	}
	
#endif
	@Override
	public boolean trim(int size) {
		int newSize = HashUtil.nextPowerOfTwo((int)Math.ceil(size / loadFactor));
		if(newSize >= nullIndex || size >= Math.min((int)Math.ceil(newSize * loadFactor), newSize - 1)) return false;
		try {
			rehash(newSize);
		}
		catch(OutOfMemoryError e) { return false; }
		return true;
	}
	
	@Override
	public void clearAndTrim(int size) {
		if(nullIndex <= size) {
			clear();
			return;
		}
		nullIndex = size;
		mask = nullIndex - 1;
		maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
		keys = NEW_KEY_ARRAY(nullIndex + 1);
	}
	
	private void ensureCapacity(int newCapacity) {
		int size = HashUtil.arraySize(newCapacity, loadFactor);
		if(size > nullIndex) rehash(size);
	}
	
	protected boolean removeIndex(int pos) {
		if(pos == nullIndex) return containsNull ? removeNullIndex() : false;
		keys[pos] = EMPTY_KEY_VALUE;
		size--;
		onNodeRemoved(pos);
		shiftKeys(pos);
		if(nullIndex > minCapacity && size < maxFill / 4 && nullIndex > HashUtil.DEFAULT_MIN_CAPACITY) rehash(nullIndex / 2);
		return true;
	}
	
	protected boolean removeNullIndex() {
		containsNull = false;
		keys[nullIndex] = EMPTY_KEY_VALUE;
		size--;
		onNodeRemoved(nullIndex);
		if(nullIndex > minCapacity && size < maxFill / 4 && nullIndex > HashUtil.DEFAULT_MIN_CAPACITY) rehash(nullIndex / 2);
		return true;
	}
	
	protected void onNodeAdded(int pos) {
		
	}
	
	protected void onNodeRemoved(int pos) {
		
	}
	
	protected void onNodeMoved(int from, int to) {
		
	}
	
	protected void shiftKeys(int startPos) {
		int slot, last;
		KEY_TYPE current;
		while(true) {
			startPos = ((last = startPos) + 1) & mask;
			while(true){
				if(KEY_EQUALS_NULL((current = keys[startPos]))) {
					keys[last] = EMPTY_KEY_VALUE;
					return;
				}
				slot = HashUtil.mix(KEY_TO_HASH(current)) & mask;
				if(last <= startPos ? (last >= slot || slot > startPos) : (last >= slot && slot > startPos)) break;
				startPos = ++startPos & mask;
			}
			keys[last] = current;
			onNodeMoved(startPos, last);
		}
	}
	
	protected void rehash(int newSize) {
		int newMask = newSize - 1;
		KEY_TYPE[] newKeys = NEW_KEY_ARRAY(newSize + 1);
		for(int i = nullIndex, pos = 0, j = (size - (containsNull ? 1 : 0));j-- != 0;) {
			while(KEY_EQUALS_NULL(keys[--i]));
			if(KEY_EQUALS_NOT_NULL(newKeys[pos = HashUtil.mix(KEY_TO_HASH(keys[i])) & newMask]))
				while(KEY_EQUALS_NOT_NULL(newKeys[pos = (++pos & newMask)]));
			newKeys[pos] = keys[i];
		}
		nullIndex = newSize;
		mask = newMask;
		maxFill = Math.min((int)Math.ceil(nullIndex * loadFactor), nullIndex - 1);
		keys = newKeys;
	}
	
	@Override
	public ITERATOR KEY_GENERIC_TYPE iterator() {
		return new SetIterator();
	}
	
	@Override
	public void clear() {
		if(size == 0) return;
		size = 0;
		containsNull = false;
		Arrays.fill(keys, EMPTY_KEY_VALUE);
	}
	
	@Override
	public int size() {
		return size;
	}
	
	private class SetIterator implements ITERATOR KEY_GENERIC_TYPE {
		int pos = nullIndex;
		int lastReturned = -1;
		int nextIndex = Integer.MIN_VALUE;
		boolean returnNull = containsNull;
		LIST KEY_GENERIC_TYPE wrapped = null;
		
		@Override
		public boolean hasNext() {
			if(nextIndex == Integer.MIN_VALUE) {
				if(returnNull) {
					returnNull = false;
					nextIndex = nullIndex;
				}
				else
				{
					while(true) {
						if(--pos < 0) {
							if(wrapped == null || wrapped.size() <= -pos - 1) break;
							nextIndex = -pos - 1;
							break;
						}
						if(KEY_EQUALS_NOT_NULL(keys[pos])){
							nextIndex = pos;
							break;
						}
					}
				}
			}
			return nextIndex != Integer.MIN_VALUE;
		}
		
		@Override
		public KEY_TYPE NEXT() {
			if(!hasNext()) throw new NoSuchElementException();
			if(nextIndex < 0){
				lastReturned = Integer.MAX_VALUE;
				KEY_TYPE value = wrapped.GET_KEY(nextIndex);
				nextIndex = Integer.MIN_VALUE;
				return value;
			}
			KEY_TYPE value = keys[(lastReturned = nextIndex)];
			nextIndex = Integer.MIN_VALUE;
			return value;
		}
		
		@Override
		public void remove() {
			if(lastReturned == -1) throw new IllegalStateException();
			if(lastReturned == nullIndex) {
				containsNull = false;
				keys[nullIndex] = EMPTY_KEY_VALUE;
			}
			else if(pos >= 0) shiftKeys(pos);
			else {
				HASH_SET.this.remove(wrapped.GET_KEY(-pos - 1));
				return;
			}
			size--;
			lastReturned = -1;
		}
		
		private void shiftKeys(int startPos) {
			int slot, last;
			KEY_TYPE current;
			while(true) {
				startPos = ((last = startPos) + 1) & mask;
				while(true){
					if(KEY_EQUALS_NULL((current = keys[startPos]))) {
						keys[last] = EMPTY_KEY_VALUE;
						return;
					}
					slot = HashUtil.mix(KEY_TO_HASH(current)) & mask;
					if(last <= startPos ? (last >= slot || slot > startPos) : (last >= slot && slot > startPos)) break;
					startPos = ++startPos & mask;
				}
				if(startPos < last) {
					if(wrapped == null) wrapped = new ARRAY_LISTBRACES(2);
					wrapped.add(keys[startPos]);
				}
				keys[last] = current;
			}
		}
	}
}