org.eclipse.collections.impl.map.mutable.primitive.AbstractMutableIntKeySet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eclipse-collections Show documentation
Show all versions of eclipse-collections Show documentation
Builds the commons-text. Requires eclipse-collections-api be built first and be excluded from
any other poms requiring it.
/*
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.map.mutable.primitive;
import java.io.IOException;
import java.io.Externalizable;
import java.io.ObjectInput;
import java.io.ObjectStreamException;
import java.io.ObjectOutput;
import java.util.Arrays;
import java.util.NoSuchElementException;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.IntIterable;
import org.eclipse.collections.api.LazyIntIterable;
import org.eclipse.collections.api.bag.primitive.MutableIntBag;
import org.eclipse.collections.api.block.function.primitive.IntToObjectFunction;
import org.eclipse.collections.api.block.function.primitive.ObjectIntToObjectFunction;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.procedure.primitive.IntProcedure;
import org.eclipse.collections.api.iterator.IntIterator;
import org.eclipse.collections.api.list.MutableList;
import org.eclipse.collections.api.list.primitive.MutableIntList;
import org.eclipse.collections.api.set.primitive.ImmutableIntSet;
import org.eclipse.collections.impl.bag.mutable.primitive.IntHashBag;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.factory.primitive.IntSets;
import org.eclipse.collections.impl.lazy.primitive.LazyIntIterableAdapter;
import org.eclipse.collections.impl.list.mutable.primitive.IntArrayList;
import org.eclipse.collections.api.set.primitive.IntSet;
import org.eclipse.collections.api.set.MutableSet;
import org.eclipse.collections.api.set.primitive.MutableIntSet;
import org.eclipse.collections.impl.factory.Sets;
import org.eclipse.collections.impl.set.mutable.primitive.IntHashSet;
import org.eclipse.collections.impl.set.mutable.primitive.SynchronizedIntSet;
import org.eclipse.collections.impl.set.mutable.primitive.UnmodifiableIntSet;
import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.tuple.primitive.IntIntPair;
/**
* This file was automatically generated from template file abstractMutablePrimitiveKeySet.stg.
*
* @since 6.0.
*/
public abstract class AbstractMutableIntKeySet implements MutableIntSet, Externalizable
{
private static final long serialVersionUID = 1L;
private static final int EMPTY_KEY = 0;
private static final int REMOVED_KEY = 1;
private static boolean isEmptyKey(int key)
{
return key == EMPTY_KEY;
}
private static boolean isRemovedKey(int key)
{
return key == REMOVED_KEY;
}
private static boolean isNonSentinel(int key)
{
return !isEmptyKey(key) && !isRemovedKey(key);
}
protected abstract int getKeyAtIndex(int index);
protected abstract int getTableSize();
protected abstract MutableIntKeysMap getOuter();
protected abstract AbstractSentinelValues getSentinelValues();
/**
* @since 7.0.
*/
@Override
public void each(IntProcedure procedure)
{
this.getOuter().forEachKey(procedure);
}
@Override
public int count(IntPredicate predicate)
{
int count = 0;
if (this.getSentinelValues() != null)
{
if (this.getSentinelValues().containsZeroKey && predicate.accept(EMPTY_KEY))
{
count++;
}
if (this.getSentinelValues().containsOneKey && predicate.accept(REMOVED_KEY))
{
count++;
}
}
for (int i = 0; i < this.getTableSize(); i++)
{
if (isNonSentinel(this.getKeyAtIndex(i)) && predicate.accept(this.getKeyAtIndex(i)))
{
count++;
}
}
return count;
}
@Override
public boolean anySatisfy(IntPredicate predicate)
{
if (this.getSentinelValues() != null)
{
if (this.getSentinelValues().containsZeroKey && predicate.accept(EMPTY_KEY))
{
return true;
}
if (this.getSentinelValues().containsOneKey && predicate.accept(REMOVED_KEY))
{
return true;
}
}
for (int i = 0; i < this.getTableSize(); i++)
{
if (isNonSentinel(this.getKeyAtIndex(i)) && predicate.accept(this.getKeyAtIndex(i)))
{
return true;
}
}
return false;
}
@Override
public boolean allSatisfy(IntPredicate predicate)
{
if (this.getSentinelValues() != null)
{
if (this.getSentinelValues().containsZeroKey && !predicate.accept(EMPTY_KEY))
{
return false;
}
if (this.getSentinelValues().containsOneKey && !predicate.accept(REMOVED_KEY))
{
return false;
}
}
for (int i = 0; i < this.getTableSize(); i++)
{
if (isNonSentinel(this.getKeyAtIndex(i)) && !predicate.accept(this.getKeyAtIndex(i)))
{
return false;
}
}
return true;
}
@Override
public boolean add(int element)
{
throw new UnsupportedOperationException("Cannot call add() on " + this.getClass().getSimpleName());
}
@Override
public boolean addAll(int... source)
{
throw new UnsupportedOperationException("Cannot call addAll() on " + this.getClass().getSimpleName());
}
@Override
public boolean addAll(IntIterable source)
{
throw new UnsupportedOperationException("Cannot call addAll() on " + this.getClass().getSimpleName());
}
@Override
public MutableIntSet select(IntPredicate predicate)
{
MutableIntSet result = new IntHashSet();
if (this.getSentinelValues() != null)
{
if (this.getSentinelValues().containsZeroKey && predicate.accept(EMPTY_KEY))
{
result.add(EMPTY_KEY);
}
if (this.getSentinelValues().containsOneKey && predicate.accept(REMOVED_KEY))
{
result.add(REMOVED_KEY);
}
}
for (int i = 0; i < this.getTableSize(); i++)
{
if (isNonSentinel(this.getKeyAtIndex(i)) && predicate.accept(this.getKeyAtIndex(i)))
{
result.add(this.getKeyAtIndex(i));
}
}
return result;
}
@Override
public MutableIntSet reject(IntPredicate predicate)
{
MutableIntSet result = new IntHashSet();
if (this.getSentinelValues() != null)
{
if (this.getSentinelValues().containsZeroKey && !predicate.accept(EMPTY_KEY))
{
result.add(EMPTY_KEY);
}
if (this.getSentinelValues().containsOneKey && !predicate.accept(REMOVED_KEY))
{
result.add(REMOVED_KEY);
}
}
for (int i = 0; i < this.getTableSize(); i++)
{
if (isNonSentinel(this.getKeyAtIndex(i)) && !predicate.accept(this.getKeyAtIndex(i)))
{
result.add(this.getKeyAtIndex(i));
}
}
return result;
}
@Override
public MutableIntSet with(int element)
{
throw new UnsupportedOperationException("Cannot call with() on " + this.getClass().getSimpleName());
}
@Override
public MutableIntSet without(int element)
{
throw new UnsupportedOperationException("Cannot call without() on " + this.getClass().getSimpleName());
}
@Override
public MutableIntSet withAll(IntIterable elements)
{
throw new UnsupportedOperationException("Cannot call withAll() on " + this.getClass().getSimpleName());
}
@Override
public MutableIntSet withoutAll(IntIterable elements)
{
throw new UnsupportedOperationException("Cannot call withoutAll() on " + this.getClass().getSimpleName());
}
@Override
public MutableSet collect(IntToObjectFunction extends V> function)
{
MutableSet result = Sets.mutable.with();
if (this.getSentinelValues() != null)
{
if (this.getSentinelValues().containsZeroKey)
{
result.add(function.valueOf(EMPTY_KEY));
}
if (this.getSentinelValues().containsOneKey)
{
result.add(function.valueOf(REMOVED_KEY));
}
}
for (int i = 0; i < this.getTableSize(); i++)
{
if (isNonSentinel(this.getKeyAtIndex(i)))
{
result.add(function.valueOf(this.getKeyAtIndex(i)));
}
}
return result;
}
@Override
public boolean remove(int key)
{
int oldSize = this.getOuter().size();
this.getOuter().removeKey(key);
return oldSize != this.getOuter().size();
}
@Override
public boolean removeAll(IntIterable source)
{
int oldSize = this.getOuter().size();
IntIterator iterator = source.intIterator();
while (iterator.hasNext())
{
this.getOuter().removeKey(iterator.next());
}
return oldSize != this.getOuter().size();
}
@Override
public boolean removeAll(int... source)
{
int oldSize = this.getOuter().size();
for (int item : source)
{
this.getOuter().removeKey(item);
}
return oldSize != this.getOuter().size();
}
@Override
public void clear()
{
this.getOuter().clear();
}
@Override
public int detectIfNone(IntPredicate predicate, int ifNone)
{
if (this.getSentinelValues() != null)
{
if (this.getSentinelValues().containsZeroKey && predicate.accept(EMPTY_KEY))
{
return EMPTY_KEY;
}
if (this.getSentinelValues().containsOneKey && predicate.accept(REMOVED_KEY))
{
return REMOVED_KEY;
}
}
for (int i = 0; i < this.getTableSize(); i++)
{
if (isNonSentinel(this.getKeyAtIndex(i)) && predicate.accept(this.getKeyAtIndex(i)))
{
return this.getKeyAtIndex(i);
}
}
return ifNone;
}
@Override
public MutableIntSet asUnmodifiable()
{
return UnmodifiableIntSet.of(this);
}
@Override
public MutableIntSet asSynchronized()
{
return SynchronizedIntSet.of(this);
}
@Override
public long sum()
{
long result = 0L;
if (this.getSentinelValues() != null)
{
if (this.getSentinelValues().containsZeroKey)
{
result += EMPTY_KEY;
}
if (this.getSentinelValues().containsOneKey)
{
result += REMOVED_KEY;
}
}
for (int i = 0; i < this.getTableSize(); i++)
{
if (isNonSentinel(this.getKeyAtIndex(i)))
{
result += this.getKeyAtIndex(i);
}
}
return result;
}
@Override
public int max()
{
if (this.getOuter().isEmpty())
{
throw new NoSuchElementException();
}
int max = 0;
boolean isMaxSet = false;
if (this.getSentinelValues() != null)
{
if (this.getSentinelValues().containsZeroKey)
{
max = EMPTY_KEY;
isMaxSet = true;
}
if (this.getSentinelValues().containsOneKey)
{
max = REMOVED_KEY;
isMaxSet = true;
}
}
for (int i = 0; i < this.getTableSize(); i++)
{
if (isNonSentinel(this.getKeyAtIndex(i)) && (!isMaxSet || max < this.getKeyAtIndex(i)))
{
max = this.getKeyAtIndex(i);
isMaxSet = true;
}
}
return max;
}
@Override
public int maxIfEmpty(int defaultValue)
{
if (this.getOuter().isEmpty())
{
return defaultValue;
}
return this.max();
}
@Override
public int min()
{
if (this.getOuter().isEmpty())
{
throw new NoSuchElementException();
}
int min = 0;
boolean isMinSet = false;
if (this.getSentinelValues() != null)
{
if (this.getSentinelValues().containsZeroKey)
{
min = EMPTY_KEY;
isMinSet = true;
}
if (this.getSentinelValues().containsOneKey && !isMinSet)
{
min = REMOVED_KEY;
isMinSet = true;
}
}
for (int i = 0; i < this.getTableSize(); i++)
{
if (isNonSentinel(this.getKeyAtIndex(i)) && (!isMinSet || this.getKeyAtIndex(i) < min))
{
min = this.getKeyAtIndex(i);
isMinSet = true;
}
}
return min;
}
@Override
public int minIfEmpty(int defaultValue)
{
if (this.getOuter().isEmpty())
{
return defaultValue;
}
return this.min();
}
@Override
public double average()
{
if (this.isEmpty())
{
throw new ArithmeticException();
}
return (double) this.sum() / (double) this.size();
}
@Override
public double median()
{
if (this.isEmpty())
{
throw new ArithmeticException();
}
int[] sortedArray = this.toSortedArray();
int middleIndex = sortedArray.length >> 1;
if (sortedArray.length > 1 && (sortedArray.length & 1) == 0)
{
int first = sortedArray[middleIndex];
int second = sortedArray[middleIndex - 1];
return ((double) first + (double) second) / 2.0;
}
return (double) sortedArray[middleIndex];
}
@Override
public int[] toSortedArray()
{
int[] array = this.toArray();
Arrays.sort(array);
return array;
}
@Override
public MutableIntList toSortedList()
{
return IntArrayList.newList(this).sortThis();
}
@Override
public int[] toArray()
{
int size = this.getOuter().size();
final int[] result = new int[size];
this.getOuter().forEachKey(new IntProcedure()
{
private int index;
public void value(int each)
{
result[this.index] = each;
this.index++;
}
});
return result;
}
@Override
public int[] toArray(int[] result)
{
int size = this.getOuter().size();
if (result.length < size)
{
result = new int[size];
}
int[] finalBypass = result;
this.getOuter().forEachKey(new IntProcedure()
{
private int index;
public void value(int each)
{
finalBypass[this.index] = each;
this.index++;
}
});
return result;
}
@Override
public boolean contains(int value)
{
return this.getOuter().containsKey(value);
}
@Override
public boolean containsAll(int... source)
{
for (int item : source)
{
if (!this.getOuter().containsKey(item))
{
return false;
}
}
return true;
}
@Override
public boolean containsAll(IntIterable source)
{
IntIterator iterator = source.intIterator();
while (iterator.hasNext())
{
if (!this.getOuter().containsKey(iterator.next()))
{
return false;
}
}
return true;
}
@Override
public LazyIterable cartesianProduct(IntSet set)
{
return IntSets.cartesianProduct(this, set);
}
@Override
public MutableIntList toList()
{
return IntArrayList.newList(this);
}
@Override
public MutableIntSet toSet()
{
return IntHashSet.newSet(this);
}
@Override
public MutableIntBag toBag()
{
return IntHashBag.newBag(this);
}
@Override
public LazyIntIterable asLazy()
{
return new LazyIntIterableAdapter(this);
}
@Override
public T injectInto(T injectedValue, ObjectIntToObjectFunction super T, ? extends T> function)
{
T result = injectedValue;
if (this.getSentinelValues() != null)
{
if (this.getSentinelValues().containsZeroKey)
{
result = function.valueOf(result, EMPTY_KEY);
}
if (this.getSentinelValues().containsOneKey)
{
result = function.valueOf(result, REMOVED_KEY);
}
}
for (int i = 0; i < this.getTableSize(); i++)
{
if (isNonSentinel(this.getKeyAtIndex(i)))
{
result = function.valueOf(result, this.getKeyAtIndex(i));
}
}
return result;
}
@Override
public ImmutableIntSet toImmutable()
{
return IntSets.immutable.withAll(this);
}
@Override
public int size()
{
return this.getOuter().size();
}
@Override
public boolean isEmpty()
{
return this.getOuter().isEmpty();
}
@Override
public boolean notEmpty()
{
return this.getOuter().notEmpty();
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (!(obj instanceof IntSet))
{
return false;
}
IntSet other = (IntSet) obj;
return this.size() == other.size() && this.containsAll(other.toArray());
}
@Override
public int hashCode()
{
int result = 0;
if (this.getSentinelValues() != null)
{
if (this.getSentinelValues().containsZeroKey)
{
result += EMPTY_KEY;
}
if (this.getSentinelValues().containsOneKey)
{
result += REMOVED_KEY;
}
}
for (int i = 0; i < this.getTableSize(); i++)
{
if (isNonSentinel(this.getKeyAtIndex(i)))
{
result += this.getKeyAtIndex(i);
}
}
return result;
}
@Override
public String toString()
{
return this.makeString("[", ", ", "]");
}
@Override
public String makeString()
{
return this.makeString(", ");
}
@Override
public String makeString(String separator)
{
return this.makeString("", separator, "");
}
@Override
public String makeString(String start, String separator, String end)
{
Appendable stringBuilder = new StringBuilder();
this.appendString(stringBuilder, start, separator, end);
return stringBuilder.toString();
}
@Override
public void appendString(Appendable appendable)
{
this.appendString(appendable, ", ");
}
@Override
public void appendString(Appendable appendable, String separator)
{
this.appendString(appendable, "", separator, "");
}
@Override
public void appendString(Appendable appendable, String start, String separator, String end)
{
try
{
appendable.append(start);
boolean first = true;
if (this.getSentinelValues() != null)
{
if (this.getSentinelValues().containsZeroKey)
{
appendable.append(String.valueOf(EMPTY_KEY));
first = false;
}
if (this.getSentinelValues().containsOneKey)
{
if (!first)
{
appendable.append(separator);
}
appendable.append(String.valueOf(REMOVED_KEY));
first = false;
}
}
for (int i = 0; i < this.getTableSize(); i++)
{
if (isNonSentinel(this.getKeyAtIndex(i)))
{
if (!first)
{
appendable.append(separator);
}
appendable.append(String.valueOf(this.getKeyAtIndex(i)));
first = false;
}
}
appendable.append(end);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
@Override
public RichIterable chunk(int size)
{
if (size <= 0)
{
throw new IllegalArgumentException("Size for groups must be positive but was: " + size);
}
MutableList result = Lists.mutable.empty();
if (this.notEmpty())
{
if (this.size() <= size)
{
result.add(IntSets.mutable.withAll(this));
}
else
{
IntIterator iterator = this.intIterator();
while (iterator.hasNext())
{
MutableIntSet batch = IntSets.mutable.empty();
for (int i = 0; i < size && iterator.hasNext(); i++)
{
batch.add(iterator.next());
}
result.add(batch);
}
}
}
return result;
}
public Object writeReplace() throws ObjectStreamException
{
return new SerRep(this);
}
@Override
public void writeExternal(ObjectOutput out) throws IOException
{
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
}
private static class SerRep implements Externalizable
{
private static final long serialVersionUID = 1L;
private transient AbstractMutableIntKeySet original;
private transient IntHashSet deserialized;
@SuppressWarnings("RedundantModifier")
public SerRep()
{
// for Externalizable
}
private SerRep(AbstractMutableIntKeySet original)
{
this.original = original;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException
{
out.writeInt(this.original.size());
IntIterator it = this.original.intIterator();
while (it.hasNext())
{
out.writeInt(it.next());
}
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
int unread = in.readInt();
this.deserialized = new IntHashSet(unread);
while (unread > 0)
{
this.deserialized.add(in.readInt());
unread--;
}
}
private Object readResolve() throws ObjectStreamException
{
return this.deserialized;
}
}
}