package org.eclipse.collections.impl.map.mutable.primitive;
import java.io.IOException;
import java.io.Serializable;
import java.util.NoSuchElementException;
import org.eclipse.collections.impl.SpreadFunctions;
import org.eclipse.collections.api.block.function.primitive.CharToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectCharToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.CharPredicate;
import org.eclipse.collections.api.block.procedure.primitive.CharProcedure;
import org.eclipse.collections.api.iterator.CharIterator;
import org.eclipse.collections.api.set.ImmutableSet;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.set.primitive.ImmutableCharSet;
import org.eclipse.collections.impl.set.immutable.primitive.ImmutableCharSetSerializationProxy;
import org.eclipse.collections.impl.set.immutable.primitive.AbstractImmutableCharSet;
import org.eclipse.collections.impl.set.mutable.UnifiedSet;
import org.eclipse.collections.impl.set.mutable.primitive.CharHashSet;
class ImmutableCharMapKeySet extends AbstractImmutableCharSet implements Serializable
{
private static final long serialVersionUID = 1L;
private static final char EMPTY_KEY = (char) 0;
private static final char REMOVED_KEY = (char) 1;
private static final int CACHE_LINE_SIZE = 64;
private static final int KEY_SIZE = 2;
private static final int INITIAL_LINEAR_PROBE = CACHE_LINE_SIZE / KEY_SIZE / 2;
private final char[] keys;
private final int occupiedWithData;
private final boolean containsZeroKey;
private final boolean containsOneKey;
ImmutableCharMapKeySet(char[] keys, int occupiedWithData, boolean containsZeroKey, boolean containsOneKey)
{
this.keys = keys;
this.occupiedWithData = occupiedWithData;
this.containsZeroKey = containsZeroKey;
this.containsOneKey = containsOneKey;
}
private static boolean isEmptyKey(char key)
{
return key == EMPTY_KEY;
}
private static boolean isRemovedKey(char key)
{
return key == REMOVED_KEY;
}
private static boolean isNonSentinel(char key)
{
return !isEmptyKey(key) && !isRemovedKey(key);
}
@Override
public int hashCode()
{
int result = 0;
if (this.containsZeroKey)
{
result += (int) EMPTY_KEY;
}
if (this.containsOneKey)
{
result += (int) REMOVED_KEY;
}
if (this.keys != null)
{
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]))
{
result += (int) this.keys[i];
}
}
}
return result;
}
@Override
public int size()
{
return this.occupiedWithData + (this.containsOneKey ? 1 : 0) + (this.containsZeroKey ? 1 : 0);
}
@Override
public void appendString(Appendable appendable, String start, String separator, String end)
{
try
{
appendable.append(start);
int count = 0;
if (this.containsZeroKey)
{
appendable.append(String.valueOf(EMPTY_KEY));
count++;
}
if (this.containsOneKey)
{
if (count > 0)
{
appendable.append(separator);
}
count++;
appendable.append(String.valueOf(REMOVED_KEY));
}
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]))
{
if (count > 0)
{
appendable.append(separator);
}
count++;
appendable.append(String.valueOf(this.keys[i]));
}
}
appendable.append(end);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
@Override
public CharIterator charIterator()
{
return new InternalCharIterator();
}
@Override
public char[] toArray()
{
char[] array = new char[this.size()];
int index = 0;
if (this.containsZeroKey)
{
array[index] = EMPTY_KEY;
index++;
}
if (this.containsOneKey)
{
array[index] = REMOVED_KEY;
index++;
}
if (this.keys != null)
{
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]))
{
array[index] = this.keys[i];
index++;
}
}
}
return array;
}
@Override
public char[] toArray(char[] array)
{
if (array.length < this.size())
{
array = new char[this.size()];
}
int index = 0;
if (this.containsZeroKey)
{
array[index] = EMPTY_KEY;
index++;
}
if (this.containsOneKey)
{
array[index] = REMOVED_KEY;
index++;
}
if (this.keys != null)
{
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]))
{
array[index] = this.keys[i];
index++;
}
}
}
return array;
}
@Override
public boolean contains(char value)
{
if (value == EMPTY_KEY)
{
return this.containsZeroKey;
}
if (value == REMOVED_KEY)
{
return this.containsOneKey;
}
return this.keys[this.probe(value)] == value;
}
@Override
public void forEach(CharProcedure procedure)
{
this.each(procedure);
}
@Override
public void each(CharProcedure procedure)
{
if (this.containsZeroKey)
{
procedure.value(EMPTY_KEY);
}
if (this.containsOneKey)
{
procedure.value(REMOVED_KEY);
}
if (this.keys != null)
{
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]))
{
procedure.value(this.keys[i]);
}
}
}
}
@Override
public ImmutableCharSet select(CharPredicate predicate)
{
CharHashSet result = new CharHashSet();
if (this.containsZeroKey && predicate.accept(EMPTY_KEY))
{
result.add(EMPTY_KEY);
}
if (this.containsOneKey && predicate.accept(REMOVED_KEY))
{
result.add(REMOVED_KEY);
}
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]) && predicate.accept(this.keys[i]))
{
result.add(this.keys[i]);
}
}
return result.toImmutable();
}
@Override
public ImmutableCharSet reject(CharPredicate predicate)
{
CharHashSet result = new CharHashSet();
if (this.containsZeroKey && !predicate.accept(EMPTY_KEY))
{
result.add(EMPTY_KEY);
}
if (this.containsOneKey && !predicate.accept(REMOVED_KEY))
{
result.add(REMOVED_KEY);
}
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]) && !predicate.accept(this.keys[i]))
{
result.add(this.keys[i]);
}
}
return result.toImmutable();
}
@Override
public <V> ImmutableSet<V> collect(CharToObjectFunction<? extends V> function)
{
MutableSet<V> target = UnifiedSet.newSet(this.size());
if (this.containsZeroKey)
{
target.add(function.valueOf(EMPTY_KEY));
}
if (this.containsOneKey)
{
target.add(function.valueOf(REMOVED_KEY));
}
if (this.keys != null)
{
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]))
{
target.add(function.valueOf(this.keys[i]));
}
}
}
return target.toImmutable();
}
@Override
public char detectIfNone(CharPredicate predicate, char ifNone)
{
if (this.containsZeroKey && predicate.accept(EMPTY_KEY))
{
return EMPTY_KEY;
}
if (this.containsOneKey && predicate.accept(REMOVED_KEY))
{
return REMOVED_KEY;
}
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]) && predicate.accept(this.keys[i]))
{
return this.keys[i];
}
}
return ifNone;
}
@Override
public int count(CharPredicate predicate)
{
int count = 0;
if (this.containsZeroKey && predicate.accept(EMPTY_KEY))
{
count++;
}
if (this.containsOneKey && predicate.accept(REMOVED_KEY))
{
count++;
}
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]) && predicate.accept(this.keys[i]))
{
count++;
}
}
return count;
}
@Override
public boolean anySatisfy(CharPredicate predicate)
{
if (this.containsZeroKey && predicate.accept(EMPTY_KEY))
{
return true;
}
if (this.containsOneKey && predicate.accept(REMOVED_KEY))
{
return true;
}
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]) && predicate.accept(this.keys[i]))
{
return true;
}
}
return false;
}
@Override
public boolean allSatisfy(CharPredicate predicate)
{
if (this.containsZeroKey && !predicate.accept(EMPTY_KEY))
{
return false;
}
if (this.containsOneKey && !predicate.accept(REMOVED_KEY))
{
return false;
}
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]) && !predicate.accept(this.keys[i]))
{
return false;
}
}
return true;
}
@Override
public boolean noneSatisfy(CharPredicate predicate)
{
if (this.containsZeroKey && predicate.accept(EMPTY_KEY))
{
return false;
}
if (this.containsOneKey && predicate.accept(REMOVED_KEY))
{
return false;
}
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]) && predicate.accept(this.keys[i]))
{
return false;
}
}
return true;
}
@Override
public long sum()
{
long result = 0L;
long compensation = 0L;
if (this.containsZeroKey)
{
long adjustedValue = EMPTY_KEY - compensation;
long nextSum = result + adjustedValue;
compensation = nextSum - result - adjustedValue;
result = nextSum;
}
if (this.containsOneKey)
{
long adjustedValue = REMOVED_KEY - compensation;
long nextSum = result + adjustedValue;
compensation = nextSum - result - adjustedValue;
result = nextSum;
}
if (this.keys != null)
{
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]))
{
long adjustedValue = this.keys[i] - compensation;
long nextSum = result + adjustedValue;
compensation = nextSum - result - adjustedValue;
result = nextSum;
}
}
}
return result;
}
@Override
public char max()
{
if (this.isEmpty())
{
throw new NoSuchElementException();
}
char max = 0;
boolean isMaxSet = false;
if (this.containsZeroKey)
{
max = EMPTY_KEY;
isMaxSet = true;
}
if (this.containsOneKey && (!isMaxSet || max < REMOVED_KEY))
{
max = REMOVED_KEY;
isMaxSet = true;
}
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]) && (!isMaxSet || max < this.keys[i]))
{
max = this.keys[i];
isMaxSet = true;
}
}
return max;
}
@Override
public char min()
{
if (this.isEmpty())
{
throw new NoSuchElementException();
}
char min = 0;
boolean isMinSet = false;
if (this.containsZeroKey)
{
min = EMPTY_KEY;
isMinSet = true;
}
if (this.containsOneKey && (!isMinSet || REMOVED_KEY < min))
{
min = REMOVED_KEY;
isMinSet = true;
}
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]) && (!isMinSet || this.keys[i] < min))
{
min = this.keys[i];
isMinSet = true;
}
}
return min;
}
@Override
public <T> T injectInto(T injectedValue, ObjectCharToObjectFunction<? super T, ? extends T> function)
{
T result = injectedValue;
if (this.containsZeroKey)
{
result = function.valueOf(result, EMPTY_KEY);
}
if (this.containsOneKey)
{
result = function.valueOf(result, REMOVED_KEY);
}
if (this.keys != null)
{
for (int i = 0; i < this.keys.length; i++)
{
if (isNonSentinel(this.keys[i]))
{
result = function.valueOf(result, this.keys[i]);
}
}
}
return result;
}
private Object writeReplace()
{
return new ImmutableCharSetSerializationProxy(this);
}
int probe(char element)
{
int index = this.mask((int) element);
char keyAtIndex = this.keys[index];
if (keyAtIndex == element || keyAtIndex == EMPTY_KEY)
{
return index;
}
int removedIndex = keyAtIndex == REMOVED_KEY ? index : -1;
for (int i = 1; i < INITIAL_LINEAR_PROBE; i++)
{
int nextIndex = (index + i) & (this.keys.length - 1);
keyAtIndex = this.keys[nextIndex];
if (keyAtIndex == element)
{
return nextIndex;
}
if (keyAtIndex == EMPTY_KEY)
{
return removedIndex == -1 ? nextIndex : removedIndex;
}
if (keyAtIndex == REMOVED_KEY && removedIndex == -1)
{
removedIndex = nextIndex;
}
}
return this.probeTwo(element, removedIndex);
}
int probeTwo(char element, int removedIndex)
{
int index = this.spreadTwoAndMask(element);
for (int i = 0; i < INITIAL_LINEAR_PROBE; i++)
{
int nextIndex = (index + i) & (this.keys.length - 1);
char keyAtIndex = this.keys[nextIndex];
if (keyAtIndex == element)
{
return nextIndex;
}
if (keyAtIndex == EMPTY_KEY)
{
return removedIndex == -1 ? nextIndex : removedIndex;
}
if (keyAtIndex == REMOVED_KEY && removedIndex == -1)
{
removedIndex = nextIndex;
}
}
return this.probeThree(element, removedIndex);
}
int probeThree(char element, int removedIndex)
{
int nextIndex = (int) SpreadFunctions.charSpreadOne(element);
int spreadTwo = Integer.reverse(SpreadFunctions.charSpreadTwo(element)) | 1;
while (true)
{
nextIndex = this.mask(nextIndex + spreadTwo);
char keyAtIndex = this.keys[nextIndex];
if (keyAtIndex == element)
{
return nextIndex;
}
if (keyAtIndex == EMPTY_KEY)
{
return removedIndex == -1 ? nextIndex : removedIndex;
}
if (keyAtIndex == REMOVED_KEY && removedIndex == -1)
{
removedIndex = nextIndex;
}
}
}
int spreadAndMask(char element)
{
int code = SpreadFunctions.charSpreadOne(element);
return this.mask(code);
}
int spreadTwoAndMask(char element)
{
int code = SpreadFunctions.charSpreadTwo(element);
return this.mask(code);
}
private int mask(int spread)
{
return spread & (this.keys.length - 1);
}
private class InternalCharIterator implements CharIterator
{
private int count;
private int position;
private boolean handledZero;
private boolean handledOne;
@Override
public boolean hasNext()
{
return this.count < ImmutableCharMapKeySet.this.size();
}
@Override
public char next()
{
if (!this.hasNext())
{
throw new NoSuchElementException("next() called, but the iterator is exhausted");
}
this.count++;
if (!this.handledZero)
{
this.handledZero = true;
if (ImmutableCharMapKeySet.this.containsZeroKey)
{
return ImmutableCharMapKeySet.EMPTY_KEY;
}
}
if (!this.handledOne)
{
this.handledOne = true;
if (ImmutableCharMapKeySet.this.containsOneKey)
{
return ImmutableCharMapKeySet.REMOVED_KEY;
}
}
char[] keys = ImmutableCharMapKeySet.this.keys;
while (!ImmutableCharMapKeySet.isNonSentinel(keys[this.position]))
{
this.position++;
}
char result = ImmutableCharMapKeySet.this.keys[this.position];
this.position++;
return result;
}
}
}