com.gs.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 gs-collections Show documentation
Show all versions of gs-collections Show documentation
GS Collections is a collections framework for Java. It has JDK-compatible List, Set and Map
implementations with a rich API and set of utility classes that work with any JDK compatible Collections,
Arrays, Maps or Strings. The iteration protocol was inspired by the Smalltalk collection framework.
/*
* Copyright 2014 Goldman Sachs.
*
* 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 com.gs.collections.impl.map.mutable.primitive;
import java.io.IOException;
import java.util.Arrays;
import java.util.NoSuchElementException;
import com.gs.collections.api.IntIterable;
import com.gs.collections.api.LazyIntIterable;
import com.gs.collections.api.bag.primitive.MutableIntBag;
import com.gs.collections.api.block.function.primitive.IntToObjectFunction;
import com.gs.collections.api.block.function.primitive.ObjectIntToObjectFunction;
import com.gs.collections.api.block.predicate.primitive.IntPredicate;
import com.gs.collections.api.block.procedure.primitive.IntProcedure;
import com.gs.collections.api.iterator.IntIterator;
import com.gs.collections.api.list.primitive.MutableIntList;
import com.gs.collections.api.set.primitive.ImmutableIntSet;
import com.gs.collections.impl.bag.mutable.primitive.IntHashBag;
import com.gs.collections.impl.factory.primitive.IntSets;
import com.gs.collections.impl.lazy.primitive.LazyIntIterableAdapter;
import com.gs.collections.impl.list.mutable.primitive.IntArrayList;
import com.gs.collections.api.set.primitive.IntSet;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.set.primitive.MutableIntSet;
import com.gs.collections.impl.factory.Sets;
import com.gs.collections.impl.set.mutable.primitive.IntHashSet;
import com.gs.collections.impl.set.mutable.primitive.SynchronizedIntSet;
import com.gs.collections.impl.set.mutable.primitive.UnmodifiableIntSet;
/**
* This file was automatically generated from template file abstractMutablePrimitiveKeySet.stg.
*
* @since 6.0.
*/
public abstract class AbstractMutableIntKeySet implements MutableIntSet
{
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();
public void forEach(IntProcedure procedure)
{
this.each(procedure);
}
/**
* @since 7.0.
*/
public void each(IntProcedure procedure)
{
this.getOuter().forEachKey(procedure);
}
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;
}
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;
}
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;
}
public boolean noneSatisfy(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;
}
public boolean add(int element)
{
throw new UnsupportedOperationException("Cannot call add() on " + this.getClass().getSimpleName());
}
public boolean addAll(int... source)
{
throw new UnsupportedOperationException("Cannot call addAll() on " + this.getClass().getSimpleName());
}
public boolean addAll(IntIterable source)
{
throw new UnsupportedOperationException("Cannot call addAll() on " + this.getClass().getSimpleName());
}
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;
}
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;
}
public MutableIntSet with(int element)
{
throw new UnsupportedOperationException("Cannot call with() on " + this.getClass().getSimpleName());
}
public MutableIntSet without(int element)
{
throw new UnsupportedOperationException("Cannot call without() on " + this.getClass().getSimpleName());
}
public MutableIntSet withAll(IntIterable elements)
{
throw new UnsupportedOperationException("Cannot call withAll() on " + this.getClass().getSimpleName());
}
public MutableIntSet withoutAll(IntIterable elements)
{
throw new UnsupportedOperationException("Cannot call withoutAll() on " + this.getClass().getSimpleName());
}
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;
}
public boolean remove(int key)
{
int oldSize = this.getOuter().size();
this.getOuter().removeKey(key);
return oldSize != this.getOuter().size();
}
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();
}
public boolean removeAll(int... source)
{
int oldSize = this.getOuter().size();
for (int item : source)
{
this.getOuter().removeKey(item);
}
return oldSize != this.getOuter().size();
}
public void clear()
{
this.getOuter().clear();
}
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;
}
public MutableIntSet asUnmodifiable()
{
return UnmodifiableIntSet.of(this);
}
public MutableIntSet asSynchronized()
{
return SynchronizedIntSet.of(this);
}
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;
}
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;
}
public int maxIfEmpty(int defaultValue)
{
if (this.getOuter().isEmpty())
{
return defaultValue;
}
return this.max();
}
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;
}
public int minIfEmpty(int defaultValue)
{
if (this.getOuter().isEmpty())
{
return defaultValue;
}
return this.min();
}
public double average()
{
if (this.isEmpty())
{
throw new ArithmeticException();
}
return (double) this.sum() / (double) this.size();
}
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];
}
public int[] toSortedArray()
{
int[] array = this.toArray();
Arrays.sort(array);
return array;
}
public MutableIntList toSortedList()
{
return IntArrayList.newList(this).sortThis();
}
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;
}
public boolean contains(int value)
{
return this.getOuter().containsKey(value);
}
public boolean containsAll(int... source)
{
for (int item : source)
{
if (!this.getOuter().containsKey(item))
{
return false;
}
}
return true;
}
public boolean containsAll(IntIterable source)
{
IntIterator iterator = source.intIterator();
while (iterator.hasNext())
{
if (!this.getOuter().containsKey(iterator.next()))
{
return false;
}
}
return true;
}
public MutableIntList toList()
{
return IntArrayList.newList(this);
}
public MutableIntSet toSet()
{
return IntHashSet.newSet(this);
}
public MutableIntBag toBag()
{
return IntHashBag.newBag(this);
}
public LazyIntIterable asLazy()
{
return new LazyIntIterableAdapter(this);
}
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;
}
public ImmutableIntSet toImmutable()
{
return IntSets.immutable.withAll(this);
}
public int size()
{
return this.getOuter().size();
}
public boolean isEmpty()
{
return this.getOuter().isEmpty();
}
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("[", ", ", "]");
}
public String makeString()
{
return this.makeString(", ");
}
public String makeString(String separator)
{
return this.makeString("", separator, "");
}
public String makeString(String start, String separator, String end)
{
Appendable stringBuilder = new StringBuilder();
this.appendString(stringBuilder, start, separator, end);
return stringBuilder.toString();
}
public void appendString(Appendable appendable)
{
this.appendString(appendable, ", ");
}
public void appendString(Appendable appendable, String separator)
{
this.appendString(appendable, "", separator, "");
}
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);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy