com.gs.collections.impl.map.mutable.primitive.AbstractMutableDoubleKeySet 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.DoubleIterable;
import com.gs.collections.api.LazyDoubleIterable;
import com.gs.collections.api.bag.primitive.MutableDoubleBag;
import com.gs.collections.api.block.function.primitive.DoubleToObjectFunction;
import com.gs.collections.api.block.function.primitive.ObjectDoubleToObjectFunction;
import com.gs.collections.api.block.predicate.primitive.DoublePredicate;
import com.gs.collections.api.block.procedure.primitive.DoubleProcedure;
import com.gs.collections.api.iterator.DoubleIterator;
import com.gs.collections.api.list.primitive.MutableDoubleList;
import com.gs.collections.api.set.primitive.ImmutableDoubleSet;
import com.gs.collections.impl.bag.mutable.primitive.DoubleHashBag;
import com.gs.collections.impl.factory.primitive.DoubleSets;
import com.gs.collections.impl.lazy.primitive.LazyDoubleIterableAdapter;
import com.gs.collections.impl.list.mutable.primitive.DoubleArrayList;
import com.gs.collections.api.set.primitive.DoubleSet;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.set.primitive.MutableDoubleSet;
import com.gs.collections.impl.factory.Sets;
import com.gs.collections.impl.set.mutable.primitive.DoubleHashSet;
import com.gs.collections.impl.set.mutable.primitive.SynchronizedDoubleSet;
import com.gs.collections.impl.set.mutable.primitive.UnmodifiableDoubleSet;
/**
* This file was automatically generated from template file abstractMutablePrimitiveKeySet.stg.
*
* @since 6.0.
*/
public abstract class AbstractMutableDoubleKeySet implements MutableDoubleSet
{
private static final double EMPTY_KEY = 0.0;
private static final double REMOVED_KEY = 1.0;
private static boolean isEmptyKey(double key)
{
return Double.compare(key, EMPTY_KEY) == 0;
}
private static boolean isRemovedKey(double key)
{
return Double.compare(key, REMOVED_KEY) == 0;
}
private static boolean isNonSentinel(double key)
{
return !isEmptyKey(key) && !isRemovedKey(key);
}
protected abstract double getKeyAtIndex(int index);
protected abstract int getTableSize();
protected abstract MutableDoubleKeysMap getOuter();
protected abstract AbstractSentinelValues getSentinelValues();
public void forEach(DoubleProcedure procedure)
{
this.each(procedure);
}
/**
* @since 7.0.
*/
public void each(DoubleProcedure procedure)
{
this.getOuter().forEachKey(procedure);
}
public int count(DoublePredicate 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(DoublePredicate 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(DoublePredicate 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(DoublePredicate 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(double element)
{
throw new UnsupportedOperationException("Cannot call add() on " + this.getClass().getSimpleName());
}
public boolean addAll(double... source)
{
throw new UnsupportedOperationException("Cannot call addAll() on " + this.getClass().getSimpleName());
}
public boolean addAll(DoubleIterable source)
{
throw new UnsupportedOperationException("Cannot call addAll() on " + this.getClass().getSimpleName());
}
public MutableDoubleSet select(DoublePredicate predicate)
{
MutableDoubleSet result = new DoubleHashSet();
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 MutableDoubleSet reject(DoublePredicate predicate)
{
MutableDoubleSet result = new DoubleHashSet();
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 MutableDoubleSet with(double element)
{
throw new UnsupportedOperationException("Cannot call with() on " + this.getClass().getSimpleName());
}
public MutableDoubleSet without(double element)
{
throw new UnsupportedOperationException("Cannot call without() on " + this.getClass().getSimpleName());
}
public MutableDoubleSet withAll(DoubleIterable elements)
{
throw new UnsupportedOperationException("Cannot call withAll() on " + this.getClass().getSimpleName());
}
public MutableDoubleSet withoutAll(DoubleIterable elements)
{
throw new UnsupportedOperationException("Cannot call withoutAll() on " + this.getClass().getSimpleName());
}
public MutableSet collect(DoubleToObjectFunction 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(double key)
{
int oldSize = this.getOuter().size();
this.getOuter().removeKey(key);
return oldSize != this.getOuter().size();
}
public boolean removeAll(DoubleIterable source)
{
int oldSize = this.getOuter().size();
DoubleIterator iterator = source.doubleIterator();
while (iterator.hasNext())
{
this.getOuter().removeKey(iterator.next());
}
return oldSize != this.getOuter().size();
}
public boolean removeAll(double... source)
{
int oldSize = this.getOuter().size();
for (double item : source)
{
this.getOuter().removeKey(item);
}
return oldSize != this.getOuter().size();
}
public void clear()
{
this.getOuter().clear();
}
public double detectIfNone(DoublePredicate predicate, double 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 MutableDoubleSet asUnmodifiable()
{
return UnmodifiableDoubleSet.of(this);
}
public MutableDoubleSet asSynchronized()
{
return SynchronizedDoubleSet.of(this);
}
public double sum()
{
double result = 0.0;
double compensation = 0.0;
if (this.getSentinelValues() != null)
{
if (this.getSentinelValues().containsZeroKey)
{
double adjustedValue = EMPTY_KEY - compensation;
double nextSum = result + adjustedValue;
compensation = nextSum - result - adjustedValue;
result = nextSum;
}
if (this.getSentinelValues().containsOneKey)
{
double adjustedValue = REMOVED_KEY - compensation;
double nextSum = result + adjustedValue;
compensation = nextSum - result - adjustedValue;
result = nextSum;
}
}
for (int i = 0; i < this.getTableSize(); i++)
{
if (isNonSentinel(this.getKeyAtIndex(i)))
{
double adjustedValue = this.getKeyAtIndex(i) - compensation;
double nextSum = result + adjustedValue;
compensation = nextSum - result - adjustedValue;
result = nextSum;
}
}
return result;
}
public double max()
{
if (this.getOuter().isEmpty())
{
throw new NoSuchElementException();
}
double 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 || Double.compare(max, this.getKeyAtIndex(i)) < 0))
{
max = this.getKeyAtIndex(i);
isMaxSet = true;
}
}
return max;
}
public double maxIfEmpty(double defaultValue)
{
if (this.getOuter().isEmpty())
{
return defaultValue;
}
return this.max();
}
public double min()
{
if (this.getOuter().isEmpty())
{
throw new NoSuchElementException();
}
double 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 || Double.compare(this.getKeyAtIndex(i), min) < 0))
{
min = this.getKeyAtIndex(i);
isMinSet = true;
}
}
return min;
}
public double minIfEmpty(double defaultValue)
{
if (this.getOuter().isEmpty())
{
return defaultValue;
}
return this.min();
}
public double average()
{
if (this.isEmpty())
{
throw new ArithmeticException();
}
return this.sum() / (double) this.size();
}
public double median()
{
if (this.isEmpty())
{
throw new ArithmeticException();
}
double[] sortedArray = this.toSortedArray();
int middleIndex = sortedArray.length >> 1;
if (sortedArray.length > 1 && (sortedArray.length & 1) == 0)
{
double first = sortedArray[middleIndex];
double second = sortedArray[middleIndex - 1];
return (first + second) / 2.0;
}
return sortedArray[middleIndex];
}
public double[] toSortedArray()
{
double[] array = this.toArray();
Arrays.sort(array);
return array;
}
public MutableDoubleList toSortedList()
{
return DoubleArrayList.newList(this).sortThis();
}
public double[] toArray()
{
int size = this.getOuter().size();
final double[] result = new double[size];
this.getOuter().forEachKey(new DoubleProcedure()
{
private int index;
public void value(double each)
{
result[this.index] = each;
this.index++;
}
});
return result;
}
public boolean contains(double value)
{
return this.getOuter().containsKey(value);
}
public boolean containsAll(double... source)
{
for (double item : source)
{
if (!this.getOuter().containsKey(item))
{
return false;
}
}
return true;
}
public boolean containsAll(DoubleIterable source)
{
DoubleIterator iterator = source.doubleIterator();
while (iterator.hasNext())
{
if (!this.getOuter().containsKey(iterator.next()))
{
return false;
}
}
return true;
}
public MutableDoubleList toList()
{
return DoubleArrayList.newList(this);
}
public MutableDoubleSet toSet()
{
return DoubleHashSet.newSet(this);
}
public MutableDoubleBag toBag()
{
return DoubleHashBag.newBag(this);
}
public LazyDoubleIterable asLazy()
{
return new LazyDoubleIterableAdapter(this);
}
public T injectInto(T injectedValue, ObjectDoubleToObjectFunction 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 ImmutableDoubleSet toImmutable()
{
return DoubleSets.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 DoubleSet))
{
return false;
}
DoubleSet other = (DoubleSet) 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 += (int) (Double.doubleToLongBits(EMPTY_KEY) ^ Double.doubleToLongBits(EMPTY_KEY) >>> 32);
}
if (this.getSentinelValues().containsOneKey)
{
result += (int) (Double.doubleToLongBits(REMOVED_KEY) ^ Double.doubleToLongBits(REMOVED_KEY) >>> 32);
}
}
for (int i = 0; i < this.getTableSize(); i++)
{
if (isNonSentinel(this.getKeyAtIndex(i)))
{
result += (int) (Double.doubleToLongBits(this.getKeyAtIndex(i)) ^ Double.doubleToLongBits(this.getKeyAtIndex(i)) >>> 32);
}
}
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