/*
 * Copyright (C) 2002-2015 Sebastiano Vigna
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package squidpony.squidmath;

import squidpony.annotation.Beta;
import squidpony.annotation.GwtIncompatible;

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.SortedSet;

/**
 * A generic linked hash set with with a fast implementation, originally from fastutil as ObjectLinkedOpenHashSet but modified to support indexed access.
 * <p>
 * <P>
 * Instances of this class use a hash table to represent a set. The table is
 * filled up to a specified <em>load factor</em>, and then doubled in size to
 * accommodate new entries. If the table is emptied below <em>one fourth</em> of
 * the load factor, it is halved in size. However, halving is not performed when
 * deleting entries from an iterator, as it would interfere with the iteration
 * process.
 *
 * <p>
 * Note that {@link #clear()} does not modify the hash table size. Rather, a
 * family of {@linkplain #trim() trimming methods} lets you control the size of
 * the table; this is particularly useful if you reuse instances of this class.
 *
 * <P>
 * Iterators generated by this set will enumerate elements in the same order in
 * which they have been added to the set (addition of elements already present
 * in the set does not change the iteration order). Note that this order has
 * nothing in common with the natural order of the keys. The order is kept by
 * means of an array list, represented <i>via</i> an IntVLA parallel to the
 * table that can be modified with methods like {@link #shuffle(RNG)}.
 *
 * <P>
 * This class implements the interface of a sorted set, so to allow easy access
 * of the iteration order: for instance, you can get the first element in
 * iteration order with {@code first()} without having to create an iterator;
 * however, this class partially violates the {@link java.util.SortedSet}
 * contract because all subset methods throw an exception and
 * {@link #comparator()} returns always <code>null</code>.
 *
 * <p>
 * Additional methods, such as <code>addAndMoveToFirst()</code>, make it easy to
 * use instances of this class as a cache (e.g., with LRU policy).
 *
 * <P>
 * The iterators provided by this class are type-specific
 * {@linkplain java.util.ListIterator list iterators}, and can be started at any
 * element <em>which is in the set</em> (if the provided element is not in the
 * set, a {@link NoSuchElementException} exception will be thrown). If, however,
 * the provided element is not the first or last element in the set, the first
 * access to the list index will require linear time, as in the worst case the
 * entire set must be scanned in iteration order to retrieve the positional
 * index of the starting element.
 * <br>
 * Thank you, Sebastiano Vigna, for making FastUtil available to the public with such high quality.
 * <br>
 * See https://github.com/vigna/fastutil for the original library.
 * @author Sebastiano Vigna (responsible for all the hard parts)
 * @author Tommy Ettinger (mostly responsible for squashing several layers of parent classes into one monster class)
 */
@Beta
public class OrderedSet<K> implements SortedSet<K>, java.io.Serializable, Cloneable {
    private static final long serialVersionUID = 0L;
    /**
     * The array of keys.
     */
    protected K[] key;
    /**
     * The array of values.
     */
    //protected V[] value;
    /**
     * The mask for wrapping a position counter.
     */
    protected int mask;
    /**
     * Whether this set contains the key zero.
     */
    protected boolean containsNull;
    /**
     * The index of the first entry in iteration order. It is valid iff {@link #size} is nonzero; otherwise, it contains -1.
     */
    protected int first = -1;
    /**
     * The index of the last entry in iteration order. It is valid iff {@link #size} is nonzero; otherwise, it contains -1.
     */
    protected int last = -1;
    /*
     * For each entry, the next and the previous entry in iteration order, stored as <code>((prev & 0xFFFFFFFFL) << 32) | (next & 0xFFFFFFFFL)</code>. The first entry contains predecessor -1, and the
     * last entry contains successor -1.
     */
    //protected long[] link;
    protected IntVLA order;
    /**
     * The current table size.
     */
    protected int n;
    /**
     * Threshold after which we rehash. It must be the table size times {@link #f}.
     */
    protected int maxFill;
    /**
     * Number of entries in the set (including the key zero, if present).
     */
    protected int size;
    /**
     * The acceptable load factor.
     */
    public final float f;

    /**
     * The initial default size of a hash table.
     */
    public static final int DEFAULT_INITIAL_SIZE = 16;
    /**
     * The default load factor of a hash table.
     */
    public static final float DEFAULT_LOAD_FACTOR = .375f; // .1875f; // .75f;
    /**
     * The load factor for a (usually small) table that is meant to be particularly fast.
     */
    public static final float FAST_LOAD_FACTOR = .5f;
    /**
     * The load factor for a (usually very small) table that is meant to be extremely fast.
     */
    public static final float VERY_FAST_LOAD_FACTOR = .25f;

    protected CrossHash.IHasher hasher = null;
    /**
     * Creates a new hash map.
     * <p>
     * <p>The actual table size will be the least power of two greater than <code>expected</code>/<code>f</code>.
     *
     * @param expected the expected number of elements in the hash set.
     * @param f        the load factor.
     */

    @SuppressWarnings("unchecked")
    public OrderedSet(final int expected, final float f) {
        if (f <= 0 || f > 1)
            throw new IllegalArgumentException("Load factor must be greater than 0 and smaller than or equal to 1");
        if (expected < 0) throw new IllegalArgumentException("The expected number of elements must be nonnegative");
        this.f = f;
        n = arraySize(expected, f);
        mask = n - 1;
        maxFill = maxFill(n, f);
        key = (K[]) new Object[n + 1];
        //link = new long[n + 1];
        order = new IntVLA(expected);
        hasher = CrossHash.defaultHasher;
    }
    /**
     * Creates a new hash set with {@link #DEFAULT_LOAD_FACTOR} as load
     * factor.
     *
     * @param expected
     *            the expected number of elements in the hash set.
     */
    public OrderedSet(final int expected) {
        this(expected, DEFAULT_LOAD_FACTOR);
    }
    /**
     * Creates a new hash set with initial expected
     * {@link #DEFAULT_INITIAL_SIZE} elements and
     * {@link #DEFAULT_LOAD_FACTOR} as load factor.
     */
    public OrderedSet() {
        this(DEFAULT_INITIAL_SIZE, DEFAULT_LOAD_FACTOR);
    }

    /**
     * Creates a new hash set copying a given collection.
     *
     * @param c
     *            a {@link Collection} to be copied into the new hash set.
     * @param f
     *            the load factor.
     */
    public OrderedSet(final Collection<? extends K> c,
                      final float f) {
        this(c.size(), f);
        addAll(c);
    }
    /**
     * Creates a new hash set with {@link #DEFAULT_LOAD_FACTOR} as load
     * factor copying a given collection.
     *
     * @param c
     *            a {@link Collection} to be copied into the new hash set.
     */
    public OrderedSet(final Collection<? extends K> c) {
        this(c, DEFAULT_LOAD_FACTOR);
    }

    /**
     * Creates a new hash set using elements provided by a type-specific
     * iterator.
     *
     * @param i
     *            a type-specific iterator whose elements will fill the set.
     * @param f
     *            the load factor.
     */
    public OrderedSet(final Iterator<? extends K> i, final float f) {
        this(DEFAULT_INITIAL_SIZE, f);
        while (i.hasNext())
            add(i.next());
    }
    /**
     * Creates a new hash set with {@link #DEFAULT_LOAD_FACTOR} as load
     * factor using elements provided by a type-specific iterator.
     *
     * @param i
     *            a type-specific iterator whose elements will fill the set.
     */
    public OrderedSet(final Iterator<? extends K> i) {
        this(i, DEFAULT_LOAD_FACTOR);
    }

    /**
     * Creates a new hash set and fills it with the elements of a given array.
     *
     * @param a
     *            an array whose elements will be used to fill the set.
     * @param offset
     *            the first element to use.
     * @param length
     *            the number of elements to use.
     * @param f
     *            the load factor.
     */
    public OrderedSet(final K[] a, final int offset,
                                   final int length, final float f) {
        this(length < 0 ? 0 : length, f);
        if ( a == null ) throw new NullPointerException( "Array passed to OrderedSet constructor cannot be null" );
        if ( offset < 0 ) throw new ArrayIndexOutOfBoundsException( "Offset (" + offset + ") is negative" );
        if ( length < 0 ) throw new IllegalArgumentException( "Length (" + length + ") is negative" );
        if ( offset + length > a.length ) {
            throw new ArrayIndexOutOfBoundsException(
                    "Last index (" + ( offset + length ) + ") is greater than array length (" + a.length + ")" );
        }
        for (int i = 0; i < length; i++)
            add(a[offset + i]);
    }
    /**
     * Creates a new hash set with {@link #DEFAULT_LOAD_FACTOR} as load
     * factor and fills it with the elements of a given array.
     *
     * @param a
     *            an array whose elements will be used to fill the set.
     * @param offset
     *            the first element to use.
     * @param length
     *            the number of elements to use.
     */
    public OrderedSet(final K[] a, final int offset,
                                   final int length) {
        this(a, offset, length, DEFAULT_LOAD_FACTOR);
    }
    /**
     * Creates a new hash set copying the elements of an array.
     *
     * @param a
     *            an array to be copied into the new hash set.
     * @param f
     *            the load factor.
     */
    public OrderedSet(final K[] a, final float f) {
        this(a, 0, a.length, f);
    }
    /**
     * Creates a new hash set with {@link #DEFAULT_LOAD_FACTOR} as load
     * factor copying the elements of an array.
     *
     * @param a
     *            an array to be copied into the new hash set.
     */
    public OrderedSet(final K[] a) {
        this(a, DEFAULT_LOAD_FACTOR);
    }

    /**
     * Creates a new hash map.
     * <p>
     * <p>The actual table size will be the least power of two greater than <code>expected</code>/<code>f</code>.
     *
     * @param expected the expected number of elements in the hash set.
     * @param f         the load factor.
     * @param hasher used to hash items; typically only needed when K is an array, where CrossHash has implementations
     */
    @SuppressWarnings("unchecked")
    public OrderedSet(final int expected, final float f, CrossHash.IHasher hasher) {
        if (f <= 0 || f > 1)
            throw new IllegalArgumentException("Load factor must be greater than 0 and smaller than or equal to 1");
        if (expected < 0) throw new IllegalArgumentException("The expected number of elements must be nonnegative");
        this.f = f;
        n = arraySize(expected, f);
        mask = n - 1;
        maxFill = maxFill(n, f);
        key = (K[]) new Object[n + 1];
        //link = new long[n + 1];
        order = new IntVLA(expected);
        this.hasher = (hasher == null) ? CrossHash.defaultHasher : hasher;
    }

    /**
     * Creates a new hash set with {@link #DEFAULT_LOAD_FACTOR} as load
     * factor.
     *
     * @param hasher used to hash items; typically only needed when K is an array, where CrossHash has implementations
     */
    public OrderedSet(CrossHash.IHasher hasher) {
        this(DEFAULT_INITIAL_SIZE, DEFAULT_LOAD_FACTOR, hasher);
    }

    /**
     * Creates a new hash set with {@link #DEFAULT_LOAD_FACTOR} as load
     * factor.
     *
     * @param hasher used to hash items; typically only needed when K is an array, where CrossHash has implementations
     */
    public OrderedSet(final int expected, CrossHash.IHasher hasher) {
        this(expected, DEFAULT_LOAD_FACTOR, hasher);
    }

    /**
     * Creates a new hash set copying a given collection.
     *
     * @param c
     *            a {@link Collection} to be copied into the new hash set.
     * @param f
     *            the load factor.
     * @param hasher used to hash items; typically only needed when K is an array, where CrossHash has implementations
     */
    public OrderedSet(final Collection<? extends K> c,
                      final float f, CrossHash.IHasher hasher) {
        this(c.size(), f, hasher);
        addAll(c);
    }
    /**
     * Creates a new hash set with {@link #DEFAULT_LOAD_FACTOR} as load
     * factor copying a given collection.
     *
     * @param c
     *            a {@link Collection} to be copied into the new hash set.
     * @param hasher used to hash items; typically only needed when K is an array, where CrossHash has implementations
     */
    public OrderedSet(final Collection<? extends K> c, CrossHash.IHasher hasher) {
        this(c, DEFAULT_LOAD_FACTOR, hasher);
    }

    /**
     * Creates a new hash set and fills it with the elements of a given array.
     *
     * @param a
     *            an array whose elements will be used to fill the set.
     * @param offset
     *            the first element to use.
     * @param length
     *            the number of elements to use.
     * @param f
     *            the load factor.
     */
    public OrderedSet(final K[] a, final int offset,
                      final int length, final float f, CrossHash.IHasher hasher) {
        this(length < 0 ? 0 : length, f, hasher);
        if ( a == null ) throw new NullPointerException( "Array passed to OrderedSet constructor cannot be null" );
        if ( offset < 0 ) throw new ArrayIndexOutOfBoundsException( "Offset (" + offset + ") is negative" );
        if ( length < 0 ) throw new IllegalArgumentException( "Length (" + length + ") is negative" );
        if ( offset + length > a.length ) {
            throw new ArrayIndexOutOfBoundsException(
                    "Last index (" + ( offset + length ) + ") is greater than array length (" + a.length + ")" );
        }
        for (int i = 0; i < length; i++)
            add(a[offset + i]);
    }
    /**
     * Creates a new hash set with {@link #DEFAULT_LOAD_FACTOR} as load
     * factor and fills it with the elements of a given array.
     *
     * @param a
     *            an array whose elements will be used to fill the set.
     * @param offset
     *            the first element to use.
     * @param length
     *            the number of elements to use.
     */
    public OrderedSet(final K[] a, final int offset,
                      final int length, CrossHash.IHasher hasher) {
        this(a, offset, length, DEFAULT_LOAD_FACTOR, hasher);
    }

    /**
     * Creates a new hash set copying the elements of an array.
     *
     * @param a
     *            an array to be copied into the new hash set.
     * @param f
     *            the load factor.
     */
    public OrderedSet(final K[] a, final float f, CrossHash.IHasher hasher) {
        this(a, 0, a.length, f, hasher);
    }
    /**
     * Creates a new hash set with {@link #DEFAULT_LOAD_FACTOR} as load
     * factor copying the elements of an array.
     *
     * @param a
     *            an array to be copied into the new hash set.
     */
    public OrderedSet(final K[] a, CrossHash.IHasher hasher) {
        this(a, DEFAULT_LOAD_FACTOR, hasher);
    }

    private int realSize() {
        return containsNull ? size - 1 : size;
    }
    private void ensureCapacity(final int capacity) {
        final int needed = arraySize(capacity, f);
        if (needed > n)
            rehash(needed);
    }
    private void tryCapacity(final long capacity) {
        final int needed = (int) Math.min(
                1 << 30,
                Math.max(2, HashCommon.nextPowerOfTwo((long) Math.ceil(capacity
                        / f))));
        if (needed > n)
            rehash(needed);
    }
    /** {@inheritDoc} */
    public boolean addAll(Collection<? extends K> c) {
        // The resulting collection will be at least c.size() big
        if (f <= .5)
            ensureCapacity(c.size()); // The resulting collection will be sized
            // for c.size() elements
        else
            tryCapacity(size() + c.size()); // The resulting collection will be
        // tentatively sized for size() +
        // c.size() elements
        boolean retVal = false;
        final Iterator<? extends K> i = c.iterator();
        int n = c.size();
        while (n-- != 0)
            if (add(i.next()))
                retVal = true;
        return retVal;
    }
    public boolean add(final K k) {
        int pos;
        if (k == null) {
            if (containsNull)
                return false;
            pos = n;
            containsNull = true;
        } else {
            K curr;
            final K[] key = this.key;
            // The starting point.
            if (!((curr = key[pos = HashCommon.mix(hasher.hash(k)) & mask]) == null)) {
                if (curr.equals(k))
                    return false;
                while (!((curr = key[pos = (pos + 1) & mask]) == null))
                    if (curr.equals(k))
                        return false;
            }
            key[pos] = k;
        }
        if (size == 0) {
            first = last = pos;
        } else {
            last = pos;
        }
        order.add(pos);
        if (size++ >= maxFill)
            rehash(arraySize(size + 1, f));
        return true;
    }
    /**
     * Add a random element if not present, get the existing value if already
     * present.
     *
     * This is equivalent to (but faster than) doing a:
     *
     * <pre>
     * K exist = set.get(k);
     * if (exist == null) {
     * 	set.add(k);
     * 	exist = k;
     * }
     * </pre>
     */
    public K addOrGet(final K k) {
        int pos;
        if (k == null) {
            if (containsNull)
                return key[n];
            pos = n;
            containsNull = true;
        } else {
            K curr;
            final K[] key = this.key;
            // The starting point.
            if (!((curr = key[pos = HashCommon.mix(hasher.hash(k)) & mask]) == null)) {
                if (curr.equals(k))
                    return curr;
                while (!((curr = key[pos = (pos + 1) & mask]) == null))
                    if (curr.equals(k))
                        return curr;
            }
            key[pos] = k;
        }

        if (size == 0) {
            first = last = pos;
            // Special case of SET_UPPER_LOWER( link[ pos ], -1, -1 );
            //link[pos] = -1L;
        } else {
            //link[last] ^= ((link[last] ^ (pos & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
            //link[pos] = ((last & 0xFFFFFFFFL) << 32) | (-1 & 0xFFFFFFFFL);
            last = pos;
        }
        order.add(pos);
        if (size++ >= maxFill)
            rehash(arraySize(size + 1, f));
        return k;
    }

    /**
     * Shifts left entries with the specified hash code, starting at the
     * specified position, and empties the resulting free entry.
     *
     * @param pos
     *            a starting position.
     */
    protected final void shiftKeys(int pos) {
        // Shift entries with the same hash.
        int last, slot;
        K curr;
        final K[] key = this.key;
        for (;;) {
            pos = ((last = pos) + 1) & mask;
            for (;;) {
                if ((curr = key[pos]) == null) {
                    key[last] = null;
                    return;
                }
                slot = HashCommon.mix(hasher.hash(curr))
                        & mask;
                if (last <= pos ? last >= slot || slot > pos : last >= slot
                        && slot > pos)
                    break;
                pos = (pos + 1) & mask;
            }
            key[last] = curr;
            fixOrder(pos, last);
        }
    }

    private boolean removeEntry(final int pos) {
        size--;
        fixOrder(pos);
        shiftKeys(pos);
        if (size < maxFill / 4 && n > DEFAULT_INITIAL_SIZE)
            rehash(n / 2);
        return true;
    }
    private boolean removeNullEntry() {
        containsNull = false;
        key[n] = null;
        size--;
        fixOrder(n);
        if (size < maxFill / 4 && n > DEFAULT_INITIAL_SIZE)
            rehash(n / 2);
        return true;
    }
    public boolean remove(final Object k) {
        if (k == null) {
            return containsNull && removeNullEntry();
        }
        K curr;
        final K[] key = this.key;
        int pos;
        // The starting point.
        if ((curr = key[pos = HashCommon.mix(hasher.hash(k)) & mask]) == null)
            return false;
        if (k.equals(curr))
            return removeEntry(pos);
        while (true) {
            if ((curr = key[pos = (pos + 1) & mask]) == null)
                return false;
            if (k.equals(curr))
                return removeEntry(pos);
        }
    }
    /**
     * Removes the first key in iteration order.
     *
     * @return the first key.
     * @throws NoSuchElementException
     *             is this set is empty.
     */
    public K removeFirst() {
        if (size == 0)
            throw new NoSuchElementException();
        final int pos = first;
        order.removeIndex(0);
        if(order.size > 0)
            first = order.get(0);
        else
            first = -1;
        // Abbreviated version of fixOrder(pos)
        /*first = (int) link[pos];
        if (0 <= first) {
            // Special case of SET_PREV( link[ first ], -1 )
            link[first] |= (-1 & 0xFFFFFFFFL) << 32;
        }*/
        final K k = key[pos];
        size--;
        if (k == null) {
            containsNull = false;
            key[n] = null;
        } else
            shiftKeys(pos);
        if (size < maxFill / 4 && n > DEFAULT_INITIAL_SIZE)
            rehash(n / 2);
        return k;
    }

    /**
     * Removes the the last key in iteration order.
     *
     * @return the last key.
     * @throws NoSuchElementException
     *             is this set is empty.
     */
    public K removeLast() {
        if (size == 0)
            throw new NoSuchElementException();
        final int pos = last;
        order.pop();
        if(order.size > 0)
            last = order.get(order.size - 1);
        else
            last = -1;

        // Abbreviated version of fixOrder(pos)
        /*
        last = (int) (link[pos] >>> 32);
        if (0 <= last) {
            // Special case of SET_NEXT( link[ last ], -1 )
            link[last] |= -1 & 0xFFFFFFFFL;
        }*/
        final K k = key[pos];
        size--;
        if (k == null) {
            containsNull = false;
            key[n] = null;
        } else
            shiftKeys(pos);
        if (size < maxFill / 4 && n > DEFAULT_INITIAL_SIZE)
            rehash(n / 2);
        return k;

    }

    private void moveIndexToFirst(final int i) {
        if(size <= 1 || first == i)
            return;
        order.moveToFirst(i);
        if (last == i) {
            last = order.peek();
            //last = (int) (link[i] >>> 32);
            // Special case of SET_NEXT( link[ last ], -1 );
            //link[last] |= -1 & 0xFFFFFFFFL;
        }/* else {
            final long linki = link[i];
            final int prev = (int) (linki >>> 32);
            final int next = (int) linki;
            link[prev] ^= ((link[prev] ^ (linki & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
            link[next] ^= ((link[next] ^ (linki & 0xFFFFFFFF00000000L)) & 0xFFFFFFFF00000000L);
        }
        link[first] ^= ((link[first] ^ ((i & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
        link[i] = ((-1 & 0xFFFFFFFFL) << 32) | (first & 0xFFFFFFFFL);
        */
        first = i;
    }
    private void moveIndexToLast(final int i) {
        if(size <= 1 || last == i)
            return;
        order.moveToLast(i);
        if (first == i) {
            first = order.get(0);
            //first = (int) link[i];
            // Special case of SET_PREV( link[ first ], -1 );
            //link[first] |= (-1 & 0xFFFFFFFFL) << 32;
        } /*else {
            final long linki = link[i];
            final int prev = (int) (linki >>> 32);
            final int next = (int) linki;
            link[prev] ^= ((link[prev] ^ (linki & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
            link[next] ^= ((link[next] ^ (linki & 0xFFFFFFFF00000000L)) & 0xFFFFFFFF00000000L);
        }
        link[last] ^= ((link[last] ^ (i & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
        link[i] = ((last & 0xFFFFFFFFL) << 32) | (-1 & 0xFFFFFFFFL);
        */
        last = i;
    }

    /**
     * Adds a key to the set; if the key is already present, it is moved to the
     * first position of the iteration order.
     *
     * @param k
     *            the key.
     * @return true if the key was not present.
     */
    public boolean addAndMoveToFirst(final K k) {
        int pos;
        if (k == null) {
            if (containsNull) {
                moveIndexToFirst(n);
                return false;
            }
            containsNull = true;
            pos = n;
        } else {
            // The starting point.
            final K key[] = this.key;
            pos = HashCommon.mix(hasher.hash(k)) & mask;
            while (!(key[pos] == null)) {
                if (k.equals(key[pos])) {
                    moveIndexToFirst(pos);
                    return false;
                }
                pos = (pos + 1) & mask;
            }
        }
        key[pos] = k;
        if (size == 0) {
            first = last = pos;
            // Special case of SET_UPPER_LOWER( link[ pos ], -1, -1 );
            //link[pos] = -1L;
        } else {
            //link[first] ^= ((link[first] ^ ((pos & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
            //link[pos] = ((-1 & 0xFFFFFFFFL) << 32) | (first & 0xFFFFFFFFL);
            first = pos;
        }
        order.insert(0, pos);
        if (size++ >= maxFill)
            rehash(arraySize(size, f));
        return true;
    }
    /**
     * Adds a key to the set; if the key is already present, it is moved to the
     * last position of the iteration order.
     *
     * @param k
     *            the key.
     * @return true if the key was not present.
     */
    public boolean addAndMoveToLast(final K k) {
        int pos;
        if (k == null) {
            if (containsNull) {
                moveIndexToLast(n);
                return false;
            }
            containsNull = true;
            pos = n;
        } else {
            // The starting point.
            final K key[] = this.key;
            pos = HashCommon.mix(hasher.hash(k)) & mask;
            // There's always an unused entry.
            while (!(key[pos] == null)) {
                if (k.equals(key[pos])) {
                    moveIndexToLast(pos);
                    return false;
                }
                pos = (pos + 1) & mask;
            }
        }
        key[pos] = k;
        if (size == 0) {
            first = last = pos;
            // Special case of SET_UPPER_LOWER( link[ pos ], -1, -1 );
            //link[pos] = -1L;
        } else {
            //link[last] ^= ((link[last] ^ (pos & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
            //link[pos] = ((last & 0xFFFFFFFFL) << 32) | (-1 & 0xFFFFFFFFL);
            last = pos;
        }
        order.add(pos);
        if (size++ >= maxFill)
            rehash(arraySize(size, f));
        return true;
    }

    /**
     * Returns the element of this set that is equal to the given key, or
     * <code>null</code>.
     *
     * @return the element of this set that is equal to the given key, or
     *         <code>null</code>.
     */
    public K get(final Object k) {
        if (k == null)
            return key[n]; // This is correct independently of the value of
        // containsNull and of the map being custom
        K curr;
        final K[] key = this.key;
        int pos;
        // The starting point.
        if ((curr = key[pos = HashCommon.mix(hasher.hash(k)) & mask]) == null)
            return null;
        if (k.equals(curr))
            return curr;
        // There's always an unused entry.
        while (true) {
            if ((curr = key[pos = (pos + 1) & mask]) == null)
                return null;
            if (k.equals(curr))
                return curr;
        }
    }

    public boolean contains(final Object k) {
        if (k == null)
            return containsNull;
        K curr;
        final K[] key = this.key;
        int pos;
        // The starting point.
        if ((curr = key[pos = HashCommon.mix(hasher.hash(k)) & mask]) == null)
            return false;
        if (k.equals(curr))
            return true;
        // There's always an unused entry.
        while (true) {
            if ((curr = key[pos = (pos + 1) & mask]) == null)
                return false;
            if (k.equals(curr))
                return true;
        }
    }
    /*
     * Removes all elements from this set.
     *
     * <P>To increase object reuse, this method does not change the table size.
     * If you want to reduce the table size, you must use {@link #trim()}.
     */
    public void clear() {
        if (size == 0)
            return;
        size = 0;
        containsNull = false;
        Arrays.fill(key, null);
        first = last = -1;
        order.clear();
    }

    public int size() {
        return size;
    }
    /**
     * Checks whether this collection contains all elements from the given
     * collection.
     *
     * @param c
     *            a collection.
     * @return <code>true</code> if this collection contains all elements of the
     *         argument.
     */
    public boolean containsAll(Collection<?> c) {
        int n = c.size();
        final Iterator<?> i = c.iterator();
        while (n-- != 0)
            if (!contains(i.next()))
                return false;
        return true;
    }
    /**
     * Retains in this collection only elements from the given collection.
     *
     * @param c
     *            a collection.
     * @return <code>true</code> if this collection changed as a result of the
     *         call.
     */
    public boolean retainAll(Collection<?> c) {
        boolean retVal = false;
        int n = size();
        final Iterator<?> i = iterator();
        while (n-- != 0) {
            if (!c.contains(i.next())) {
                i.remove();
                retVal = true;
            }
        }
        return retVal;
    }
    /**
     * Remove from this collection all elements in the given collection. If the
     * collection is an instance of this class, it uses faster iterators.
     *
     * @param c
     *            a collection.
     * @return <code>true</code> if this collection changed as a result of the
     *         call.
     */
    public boolean removeAll(Collection<?> c) {
        boolean retVal = false;
        int n = c.size();
        final Iterator<?> i = c.iterator();
        while (n-- != 0)
            if (remove(i.next()))
                retVal = true;
        return retVal;
    }
    public boolean isEmpty() {
        return size() == 0;
    }


    /**
     * Modifies the link vector so that the given entry is removed. This method will complete in logarithmic time.
     *
     * @param i the index of an entry.
     */
    protected void fixOrder(final int i) {
        if (size == 0) {
            first = last = -1;
            return;
        }
        order.removeValue(i);
        if (first == i) {
            first = order.get(0);
            //first = (int) link[i];
            /*
            if (0 <= first) {
                // Special case of SET_PREV( link[ first ], -1 )
                link[first] |= (-1 & 0xFFFFFFFFL) << 32;
            }
            return;
            */
        }
        if (last == i) {
            last = order.peek();
            //last = (int) (link[i] >>> 32);
            /*if (0 <= last) {
                // Special case of SET_NEXT( link[ last ], -1 )
                link[last] |= -1 & 0xFFFFFFFFL;
            }
            return;
            */
        }
        /*
        final long linki = link[i];
        final int prev = (int) (linki >>> 32);
        final int next = (int) linki;
        link[prev] ^= ((link[prev] ^ (linki & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
        link[next] ^= ((link[next] ^ (linki & 0xFFFFFFFF00000000L)) & 0xFFFFFFFF00000000L);
        */
    }

    /**
     * Modifies the ordering for a shift from s to d.
     * <br>
     * This method will complete in logarithmic time or better.
     *
     * @param s the source position.
     * @param d the destination position.
     */
    protected void fixOrder(int s, int d) {
        if (size == 1) {
            first = last = d;
            // Special case of SET_UPPER_LOWER( link[ d ], -1, -1 )
            //link[d] = -1L;
            order.set(0, d);
        }
        else if (first == s) {
            first = d;
            order.set(0, d);
            //link[(int) link[s]] ^= ((link[(int) link[s]] ^ ((d & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
            //link[d] = link[s];
        }
        else if (last == s) {
            last = d;
            order.set(order.size - 1, d);
            //link[(int) (link[s] >>> 32)] ^= ((link[(int) (link[s] >>> 32)] ^ (d & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
            //link[d] = link[s];
        }
        else
        {
            order.set(order.indexOf(s), d);
        }
        /*
        final long links = link[s];
        final int prev = (int) (links >>> 32);
        final int next = (int) links;
        link[prev] ^= ((link[prev] ^ (d & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
        link[next] ^= ((link[next] ^ ((d & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
        link[d] = links;
        */
    }

    /**
     * Returns the first element of this set in iteration order.
     *
     * @return the first element in iteration order.
     */
    public K first() {
        if (size == 0)
            throw new NoSuchElementException();
        return key[first];
    }
    /**
     * Returns the last element of this set in iteration order.
     *
     * @return the last element in iteration order.
     */
    public K last() {
        if (size == 0)
            throw new NoSuchElementException();
        return key[last];
    }
    public SortedSet<K> tailSet(K from) {
        throw new UnsupportedOperationException();
    }
    public SortedSet<K> headSet(K to) {
        throw new UnsupportedOperationException();
    }
    public SortedSet<K> subSet(K from, K to) {
        throw new UnsupportedOperationException();
    }
    public Comparator<? super K> comparator() {
        return null;
    }
    /**
     * A list iterator over a linked set.
     *
     * <P>
     * This class provides a list iterator over a linked hash set. The
     * constructor runs in constant time.
     */
    private class SetIterator implements ListIterator<K> {
        /**
         * The entry that will be returned by the next call to
         * {@link java.util.ListIterator#previous()} (or <code>null</code> if no
         * previous entry exists).
         */
        int prev = -1;
        /**
         * The entry that will be returned by the next call to
         * {@link java.util.ListIterator#next()} (or <code>null</code> if no
         * next entry exists).
         */
        int next = -1;
        /**
         * The last entry that was returned (or -1 if we did not iterate or used
         * {@link #remove()}).
         */
        int curr = -1;
        /**
         * The current index (in the sense of a {@link java.util.ListIterator}).
         * When -1, we do not know the current index.
         */
        int index = -1;
        SetIterator() {
            next = first;
            index = 0;
        }
        public boolean hasNext() {
            return next != -1;
        }
        public boolean hasPrevious() {
            return prev != -1;
        }
        public K next() {
            if (!hasNext())
                throw new NoSuchElementException();
            curr = next;
            if(++index >= order.size)
                next = -1;
            else
                next = order.get(index);//(int) link[curr];
            prev = curr;
            return key[curr];
        }
        public K previous() {
            if (!hasPrevious())
                throw new NoSuchElementException();
            curr = prev;
            if(--index < 1)
                prev = -1;
            else
                prev = order.get(index - 1);
            next = curr;
            return key[curr];
        }
        private void ensureIndexKnown() {
            if (index >= 0)
                return;
            if (prev == -1) {
                index = 0;
                return;
            }
            if (next == -1) {
                index = size;
                return;
            }
            index = 0;
        }
        public int nextIndex() {
            ensureIndexKnown();
            return index+1;
        }
        public int previousIndex() {
            ensureIndexKnown();
            return index - 1;
        }
        public void remove() {
            ensureIndexKnown();
            if (curr == -1)
                throw new IllegalStateException();
            if (curr == prev) {
				/*
				 * If the last operation was a next(), we are removing an entry
				 * that precedes the current index, and thus we must decrement
				 * it.
				 */
                if (--index >= 1)
                    prev = order.get(index - 1); //(int) (link[curr] >>> 32);
                else
                    prev = -1;
            } else {
                if (index < order.size - 1)
                    next = order.get(index + 1);
                else
                    next = -1;
            }
            /*
			 * Now we manually fix the pointers. Because of our knowledge of
			 * next and prev, this is going to be faster than calling
			 * fixOrder().
			 */
            if (prev == -1)
                first = next;
            if (next == -1)
                last = prev;
            order.removeIndex(index);
            size--;
            int last, slot, pos = curr;
            curr = -1;
            if (pos == n) {
                containsNull = false;
                key[n] = null;
                //order.removeValue(pos);
            } else {
                K curr;
                final K[] key = OrderedSet.this.key;
                // We have to horribly duplicate the shiftKeys() code because we
                // need to update next/prev.
                for (; ; ) {
                    pos = ((last = pos) + 1) & mask;
                    for (; ; ) {
                        if ((curr = key[pos]) == null) {
                            key[last] = null;
                            return;
                        }
                        slot = HashCommon.mix(hasher.hash(curr)) & mask;
                        if (last <= pos
                                ? last >= slot || slot > pos
                                : last >= slot && slot > pos)
                            break;
                        pos = (pos + 1) & mask;
                    }
                    key[last] = curr;
                    if (next == pos)
                        next = last;
                    if (prev == pos)
                        prev = last;
                    fixOrder(pos, last);
                }
            }

        }

        /**
         * Replaces the last element returned by {@link #next} or
         * {@link #previous} with the specified element (optional operation).
         * This call can be made only if neither {@link #remove} nor {@link
         * #add} have been called after the last call to {@code next} or
         * {@code previous}.
         *
         * @param k the element with which to replace the last element returned by
         *          {@code next} or {@code previous}
         * @throws UnsupportedOperationException if the {@code set} operation
         *                                       is not supported by this list iterator
         * @throws ClassCastException            if the class of the specified element
         *                                       prevents it from being added to this list
         * @throws IllegalArgumentException      if some aspect of the specified
         *                                       element prevents it from being added to this list
         * @throws IllegalStateException         if neither {@code next} nor
         *                                       {@code previous} have been called, or {@code remove} or
         *                                       {@code add} have been called after the last call to
         *                                       {@code next} or {@code previous}
         */
        @Override
        public void set(K k) {
            throw new UnsupportedOperationException("set() not supported on OrderedSet iterator");
        }

        /**
         * Inserts the specified element into the list (optional operation).
         * The element is inserted immediately before the element that
         * would be returned by {@link #next}, if any, and after the element
         * that would be returned by {@link #previous}, if any.  (If the
         * list contains no elements, the new element becomes the sole element
         * on the list.)  The new element is inserted before the implicit
         * cursor: a subsequent call to {@code next} would be unaffected, and a
         * subsequent call to {@code previous} would return the new element.
         * (This call increases by one the value that would be returned by a
         * call to {@code nextIndex} or {@code previousIndex}.)
         *
         * @param k the element to insert
         * @throws UnsupportedOperationException if the {@code add} method is
         *                                       not supported by this list iterator
         * @throws ClassCastException            if the class of the specified element
         *                                       prevents it from being added to this list
         * @throws IllegalArgumentException      if some aspect of this element
         *                                       prevents it from being added to this list
         */
        @Override
        public void add(K k) {
            throw new UnsupportedOperationException("add() not supported on OrderedSet iterator");
        }
    }
    public ListIterator<K> iterator() {
        return new SetIterator();
    }

    /**
     * A no-op for backward compatibility. The kind of tables implemented by this class never need rehashing.
     * <p>
     * <P>If you need to reduce the table size to fit exactly this set, use {@link #trim()}.
     *
     * @return true.
     * @see #trim()
     * @deprecated A no-op.
     */
    @Deprecated
    public boolean rehash() {
        return true;
    }

    /**
     * Rehashes the map, making the table as small as possible.
     * <p>
     * <P>This method rehashes the table to the smallest size satisfying the load factor. It can be used when the set will not be changed anymore, so to optimize access speed and size.
     * <p>
     * <P>If the table size is already the minimum possible, this method does nothing.
     *
     * @return true if there was enough memory to trim the map.
     * @see #trim(int)
     */
    public boolean trim() {
        final int l = arraySize(size, f);
        if (l >= n || size > maxFill(l, f)) return true;
        try {
            rehash(l);
        } catch (Exception cantDoIt) {
            return false;
        }
        return true;
    }

    /**
     * Rehashes this map if the table is too large.
     * <p>
     * <P>Let <var>N</var> be the smallest table size that can hold <code>max(n,{@link #size()})</code> entries, still satisfying the load factor. If the current table size is smaller than or equal to
     * <var>N</var>, this method does nothing. Otherwise, it rehashes this map in a table of size <var>N</var>.
     * <p>
     * <P>This method is useful when reusing maps. {@linkplain #clear() Clearing a map} leaves the table size untouched. If you are reusing a map many times, you can call this method with a typical
     * size to avoid keeping around a very large table just because of a few large transient maps.
     *
     * @param n the threshold for the trimming.
     * @return true if there was enough memory to trim the map.
     * @see #trim()
     */
    public boolean trim(final int n) {
        final int l = HashCommon.nextPowerOfTwo((int) Math.ceil(n / f));
        if (l >= n || size > maxFill(l, f)) return true;
        try {
            rehash(l);
        } catch (Exception cantDoIt) {
            return false;
        }
        return true;
    }

    /**
     * Rehashes the map.
     *
     * <P>
     * This method implements the basic rehashing strategy, and may be overriden
     * by subclasses implementing different rehashing strategies (e.g.,
     * disk-based rehashing). However, you should not override this method
     * unless you understand the internal workings of this class.
     *
     * @param newN
     *            the new size
     */

    @SuppressWarnings("unchecked")
    protected void rehash(final int newN) {
        final K key[] = this.key;
        final int mask = newN - 1; // Note that this is used by the hashing
        // macro
        final K newKey[] = (K[]) new Object[newN + 1];
        int i, pos, sz = order.size, originalFirst = first, originalLast = last;
        for (int q = 0; q < sz; q++) {
            i = order.get(q);
            if (key[i] == null)
                pos = newN;
            else {
                pos = HashCommon.mix(hasher.hash(key[i])) & mask;
                while (!(newKey[pos] == null))
                    pos = (pos + 1) & mask;
            }
            newKey[pos] = key[i];
            order.set(q, pos);
            if(i == originalFirst)
                first = pos;
            if(i == originalLast)
                last = pos;
        }
        n = newN;
        this.mask = mask;
        maxFill = maxFill(n, f);
        this.key = newKey;
    }
    /*
    @SuppressWarnings("unchecked")
    protected void rehash(final int newN) {
        final K key[] = this.key;
        final V value[] = this.value;
        final int mask = newN - 1; // Note that this is used by the hashing
        // macro
        final K newKey[] = (K[]) new Object[newN + 1];
        final V newValue[] = (V[]) new Object[newN + 1];
        int i = first, prev = -1, newPrev = -1, t, pos;
        final long link[] = this.link;
        final long newLink[] = new long[newN + 1];
        first = -1;
        for (int j = size; j-- != 0;) {
            if (((key[i]) == null))
                pos = newN;
            else {
                pos = (HashCommon.mix((key[i]).hashCode())) & mask;
                while (!((newKey[pos]) == null))
                    pos = (pos + 1) & mask;
            }
            newKey[pos] = key[i];
            newValue[pos] = value[i];
            if (prev != -1) {
                newLink[newPrev] ^= ((newLink[newPrev] ^ (pos & 0xFFFFFFFFL)) & 0xFFFFFFFFL);
                newLink[pos] ^= ((newLink[pos] ^ ((newPrev & 0xFFFFFFFFL) << 32)) & 0xFFFFFFFF00000000L);
                newPrev = pos;
            } else {
                newPrev = first = pos;
                // Special case of SET(newLink[ pos ], -1, -1);
                newLink[pos] = -1L;
            }
            t = i;
            i = (int) link[i];
            prev = t;
        }
        this.link = newLink;
        this.last = newPrev;
        if (newPrev != -1)
            // Special case of SET_NEXT( newLink[ newPrev ], -1 );
            newLink[newPrev] |= -1 & 0xFFFFFFFFL;
        n = newN;
        this.mask = mask;
        maxFill = maxFill(n, f);
        this.key = newKey;
        this.value = newValue;
    }
    */
    /**
     * Returns a deep copy of this map.
     *
     * <P>
     * This method performs a deep copy of this hash map; the data stored in the
     * map, however, is not cloned. Note that this makes a difference only for
     * object keys.
     *
     * @return a deep copy of this map.
     */
    @SuppressWarnings("unchecked")
    @GwtIncompatible
    public Object clone() {
        OrderedSet<K> c;
        try {
            c = (OrderedSet<K>) super.clone();
            c.key = (K[]) new Object[n + 1];
            System.arraycopy(key, 0, c.key, 0, n + 1);
            c.order = (IntVLA) order.clone();
            c.hasher = hasher;
            return c;
        } catch (Exception cantHappen) {
            throw new UnsupportedOperationException(cantHappen + (cantHappen.getMessage() != null ?
                    "; " + cantHappen.getMessage() : ""));
        }
    }
    /**
     * Returns a hash code for this set.
     *
     * This method overrides the generic method provided by the superclass.
     * Since <code>equals()</code> is not overriden, it is important that the
     * value returned by this method is the same value as the one returned by
     * the overriden method.
     *
     * @return a hash code for this set.
     */
    public int hashCode() {
        int h = 0;
        for (int j = realSize(), i = 0; j-- != 0;) {
            while (key[i] == null)
                i++;
            if (this != key[i])
                h += hasher.hash(key[i]);
            i++;
        }
        // Zero / null have hash zero.
        return h;
    }
    /**
     * Returns the maximum number of entries that can be filled before rehashing.
     *
     * @param n the size of the backing array.
     * @param f the load factor.
     * @return the maximum number of entries before rehashing.
     */
    public static int maxFill(final int n, final float f) {
        /* We must guarantee that there is always at least
		 * one free entry (even with pathological load factors). */
        return Math.min((int) Math.ceil(n * f), n - 1);
    }

    /**
     * Returns the maximum number of entries that can be filled before rehashing.
     *
     * @param n the size of the backing array.
     * @param f the load factor.
     * @return the maximum number of entries before rehashing.
     */
    public static long maxFill(final long n, final float f) {
		/* We must guarantee that there is always at least
		 * one free entry (even with pathological load factors). */
        return Math.min((long) Math.ceil(n * f), n - 1);
    }

    /**
     * Returns the least power of two smaller than or equal to 2<sup>30</sup> and larger than or equal to <code>Math.ceil( expected / f )</code>.
     *
     * @param expected the expected number of elements in a hash table.
     * @param f        the load factor.
     * @return the minimum possible size for a backing array.
     * @throws IllegalArgumentException if the necessary size is larger than 2<sup>30</sup>.
     */
    public static int arraySize(final int expected, final float f) {
        final long s = Math.max(2, HashCommon.nextPowerOfTwo((long) Math.ceil(expected / f)));
        if (s > (1 << 30))
            throw new IllegalArgumentException("Too large (" + expected + " expected elements with load factor " + f + ")");
        return (int) s;
    }

    private static class HashCommon {

        private HashCommon() {
        }

        /**
         * 2<sup>32</sup> &middot; &phi;, &phi; = (&#x221A;5 &minus; 1)/2.
         */
        private static final int INT_PHI = 0x9E3779B9;
        /**
         * The reciprocal of {@link #INT_PHI} modulo 2<sup>32</sup>.
         */
        private static final int INV_INT_PHI = 0x144cbc89;
        /**
         * 2<sup>64</sup> &middot; &phi;, &phi; = (&#x221A;5 &minus; 1)/2.
         */
        private static final long LONG_PHI = 0x9E3779B97F4A7C15L;
        /**
         * The reciprocal of {@link #LONG_PHI} modulo 2<sup>64</sup>.
         */
        private static final long INV_LONG_PHI = 0xf1de83e19937733dL;

        /**
         * Avalanches the bits of an integer by applying the finalisation step of MurmurHash3.
         * <br>
         * <br>This method implements the finalisation step of Austin Appleby's <a href="http://code.google.com/p/smhasher/">MurmurHash3</a>.
         * Its purpose is to avalanche the bits of the argument to within 0.25% bias.
         *
         * @param x an integer.
         * @return a hash value with good avalanching properties.
         */
        static int murmurHash3(int x) {
            x ^= x >>> 16;
            x *= 0x85ebca6b;
            x ^= x >>> 13;
            x *= 0xc2b2ae35;
            x ^= x >>> 16;
            return x;
        }


        /**
         * Avalanches the bits of a long integer by applying the finalisation step of MurmurHash3.
         * <br>
         * <br>This method implements the finalisation step of Austin Appleby's <a href="http://code.google.com/p/smhasher/">MurmurHash3</a>.
         * Its purpose is to avalanche the bits of the argument to within 0.25% bias.
         *
         * @param x a long integer.
         * @return a hash value with good avalanching properties.
         */
        static long murmurHash3(long x) {
            x ^= x >>> 33;
            x *= 0xff51afd7ed558ccdL;
            x ^= x >>> 33;
            x *= 0xc4ceb9fe1a85ec53L;
            x ^= x >>> 33;
            return x;
        }

        /**
         * Quickly mixes the bits of an integer.
         * <br>This method mixes the bits of the argument by multiplying by the golden ratio and
         * xorshifting the result. It is borrowed from <a href="https://github.com/OpenHFT/Koloboke">Koloboke</a>, and
         * it has slightly worse behaviour than {@link #murmurHash3(int)} (in open-addressing hash tables the average number of probes
         * is slightly larger), but it's much faster.
         *
         * @param x an integer.
         * @return a hash value obtained by mixing the bits of {@code x}.
         * @see #invMix(int)
         */
        static int mix(final int x) {
            final int h = x * INT_PHI;
            return h ^ (h >>> 16);
        }

        /**
         * The inverse of {@link #mix(int)}. This method is mainly useful to create unit tests.
         *
         * @param x an integer.
         * @return a value that passed through {@link #mix(int)} would give {@code x}.
         */
        static int invMix(final int x) {
            return (x ^ x >>> 16) * INV_INT_PHI;
        }

        /**
         * Quickly mixes the bits of a long integer.
         * <br>This method mixes the bits of the argument by multiplying by the golden ratio and
         * xorshifting twice the result. It is borrowed from <a href="https://github.com/OpenHFT/Koloboke">Koloboke</a>, and
         * it has slightly worse behaviour than {@link #murmurHash3(long)} (in open-addressing hash tables the average number of probes
         * is slightly larger), but it's much faster.
         *
         * @param x a long integer.
         * @return a hash value obtained by mixing the bits of {@code x}.
         */
        static long mix(final long x) {
            long h = x * LONG_PHI;
            h ^= h >>> 32;
            return h ^ (h >>> 16);
        }

        /**
         * The inverse of {@link #mix(long)}. This method is mainly useful to create unit tests.
         *
         * @param x a long integer.
         * @return a value that passed through {@link #mix(long)} would give {@code x}.
         */
        static long invMix(long x) {
            x ^= x >>> 32;
            x ^= x >>> 16;
            return (x ^ x >>> 32) * INV_LONG_PHI;
        }

        /**
         * Return the least power of two greater than or equal to the specified value.
         * <br>Note that this function will return 1 when the argument is 0.
         *
         * @param x an integer smaller than or equal to 2<sup>30</sup>.
         * @return the least power of two greater than or equal to the specified value.
         */
        static int nextPowerOfTwo(int x) {
            if (x == 0) return 1;
            x--;
            x |= x >> 1;
            x |= x >> 2;
            x |= x >> 4;
            x |= x >> 8;
            return (x | x >> 16) + 1;
        }

        /**
         * Return the least power of two greater than or equal to the specified value.
         * <br>Note that this function will return 1 when the argument is 0.
         *
         * @param x a long integer smaller than or equal to 2<sup>62</sup>.
         * @return the least power of two greater than or equal to the specified value.
         */
        static long nextPowerOfTwo(long x) {
            if (x == 0) return 1;
            x--;
            x |= x >> 1;
            x |= x >> 2;
            x |= x >> 4;
            x |= x >> 8;
            x |= x >> 16;
            return (x | x >> 32) + 1;
        }
    }
    @Override
    public Object[] toArray() {
        final Object[] a = new Object[size()];
        objectUnwrap(iterator(), a);
        return a;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> T[] toArray(T[] a) {
        final int size = size();
        objectUnwrap(iterator(), a);
        if (size < a.length)
            a[size] = null;
        return a;
    }


    /** Unwraps an iterator into an array starting at a given offset for a given number of elements.
     *
     * <P>This method iterates over the given type-specific iterator and stores the elements returned, up to a maximum of <code>length</code>, in the given array starting at <code>offset</code>. The
     * number of actually unwrapped elements is returned (it may be less than <code>max</code> if the iterator emits less than <code>max</code> elements).
     *
     * @param i a type-specific iterator.
     * @param array an array to contain the output of the iterator.
     * @param offset the first element of the array to be returned.
     * @param max the maximum number of elements to unwrap.
     * @return the number of elements unwrapped. */
    private static <K> int objectUnwrap(final Iterator<? extends K> i, final K array[], int offset, final int max ) {
        if ( max < 0 ) throw new IllegalArgumentException( "The maximum number of elements (" + max + ") is negative" );
        if ( offset < 0 || offset + max > array.length ) throw new IllegalArgumentException();
        int j = max;
        while ( j-- != 0 && i.hasNext() )
            array[ offset++ ] = i.next();
        return max - j - 1;
    }

    /** Unwraps an iterator into an array.
     *
     * <P>This method iterates over the given type-specific iterator and stores the elements returned in the given array. The iteration will stop when the iterator has no more elements or when the end
     * of the array has been reached.
     *
     * @param i a type-specific iterator.
     * @param array an array to contain the output of the iterator.
     * @return the number of elements unwrapped. */
    private static <K> int objectUnwrap(final Iterator<? extends K> i, final K array[] ) {
        return objectUnwrap(i, array, 0, array.length );
    }
    @Override
    public String toString() {
        final StringBuilder s = new StringBuilder();
        int n = size(), i = 0;
        boolean first = true;
        s.append("OrderedSet{");
        while (i < n) {
            if (first) first = false;
            else s.append(", ");
            s.append(getAt(i++));
        }
        s.append("}");
        return s.toString();
    }

    @Override
    public boolean equals(final Object o) {
        if (o == this)
            return true;
        if (!(o instanceof Set))
            return false;
        Set<?> s = (Set<?>) o;
        if (s.size() != size())
            return false;
        return containsAll(s);
    }
    @GwtIncompatible
    private void writeObject(java.io.ObjectOutputStream s)
            throws java.io.IOException {
        final ListIterator<K> i = iterator();
        s.defaultWriteObject();
        s.writeObject(hasher);
        for (int j = size; j-- != 0;)
            s.writeObject(i.next());
    }
    @GwtIncompatible
    @SuppressWarnings("unchecked")
    private void readObject(java.io.ObjectInputStream s)
            throws java.io.IOException, ClassNotFoundException {
        s.defaultReadObject();
        hasher = (CrossHash.IHasher) s.readObject();
        n = arraySize(size, f);
        maxFill = maxFill(n, f);
        mask = n - 1;
        final K key[] = this.key = (K[]) new Object[n + 1];
        final IntVLA order = this.order = new IntVLA(n + 1);
        int prev = -1;
        first = last = -1;
        K k;
        for (int i = size, pos; i-- != 0;) {
            k = (K) s.readObject();
            if (k == null) {
                pos = n;
                containsNull = true;
            } else {
                if (!(key[pos = HashCommon.mix(hasher.hash(k)) & mask] == null))
                    while (!(key[pos = (pos + 1) & mask] == null));
            }
            key[pos] = k;
            if (first != -1) {
                last = pos;
            } else {
                first = last = pos;
            }
            order.add(pos);
        }
    }
    /**
     * Gets the item at the given index in the iteration order in constant time (random-access).
     * @param idx the index in the iteration order of the key to fetch
     * @return the key at the index, if the index is valid, otherwise null
     */
    public K getAt(final int idx) {
        if (idx < 0 || idx >= order.size)
            return null;
        final K[] key = this.key;
        // The starting point.
        return key[order.get(idx)];
    }

    /**
     * Removes the item at the given index in the iteration order in not-exactly constant time (though it still should
     * be efficient).
     * @param idx the index in the iteration order of the item to remove
     * @return true if this Set was changed as a result of this call, or false if nothing changed.
     */
    public boolean removeAt(final int idx) {
        if (idx < 0 || idx >= order.size)
            throw new NoSuchElementException();
        int pos = order.get(idx);
        if (key[pos] == null) {
            if (containsNull)
                return removeNullEntry();
            return false;
        }
        return removeEntry(pos);
    }
    /**
     * Gets a random value from this OrderedSet in constant time, using the given RNG to generate a random number.
     * @param rng used to generate a random index for a value
     * @return a random value from this OrderedSet
     */
    public K randomItem(RNG rng)
    {
        return getAt(rng.nextInt(order.size));
    }

    /**
     * Randomly alters the iteration order for this OrderedSet using the given RNG to shuffle.
     * @param rng used to generate a random ordering
     * @return this for chaining
     */
    public OrderedSet<K> shuffle(RNG rng)
    {
        if(size < 2 || rng == null)
            return this;
        order.shuffle(rng);
        first = order.get(0);
        last = order.peek();
        return this;
    }
    /**
     * Given an array or varargs of replacement indices for this OrderedSet's iteration order, reorders this so the
     * first item in the returned version is the same as {@code getAt(ordering[0])} (with some care taken for negative
     * or too-large indices), the second item in the returned version is the same as {@code getAt(ordering[1])}, etc.
     * <br>
     * Negative indices are considered reversed distances from the end of ordering, so -1 refers to the same index as
     * {@code ordering[ordering.length - 1]}. If ordering is smaller than {@code size()}, only the indices up to the
     * length of ordering will be modified. If ordering is larger than {@code size()}, only as many indices will be
     * affected as {@code size()}, and reversed distances are measured from the end of this Set's entries instead of
     * the end of ordering. Duplicate values in ordering will produce duplicate values in the returned Set.
     * <br>
     * This method modifies this OrderedSet in-place and also returns it for chaining.
     * @param ordering an array or varargs of int indices, where the nth item in ordering changes the nth item in this
     *                 Set to have the value currently in this Set at the index specified by the value in ordering
     * @return this for chaining, after modifying it in-place
     */
    public OrderedSet<K> reorder(int... ordering)
    {
        order.reorder(ordering);
        first = order.get(0);
        last = order.peek();
        return this;
    }
    private boolean alterEntry(final int pos, final K replacement) {
        int rep;
        if (replacement == null) {
            if (containsNull)
                return false;
            rep = n;
            containsNull = true;
        } else {
            K curr;
            final K[] key = this.key;
            key[pos] = null;
            // The starting point.
            if (!((curr = key[rep = HashCommon.mix(hasher.hash(replacement)) & mask]) == null)) {
                if (curr.equals(replacement))
                    return false;
                while (!((curr = key[rep = (rep + 1) & mask]) == null))
                    if (curr.equals(replacement))
                        return false;
            }
            key[rep] = replacement;
        }
        fixOrder(pos, rep);
        return true;
    }
    private boolean alterNullEntry(final K replacement) {
        containsNull = false;
        key[n] = null;
        int rep;
        if (replacement == null) {
            rep = n;
            containsNull = true;
        } else {
            K curr;
            final K[] key = this.key;
            // The starting point.
            if (!((curr = key[rep = HashCommon.mix(hasher.hash(replacement)) & mask]) == null)) {
                if (curr.equals(replacement))
                    return false;
                while (!((curr = key[rep = (rep + 1) & mask]) == null))
                    if (curr.equals(replacement))
                        return false;
            }
            key[rep] = replacement;
        }
        fixOrder(n, rep);
        return true;
    }

    /**
     * Changes a K, original, to another, replacement, while keeping replacement at the same point in the ordering.
     * @param original a K value that will be removed from this Set if present, and its iteration index remembered
     * @param replacement another K value that will replace original at the remembered index
     * @return true if the Set changed, or false if it didn't (such as if the two arguments are equal, or replacement was already in the Set but original was not)
     */
    public boolean alter(K original, K replacement)
    {
        if (original == null) {
            if (containsNull) {
                return replacement != null && alterNullEntry(replacement);
            }
            else
                return add(replacement);
        }
        else if(original.equals(replacement))
            return false;
        K curr;
        final K[] key = this.key;
        int pos;
        // The starting point.
        if ((curr = key[pos = HashCommon.mix(hasher.hash(original)) & mask]) == null)
            return add(replacement);
        if (original.equals(curr))
            return alterEntry(pos, replacement);
        while (true) {
            if ((curr = key[pos = (pos + 1) & mask]) == null)
                return add(replacement);
            if (original.equals(curr))
                return alterEntry(pos, replacement);
        }
    }
}
