com.gs.collections.impl.bag.mutable.primitive.DoubleHashBag 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.bag.mutable.primitive;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.NoSuchElementException;
import com.gs.collections.api.DoubleIterable;
import com.gs.collections.api.bag.MutableBag;
import com.gs.collections.api.bag.primitive.DoubleBag;
import com.gs.collections.api.bag.primitive.ImmutableDoubleBag;
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.function.primitive.IntToIntFunction;
import com.gs.collections.api.block.predicate.primitive.DoublePredicate;
import com.gs.collections.api.block.procedure.primitive.DoubleProcedure;
import com.gs.collections.api.block.procedure.primitive.DoubleIntProcedure;
import com.gs.collections.api.iterator.DoubleIterator;
import com.gs.collections.api.iterator.MutableDoubleIterator;
import com.gs.collections.api.set.primitive.DoubleSet;
import com.gs.collections.impl.Counter;
import com.gs.collections.impl.bag.mutable.HashBag;
import com.gs.collections.impl.block.factory.primitive.IntToIntFunctions;
import com.gs.collections.impl.factory.primitive.DoubleBags;
import com.gs.collections.impl.map.mutable.primitive.DoubleIntHashMap;
import com.gs.collections.impl.primitive.AbstractDoubleIterable;
import com.gs.collections.impl.set.mutable.primitive.DoubleHashSet;
import net.jcip.annotations.NotThreadSafe;
/**
* DoubleHashBag is similar to {@link HashBag}, and is memory-optimized for double primitives.
* This file was automatically generated from template file primitiveHashBag.stg.
*
* @since 3.0.
*/
@NotThreadSafe
public final class DoubleHashBag extends AbstractDoubleIterable implements MutableDoubleBag, Externalizable
{
private static final long serialVersionUID = 1L;
private DoubleIntHashMap items;
private int size;
public DoubleHashBag()
{
this.items = new DoubleIntHashMap();
}
public DoubleHashBag(int size)
{
this.items = new DoubleIntHashMap(size);
}
public DoubleHashBag(DoubleIterable iterable)
{
this();
this.addAll(iterable);
}
public DoubleHashBag(double... elements)
{
this();
this.addAll(elements);
}
public DoubleHashBag(DoubleHashBag bag)
{
this.items = new DoubleIntHashMap(bag.sizeDistinct());
this.addAll(bag);
}
public static DoubleHashBag newBag(int size)
{
return new DoubleHashBag(size);
}
public static DoubleHashBag newBagWith(double... source)
{
return new DoubleHashBag(source);
}
public static DoubleHashBag newBag(DoubleIterable source)
{
if (source instanceof DoubleHashBag)
{
return new DoubleHashBag((DoubleHashBag) source);
}
return new DoubleHashBag(source);
}
public static DoubleHashBag newBag(DoubleBag source)
{
return new DoubleHashBag(source);
}
@Override
public boolean isEmpty()
{
return this.items.isEmpty();
}
@Override
public boolean notEmpty()
{
return this.items.notEmpty();
}
@Override
public int size()
{
return this.size;
}
public int sizeDistinct()
{
return this.items.size();
}
public void clear()
{
this.items.clear();
this.size = 0;
}
public DoubleHashBag with(double element)
{
this.add(element);
return this;
}
public DoubleHashBag with(double element1, double element2)
{
this.add(element1);
this.add(element2);
return this;
}
public DoubleHashBag with(double element1, double element2, double element3)
{
this.add(element1);
this.add(element2);
this.add(element3);
return this;
}
public DoubleHashBag withAll(DoubleIterable iterable)
{
this.addAll(iterable);
return this;
}
public DoubleHashBag without(double element)
{
this.remove(element);
return this;
}
public DoubleHashBag withoutAll(DoubleIterable iterable)
{
this.removeAll(iterable);
return this;
}
public boolean contains(double value)
{
return this.items.containsKey(value);
}
public int occurrencesOf(double item)
{
return this.items.get(item);
}
public void forEachWithOccurrences(DoubleIntProcedure procedure)
{
this.items.forEachKeyValue(procedure);
}
public boolean add(double item)
{
this.items.updateValue(item, 0, IntToIntFunctions.increment());
this.size++;
return true;
}
public boolean remove(double item)
{
int newValue = this.items.updateValue(item, 0, IntToIntFunctions.decrement());
if (newValue <= 0)
{
this.items.removeKey(item);
if (newValue == 0)
{
this.size--;
}
return newValue == 0;
}
this.size--;
return true;
}
public boolean addAll(double... source)
{
if (source.length == 0)
{
return false;
}
for (double each : source)
{
this.add(each);
}
return true;
}
public boolean addAll(DoubleIterable source)
{
if (source.isEmpty())
{
return false;
}
if (source instanceof DoubleBag)
{
DoubleBag otherBag = (DoubleBag) source;
otherBag.forEachWithOccurrences(new DoubleIntProcedure()
{
public void value(double each, int occurrences)
{
DoubleHashBag.this.addOccurrences(each, occurrences);
}
});
}
else
{
DoubleIterator iterator = source.doubleIterator();
while (iterator.hasNext())
{
double each = iterator.next();
this.add(each);
}
}
return true;
}
public boolean removeAll(double... source)
{
if (source.length == 0)
{
return false;
}
int oldSize = this.size();
for (double each : source)
{
int occurrences = this.items.removeKeyIfAbsent(each, 0);
this.size -= occurrences;
}
return this.size() != oldSize;
}
public boolean removeAll(DoubleIterable source)
{
if (source.isEmpty())
{
return false;
}
int oldSize = this.size();
if (source instanceof DoubleBag)
{
DoubleBag otherBag = (DoubleBag) source;
otherBag.forEachWithOccurrences(new DoubleIntProcedure()
{
public void value(double each, int occurrences)
{
int oldOccurrences = DoubleHashBag.this.items.removeKeyIfAbsent(each, 0);
DoubleHashBag.this.size -= oldOccurrences;
}
});
}
else
{
DoubleIterator iterator = source.doubleIterator();
while (iterator.hasNext())
{
double each = iterator.next();
int occurrences = this.items.removeKeyIfAbsent(each, 0);
this.size -= occurrences;
}
}
return this.size() != oldSize;
}
public boolean retainAll(DoubleIterable source)
{
int oldSize = this.size();
final DoubleSet sourceSet = source instanceof DoubleSet ? (DoubleSet) source : source.toSet();
DoubleHashBag retained = this.select(new DoublePredicate()
{
public boolean accept(double key)
{
return sourceSet.contains(key);
}
});
if (retained.size() != oldSize)
{
this.items = retained.items;
this.size = retained.size;
return true;
}
return false;
}
public boolean retainAll(double... source)
{
return this.retainAll(DoubleHashSet.newSetWith(source));
}
public void addOccurrences(double item, final int occurrences)
{
if (occurrences < 0)
{
throw new IllegalArgumentException("Cannot add a negative number of occurrences");
}
if (occurrences > 0)
{
this.items.updateValue(item, 0, new IntToIntFunction()
{
public int valueOf(int intParameter)
{
return intParameter + occurrences;
}
});
this.size += occurrences;
}
}
public boolean removeOccurrences(double item, final int occurrences)
{
if (occurrences < 0)
{
throw new IllegalArgumentException("Cannot remove a negative number of occurrences");
}
if (occurrences == 0)
{
return false;
}
int newValue = this.items.updateValue(item, 0, new IntToIntFunction()
{
public int valueOf(int intParameter)
{
return intParameter - occurrences;
}
});
if (newValue <= 0)
{
this.size -= occurrences - newValue;
this.items.removeKey(item);
return newValue + occurrences != 0;
}
this.size -= occurrences;
return true;
}
public void forEach(final DoubleProcedure procedure)
{
this.each(procedure);
}
/**
* @since 7.0.
*/
public void each(final DoubleProcedure procedure)
{
this.items.forEachKeyValue(new DoubleIntProcedure()
{
public void value(double key, int occurrences)
{
for (int i = 0; i < occurrences; i++)
{
procedure.value(key);
}
}
});
}
public DoubleHashBag select(final DoublePredicate predicate)
{
final DoubleHashBag result = new DoubleHashBag();
this.forEachWithOccurrences(new DoubleIntProcedure()
{
public void value(double each, int occurrences)
{
if (predicate.accept(each))
{
result.addOccurrences(each, occurrences);
}
}
});
return result;
}
public DoubleHashBag reject(final DoublePredicate predicate)
{
final DoubleHashBag result = new DoubleHashBag();
this.forEachWithOccurrences(new DoubleIntProcedure()
{
public void value(double each, int occurrences)
{
if (!predicate.accept(each))
{
result.addOccurrences(each, occurrences);
}
}
});
return result;
}
public T injectInto(T injectedValue, ObjectDoubleToObjectFunction super T, ? extends T> function)
{
T result = injectedValue;
DoubleIterator it = this.doubleIterator();
while (it.hasNext())
{
result = function.valueOf(result, it.next());
}
return result;
}
@Override
public boolean equals(Object otherBag)
{
if (otherBag == this)
{
return true;
}
if (!(otherBag instanceof DoubleBag))
{
return false;
}
final DoubleBag bag = (DoubleBag) otherBag;
if (this.sizeDistinct() != bag.sizeDistinct())
{
return false;
}
return this.items.keysView().allSatisfy(new DoublePredicate()
{
public boolean accept(double key)
{
return DoubleHashBag.this.occurrencesOf(key) == bag.occurrencesOf(key);
}
});
}
@Override
public int hashCode()
{
final Counter result = new Counter();
this.forEachWithOccurrences(new DoubleIntProcedure()
{
public void value(double eachItem, int occurrences)
{
result.add((int) (Double.doubleToLongBits(eachItem) ^ Double.doubleToLongBits(eachItem) >>> 32) ^ occurrences);
}
});
return result.getCount();
}
public void appendString(
final Appendable appendable,
String start,
final String separator,
String end)
{
final boolean[] firstItem = {true};
try
{
appendable.append(start);
this.items.forEachKeyValue(new DoubleIntProcedure()
{
public void value(double each, int occurrences)
{
try
{
for (int i = 0; i < occurrences; i++)
{
if (!firstItem[0])
{
appendable.append(separator);
}
appendable.append(String.valueOf(each));
firstItem[0] = false;
}
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
});
appendable.append(end);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public int count(final DoublePredicate predicate)
{
final Counter result = new Counter();
this.forEachWithOccurrences(new DoubleIntProcedure()
{
public void value(double each, int occurrences)
{
if (predicate.accept(each))
{
result.add(occurrences);
}
}
});
return result.getCount();
}
public boolean anySatisfy(DoublePredicate predicate)
{
return this.items.keysView().anySatisfy(predicate);
}
public boolean allSatisfy(DoublePredicate predicate)
{
return this.items.keysView().allSatisfy(predicate);
}
public boolean noneSatisfy(DoublePredicate predicate)
{
return this.items.keysView().noneSatisfy(predicate);
}
public double detectIfNone(DoublePredicate predicate, double ifNone)
{
return this.items.keysView().detectIfNone(predicate, ifNone);
}
public MutableBag collect(final DoubleToObjectFunction extends V> function)
{
final HashBag result = HashBag.newBag(this.items.size());
this.forEachWithOccurrences(new DoubleIntProcedure()
{
public void value(double each, int occurrences)
{
result.addOccurrences(function.valueOf(each), occurrences);
}
});
return result;
}
public double max()
{
if (this.isEmpty())
{
throw new NoSuchElementException();
}
return this.items.keysView().max();
}
public double min()
{
if (this.isEmpty())
{
throw new NoSuchElementException();
}
return this.items.keysView().min();
}
public double sum()
{
final double[] result = {0.0};
final double[] compensation = {0.0};
this.forEachWithOccurrences(new DoubleIntProcedure()
{
public void value(double each, int occurrences)
{
for(int i = 0; i < occurrences; i++)
{
double adjustedValue = (double) each - compensation[0];
double nextSum = result[0] + adjustedValue;
compensation[0] = nextSum - result[0] - adjustedValue;
result[0] = nextSum;
}
}
});
return result[0];
}
public double[] toArray()
{
final double[] array = new double[this.size()];
final int[] index = {0};
this.forEachWithOccurrences(new DoubleIntProcedure()
{
public void value(double each, int occurrences)
{
for (int i = 0; i < occurrences; i++)
{
array[index[0]] = each;
index[0]++;
}
}
});
return array;
}
public MutableDoubleBag asUnmodifiable()
{
return new UnmodifiableDoubleBag(this);
}
public MutableDoubleBag asSynchronized()
{
return new SynchronizedDoubleBag(this);
}
public ImmutableDoubleBag toImmutable()
{
return DoubleBags.immutable.withAll(this);
}
public MutableDoubleIterator doubleIterator()
{
return new InternalIterator();
}
public void writeExternal(final ObjectOutput out) throws IOException
{
out.writeInt(this.items.size());
try
{
this.items.forEachKeyValue(new DoubleIntProcedure()
{
public void value(double each, int occurrences)
{
try
{
out.writeDouble(each);
out.writeInt(occurrences);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
});
}
catch (RuntimeException e)
{
if (e.getCause() instanceof IOException)
{
throw (IOException) e.getCause();
}
throw e;
}
}
public void readExternal(ObjectInput in) throws IOException
{
int size = in.readInt();
this.items = new DoubleIntHashMap(size);
for (int i = 0; i < size; i++)
{
this.addOccurrences(in.readDouble(), in.readInt());
}
}
private class InternalIterator implements MutableDoubleIterator
{
private final MutableDoubleIterator doubleIterator = DoubleHashBag.this.items.keySet().doubleIterator();
private double currentItem;
private int occurrences;
private boolean canRemove;
public boolean hasNext()
{
return this.occurrences > 0 || this.doubleIterator.hasNext();
}
public double next()
{
if (this.occurrences == 0)
{
this.currentItem = this.doubleIterator.next();
this.occurrences = DoubleHashBag.this.occurrencesOf(this.currentItem);
}
this.occurrences--;
this.canRemove = true;
return this.currentItem;
}
public void remove()
{
if (!this.canRemove)
{
throw new IllegalStateException();
}
if (this.occurrences == 0)
{
this.doubleIterator.remove();
DoubleHashBag.this.size--;
}
else
{
DoubleHashBag.this.remove(this.currentItem);
}
this.canRemove = false;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy