Class BigDogArrayBase<Array>

java.lang.Object
org.ddogleg.struct.BigDogArrayBase<Array>
Direct Known Subclasses:
BigDogArray_B, BigDogArray_F32, BigDogArray_F64, BigDogArray_I32, BigDogArray_I64, BigDogArray_I8

public abstract class BigDogArrayBase<Array>
extends Object
A growable array that is composed of internal blocks. This is intended to reduce overhead when growing a very large array. Contrast this with DogArray for which its entire internal array needs to be copied when growing. While more complex and in some classes slightly slower, the approach employed here is much more memory and speed efficient while growing. See BigDogGrowth for a description of different growth strategies. When operations are used which add/append to the end of the array then extra room is typically added, if a grow strategy is employed. This is done to avoid excessive amount of memory copy operations.
  • Nested Class Summary

    Nested Classes
    Modifier and Type Class Description
    static interface  BigDogArrayBase.FunctionEachRange<Array>  
  • Field Summary

    Fields
    Modifier and Type Field Description
    protected FastArray<Array> blocks
    Storage for blocks.
    protected int blockSize
    Number of elements in a full block
    static int DEFAULT_BLOCK_SIZE
    Default block size.
    protected BigDogGrowth growth
    Approach used for growth.
    int size
    Number of elements in the array being used.
  • Constructor Summary

    Constructors
    Modifier Constructor Description
    protected BigDogArrayBase​(int initialAllocation, int blockSize, BigDogGrowth growth)  
  • Method Summary

    Modifier and Type Method Description
    protected void allocate​(int desiredSize, boolean saveValues, boolean addExtra)
    Allocate more memory so that an array of the specified desiredSize can be stored.
    void append​(Array array, int offset, int length)
    Adds the input array to the end of this array.
    protected abstract int arrayLength​(Array array)  
    protected int blockArrayLength​(int block)  
    protected int getDesiredBlocks​(int desiredSize)
    Returns the number of blocks needed to store an array of the specified size
    int getTotalAllocation()
    Returns the number of elements which have been allocated.
    boolean isIndexOutOfBounds​(int index)
    Returns true if the specified array index is outside the allowed value range
    boolean isValidStructure()
    Performs an internal check to make sure all data structures and values are internally consistent.
    protected abstract Array newArrayInstance​(int size)  
    void processByBlock​(int idx0, int idx1, BigDogArrayBase.FunctionEachRange<Array> op)
    Passes in array elements to the operator one block at a time.
    void reserve​(int desiredSize)
    Ensures that the internal data can store up to this number of elements before needing to allocate more memory.
    void reset()
    Sets the array size to zero.
    void resize​(int desiredSize)
    Either increased or decreases the array size.
    void setArray​(long location, Array array, int offset, int length)
    Copies the passed in array into this array at the specified location
    void setInitialBlockSize​(int initialBlockSize)
    Assigns a new initial block size.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_BLOCK_SIZE

      public static final int DEFAULT_BLOCK_SIZE
      Default block size. It's assumed that this is used in fairly large arrays.
      See Also:
      Constant Field Values
    • blocks

      protected final FastArray<Array> blocks
      Storage for blocks. Note that the size is always the number of elements with non-null values.
    • blockSize

      protected final int blockSize
      Number of elements in a full block
    • size

      public int size
      Number of elements in the array being used. Know what you're doing before modifying.
    • growth

      protected final BigDogGrowth growth
      Approach used for growth. See enum for a description
  • Constructor Details

    • BigDogArrayBase

      protected BigDogArrayBase​(int initialAllocation, int blockSize, BigDogGrowth growth)
  • Method Details

    • isIndexOutOfBounds

      public boolean isIndexOutOfBounds​(int index)
      Returns true if the specified array index is outside the allowed value range
    • reset

      public void reset()
      Sets the array size to zero. No memory is freed.
    • reserve

      public void reserve​(int desiredSize)
      Ensures that the internal data can store up to this number of elements before needing to allocate more memory. No extra data is added and this function is only recommended when the array has a known max size.
    • allocate

      protected void allocate​(int desiredSize, boolean saveValues, boolean addExtra)
      Allocate more memory so that an array of the specified desiredSize can be stored. Optionally copy old values into new arrays when growing
      Parameters:
      desiredSize - New size of internal array, not just a single block.
      saveValues - If old values should be copied.
      addExtra - If using a grow strategy, is this a case where it should add extra elements or do the exact request?
    • resize

      public void resize​(int desiredSize)
      Either increased or decreases the array size. If it's increased then the new elements will be filled with undefined values, depending on their previous state.
      Parameters:
      desiredSize - (Input) New array size
    • append

      public void append​(Array array, int offset, int length)
      Adds the input array to the end of this array.
      Parameters:
      array - (Input) Array which is to be copied
      offset - (Input) First element in the array which is to be copied
      length - (Input) Number of elements which are to be copied
    • setArray

      public void setArray​(long location, Array array, int offset, int length)
      Copies the passed in array into this array at the specified location
      Parameters:
      location - (Input) First element that the array is to be inserted at
      array - (Input) Array which is to be copied in
      offset - (Input) Offset inside of array that it should be copied from
      length - (Input) Number of elements in array to copy
    • processByBlock

      public void processByBlock​(int idx0, int idx1, BigDogArrayBase.FunctionEachRange<Array> op)
      Passes in array elements to the operator one block at a time. What's given to the operator is the first index in the block it should process, the last (exclusive) index in the block, and the number of elements offset from the original range requested.
      Parameters:
      idx0 - (Input) First index, inclusive.
      idx1 - (Input) Last index, exclusive.
      op - The operator which processes the values
    • getDesiredBlocks

      protected final int getDesiredBlocks​(int desiredSize)
      Returns the number of blocks needed to store an array of the specified size
    • isValidStructure

      public boolean isValidStructure()
      Performs an internal check to make sure all data structures and values are internally consistent. Used for debugging and paranoia.
    • getTotalAllocation

      public int getTotalAllocation()
      Returns the number of elements which have been allocated. This array size has to be less than or equal to this number\
    • blockArrayLength

      protected int blockArrayLength​(int block)
    • setInitialBlockSize

      public void setInitialBlockSize​(int initialBlockSize)
      Assigns a new initial block size. The value is adjusted to ensure that it is valid. Can't be larger than a block or less than 1.
    • newArrayInstance

      protected abstract Array newArrayInstance​(int size)
    • arrayLength

      protected abstract int arrayLength​(Array array)