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

com.flowpowered.commons.map.impl.TTripleInt21ObjectHashMap Maven / Gradle / Ivy

Go to download

A library for Java that provides re-usable components commonly used by Flow libraries.

The newest version!
/*
 * This file is part of Flow Commons, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2013 Flow Powered 
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.flowpowered.commons.map.impl;

import java.util.Collection;

import gnu.trove.iterator.TLongObjectIterator;
import gnu.trove.map.TLongObjectMap;
import gnu.trove.map.hash.TLongObjectHashMap;
import gnu.trove.set.TLongSet;

import com.flowpowered.commons.hashing.Int21TripleHashed;
import com.flowpowered.commons.map.TripleIntObjectMap;

/**
 * A simplistic map that supports a 3 21 bit integers for keys, using a trove long Object hashmap in the backend. 1 bit is wasted.
 *
 * @see {@link Int21TripleHashed}
 */
public class TTripleInt21ObjectHashMap implements TripleIntObjectMap {
    protected final TLongObjectMap map;

    /**
     * Creates a new TTripleInt21ObjectHashMap instance backend by a {@see TLongObjectHashMap} instance with an capacity of 100 and the default load factor.
     */
    public TTripleInt21ObjectHashMap() {
        map = new TLongObjectHashMap<>(100);
    }

    /**
     * Creates a new TTripleInt21ObjectHashMap instance backend by a {@see TLongObjectHashMap} instance with a prime capacity equal to or greater than capacity and with the
     * default load factor.
     *
     * @param capacity an int value
     */
    public TTripleInt21ObjectHashMap(int capacity) {
        map = new TLongObjectHashMap<>(capacity);
    }

    /**
     * Creates a new TTripleInt21ObjectHashMap instance backend by map
     */
    public TTripleInt21ObjectHashMap(TTripleInt21ObjectHashMap map) {
        if (map == null) {
            throw new IllegalArgumentException("The backend can not be null.");
        }

        this.map = map.map;
    }

    /**
     * Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value.
     * (A map m is said to contain a mapping for a key k if and only if {@see #containsKey(int, int, int) m.containsKey(k)} would return true.)
     *
     * @param x an int value
     * @param y an int value
     * @param z an int value
     * @return the previous value associated with key(x, y, z), or no_entry_value if there was no mapping for key(x, y, z). (A no_entry_value return can also indicate that
     * the map previously associated null with key, if the implementation supports null values.)
     */
    @Override
    public T put(int x, int y, int z, T value) {
        long key = Int21TripleHashed.key(x, y, z);
        return map.put(key, value);
    }

    @Override
    public T putIfAbsent(int x, int y, int z, T value) {
        return null;
    }

    /**
     * Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
More formally, if this map contains a mapping from a key * k to a value v such that (key==null ? k==null : key.equals(k)), then this method returns v; otherwise it returns null. (There * can be at most one such mapping.)
If this map permits null values, then a return value of null does not necessarily indicate that the map contains no * mapping for the key; it's also possible that the map explicitly maps the key to null. The {@see #containsKey(int, int, int) containsKey} operation may be used to distinguish these * two cases. * * @param x an int value * @param y an int value * @param z an int value * @return the value to which the specified key(x, y, z) is mapped, or null if this map contains no mapping for the key. */ @Override public T get(int x, int y, int z) { long key = Int21TripleHashed.key(x, y, z); return map.get(key); } /** * Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that * key.equals(k). (There can be at most one such mapping.) * * @param x an int value * @param y an int value * @param z an int value * @return true if this map contains a mapping for the specified key(x, y, z). */ @Override public boolean containsKey(int x, int y, int z) { long key = Int21TripleHashed.key(x, y, z); return map.containsKey(key); } /** * Removes all of the mappings from this map (optional operation). The map will be empty after this call returns. */ @Override public void clear() { map.clear(); } /** * Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such * that key.equals(k). (There can be at most one such mapping.) * * @param val value whose presence in this map is to be tested * @return true if this map maps one or more keys to the specified value */ @Override public boolean containsValue(T val) { return map.containsValue(val); } /** * Returns true if this map contains no key-value mappings. * * @return true if this map contains no key-value mappings. */ @Override public boolean isEmpty() { return map.isEmpty(); } /** * Returns a {@see TLongObjectIterator} with access to this map's keys and values. * * @return a {@see TLongObjectIterator} with access to this map's keys and values. */ @Override public TLongObjectIterator iterator() { return map.iterator(); } /** * Returns a {@see TLongSet} view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an * iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the * corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not * support the add or addAll operations. * * @return a set view of the keys contained in this map. */ @Override public TLongSet keySet() { return map.keySet(); } /** * Returns a copy of the keys of the map as an array. Changes to the array of keys will not be reflected in the map nor vice-versa. * * @return a copy of the keys of the map as an array. */ @Override public long[] keys() { return map.keys(); } /** * Removes the mapping for a key from this map if it is present (optional operation). More formally, if this map contains a mapping from key k to value v such that * key.equals(k), that mapping is removed. (The map can contain at most one such mapping.)
Returns the value to which this map previously associated the key, or null * if the map contained no mapping for the key.
If this map permits null values, then a return value of null does not necessarily indicate that the map contained no * mapping for the key; it's also possible that the map explicitly mapped the key to null.
The map will not contain a mapping for the specified key once the call returns. * * @param x an int value * @param y an int value * @param z an int value * @return the previous long value associated with key(x, y, z), or null if there was no mapping for key. */ @Override public T remove(int x, int y, int z) { long key = Int21TripleHashed.key(x, y, z); return map.remove(key); } /** * Returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE. * * @return the number of key-value mappings in this map */ @Override public int size() { return map.size(); } /** * Returns a {@see Collection} view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is * modified while an iteration over the collection is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The collection supports element * removal, which removes the corresponding mapping from the map, via the Iterator.remove, Collection.remove, removeAll, retainAll and * clear operations. It does not support the add or addAll operations. */ @Override public Collection valueCollection() { return map.valueCollection(); } /** * Returns the values of the map as an array of long values. Changes to the array of values will not be reflected in the map nor vice-versa. * * @return the values of the map as an array of long values. */ @SuppressWarnings ("unchecked") public T[] values() { return (T[]) map.values(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy