All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gs.collections.impl.map.mutable.primitive.FloatBooleanHashMap Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 7.0.3
Show newest version
/*
 * 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.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Iterator;
import java.util.NoSuchElementException;

import com.gs.collections.api.BooleanIterable;
import com.gs.collections.api.FloatIterable;
import com.gs.collections.api.LazyFloatIterable;
import com.gs.collections.api.LazyIterable;
import com.gs.collections.api.RichIterable;
import com.gs.collections.api.bag.primitive.MutableFloatBag;
import com.gs.collections.api.block.function.primitive.BooleanFunction;
import com.gs.collections.api.block.function.primitive.BooleanFunction0;
import com.gs.collections.api.block.function.primitive.BooleanToBooleanFunction;
import com.gs.collections.api.block.function.primitive.BooleanToObjectFunction;
import com.gs.collections.api.block.function.primitive.FloatToBooleanFunction;
import com.gs.collections.api.block.function.primitive.FloatToObjectFunction;
import com.gs.collections.api.block.function.primitive.ObjectFloatToObjectFunction;
import com.gs.collections.api.block.function.primitive.ObjectBooleanToObjectFunction;
import com.gs.collections.api.block.predicate.primitive.BooleanPredicate;
import com.gs.collections.api.block.predicate.primitive.FloatBooleanPredicate;
import com.gs.collections.api.block.predicate.primitive.FloatPredicate;
import com.gs.collections.api.block.procedure.Procedure;
import com.gs.collections.api.block.procedure.Procedure2;
import com.gs.collections.api.block.procedure.primitive.BooleanProcedure;
import com.gs.collections.api.block.procedure.primitive.FloatBooleanProcedure;
import com.gs.collections.api.block.procedure.primitive.FloatProcedure;
import com.gs.collections.api.block.procedure.primitive.ObjectIntProcedure;
import com.gs.collections.api.collection.MutableCollection;
import com.gs.collections.api.collection.primitive.MutableBooleanCollection;
import com.gs.collections.api.iterator.FloatIterator;
import com.gs.collections.api.iterator.MutableBooleanIterator;
import com.gs.collections.api.iterator.MutableFloatIterator;
import com.gs.collections.api.list.primitive.MutableFloatList;
import com.gs.collections.api.map.primitive.FloatBooleanMap;
import com.gs.collections.api.map.primitive.ImmutableFloatBooleanMap;
import com.gs.collections.api.map.primitive.MutableFloatBooleanMap;
import com.gs.collections.api.set.primitive.BooleanSet;
import com.gs.collections.api.set.primitive.FloatSet;
import com.gs.collections.api.set.primitive.MutableFloatSet;
import com.gs.collections.api.tuple.primitive.FloatBooleanPair;
import com.gs.collections.impl.SpreadFunctions;
import com.gs.collections.impl.bag.mutable.primitive.FloatHashBag;
import com.gs.collections.impl.block.factory.primitive.FloatPredicates;
import com.gs.collections.impl.factory.primitive.FloatBooleanMaps;
import com.gs.collections.impl.iterator.UnmodifiableFloatIterator;
import com.gs.collections.impl.lazy.AbstractLazyIterable;
import com.gs.collections.impl.lazy.primitive.AbstractLazyFloatIterable;
import com.gs.collections.impl.lazy.primitive.CollectFloatToObjectIterable;
import com.gs.collections.impl.lazy.primitive.SelectFloatIterable;
import com.gs.collections.impl.list.mutable.FastList;
import com.gs.collections.impl.list.mutable.primitive.BooleanArrayList;
import com.gs.collections.impl.list.mutable.primitive.FloatArrayList;
import com.gs.collections.impl.set.mutable.primitive.FloatHashSet;
import com.gs.collections.impl.tuple.primitive.PrimitiveTuples;

/**
 * This file was automatically generated from template file primitiveBooleanHashMap.stg.
 *
 * @since 3.0.
 */
public class FloatBooleanHashMap extends AbstractMutableBooleanValuesMap implements MutableFloatBooleanMap, MutableFloatKeysMap, Externalizable
{
    static final boolean EMPTY_VALUE = false;
    private static final long serialVersionUID = 1L;
    private static final float EMPTY_KEY = 0.0f;
    private static final float REMOVED_KEY = 1.0f;

    /**
     * @deprecated in 5.1.0.
     */
    @Deprecated
    private static final float DEFAULT_LOAD_FACTOR = 0.5f;
    private static final int OCCUPIED_DATA_RATIO = 2;
    private static final int OCCUPIED_SENTINEL_RATIO = 4;
    private static final int DEFAULT_INITIAL_CAPACITY = 8;
    private static final int CACHE_LINE_SIZE = 64;
    private static final int KEY_SIZE = 4;
    private static final int INITIAL_LINEAR_PROBE = CACHE_LINE_SIZE / KEY_SIZE / 2; /* half a cache line */

    private float[] keys;
    private BitSet values;

    private int occupiedWithData;
    private int occupiedWithSentinels;
    private SentinelValues sentinelValues;

    public FloatBooleanHashMap()
    {
        this.allocateTable(DEFAULT_INITIAL_CAPACITY << 1);
    }

    public FloatBooleanHashMap(int initialCapacity)
    {
        if (initialCapacity < 0)
        {
            throw new IllegalArgumentException("initial capacity cannot be less than 0");
        }
        int capacity = this.smallestPowerOfTwoGreaterThan(this.fastCeil(initialCapacity * OCCUPIED_DATA_RATIO));
        this.allocateTable(capacity);
    }

    public FloatBooleanHashMap(FloatBooleanMap map)
    {
        this(Math.max(map.size(), DEFAULT_INITIAL_CAPACITY));
        this.putAll(map);
    }

    /**
     * @deprecated in 5.1.0.
     */
    @Deprecated
    public FloatBooleanHashMap(int initialCapacity, float loadFactor)
    {
        this(initialCapacity);
    }

    @Override
    protected int getOccupiedWithData()
    {
        return this.occupiedWithData;
    }

    @Override
    protected SentinelValues getSentinelValues()
    {
        return this.sentinelValues;
    }

    @Override
    protected void setSentinelValuesNull()
    {
        this.sentinelValues = null;
    }

    @Override
    protected boolean getEmptyValue()
    {
        return EMPTY_VALUE;
    }

    @Override
    protected int getTableSize()
    {
        return this.keys.length;
    }

    @Override
    protected boolean getValueAtIndex(int index)
    {
        return this.values.get(index);
    }

    @Override
    protected boolean isNonSentinelAtIndex(int index)
    {
        return !isEmptyKey(this.keys[index]) && !isRemovedKey(this.keys[index]);
    }

    private int smallestPowerOfTwoGreaterThan(int n)
    {
        return n > 1 ? Integer.highestOneBit(n - 1) << 1 : 1;
    }

    public MutableFloatBooleanMap asUnmodifiable()
    {
        return new UnmodifiableFloatBooleanMap(this);
    }

    public MutableFloatBooleanMap asSynchronized()
    {
        return new SynchronizedFloatBooleanMap(this);
    }

    public ImmutableFloatBooleanMap toImmutable()
    {
        return FloatBooleanMaps.immutable.withAll(this);
    }

    public static FloatBooleanHashMap newWithKeysValues(float key1, boolean value1)
    {
        return new FloatBooleanHashMap(1).withKeyValue(key1, value1);
    }

    public static FloatBooleanHashMap newWithKeysValues(float key1, boolean value1, float key2, boolean value2)
    {
        return new FloatBooleanHashMap(2).withKeysValues(key1, value1, key2, value2);
    }

    public static FloatBooleanHashMap newWithKeysValues(float key1, boolean value1, float key2, boolean value2, float key3, boolean value3)
    {
        return new FloatBooleanHashMap(3).withKeysValues(key1, value1, key2, value2, key3, value3);
    }

    public static FloatBooleanHashMap newWithKeysValues(float key1, boolean value1, float key2, boolean value2, float key3, boolean value3, float key4, boolean value4)
    {
        return new FloatBooleanHashMap(4).withKeysValues(key1, value1, key2, value2, key3, value3, key4, value4);
    }

    public FloatBooleanHashMap withKeyValue(float key1, boolean value1)
    {
        this.put(key1, value1);
        return this;
    }

    public FloatBooleanHashMap withKeysValues(float key1, boolean value1, float key2, boolean value2)
    {
        this.put(key1, value1);
        this.put(key2, value2);
        return this;
    }

    public FloatBooleanHashMap withKeysValues(float key1, boolean value1, float key2, boolean value2, float key3, boolean value3)
    {
        this.put(key1, value1);
        this.put(key2, value2);
        this.put(key3, value3);
        return this;
    }

    public FloatBooleanHashMap withKeysValues(float key1, boolean value1, float key2, boolean value2, float key3, boolean value3, float key4, boolean value4)
    {
        this.put(key1, value1);
        this.put(key2, value2);
        this.put(key3, value3);
        this.put(key4, value4);
        return this;
    }

    public FloatBooleanHashMap withoutKey(float key)
    {
        this.removeKey(key);
        return this;
    }

    public FloatBooleanHashMap withoutAllKeys(FloatIterable keys)
    {
        keys.forEach(new FloatProcedure()
        {
            public void value(float key)
            {
                FloatBooleanHashMap.this.removeKey(key);
            }
        });
        return this;
    }

    private int fastCeil(float v)
    {
        int possibleResult = (int) v;
        if (v - possibleResult > 0.0F)
        {
            possibleResult++;
        }
        return possibleResult;
    }

    private static boolean isEmptyKey(float key)
    {
        return Float.compare(key, EMPTY_KEY) == 0;
    }

    private static boolean isRemovedKey(float key)
    {
        return Float.compare(key, REMOVED_KEY) == 0;
    }

    private static boolean isNonSentinel(float key)
    {
        return !isEmptyKey(key) && !isRemovedKey(key);
    }

    private void allocateTable(int sizeToAllocate)
    {
        this.keys = new float[sizeToAllocate];
        this.values = new BitSet(sizeToAllocate);
    }

    /**
     * Rehashes every element in the set into a new backing table of the smallest possible size and eliminating removed sentinels.
     */
    public void compact()
    {
        this.rehash(this.smallestPowerOfTwoGreaterThan(this.size()));
    }

    private void rehash()
    {
        this.rehash(this.keys.length);
    }

    private void rehashAndGrow()
    {
        this.rehash(this.keys.length << 1);
    }

    private void rehash(int newCapacity)
    {
        int oldLength = this.keys.length;
        float[] old = this.keys;
        BitSet oldValues = this.values;
        this.allocateTable(newCapacity);
        this.occupiedWithData = 0;
        this.occupiedWithSentinels = 0;

        for (int i = 0; i < oldLength; i++)
        {
            if (isNonSentinel(old[i]))
            {
                this.put(old[i], oldValues.get(i));
            }
        }
    }

    // exposed for testing
    int probe(float element)
    {
        int index = this.spreadAndMask(element);
        float keyAtIndex = this.keys[index];

        if (Float.compare(keyAtIndex, element) == 0 || Float.compare(keyAtIndex, EMPTY_KEY) == 0)
        {
            return index;
        }

        int removedIndex = Float.compare(keyAtIndex, REMOVED_KEY) == 0 ? index : -1;
        for (int i = 1; i < INITIAL_LINEAR_PROBE; i++)
        {
            int nextIndex = (index + i) & (this.keys.length - 1);
            keyAtIndex = this.keys[nextIndex];
            if (Float.compare(keyAtIndex, element) == 0)
            {
                return nextIndex;
            }
            if (Float.compare(keyAtIndex, EMPTY_KEY) == 0)
            {
                return removedIndex == -1 ? nextIndex : removedIndex;
            }
            if (Float.compare(keyAtIndex, REMOVED_KEY) == 0 && removedIndex == -1)
            {
                removedIndex = nextIndex;
            }
        }
        return this.probeTwo(element, removedIndex);
    }

    int probeTwo(float element, int removedIndex)
    {
        int index = this.spreadTwoAndMask(element);
        for (int i = 0; i < INITIAL_LINEAR_PROBE; i++)
        {
            int nextIndex = (index + i) & (this.keys.length - 1);
            float keyAtIndex = this.keys[nextIndex];
            if (Float.compare(keyAtIndex, element) == 0)
            {
                return nextIndex;
            }
            if (Float.compare(keyAtIndex, EMPTY_KEY) == 0)
            {
                return removedIndex == -1 ? nextIndex : removedIndex;
            }
            if (Float.compare(keyAtIndex, REMOVED_KEY) == 0 && removedIndex == -1)
            {
                removedIndex = nextIndex;
            }
        }
        return this.probeThree(element, removedIndex);
    }

    int probeThree(float element, int removedIndex)
    {
        int nextIndex = Integer.reverse(SpreadFunctions.floatSpreadOne(element));
        int spreadTwo = Integer.reverse(SpreadFunctions.floatSpreadTwo(element)) | 1;

        while(true)
        {
            nextIndex = this.mask(nextIndex + spreadTwo);
            float keyAtIndex = this.keys[nextIndex];
            if (Float.compare(keyAtIndex, element) == 0)
            {
                return nextIndex;
            }
            if (Float.compare(keyAtIndex, EMPTY_KEY) == 0)
            {
                return removedIndex == -1 ? nextIndex : removedIndex;
            }
            if (Float.compare(keyAtIndex, REMOVED_KEY) == 0 && removedIndex == -1)
            {
                removedIndex = nextIndex;
            }
        }
    }

    // exposed for testing
    int spreadAndMask(float element)
    {
        int code = SpreadFunctions.floatSpreadOne(element);
        return this.mask(code);
    }

    int spreadTwoAndMask(float element)
    {
        int code = SpreadFunctions.floatSpreadTwo(element);
        return this.mask(code);
    }

    private int mask(int spread)
    {
        return spread & (this.keys.length - 1);
    }

    public void clear()
    {
        this.sentinelValues = null;
        this.occupiedWithData = 0;
        this.occupiedWithSentinels = 0;
        Arrays.fill(this.keys, EMPTY_KEY);
        this.values.clear();
    }

    public void put(float key, boolean value)
    {
        if (isEmptyKey(key))
        {
            if (this.getSentinelValues() == null)
            {
                this.sentinelValues = new SentinelValues();
            }
            this.getSentinelValues().containsZeroKey = true;
            this.getSentinelValues().zeroValue = value;
            return;
        }

        if (isRemovedKey(key))
        {
            if (this.sentinelValues == null)
            {
                this.sentinelValues = new SentinelValues();
            }
            this.getSentinelValues().containsOneKey = true;
            this.getSentinelValues().oneValue = value;
            return;
        }

        int index = this.probe(key);

        if (Float.compare(this.keys[index], key) == 0)
        {
            // key already present in map
            this.values.set(index, value);
            return;
        }

        this.addKeyValueAtIndex(key, value, index);
    }

    public void putAll(FloatBooleanMap map)
    {
        map.forEachKeyValue(new FloatBooleanProcedure()
        {
            public void value(float key, boolean value)
            {
                FloatBooleanHashMap.this.put(key, value);
            }
        });
    }

    public boolean containsKey(float key)
    {
        if (isEmptyKey(key))
        {
            return this.getSentinelValues() != null && this.getSentinelValues().containsZeroKey;
        }
        if (isRemovedKey(key))
        {
            return this.getSentinelValues() != null && this.getSentinelValues().containsOneKey;
        }
        return Float.compare(this.keys[this.probe(key)], key) == 0;
    }

    public boolean containsValue(boolean value)
    {
        if (this.getSentinelValues() != null && this.getSentinelValues().containsValue(value))
        {
            return true;
        }
        for (int i = 0; i < this.keys.length; i++)
        {
            if (isNonSentinel(this.keys[i]) && this.getValueAtIndex(i) == value)
            {
                return true;
            }
        }
        return false;
    }

    public boolean get(float key)
    {
        return this.getIfAbsent(key, this.getEmptyValue());
    }

    public boolean getIfAbsent(float key, boolean ifAbsent)
    {
        if (isEmptyKey(key))
        {
            if (this.sentinelValues == null || !this.getSentinelValues().containsZeroKey)
            {
                return ifAbsent;
            }
            return this.getSentinelValues().zeroValue;
        }
        if (isRemovedKey(key))
        {
            if (this.sentinelValues == null || !this.getSentinelValues().containsOneKey)
            {
                return ifAbsent;
            }
            return this.getSentinelValues().oneValue;
        }
        int index = this.probe(key);
        if (this.isNonSentinelAtIndex(index))
        {
            return this.values.get(index);
        }
        return ifAbsent;
    }

    public boolean getOrThrow(float key)
    {
        if (isEmptyKey(key))
        {
            if (this.sentinelValues == null || !this.getSentinelValues().containsZeroKey)
            {
                throw new IllegalStateException("Key " + key + " not present.");
            }
            return this.getSentinelValues().zeroValue;
        }
        if (isRemovedKey(key))
        {
            if (this.sentinelValues == null || !this.getSentinelValues().containsOneKey)
            {
                throw new IllegalStateException("Key " + key + " not present.");
            }
            return this.getSentinelValues().oneValue;
        }
        int index = this.probe(key);
        if (this.isNonSentinelAtIndex(index))
        {
            return this.values.get(index);
        }
        throw new IllegalStateException("Key " + key + " not present.");
    }

    public boolean getIfAbsentPut(float key, boolean value)
    {
        if (isEmptyKey(key))
        {
            if (this.sentinelValues == null)
            {
                this.sentinelValues = new SentinelValues();
                this.addEmptyKeyValue(value);
                return value;
            }
            if (this.getSentinelValues().containsZeroKey)
            {
                return this.getSentinelValues().zeroValue;
            }
            this.addEmptyKeyValue(value);
            return value;
        }
        if (isRemovedKey(key))
        {
            if (this.sentinelValues == null)
            {
                this.sentinelValues = new SentinelValues();
                this.addRemovedKeyValue(value);
                return value;
            }
            if (this.getSentinelValues().containsOneKey)
            {
                return this.getSentinelValues().oneValue;
            }
            this.addRemovedKeyValue(value);
            return value;
        }
        int index = this.probe(key);
        if (Float.compare(this.keys[index], key) == 0)
        {
            return this.values.get(index);
        }
        this.addKeyValueAtIndex(key, value, index);
        return value;
    }

    public boolean getIfAbsentPut(float key, BooleanFunction0 function)
    {
        if (isEmptyKey(key))
        {
            if (this.sentinelValues == null)
            {
                boolean value = function.value();
                this.sentinelValues = new SentinelValues();
                this.addEmptyKeyValue(value);
                return value;
            }
            if (this.getSentinelValues().containsZeroKey)
            {
                return this.getSentinelValues().zeroValue;
            }
            boolean value = function.value();
            this.addEmptyKeyValue(value);
            return value;
        }
        if (isRemovedKey(key))
        {
            if (this.sentinelValues == null)
            {
                boolean value = function.value();
                this.sentinelValues = new SentinelValues();
                this.addRemovedKeyValue(value);
                return value;
            }
            if (this.getSentinelValues().containsOneKey)
            {
                return this.getSentinelValues().oneValue;
            }
            boolean value = function.value();
            this.addRemovedKeyValue(value);
            return value;
        }
        int index = this.probe(key);
        if (Float.compare(this.keys[index], key) == 0)
        {
            return this.values.get(index);
        }
        boolean value = function.value();
        this.addKeyValueAtIndex(key, value, index);
        return value;
    }

    public 

boolean getIfAbsentPutWith(float key, BooleanFunction function, P parameter) { if (isEmptyKey(key)) { if (this.sentinelValues == null) { boolean value = function.booleanValueOf(parameter); this.sentinelValues = new SentinelValues(); this.addEmptyKeyValue(value); return value; } if (this.getSentinelValues().containsZeroKey) { return this.getSentinelValues().zeroValue; } boolean value = function.booleanValueOf(parameter); this.addEmptyKeyValue(value); return value; } if (isRemovedKey(key)) { if (this.sentinelValues == null) { boolean value = function.booleanValueOf(parameter); this.sentinelValues = new SentinelValues(); this.addRemovedKeyValue(value); return value; } if (this.getSentinelValues().containsOneKey) { return this.getSentinelValues().oneValue; } boolean value = function.booleanValueOf(parameter); this.addRemovedKeyValue(value); return value; } int index = this.probe(key); if (Float.compare(this.keys[index], key) == 0) { return this.values.get(index); } boolean value = function.booleanValueOf(parameter); this.addKeyValueAtIndex(key, value, index); return value; } public boolean getIfAbsentPutWithKey(float key, FloatToBooleanFunction function) { if (isEmptyKey(key)) { if (this.sentinelValues == null) { boolean value = function.valueOf(key); this.sentinelValues = new SentinelValues(); this.addEmptyKeyValue(value); return value; } if (this.getSentinelValues().containsZeroKey) { return this.getSentinelValues().zeroValue; } boolean value = function.valueOf(key); this.addEmptyKeyValue(value); return value; } if (isRemovedKey(key)) { if (this.sentinelValues == null) { boolean value = function.valueOf(key); this.sentinelValues = new SentinelValues(); this.addRemovedKeyValue(value); return value; } if (this.getSentinelValues().containsOneKey) { return this.getSentinelValues().oneValue; } boolean value = function.valueOf(key); this.addRemovedKeyValue(value); return value; } int index = this.probe(key); if (Float.compare(this.keys[index], key) == 0) { return this.values.get(index); } boolean value = function.valueOf(key); this.addKeyValueAtIndex(key, value, index); return value; } public boolean updateValue(float key, boolean initialValueIfAbsent, BooleanToBooleanFunction function) { if (isEmptyKey(key)) { if (this.sentinelValues == null) { this.sentinelValues = new SentinelValues(); this.addEmptyKeyValue(function.valueOf(initialValueIfAbsent)); } else if (this.getSentinelValues().containsZeroKey) { this.getSentinelValues().zeroValue = function.valueOf(this.getSentinelValues().zeroValue); } else { this.addEmptyKeyValue(function.valueOf(initialValueIfAbsent)); } return this.getSentinelValues().zeroValue; } if (isRemovedKey(key)) { if (this.sentinelValues == null) { this.sentinelValues = new SentinelValues(); this.addRemovedKeyValue(function.valueOf(initialValueIfAbsent)); } else if (this.getSentinelValues().containsOneKey) { this.getSentinelValues().oneValue = function.valueOf(this.getSentinelValues().oneValue); } else { this.addRemovedKeyValue(function.valueOf(initialValueIfAbsent)); } return this.getSentinelValues().oneValue; } int index = this.probe(key); if (Float.compare(this.keys[index], key) == 0) { this.values.set(index, function.valueOf(this.values.get(index))); return this.values.get(index); } boolean value = function.valueOf(initialValueIfAbsent); this.addKeyValueAtIndex(key, value, index); return value; } private void addKeyValueAtIndex(float key, boolean value, int index) { if (Float.compare(this.keys[index], REMOVED_KEY) == 0) { this.occupiedWithSentinels--; } this.keys[index] = key; this.values.set(index, value); this.occupiedWithData++; if (this.occupiedWithData > this.maxOccupiedWithData()) { this.rehashAndGrow(); } } public void removeKey(float key) { if (isEmptyKey(key)) { if (this.sentinelValues == null || !this.getSentinelValues().containsZeroKey) { return; } this.removeEmptyKey(); return; } if (isRemovedKey(key)) { if (this.sentinelValues == null || !this.getSentinelValues().containsOneKey) { return; } this.removeRemovedKey(); return; } int index = this.probe(key); if (Float.compare(this.keys[index], key) == 0) { this.keys[index] = REMOVED_KEY; this.values.set(index, this.getEmptyValue()); this.occupiedWithData--; this.occupiedWithSentinels++; if (this.occupiedWithSentinels > this.maxOccupiedWithSentinels()) { this.rehash(); } } } public void remove(float key) { this.removeKey(key); } public boolean removeKeyIfAbsent(float key, boolean value) { if (isEmptyKey(key)) { if (this.sentinelValues == null || !this.getSentinelValues().containsZeroKey) { return value; } boolean oldValue = this.getSentinelValues().zeroValue; this.removeEmptyKey(); return oldValue; } if (isRemovedKey(key)) { if (this.sentinelValues == null || !this.getSentinelValues().containsOneKey) { return value; } boolean oldValue = this.getSentinelValues().oneValue; this.removeRemovedKey(); return oldValue; } int index = this.probe(key); if (Float.compare(this.keys[index], key) == 0) { this.keys[index] = REMOVED_KEY; boolean oldValue = this.values.get(index); this.values.set(index, this.getEmptyValue()); this.occupiedWithData--; this.occupiedWithSentinels++; if (this.occupiedWithSentinels > this.maxOccupiedWithSentinels()) { this.rehash(); } return oldValue; } return value; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof FloatBooleanMap)) { return false; } FloatBooleanMap other = (FloatBooleanMap) obj; if (this.size() != other.size()) { return false; } if (this.sentinelValues == null) { if (other.containsKey(EMPTY_KEY) || other.containsKey(REMOVED_KEY)) { return false; } } else { if (this.getSentinelValues().containsZeroKey && (!other.containsKey(EMPTY_KEY) || this.getSentinelValues().zeroValue != other.getOrThrow(EMPTY_KEY))) { return false; } if (this.getSentinelValues().containsOneKey && (!other.containsKey(REMOVED_KEY) || this.getSentinelValues().oneValue != other.getOrThrow(REMOVED_KEY))) { return false; } } for (int i = 0; i < this.keys.length; i++) { if (this.isNonSentinelAtIndex(i) && (!other.containsKey(this.keys[i]) || this.getValueAtIndex(i) != other.getOrThrow(this.keys[i]))) { return false; } } return true; } @Override public int hashCode() { int result = 0; if (this.sentinelValues != null) { if (this.getSentinelValues().containsZeroKey) { result += Float.floatToIntBits(EMPTY_KEY) ^ (this.getSentinelValues().zeroValue ? 1231 : 1237); } if (this.getSentinelValues().containsOneKey) { result += Float.floatToIntBits(REMOVED_KEY) ^ (this.getSentinelValues().oneValue ? 1231 : 1237); } } for (int i = 0; i < this.keys.length; i++) { if (isNonSentinel(this.keys[i])) { result += Float.floatToIntBits(this.keys[i]) ^ (this.getValueAtIndex(i) ? 1231 : 1237); } } return result; } @Override public String toString() { StringBuilder appendable = new StringBuilder(); appendable.append("{"); boolean first = true; if (this.sentinelValues != null) { if (this.getSentinelValues().containsZeroKey) { appendable.append(EMPTY_KEY).append("=").append(this.getSentinelValues().zeroValue); first = false; } if (this.getSentinelValues().containsOneKey) { if (!first) { appendable.append(", "); } appendable.append(REMOVED_KEY).append("=").append(this.getSentinelValues().oneValue); first = false; } } for (int i = 0; i < this.keys.length; i++) { if (this.isNonSentinelAtIndex(i)) { if (!first) { appendable.append(", "); } appendable.append(this.keys[i]).append("=").append(this.getValueAtIndex(i)); first = false; } } appendable.append("}"); return appendable.toString(); } public MutableBooleanIterator booleanIterator() { return new InternalBooleanIterator(); } public void forEachKey(FloatProcedure procedure) { if (this.sentinelValues != null) { if (this.getSentinelValues().containsZeroKey) { procedure.value(EMPTY_KEY); } if (this.getSentinelValues().containsOneKey) { procedure.value(REMOVED_KEY); } } for (float key : this.keys) { if (isNonSentinel(key)) { procedure.value(key); } } } public void forEachKeyValue(FloatBooleanProcedure procedure) { if (this.sentinelValues != null) { if (this.getSentinelValues().containsZeroKey) { procedure.value(EMPTY_KEY, this.getSentinelValues().zeroValue); } if (this.getSentinelValues().containsOneKey) { procedure.value(REMOVED_KEY, this.getSentinelValues().oneValue); } } for (int i = 0; i < this.keys.length; i++) { if (isNonSentinel(this.keys[i])) { procedure.value(this.keys[i], this.getValueAtIndex(i)); } } } public FloatBooleanHashMap select(FloatBooleanPredicate predicate) { FloatBooleanHashMap result = new FloatBooleanHashMap(); if (this.sentinelValues != null) { if (this.getSentinelValues().containsZeroKey && predicate.accept(EMPTY_KEY, this.getSentinelValues().zeroValue)) { result.put(EMPTY_KEY, this.getSentinelValues().zeroValue); } if (this.getSentinelValues().containsOneKey && predicate.accept(REMOVED_KEY, this.getSentinelValues().oneValue)) { result.put(REMOVED_KEY, this.getSentinelValues().oneValue); } } for (int i = 0; i < this.keys.length; i++) { if (isNonSentinel(this.keys[i]) && predicate.accept(this.keys[i], this.getValueAtIndex(i))) { result.put(this.keys[i], this.getValueAtIndex(i)); } } return result; } public FloatBooleanHashMap reject(FloatBooleanPredicate predicate) { FloatBooleanHashMap result = new FloatBooleanHashMap(); if (this.sentinelValues != null) { if (this.getSentinelValues().containsZeroKey && !predicate.accept(EMPTY_KEY, this.getSentinelValues().zeroValue)) { result.put(EMPTY_KEY, this.getSentinelValues().zeroValue); } if (this.getSentinelValues().containsOneKey && !predicate.accept(REMOVED_KEY, this.getSentinelValues().oneValue)) { result.put(REMOVED_KEY, this.getSentinelValues().oneValue); } } for (int i = 0; i < this.keys.length; i++) { if (isNonSentinel(this.keys[i]) && !predicate.accept(this.keys[i], this.getValueAtIndex(i))) { result.put(this.keys[i], this.getValueAtIndex(i)); } } return result; } public LazyFloatIterable keysView() { return new KeysView(); } public RichIterable keyValuesView() { return new KeyValuesView(); } public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(this.size()); /** * @deprecated in 5.1.0. */ out.writeFloat(DEFAULT_LOAD_FACTOR); if (this.sentinelValues != null) { if (this.getSentinelValues().containsZeroKey) { out.writeFloat(EMPTY_KEY); out.writeBoolean(this.getSentinelValues().zeroValue); } if (this.getSentinelValues().containsOneKey) { out.writeFloat(REMOVED_KEY); out.writeBoolean(this.getSentinelValues().oneValue); } } for (int i = 0; i < this.keys.length; i++) { if (isNonSentinel(this.keys[i])) { out.writeFloat(this.keys[i]); out.writeBoolean(this.getValueAtIndex(i)); } } } public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { int size = in.readInt(); /** * @deprecated in 5.1.0. */ in.readFloat(); for (int i = 0; i < size; i++) { this.put(in.readFloat(), in.readBoolean()); } } private int maxOccupiedWithData() { int capacity = this.keys.length; // need at least one free slot for open addressing return Math.min(capacity - 1, capacity / OCCUPIED_DATA_RATIO); } private int maxOccupiedWithSentinels() { return this.keys.length / OCCUPIED_SENTINEL_RATIO; } private class InternalBooleanIterator implements MutableBooleanIterator { private int count; private int position; private float lastKey; private boolean handledZero; private boolean handledOne; private boolean canRemove; public boolean hasNext() { return this.count < FloatBooleanHashMap.this.size(); } public boolean next() { if (!this.hasNext()) { throw new NoSuchElementException("next() called, but the iterator is exhausted"); } this.count++; this.canRemove = true; if (!this.handledZero) { this.handledZero = true; if (FloatBooleanHashMap.this.containsKey(EMPTY_KEY)) { this.lastKey = EMPTY_KEY; return FloatBooleanHashMap.this.getSentinelValues().zeroValue; } } if (!this.handledOne) { this.handledOne = true; if (FloatBooleanHashMap.this.containsKey(REMOVED_KEY)) { this.lastKey = REMOVED_KEY; return FloatBooleanHashMap.this.getSentinelValues().oneValue; } } float[] keys = FloatBooleanHashMap.this.keys; while (!isNonSentinel(keys[this.position])) { this.position++; } this.lastKey = keys[this.position]; boolean result = FloatBooleanHashMap.this.values.get(this.position); this.position++; return result; } public void remove() { if (!this.canRemove) { throw new IllegalStateException(); } FloatBooleanHashMap.this.removeKey(this.lastKey); this.count--; this.canRemove = false; } } private class KeysView extends AbstractLazyFloatIterable { public boolean isEmpty() { return FloatBooleanHashMap.this.isEmpty(); } public boolean notEmpty() { return FloatBooleanHashMap.this.notEmpty(); } public int size() { return FloatBooleanHashMap.this.size(); } public boolean contains(float key) { return FloatBooleanHashMap.this.containsKey(key); } public boolean containsAll(float... keys) { for (float key : keys) { if (!FloatBooleanHashMap.this.containsKey(key)) { return false; } } return true; } public boolean containsAll(FloatIterable source) { return source.allSatisfy(new FloatPredicate() { public boolean accept(float key) { return FloatBooleanHashMap.this.containsKey(key); } }); } public FloatIterator floatIterator() { return new UnmodifiableFloatIterator(new KeySetIterator()); } /** * @since 7.0. */ public void each(FloatProcedure procedure) { FloatBooleanHashMap.this.forEachKey(procedure); } public void appendString(Appendable appendable, String start, String separator, String end) { try { appendable.append(start); boolean first = true; if (FloatBooleanHashMap.this.sentinelValues != null) { if (FloatBooleanHashMap.this.getSentinelValues().containsZeroKey) { appendable.append(String.valueOf(EMPTY_KEY)); first = false; } if (FloatBooleanHashMap.this.getSentinelValues().containsOneKey) { if (!first) { appendable.append(separator); } appendable.append(String.valueOf(REMOVED_KEY)); first = false; } } for (float key : FloatBooleanHashMap.this.keys) { if (isNonSentinel(key)) { if (!first) { appendable.append(separator); } appendable.append(String.valueOf(key)); first = false; } } appendable.append(end); } catch (IOException e) { throw new RuntimeException(e); } } public int count(FloatPredicate predicate) { int count = 0; if (FloatBooleanHashMap.this.sentinelValues != null) { if (FloatBooleanHashMap.this.getSentinelValues().containsZeroKey && predicate.accept(EMPTY_KEY)) { count++; } if (FloatBooleanHashMap.this.getSentinelValues().containsOneKey && predicate.accept(REMOVED_KEY)) { count++; } } for (float key : FloatBooleanHashMap.this.keys) { if (isNonSentinel(key) && predicate.accept(key)) { count++; } } return count; } public boolean anySatisfy(FloatPredicate predicate) { if (FloatBooleanHashMap.this.sentinelValues != null) { if (FloatBooleanHashMap.this.getSentinelValues().containsZeroKey && predicate.accept(EMPTY_KEY)) { return true; } if (FloatBooleanHashMap.this.getSentinelValues().containsOneKey && predicate.accept(REMOVED_KEY)) { return true; } } for (float key : FloatBooleanHashMap.this.keys) { if (isNonSentinel(key) && predicate.accept(key)) { return true; } } return false; } public boolean allSatisfy(FloatPredicate predicate) { if (FloatBooleanHashMap.this.sentinelValues != null) { if (FloatBooleanHashMap.this.getSentinelValues().containsZeroKey && !predicate.accept(EMPTY_KEY)) { return false; } if (FloatBooleanHashMap.this.getSentinelValues().containsOneKey && !predicate.accept(REMOVED_KEY)) { return false; } } for (float key : FloatBooleanHashMap.this.keys) { if (isNonSentinel(key) && !predicate.accept(key)) { return false; } } return true; } public boolean noneSatisfy(FloatPredicate predicate) { return !this.anySatisfy(predicate); } public float detectIfNone(FloatPredicate predicate, float value) { if (FloatBooleanHashMap.this.sentinelValues != null) { if (FloatBooleanHashMap.this.getSentinelValues().containsZeroKey && predicate.accept(EMPTY_KEY)) { return EMPTY_KEY; } if (FloatBooleanHashMap.this.getSentinelValues().containsOneKey && predicate.accept(REMOVED_KEY)) { return REMOVED_KEY; } } for (float key : FloatBooleanHashMap.this.keys) { if (isNonSentinel(key) && predicate.accept(key)) { return key; } } return value; } public double sum() { double result = 0.0; if (FloatBooleanHashMap.this.sentinelValues != null) { if (FloatBooleanHashMap.this.getSentinelValues().containsZeroKey) { result += EMPTY_KEY; } if (FloatBooleanHashMap.this.getSentinelValues().containsOneKey) { result += REMOVED_KEY; } } for (float key : FloatBooleanHashMap.this.keys) { if (isNonSentinel(key)) { result += key; } } return result; } public float max() { if (this.isEmpty()) { throw new NoSuchElementException(); } FloatIterator iterator = this.floatIterator(); float max = iterator.next(); while (iterator.hasNext()) { float value = iterator.next(); if (Float.compare(max, value) < 0) { max = value; } } return max; } public float min() { if (this.isEmpty()) { throw new NoSuchElementException(); } FloatIterator iterator = this.floatIterator(); float min = iterator.next(); while (iterator.hasNext()) { float value = iterator.next(); if (Float.compare(value, min) < 0) { min = value; } } return min; } public float[] toSortedArray() { float[] array = this.toArray(); Arrays.sort(array); return array; } public float[] toArray() { int size = FloatBooleanHashMap.this.size(); final float[] result = new float[size]; FloatBooleanHashMap.this.forEachKey(new FloatProcedure() { private int index; public void value(float each) { result[this.index] = each; this.index++; } }); return result; } public T injectInto(T injectedValue, ObjectFloatToObjectFunction function) { T result = injectedValue; if (FloatBooleanHashMap.this.sentinelValues != null) { if (FloatBooleanHashMap.this.getSentinelValues().containsZeroKey) { result = function.valueOf(result, EMPTY_KEY); } if (FloatBooleanHashMap.this.getSentinelValues().containsOneKey) { result = function.valueOf(result, REMOVED_KEY); } } for (int i = 0; i < FloatBooleanHashMap.this.keys.length; i++) { if (isNonSentinel(FloatBooleanHashMap.this.keys[i])) { result = function.valueOf(result, FloatBooleanHashMap.this.keys[i]); } } return result; } public MutableFloatList toList() { return FloatArrayList.newList(this); } public MutableFloatSet toSet() { return FloatHashSet.newSet(this); } public MutableFloatBag toBag() { return FloatHashBag.newBag(this); } } public MutableFloatSet keySet() { return new KeySet(); } private class KeySet extends AbstractMutableFloatKeySet { @Override protected float getKeyAtIndex(int index) { return FloatBooleanHashMap.this.keys[index]; } @Override protected int getTableSize() { return FloatBooleanHashMap.this.keys.length; } @Override protected MutableFloatKeysMap getOuter() { return FloatBooleanHashMap.this; } @Override protected SentinelValues getSentinelValues() { return FloatBooleanHashMap.this.sentinelValues; } public MutableFloatIterator floatIterator() { return new KeySetIterator(); } public boolean retainAll(FloatIterable source) { int oldSize = FloatBooleanHashMap.this.size(); final FloatSet sourceSet = source instanceof FloatSet ? (FloatSet) source : source.toSet(); FloatBooleanHashMap retained = FloatBooleanHashMap.this.select(new FloatBooleanPredicate() { public boolean accept(float key, boolean value) { return sourceSet.contains(key); } }); if (retained.size() != oldSize) { FloatBooleanHashMap.this.keys = retained.keys; FloatBooleanHashMap.this.values = retained.values; FloatBooleanHashMap.this.occupiedWithData = retained.occupiedWithData; FloatBooleanHashMap.this.occupiedWithSentinels = retained.occupiedWithSentinels; FloatBooleanHashMap.this.sentinelValues = retained.sentinelValues; return true; } return false; } public boolean retainAll(float... source) { return this.retainAll(FloatHashSet.newSetWith(source)); } public FloatSet freeze() { throw new UnsupportedOperationException(this.getClass().getSimpleName() + ".freeze() not implemented yet"); } } public MutableBooleanCollection values() { return new ValuesCollection(); } private class ValuesCollection extends AbstractBooleanValuesCollection { public void appendString(Appendable appendable, String start, String separator, String end) { try { appendable.append(start); boolean first = true; if (FloatBooleanHashMap.this.sentinelValues != null) { if (FloatBooleanHashMap.this.getSentinelValues().containsZeroKey) { appendable.append(String.valueOf(FloatBooleanHashMap.this.getSentinelValues().zeroValue)); first = false; } if (FloatBooleanHashMap.this.getSentinelValues().containsOneKey) { if (!first) { appendable.append(separator); } appendable.append(String.valueOf(FloatBooleanHashMap.this.getSentinelValues().oneValue)); first = false; } } for (int i = 0; i < FloatBooleanHashMap.this.keys.length; i++) { if (FloatBooleanHashMap.this.isNonSentinelAtIndex(i)) { if (!first) { appendable.append(separator); } appendable.append(String.valueOf(FloatBooleanHashMap.this.getValueAtIndex(i))); first = false; } } appendable.append(end); } catch (IOException e) { throw new RuntimeException(e); } } public MutableBooleanIterator booleanIterator() { return FloatBooleanHashMap.this.booleanIterator(); } public boolean remove(boolean item) { int oldSize = FloatBooleanHashMap.this.size(); if (FloatBooleanHashMap.this.sentinelValues != null) { if (FloatBooleanHashMap.this.getSentinelValues().containsZeroKey && item == FloatBooleanHashMap.this.getSentinelValues().zeroValue) { FloatBooleanHashMap.this.removeKey(EMPTY_KEY); } if (FloatBooleanHashMap.this.getSentinelValues().containsOneKey && item == FloatBooleanHashMap.this.getSentinelValues().oneValue) { FloatBooleanHashMap.this.removeKey(REMOVED_KEY); } } for (int i = 0; i < FloatBooleanHashMap.this.keys.length; i++) { if (isNonSentinel(FloatBooleanHashMap.this.keys[i]) && item == FloatBooleanHashMap.this.getValueAtIndex(i)) { FloatBooleanHashMap.this.removeKey(FloatBooleanHashMap.this.keys[i]); } } return oldSize != FloatBooleanHashMap.this.size(); } public boolean retainAll(BooleanIterable source) { int oldSize = FloatBooleanHashMap.this.size(); final BooleanSet sourceSet = source instanceof BooleanSet ? (BooleanSet) source : source.toSet(); FloatBooleanHashMap retained = FloatBooleanHashMap.this.select(new FloatBooleanPredicate() { public boolean accept(float key, boolean value) { return sourceSet.contains(value); } }); if (retained.size() != oldSize) { FloatBooleanHashMap.this.keys = retained.keys; FloatBooleanHashMap.this.values = retained.values; FloatBooleanHashMap.this.occupiedWithData = retained.occupiedWithData; FloatBooleanHashMap.this.occupiedWithSentinels = retained.occupiedWithSentinels; FloatBooleanHashMap.this.sentinelValues = retained.sentinelValues; return true; } return false; } } private class KeySetIterator implements MutableFloatIterator { private int count; private int position; private float lastKey; private boolean handledZero; private boolean handledOne; private boolean removed = true; public boolean hasNext() { return this.count < FloatBooleanHashMap.this.size(); } public float next() { if (!this.hasNext()) { throw new NoSuchElementException("next() called, but the iterator is exhausted"); } this.count++; this.removed = false; if (!this.handledZero) { this.handledZero = true; if (FloatBooleanHashMap.this.containsKey(EMPTY_KEY)) { this.lastKey = EMPTY_KEY; return this.lastKey; } } if (!this.handledOne) { this.handledOne = true; if (FloatBooleanHashMap.this.containsKey(REMOVED_KEY)) { this.lastKey = REMOVED_KEY; return this.lastKey; } } float[] keys = FloatBooleanHashMap.this.keys; while (!isNonSentinel(keys[this.position])) { this.position++; } this.lastKey = keys[this.position]; this.position++; return this.lastKey; } public void remove() { if (this.removed) { throw new IllegalStateException(); } FloatBooleanHashMap.this.removeKey(this.lastKey); this.count--; this.removed = true; } } private class KeyValuesView extends AbstractLazyIterable { public void each(Procedure procedure) { if (FloatBooleanHashMap.this.sentinelValues != null) { if (FloatBooleanHashMap.this.getSentinelValues().containsZeroKey) { procedure.value(PrimitiveTuples.pair(EMPTY_KEY, FloatBooleanHashMap.this.getSentinelValues().zeroValue)); } if (FloatBooleanHashMap.this.getSentinelValues().containsOneKey) { procedure.value(PrimitiveTuples.pair(REMOVED_KEY, FloatBooleanHashMap.this.getSentinelValues().oneValue)); } } for (int i = 0; i < FloatBooleanHashMap.this.keys.length; i++) { if (isNonSentinel(FloatBooleanHashMap.this.keys[i])) { procedure.value(PrimitiveTuples.pair(FloatBooleanHashMap.this.keys[i], FloatBooleanHashMap.this.getValueAtIndex(i))); } } } public void forEachWithIndex(ObjectIntProcedure objectIntProcedure) { int index = 0; if (FloatBooleanHashMap.this.sentinelValues != null) { if (FloatBooleanHashMap.this.getSentinelValues().containsZeroKey) { objectIntProcedure.value(PrimitiveTuples.pair(EMPTY_KEY, FloatBooleanHashMap.this.getSentinelValues().zeroValue), index); index++; } if (FloatBooleanHashMap.this.getSentinelValues().containsOneKey) { objectIntProcedure.value(PrimitiveTuples.pair(REMOVED_KEY, FloatBooleanHashMap.this.getSentinelValues().oneValue), index); index++; } } for (int i = 0; i < FloatBooleanHashMap.this.keys.length; i++) { if (isNonSentinel(FloatBooleanHashMap.this.keys[i])) { objectIntProcedure.value(PrimitiveTuples.pair(FloatBooleanHashMap.this.keys[i], FloatBooleanHashMap.this.getValueAtIndex(i)), index); index++; } } } public

void forEachWith(Procedure2 procedure, P parameter) { if (FloatBooleanHashMap.this.sentinelValues != null) { if (FloatBooleanHashMap.this.getSentinelValues().containsZeroKey) { procedure.value(PrimitiveTuples.pair(EMPTY_KEY, FloatBooleanHashMap.this.getSentinelValues().zeroValue), parameter); } if (FloatBooleanHashMap.this.getSentinelValues().containsOneKey) { procedure.value(PrimitiveTuples.pair(REMOVED_KEY, FloatBooleanHashMap.this.getSentinelValues().oneValue), parameter); } } for (int i = 0; i < FloatBooleanHashMap.this.keys.length; i++) { if (isNonSentinel(FloatBooleanHashMap.this.keys[i])) { procedure.value(PrimitiveTuples.pair(FloatBooleanHashMap.this.keys[i], FloatBooleanHashMap.this.getValueAtIndex(i)), parameter); } } } public Iterator iterator() { return new InternalKeyValuesIterator(); } public class InternalKeyValuesIterator implements Iterator { private int count; private int position; private boolean handledZero; private boolean handledOne; public FloatBooleanPair next() { if (!this.hasNext()) { throw new NoSuchElementException("next() called, but the iterator is exhausted"); } this.count++; if (!this.handledZero) { this.handledZero = true; if (FloatBooleanHashMap.this.containsKey(EMPTY_KEY)) { return PrimitiveTuples.pair(EMPTY_KEY, FloatBooleanHashMap.this.getSentinelValues().zeroValue); } } if (!this.handledOne) { this.handledOne = true; if (FloatBooleanHashMap.this.containsKey(REMOVED_KEY)) { return PrimitiveTuples.pair(REMOVED_KEY, FloatBooleanHashMap.this.getSentinelValues().oneValue); } } float[] keys = FloatBooleanHashMap.this.keys; while (!isNonSentinel(keys[this.position])) { this.position++; } FloatBooleanPair result = PrimitiveTuples.pair(keys[this.position], FloatBooleanHashMap.this.values.get(this.position)); this.position++; return result; } public void remove() { throw new UnsupportedOperationException("Cannot call remove() on " + this.getClass().getSimpleName()); } public boolean hasNext() { return this.count != FloatBooleanHashMap.this.size(); } } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy