Class FastAccessNumberMap<T>

  • Type Parameters:
    T - type of objects to store in this map
    All Implemented Interfaces:
    java.lang.Iterable<T>

    public class FastAccessNumberMap<T>
    extends java.lang.Object
    implements java.lang.Iterable<T>
    A map from long to T. Gives O(1) access to indexes that are between 0 and pageSize*maxPageCount at the cost of high memory use. Values are kept in dynamically allocated pages, each of a fixed size.

    It can be thought of as a big array that is split up into pages of a fixed size. This is useful if you want to use a sparse set of indexes since you don't have to allocate the full array immediately. If you are going to fill all indexes then it is more practical to use a normal array or list.

    If you try to access an index outside of the max page count then an overflow hash map is used as a fallback mechanism. In that case access will be slower than O(1).

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Map<java.lang.Long,​T> overflow  
      private java.lang.Object[][] pages  
      private int pageSize  
      private int pagesUpperLimit  
    • Constructor Summary

      Constructors 
      Constructor Description
      FastAccessNumberMap()
      Constructs a map with O(1) access up to index 5000.
      FastAccessNumberMap​(int pageSize, int maxPageCount)
      Constructs a map with O(1) access up to index pageSize*maxPageCount.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T get​(long index)
      Get the value at an index.
      private T getLow​(int index)  
      private java.util.Map<java.lang.Long,​T> getOverflowMap()  
      private java.lang.Object[] getPage​(int pageIndex)  
      java.util.Iterator<T> iterator()  
      void put​(long index, T value)
      Store a value at an index.
      private void putLow​(int index, T object)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Field Detail

      • pagesUpperLimit

        private final int pagesUpperLimit
      • pageSize

        private final int pageSize
      • pages

        private java.lang.Object[][] pages
      • overflow

        private java.util.Map<java.lang.Long,​T> overflow
    • Constructor Detail

      • FastAccessNumberMap

        public FastAccessNumberMap()
        Constructs a map with O(1) access up to index 5000.
      • FastAccessNumberMap

        public FastAccessNumberMap​(int pageSize,
                                   int maxPageCount)
        Constructs a map with O(1) access up to index pageSize*maxPageCount.
        Parameters:
        pageSize - page size
        maxPageCount - max page count
    • Method Detail

      • getPage

        private java.lang.Object[] getPage​(int pageIndex)
      • getLow

        private T getLow​(int index)
      • putLow

        private void putLow​(int index,
                            T object)
      • get

        public T get​(long index)
        Get the value at an index.
        Parameters:
        index - value index
        Returns:
        value at index
      • put

        public void put​(long index,
                        T value)
        Store a value at an index.
        Parameters:
        index - value index
        value - value to store
      • getOverflowMap

        private java.util.Map<java.lang.Long,​T> getOverflowMap()
      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<T>