Class DoubleArrayGrid

java.lang.Object
civitas.celestis.util.grid.DoubleArrayGrid
All Implemented Interfaces:
BaseGrid<Double>, DoubleGrid, Serializable, Iterable<Double>
Direct Known Subclasses:
Matrix

public class DoubleArrayGrid extends Object implements DoubleGrid
A static grid implemented using primitive arrays. An array grid's size cannot be changed after instantiation. Since the underlying data structure is a fixed-size array, array grids are memory-efficient and random access operations tend to be faster than other grid implementations.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final int
    The number of columns this grid has.
    protected final int
    The number of rows this grid has.
    protected final double[][]
    The internal 2D array of values.
  • Constructor Summary

    Constructors
    Constructor
    Description
    DoubleArrayGrid(int rows, int columns)
    Creates a new array grid.
    Creates a new array grid.
  • Method Summary

    Modifier and Type
    Method
    Description
    double[]
    Returns an array containing every element within this grid.
    Returns a new grid whose values are populated from that of this grid's values, encapsulated inside a Double object.
    Returns a collection containing every element within this grid.
    int
    Returns the number of columns (the width) of this grid.
    boolean
    contains(double v)
    Checks if this grid contains the provided value v.
    boolean
    Checks if this grid contains multiple values.
    boolean
    Checks for equality between this grid and the provided object obj.
    void
    fill(double v)
    Fills this grid, populating every index with the provided value v.
    void
    fillRange(int r1, int c1, int r2, int c2, double v)
    Fills this grid, but only does so within the specified range.
    void
    Executes the provided action for each element of this grid.
    void
    forEach(Consumer<? super Double> a)
    Executes the provided action for each element of this grid.
    double
    get(int r, int c)
    Returns the element at the specified index.
    int
    Returns the hash code of this instance.
    Returns an iterator of every element of this grid.
    Applies the provided mapper function f to every element of this grid, then returns a new grid whose values are populated from that of the provided function's return values.
    <F> Grid<F>
    mapToObj(DoubleFunction<? extends F> f)
    Applies the provided mapper function f to every element of this grid, then returns a new grid whose values are populated from that of the provided function's return values.
    Between this grid and the provided grid g, this applies the merger function f for each corresponding pair of elements, then returns a new grid whose elements are populated from that of the return values of the merger function f.
    of(double[][] values)
    Creates a new array grid from a 2D array of values.
    void
    replaceAll(double oldValue, double newValue)
    Replaces all instances of the old value with the new value within this grid.
    resize(int rows, int columns)
    Returns a resized grid, whose elements are populated from that of this grid's elements.
    int
    Returns the number of rows (the height) of this grid.
    set()
    Returns a set containing every element of this grid.
    void
    set(int r, int c, double v)
    Sets the value of the specified index.
    void
    setRange(int r1, int c1, int r2, int c2, DoubleGrid g)
    Sets a sub-grid of this grid, copying values from the provided sub-grid g.
    int
    Returns the size (the geometric area) of this grid.
    Returns a stream whose source is the elements of this grid.
    subGrid(int r1, int c1, int r2, int c2)
    Returns a sub-grid of this grid, whose values are populated from that of this grid's elements within the specified range.
    Serializes this grid into a string.
    Returns the transpose of this grid.
    void
    update(TriFunction<? super Integer,? super Integer,? super Double,Double> f)
    Applies the provided update function f to every element of this grid, assigning the return value of the function to the corresponding index of this grid.
    void
    Applies the provided update function f to every element of this grid, assigning the return value of the function to the corresponding index of this grid.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    spliterator
  • Field Details

    • values

      protected final double[][] values
      The internal 2D array of values.
    • rows

      protected final int rows
      The number of rows this grid has.
    • columns

      protected final int columns
      The number of columns this grid has.
  • Constructor Details

    • DoubleArrayGrid

      public DoubleArrayGrid(int rows, int columns)
      Creates a new array grid.
      Parameters:
      rows - The number of rows to initialize
      columns - The number of columns to initialize
    • DoubleArrayGrid

      public DoubleArrayGrid(@Nonnull DoubleGrid g)
      Creates a new array grid.
      Parameters:
      g - The grid of which to copy component values from
  • Method Details

    • of

      @Nonnull public static DoubleArrayGrid of(@Nonnull double[][] values)
      Creates a new array grid from a 2D array of values.
      Parameters:
      values - The values of which to contain in the grid
      Returns:
      The constructed grid
    • rows

      public int rows()
      Returns the number of rows (the height) of this grid.
      Specified by:
      rows in interface BaseGrid<Double>
      Specified by:
      rows in interface DoubleGrid
      Returns:
      The number of rows this grid has
    • columns

      public int columns()
      Returns the number of columns (the width) of this grid.
      Specified by:
      columns in interface BaseGrid<Double>
      Specified by:
      columns in interface DoubleGrid
      Returns:
      The number of columns this grid has
    • size

      public int size()
      Returns the size (the geometric area) of this grid. In other words, this return the maximum capacity of this grid.
      Specified by:
      size in interface BaseGrid<Double>
      Specified by:
      size in interface DoubleGrid
      Returns:
      The number of element this gird can potentially hold
    • contains

      public boolean contains(double v)
      Checks if this grid contains the provided value v.
      Specified by:
      contains in interface DoubleGrid
      Parameters:
      v - The value of which to check for containment
      Returns:
      true if there is at least one element within this grid which is equal to that of the provided value v
    • containsAll

      public boolean containsAll(@Nonnull Iterable<Double> i)
      Checks if this grid contains multiple values.
      Specified by:
      containsAll in interface DoubleGrid
      Parameters:
      i - The iterable object of which to check for containment
      Returns:
      true if this grid contains every value of the iterable object
    • get

      public double get(int r, int c) throws IndexOutOfBoundsException
      Returns the element at the specified index.
      Specified by:
      get in interface DoubleGrid
      Parameters:
      r - The index of the row to get
      c - The index of the column to get
      Returns:
      The element at the specified index
      Throws:
      IndexOutOfBoundsException - When the indices are out of bounds
    • set

      public void set(int r, int c, double v) throws IndexOutOfBoundsException
      Sets the value of the specified index.
      Specified by:
      set in interface DoubleGrid
      Parameters:
      r - The index of the row to set
      c - The index of the column to set
      v - The value of which to assign to the specified index
      Throws:
      IndexOutOfBoundsException - When the indices are out of bounds
    • fill

      public void fill(double v)
      Fills this grid, populating every index with the provided value v.
      Specified by:
      fill in interface DoubleGrid
      Parameters:
      v - The value of which to fill this grid with
    • fillRange

      public void fillRange(int r1, int c1, int r2, int c2, double v) throws IndexOutOfBoundsException
      Fills this grid, but only does so within the specified range.
      Specified by:
      fillRange in interface DoubleGrid
      Parameters:
      r1 - The starting position's row index
      c1 - The starting position's column index
      r2 - The ending position's row index
      c2 - The ending position's column index
      v - The value of which to fill the specified range with
      Throws:
      IndexOutOfBoundsException - When the range is out of bounds
    • update

      public void update(@Nonnull DoubleUnaryOperator f)
      Applies the provided update function f to every element of this grid, assigning the return value of the function to the corresponding index of this grid.
      Specified by:
      update in interface DoubleGrid
      Parameters:
      f - The function of which to apply to each element of this grid
    • update

      public void update(@Nonnull TriFunction<? super Integer,? super Integer,? super Double,Double> f)
      Applies the provided update function f to every element of this grid, assigning the return value of the function to the corresponding index of this grid. The row and column indices are provided as the first and second parameter of the function respectively, and the original element is provided as the third parameter of ths function.
      Specified by:
      update in interface DoubleGrid
      Parameters:
      f - The function of which to apply to each element of this grid
    • replaceAll

      public void replaceAll(double oldValue, double newValue)
      Replaces all instances of the old value with the new value within this grid.
      Specified by:
      replaceAll in interface DoubleGrid
      Parameters:
      oldValue - The old value of which to replace
      newValue - The new value of which to replace to
    • subGrid

      @Nonnull public DoubleGrid subGrid(int r1, int c1, int r2, int c2) throws IndexOutOfBoundsException
      Returns a sub-grid of this grid, whose values are populated from that of this grid's elements within the specified range. This is not a direct reference to this grid, and changes in the sub-grid will not be reflected to the original grid.
      Specified by:
      subGrid in interface DoubleGrid
      Parameters:
      r1 - The starting position's row index
      c1 - The starting position's column index
      r2 - The ending position's row index
      c2 - The ending position's column index
      Returns:
      The sub-grid of this grid corresponding to the specified range
      Throws:
      IndexOutOfBoundsException - When the range is out of bounds
    • setRange

      public void setRange(int r1, int c1, int r2, int c2, @Nonnull DoubleGrid g) throws IndexOutOfBoundsException
      Sets a sub-grid of this grid, copying values from the provided sub-grid g. This is not a direct reference to this grid, and changes in the sub-grid will not be reflected to the original grid.
      Specified by:
      setRange in interface DoubleGrid
      Parameters:
      r1 - The starting position's row index
      c1 - The starting position's column index
      r2 - The ending position's row index
      c2 - The ending position's column index
      g - The sub-grid of this grid containing the values to assign
      Throws:
      IndexOutOfBoundsException - When the range is out of bounds
    • resize

      @Nonnull public DoubleGrid resize(int rows, int columns)
      Returns a resized grid, whose elements are populated from that of this grid's elements. If the requested size is larger than that of this grid's size, the oversized portion will not be populated, leaving it uninitialized as null.
      Specified by:
      resize in interface DoubleGrid
      Parameters:
      rows - The number of rows the resized grid should have
      columns - The number of columns the resized grid should have
      Returns:
      A new grid with the specified dimensions
    • transpose

      @Nonnull public DoubleGrid transpose()
      Returns the transpose of this grid. The transpose is a grid whose rows and columns are inverted from that os this grid's rows and columns. The elements are mapped according to the function f({r, c}) -> {c, r}, where r represents the row index, and c represents the column index. In simple terms, the rows and columns are inverted.
      Specified by:
      transpose in interface DoubleGrid
      Returns:
      The transpose of this grid
    • map

      @Nonnull public DoubleGrid map(@Nonnull DoubleUnaryOperator f)
      Applies the provided mapper function f to every element of this grid, then returns a new grid whose values are populated from that of the provided function's return values.
      Specified by:
      map in interface DoubleGrid
      Parameters:
      f - The function of which to apply to each element of this grid
      Returns:
      The resulting grid
    • mapToObj

      @Nonnull public <F> Grid<F> mapToObj(@Nonnull DoubleFunction<? extends F> f)
      Applies the provided mapper function f to every element of this grid, then returns a new grid whose values are populated from that of the provided function's return values.
      Specified by:
      mapToObj in interface DoubleGrid
      Type Parameters:
      F - The type of element to map this grid to
      Parameters:
      f - The function of which to apply to each element of this grid
      Returns:
      The resulting grid
    • merge

      @Nonnull public DoubleGrid merge(@Nonnull DoubleGrid g, @Nonnull DoubleBinaryOperator f) throws IllegalArgumentException
      Between this grid and the provided grid g, this applies the merger function f for each corresponding pair of elements, then returns a new grid whose elements are populated from that of the return values of the merger function f.
      Specified by:
      merge in interface DoubleGrid
      Parameters:
      g - The grid of which to merge this grid with
      f - The merger function to handle the merging of the two grids
      Returns:
      The resulting grid
      Throws:
      IllegalArgumentException - When the provided grid g's dimensions are different from that of this grid's dimensions
    • iterator

      @Nonnull public Iterator<Double> iterator()
      Returns an iterator of every element of this grid.
      Specified by:
      iterator in interface BaseGrid<Double>
      Specified by:
      iterator in interface DoubleGrid
      Specified by:
      iterator in interface Iterable<Double>
      Returns:
      An iterator of every element of this grid
    • forEach

      public void forEach(@Nonnull Consumer<? super Double> a)
      Executes the provided action for each element of this grid. The order of execution is not guaranteed to be consistent.
      Specified by:
      forEach in interface BaseGrid<Double>
      Specified by:
      forEach in interface DoubleGrid
      Specified by:
      forEach in interface Iterable<Double>
      Parameters:
      a - The action of which to execute for each element of this grid
    • forEach

      public void forEach(@Nonnull TriConsumer<Integer,Integer,? super Double> a)
      Executes the provided action for each element of this grid. The row and column indices are provided as the first and second parameter of the function respectively, and the current element is provided as the third parameter of the function. The order of execution is not guaranteed to be consistent.
      Specified by:
      forEach in interface BaseGrid<Double>
      Specified by:
      forEach in interface DoubleGrid
      Parameters:
      a - The action of which to execute for each element of this grid
    • array

      @Nonnull public double[] array()
      Returns an array containing every element within this grid. The length of the array is not guaranteed to be equal to the size of this grid, and the order is not guaranteed to be consistent.
      Specified by:
      array in interface DoubleGrid
      Returns:
      The array representation of this grid
    • stream

      @Nonnull public DoubleStream stream()
      Returns a stream whose source is the elements of this grid.
      Specified by:
      stream in interface DoubleGrid
      Returns:
      A stream whose source is the elements of this grid
      See Also:
    • collect

      @Nonnull public Collection<Double> collect()
      Returns a collection containing every element within this grid. The size of the collection is not guaranteed to be equal to the size of this grid, and the order is not guaranteed to be consistent, if there even is one.
      Specified by:
      collect in interface BaseGrid<Double>
      Specified by:
      collect in interface DoubleGrid
      Returns:
      The collection representation of this grid
      See Also:
    • set

      @Nonnull public Set<Double> set()
      Returns a set containing every element of this grid. Due to the nature of sets, every duplicate element will be counted as one element. This property can be useful for certain applications.
      Specified by:
      set in interface BaseGrid<Double>
      Specified by:
      set in interface DoubleGrid
      Returns:
      The set representation of this grid
      See Also:
    • boxed

      @Nonnull public Grid<Double> boxed()
      Returns a new grid whose values are populated from that of this grid's values, encapsulated inside a Double object.
      Specified by:
      boxed in interface DoubleGrid
      Returns:
      The boxed grid representation of this grid
      See Also:
    • equals

      public boolean equals(@Nullable Object obj)
      Checks for equality between this grid and the provided object obj.
      Specified by:
      equals in interface DoubleGrid
      Overrides:
      equals in class Object
      Parameters:
      obj - The object to compare to
      Returns:
      true if the object is also a double grid, and the dimensions, order of elements, and the elements' values are all equal
    • toString

      @Nonnull public String toString()
      Serializes this grid into a string.
      Specified by:
      toString in interface DoubleGrid
      Overrides:
      toString in class Object
      Returns:
      The string representation of this grid
    • hashCode

      public int hashCode()
      Returns the hash code of this instance.
      Overrides:
      hashCode in class Object
      Returns:
      The hash code of this instance