Class Long2LongCounterMap


  • public class Long2LongCounterMap
    extends java.lang.Object
    An open-addressing with linear probing hash map specialised for primitive key and counter pairs. A counter map views counters which hit initialValue as deleted. This means that changing a counter may impact size().
    • Constructor Summary

      Constructors 
      Constructor Description
      Long2LongCounterMap​(int initialCapacity, float loadFactor, long initialValue)
      Construct a new counter map with the initial value for the counter provided.
      Long2LongCounterMap​(long initialValue)
      Construct a new counter map with the initial value for the counter provided.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      long addAndGet​(long key, long amount)
      Add amount to the current value associated with this key.
      int capacity()
      Get the total capacity for the map to which the load factor will be a fraction of.
      private void capacity​(int newCapacity)  
      void clear()
      Clear out all entries.
      void compact()
      Compact the backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor.
      private void compactChain​(int deleteKeyIndex)  
      long computeIfAbsent​(long key, java.util.function.LongUnaryOperator mappingFunction)
      Try get(long) a value for a key and if not present then apply mapping function.
      boolean containsKey​(long key)
      Does the map contain a value for a given key which is not initialValue().
      boolean containsValue​(long value)
      Iterate over the values to see if any match the provided value.
      long decrementAndGet​(long key)
      Convenience version of addAndGet(long, long) (key, -1).
      void forEach​(LongLongConsumer consumer)
      Iterate over all value in the map which are not at initialValue().
      long get​(long key)
      Get the value of a counter associated with a key or initialValue() if not found.
      long getAndAdd​(long key, long amount)
      Add amount to the current value associated with this key.
      long getAndDecrement​(long key)
      Convenience version of getAndAdd(long, long) (key, -1).
      long getAndIncrement​(long key)
      Convenience version of getAndAdd(long, long) (key, 1).
      private void increaseCapacity()  
      long incrementAndGet​(long key)
      Convenience version of addAndGet(long, long) (key, 1).
      long initialValue()
      The value to be used as a null marker in the map.
      boolean isEmpty()
      Is the map empty.
      float loadFactor()
      Get the load factor applied for resize operations.
      long maxValue()
      Get the maximum value stored in the map.
      long minValue()
      Get the minimum value stored in the map.
      private static int next​(int index, int mask)  
      long put​(long key, long value)
      Put the value for a key in the map.
      private void rehash​(int newCapacity)  
      long remove​(long key)
      Remove a counter value for a given key.
      int resizeThreshold()
      Get the actual threshold which when reached the map will resize.
      int size()
      The current size of the map which at not at initialValue().
      java.lang.String toString()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • loadFactor

        private final float loadFactor
      • initialValue

        private final long initialValue
      • resizeThreshold

        private int resizeThreshold
      • size

        private int size
      • entries

        private long[] entries
    • Constructor Detail

      • Long2LongCounterMap

        public Long2LongCounterMap​(long initialValue)
        Construct a new counter map with the initial value for the counter provided.
        Parameters:
        initialValue - to be used for each counter.
      • Long2LongCounterMap

        public Long2LongCounterMap​(int initialCapacity,
                                   float loadFactor,
                                   long initialValue)
        Construct a new counter map with the initial value for the counter provided.
        Parameters:
        initialCapacity - of the map.
        loadFactor - applied for resize operations.
        initialValue - to be used for each counter.
    • Method Detail

      • initialValue

        public long initialValue()
        The value to be used as a null marker in the map.
        Returns:
        value to be used as a null marker in the map.
      • loadFactor

        public float loadFactor()
        Get the load factor applied for resize operations.
        Returns:
        the load factor applied for resize operations.
      • resizeThreshold

        public int resizeThreshold()
        Get the actual threshold which when reached the map will resize. This is a function of the current capacity and load factor.
        Returns:
        the threshold when the map will resize.
      • capacity

        public int capacity()
        Get the total capacity for the map to which the load factor will be a fraction of.
        Returns:
        the total capacity for the map.
      • size

        public int size()
        The current size of the map which at not at initialValue().
        Returns:
        map size, counters at initialValue() are not counted
      • isEmpty

        public boolean isEmpty()
        Is the map empty.
        Returns:
        size == 0
      • get

        public long get​(long key)
        Get the value of a counter associated with a key or initialValue() if not found.
        Parameters:
        key - to lookup.
        Returns:
        counter value associated with key or initialValue() if not found.
      • put

        public long put​(long key,
                        long value)
        Put the value for a key in the map.
        Parameters:
        key - lookup key.
        value - new value, must not be initialValue.
        Returns:
        current counter value associated with key, or initialValue() if none found.
        Throws:
        java.lang.IllegalArgumentException - if value is initialValue().
      • incrementAndGet

        public long incrementAndGet​(long key)
        Convenience version of addAndGet(long, long) (key, 1).
        Parameters:
        key - for the counter.
        Returns:
        the new value.
      • decrementAndGet

        public long decrementAndGet​(long key)
        Convenience version of addAndGet(long, long) (key, -1).
        Parameters:
        key - for the counter.
        Returns:
        the new value.
      • addAndGet

        public long addAndGet​(long key,
                              long amount)
        Add amount to the current value associated with this key. If no such value exists use initialValue() as current value and associate key with initialValue() + amount unless amount is 0, in which case map remains unchanged.
        Parameters:
        key - new or existing
        amount - to be added
        Returns:
        the new value associated with the key, or initialValue() + amount if no mapping for key.
      • getAndIncrement

        public long getAndIncrement​(long key)
        Convenience version of getAndAdd(long, long) (key, 1).
        Parameters:
        key - for the counter.
        Returns:
        the old value.
      • getAndDecrement

        public long getAndDecrement​(long key)
        Convenience version of getAndAdd(long, long) (key, -1).
        Parameters:
        key - for the counter.
        Returns:
        the old value.
      • getAndAdd

        public long getAndAdd​(long key,
                              long amount)
        Add amount to the current value associated with this key. If no such value exists use initialValue() as current value and associate key with initialValue() + amount unless amount is 0, in which case map remains unchanged.
        Parameters:
        key - new or existing.
        amount - to be added.
        Returns:
        the previous value associated with the key, or initialValue() if no mapping for key.
      • forEach

        public void forEach​(LongLongConsumer consumer)
        Iterate over all value in the map which are not at initialValue().
        Parameters:
        consumer - called for each key/value pair in the map.
      • containsKey

        public boolean containsKey​(long key)
        Does the map contain a value for a given key which is not initialValue().
        Parameters:
        key - the key to check.
        Returns:
        true if the map contains the key with a value other than initialValue(), false otherwise.
      • containsValue

        public boolean containsValue​(long value)
        Iterate over the values to see if any match the provided value.

        If value provided is initialValue() then it will always return false.

        Parameters:
        value - the key to check.
        Returns:
        true if the map contains value as a mapped value, false otherwise.
      • clear

        public void clear()
        Clear out all entries.
      • compact

        public void compact()
        Compact the backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor.
      • computeIfAbsent

        public long computeIfAbsent​(long key,
                                    java.util.function.LongUnaryOperator mappingFunction)
        Try get(long) a value for a key and if not present then apply mapping function.
        Parameters:
        key - to search on.
        mappingFunction - to provide a value if the get returns null.
        Returns:
        the value if found otherwise the missing value.
      • remove

        public long remove​(long key)
        Remove a counter value for a given key.
        Parameters:
        key - to be removed
        Returns:
        old value for key
      • minValue

        public long minValue()
        Get the minimum value stored in the map. If the map is empty then it will return initialValue().
        Returns:
        the minimum value stored in the map.
      • maxValue

        public long maxValue()
        Get the maximum value stored in the map. If the map is empty then it will return initialValue().
        Returns:
        the maximum value stored in the map.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • next

        private static int next​(int index,
                                int mask)
      • compactChain

        private void compactChain​(int deleteKeyIndex)
      • capacity

        private void capacity​(int newCapacity)
      • increaseCapacity

        private void increaseCapacity()
      • rehash

        private void rehash​(int newCapacity)