com.gs.collections.impl.map.mutable.primitive.ImmutableByteByteMapKeySet 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.io.Serializable;
import java.util.NoSuchElementException;
import com.gs.collections.impl.SpreadFunctions;
import com.gs.collections.api.block.function.primitive.ByteToObjectFunction;
import com.gs.collections.api.block.function.primitive.ObjectByteToObjectFunction;
import com.gs.collections.api.block.predicate.primitive.BytePredicate;
import com.gs.collections.api.block.procedure.primitive.ByteProcedure;
import com.gs.collections.api.iterator.ByteIterator;
import com.gs.collections.api.set.ImmutableSet;
import com.gs.collections.api.set.MutableSet;
import com.gs.collections.api.set.primitive.ImmutableByteSet;
import com.gs.collections.impl.set.immutable.primitive.ImmutableByteSetSerializationProxy;
import com.gs.collections.impl.set.immutable.primitive.AbstractImmutableByteSet;
import com.gs.collections.impl.set.mutable.UnifiedSet;
import com.gs.collections.impl.set.mutable.primitive.ByteHashSet;
/**
* This file was automatically generated from template file immutablePrimitivePrimitiveMapKeySet.stg
*
* @since 6.0.
*/
class ImmutableByteByteMapKeySet extends AbstractImmutableByteSet implements Serializable
{
private static final long serialVersionUID = 1L;
private static final byte EMPTY_KEY = (byte) 0;
private static final byte REMOVED_KEY = (byte) 1;
private static final int CACHE_LINE_SIZE = 64;
private static final int KEY_SIZE = 1;
private static final int INITIAL_LINEAR_PROBE = CACHE_LINE_SIZE / KEY_SIZE / 2; /* half a cache line */
private final byte[] keysValues;
private final int occupiedWithData;
private final boolean containsZeroKey;
private final boolean containsOneKey;
ImmutableByteByteMapKeySet(byte[] keysValues, int occupiedWithData, boolean containsZeroKey, boolean containsOneKey)
{
this.keysValues = keysValues;
this.occupiedWithData = occupiedWithData;
this.containsZeroKey = containsZeroKey;
this.containsOneKey = containsOneKey;
}
private static boolean isEmptyKey(byte key)
{
return key == EMPTY_KEY;
}
private static boolean isRemovedKey(byte key)
{
return key == REMOVED_KEY;
}
private static boolean isNonSentinel(byte 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.keysValues != null)
{
for (int i = 0; i < this.keysValues.length; i += 2)
{
if (isNonSentinel(this.keysValues[i]))
{
result += (int) this.keysValues[i];
}
}
}
return result;
}
public int size()
{
return this.occupiedWithData + (this.containsOneKey ? 1 : 0) + (this.containsZeroKey ? 1 : 0);
}
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.keysValues.length; i += 2)
{
if (isNonSentinel(this.keysValues[i]))
{
if (count > 0)
{
appendable.append(separator);
}
count++;
appendable.append(String.valueOf(this.keysValues[i]));
}
}
appendable.append(end);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public ByteIterator byteIterator()
{
return new InternalByteIterator();
}
public byte[] toArray()
{
byte[] array = new byte[this.size()];
int index = 0;
if (this.containsZeroKey)
{
array[index] = EMPTY_KEY;
index++;
}
if (this.containsOneKey)
{
array[index] = REMOVED_KEY;
index++;
}
if (this.keysValues != null)
{
for (int i = 0; i < this.keysValues.length; i += 2)
{
if (isNonSentinel(this.keysValues[i]))
{
array[index] = this.keysValues[i];
index++;
}
}
}
return array;
}
public boolean contains(byte value)
{
if (value == EMPTY_KEY)
{
return this.containsZeroKey;
}
if (value == REMOVED_KEY)
{
return this.containsOneKey;
}
return this.keysValues[this.probe(value)] == value;
}
public void forEach(ByteProcedure procedure)
{
this.each(procedure);
}
/**
* @since 7.0.
*/
public void each(ByteProcedure procedure)
{
if (this.containsZeroKey)
{
procedure.value(EMPTY_KEY);
}
if (this.containsOneKey)
{
procedure.value(REMOVED_KEY);
}
if (this.keysValues != null)
{
for (int i = 0; i < this.keysValues.length; i += 2)
{
if (isNonSentinel(this.keysValues[i]))
{
procedure.value(this.keysValues[i]);
}
}
}
}
public ImmutableByteSet select(BytePredicate predicate)
{
ByteHashSet result = new ByteHashSet();
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.keysValues.length; i += 2)
{
if (isNonSentinel(this.keysValues[i]) && predicate.accept(this.keysValues[i]))
{
result.add(this.keysValues[i]);
}
}
return result.toImmutable();
}
public ImmutableByteSet reject(BytePredicate predicate)
{
ByteHashSet result = new ByteHashSet();
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.keysValues.length; i += 2)
{
if (isNonSentinel(this.keysValues[i]) && !predicate.accept(this.keysValues[i]))
{
result.add(this.keysValues[i]);
}
}
return result.toImmutable();
}
public ImmutableSet collect(ByteToObjectFunction extends V> function)
{
MutableSet 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.keysValues != null)
{
for (int i = 0; i < this.keysValues.length; i += 2)
{
if (isNonSentinel(this.keysValues[i]))
{
target.add(function.valueOf(this.keysValues[i]));
}
}
}
return target.toImmutable();
}
public byte detectIfNone(BytePredicate predicate, byte 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.keysValues.length; i += 2)
{
if (isNonSentinel(this.keysValues[i]) && predicate.accept(this.keysValues[i]))
{
return this.keysValues[i];
}
}
return ifNone;
}
public int count(BytePredicate 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.keysValues.length; i += 2)
{
if (isNonSentinel(this.keysValues[i]) && predicate.accept(this.keysValues[i]))
{
count++;
}
}
return count;
}
public boolean anySatisfy(BytePredicate 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.keysValues.length; i += 2)
{
if (isNonSentinel(this.keysValues[i]) && predicate.accept(this.keysValues[i]))
{
return true;
}
}
return false;
}
public boolean allSatisfy(BytePredicate 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.keysValues.length; i += 2)
{
if (isNonSentinel(this.keysValues[i]) && !predicate.accept(this.keysValues[i]))
{
return false;
}
}
return true;
}
public boolean noneSatisfy(BytePredicate 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.keysValues.length; i += 2)
{
if (isNonSentinel(this.keysValues[i]) && predicate.accept(this.keysValues[i]))
{
return false;
}
}
return true;
}
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.keysValues != null)
{
for (int i = 0; i < this.keysValues.length; i += 2)
{
if (isNonSentinel(this.keysValues[i]))
{
long adjustedValue = this.keysValues[i] - compensation;
long nextSum = result + adjustedValue;
compensation = nextSum - result - adjustedValue;
result = nextSum;
}
}
}
return result;
}
public byte max()
{
if (this.isEmpty())
{
throw new NoSuchElementException();
}
byte 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.keysValues.length; i += 2)
{
if (isNonSentinel(this.keysValues[i]) && (!isMaxSet || max < this.keysValues[i]))
{
max = this.keysValues[i];
isMaxSet = true;
}
}
return max;
}
public byte min()
{
if (this.isEmpty())
{
throw new NoSuchElementException();
}
byte 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.keysValues.length; i += 2)
{
if (isNonSentinel(this.keysValues[i]) && (!isMinSet || this.keysValues[i] < min))
{
min = this.keysValues[i];
isMinSet = true;
}
}
return min;
}
public T injectInto(T injectedValue, ObjectByteToObjectFunction 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.keysValues != null)
{
for (int i = 0; i < this.keysValues.length; i += 2)
{
if (isNonSentinel(this.keysValues[i]))
{
result = function.valueOf(result, this.keysValues[i]);
}
}
}
return result;
}
private Object writeReplace()
{
return new ImmutableByteSetSerializationProxy(this);
}
// exposed for testing
int probe(byte element)
{
int index = this.spreadAndMask(element) << 1;
byte keyAtIndex = this.keysValues[index];
if (keyAtIndex == element || keyAtIndex == EMPTY_KEY)
{
return index;
}
int removedIndex = keyAtIndex == REMOVED_KEY ? index : -1;
int nextIndex = index;
int probe = 34;
// loop until an empty slot is reached
while (true)
{
// Probe algorithm: 17*n*(n+1)/2 where n = number of collisions
nextIndex += probe;
probe += 34;
nextIndex &= this.keysValues.length - 1;
if (this.keysValues[nextIndex] == element)
{
return nextIndex;
}
if (this.keysValues[nextIndex] == REMOVED_KEY)
{
if (removedIndex == -1)
{
removedIndex = nextIndex;
}
}
else if (this.keysValues[nextIndex] == EMPTY_KEY)
{
return removedIndex == -1 ? nextIndex : removedIndex;
}
}
}
// exposed for testing
int spreadAndMask(byte element)
{
// No spreading necessary for 8-bit types
return this.mask(element);
}
private int mask(int spread)
{
return spread & ((this.keysValues.length >> 1) - 1);
}
private class InternalByteIterator implements ByteIterator
{
private int count;
private int position;
private boolean handledZero;
private boolean handledOne;
public boolean hasNext()
{
return this.count < ImmutableByteByteMapKeySet.this.size();
}
public byte next()
{
if (!this.hasNext())
{
throw new NoSuchElementException("next() called, but the iterator is exhausted");
}
this.count++;
if (!this.handledZero)
{
this.handledZero = true;
if (ImmutableByteByteMapKeySet.this.containsZeroKey)
{
return ImmutableByteByteMapKeySet.EMPTY_KEY;
}
}
if (!this.handledOne)
{
this.handledOne = true;
if (ImmutableByteByteMapKeySet.this.containsOneKey)
{
return ImmutableByteByteMapKeySet.REMOVED_KEY;
}
}
byte[] keysValues = ImmutableByteByteMapKeySet.this.keysValues;
while (!ImmutableByteByteMapKeySet.isNonSentinel(keysValues[this.position]))
{
this.position += 2;
}
byte result = ImmutableByteByteMapKeySet.this.keysValues[this.position];
this.position += 2;
return result;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy