Class HashGrid<E>

java.lang.Object
civitas.celestis.util.grid.HashGrid<E>
Type Parameters:
E - The type of element this grid should hold
All Implemented Interfaces:
BaseGrid<E>, DynamicGrid<E>, Grid<E>, Serializable, Iterable<E>

public class HashGrid<E> extends Object implements DynamicGrid<E>
A grid whose data is stored as a HashMap.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int
    The last-known number of columns.
    protected int
    The last-known number of rows.
    protected final HashMap<Integer,E>
    The internal map of values.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new 0x0 hash grid.
    HashGrid(int rows, int columns)
    Creates a new hash grid.
    HashGrid(Grid<? extends E> g)
    Creates a new hash grid.
  • Method Summary

    Modifier and Type
    Method
    Description
    E[]
    Returns an array containing every element within this grid.
    protected int[]
    Calculates the minimum required dimensions to hold all elements of this grid.
    void
    Cleans this grid, removing all instance of null.
    void
    Cleans this grid, then trims it to its minimum possible size.
    void
    Clears all entries within this grid, removing them from memory.
    Returns a collection containing every element within this grid.
    int
    Returns the number of columns (the width) of this grid.
    boolean
    Checks if this grid contains the provided object obj.
    boolean
    Checks if this grid contains multiple objects.
    boolean
    Checks for equality between this grid and the provided object obj.
    void
    fill(E v)
    Fills this grid, populating every index with the provided value v.
    void
    Fills this grid, but only does so if the original element currently occupying the corresponding slot is null.
    void
    fillRange(int r1, int c1, int r2, int c2, E 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 E> a)
    Executes the provided action for each element of this grid.
    get(int r, int c)
    Returns the element at the specified index.
    getOrDefault(int r, int c, E fallback)
    Returns the element at the specified index, but returns the fallback value if the existing element is null.
    int
    Returns the hash code of this instance.
    Returns an iterator of every element of this grid.
    <F> Grid<F>
    map(Function<? super E,? 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.
    Applies the provided mapper function f to every element of this grid, then returns a new double grid whose values are populated from that of the provided function's return values.
    Applies the provided mapper function f to every element of this grid, then returns a new float grid whose values are populated from that of the provided function's return values.
    mapToInt(ToIntFunction<? super E> f)
    Applies the provided mapper function f to every element of this grid, then returns a new integer grid whose values are populated from that of the provided function's return values.
    Applies the provided mapper function f to every element of this grid, then returns a new long grid whose values are populated from that of the provided function's return values.
    <F, G> Grid<G>
    merge(Grid<F> g, BiFunction<? super E,? super F,G> f)
    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.
    static <E> HashGrid<E>
    of(E[][] values)
    Creates a new hash grid from a 2D array of values.
    protected void
    remapValues(int newColumns)
    Given a new column count, this remaps the value map of this grid.
    boolean
    remove(int r, int c)
    Removes the element from the specified position.
    protected void
    Removes all entries which are outside the current bounds.
    void
    replaceAll(E oldValue, E 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, E v)
    Sets the value of the specified index.
    void
    setRange(int r1, int c1, int r2, int c2, Grid<? extends E> g)
    Sets a sub-grid of this grid, copying values from the provided sub-grid g.
    void
    setSize(int rows, int columns)
    Forcefully sets the size of this grid, without regard to the data's integrity.
    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
    Trims this grid, reducing its size to the minimum possible size required to hold all elements.
    void
    update(TriFunction<? super Integer,? super Integer,? super E,E> 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
    update(Function<? super E,E> 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.

    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 HashMap<Integer,E> values
      The internal map of values.
    • rows

      protected volatile int rows
      The last-known number of rows.
    • columns

      protected volatile int columns
      The last-known number of columns.
  • Constructor Details

    • HashGrid

      public HashGrid()
      Creates a new 0x0 hash grid.
    • HashGrid

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

      public HashGrid(@Nonnull Grid<? extends E> g)
      Creates a new hash grid. Entries with the value null will be ignored.
      Parameters:
      g - The grid of which to copy component values from
  • Method Details

    • of

      @Nonnull public static <E> HashGrid<E> of(@Nonnull E[][] values)
      Creates a new hash grid from a 2D array of values.
      Type Parameters:
      E - The type of element to contain
      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<E>
      Specified by:
      rows in interface Grid<E>
      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<E>
      Specified by:
      columns in interface Grid<E>
      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<E>
      Specified by:
      size in interface Grid<E>
      Returns:
      The number of element this gird can potentially hold
    • contains

      public boolean contains(@Nullable Object obj)
      Checks if this grid contains the provided object obj.
      Specified by:
      contains in interface Grid<E>
      Parameters:
      obj - The object 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 object obj
    • containsAll

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

      public E get(int r, int c) throws IndexOutOfBoundsException
      Returns the element at the specified index.
      Specified by:
      get in interface Grid<E>
      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
    • getOrDefault

      public E getOrDefault(int r, int c, E fallback) throws IndexOutOfBoundsException
      Returns the element at the specified index, but returns the fallback value if the existing element is null.
      Specified by:
      getOrDefault in interface Grid<E>
      Parameters:
      r - The index of the row to get
      c - The index of the column to get
      fallback - The fallback value of which to return if the element is null
      Returns:
      The element at the specified index, or the fallback value if the existing element is equal to null
      Throws:
      IndexOutOfBoundsException - When the indices are out of bounds
    • set

      public void set(int r, int c, E v) throws IndexOutOfBoundsException
      Sets the value of the specified index.
      Specified by:
      set in interface Grid<E>
      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
    • clear

      public void clear()
      Clears all entries within this grid, removing them from memory. This differs from filling the grid with null in that it reduces the footprint of this grid. This is the equivalent of setting this grid's size to {0, 0}, then resizing it back to its original size.
      Specified by:
      clear in interface DynamicGrid<E>
    • remove

      public boolean remove(int r, int c)
      Removes the element from the specified position. This differs from setting the value to null in that it physically removes the entry corresponding to the specified index, as opposed to simply setting the value to null.
      Specified by:
      remove in interface DynamicGrid<E>
      Parameters:
      r - The index of the row to remove
      c - The index of the column to remove
      Returns:
      true if there was a value (including null) at the specified position, and thus the state of this grid has changed as a result of this operation
    • fill

      public void fill(E v)
      Fills this grid, populating every index with the provided value v.
      Specified by:
      fill in interface Grid<E>
      Parameters:
      v - The value of which to fill this grid with
    • fillEmpty

      public void fillEmpty(E v)
      Fills this grid, but only does so if the original element currently occupying the corresponding slot is null.
      Specified by:
      fillEmpty in interface Grid<E>
      Parameters:
      v - The values of which to selectively fill this grid with
    • fillRange

      public void fillRange(int r1, int c1, int r2, int c2, E v) throws IndexOutOfBoundsException
      Fills this grid, but only does so within the specified range.
      Specified by:
      fillRange in interface Grid<E>
      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 Function<? super E,E> 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 Grid<E>
      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 E,E> 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 Grid<E>
      Parameters:
      f - The function of which to apply to each element of this grid
    • replaceAll

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

      @Nonnull public Grid<E> 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 Grid<E>
      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 Grid<? extends E> 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 Grid<E>
      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
    • calculateRequiredDimensions

      protected int[] calculateRequiredDimensions()
      Calculates the minimum required dimensions to hold all elements of this grid.
      Returns:
      The minimum dimensions in rows-columns order
    • removeUncontainableEntries

      protected void removeUncontainableEntries()
      Removes all entries which are outside the current bounds.
    • remapValues

      protected void remapValues(int newColumns)
      Given a new column count, this remaps the value map of this grid.
      Parameters:
      newColumns - The new column count
    • clean

      public void clean()
      Cleans this grid, removing all instance of null. This does not trim the grid to its minimum possible size.
      Specified by:
      clean in interface DynamicGrid<E>
      See Also:
    • trim

      public void trim()
      Trims this grid, reducing its size to the minimum possible size required to hold all elements. This does not clean null values.
      Specified by:
      trim in interface DynamicGrid<E>
      See Also:
    • cleanAndTrim

      public void cleanAndTrim()
      Cleans this grid, then trims it to its minimum possible size. In other words, this performs DynamicGrid.clean(), then trims the grid.
      Specified by:
      cleanAndTrim in interface DynamicGrid<E>
      See Also:
    • setSize

      public void setSize(int rows, int columns)
      Forcefully sets the size of this grid, without regard to the data's integrity. If this operation results in the reduction of this grid's size, all elements which are now outside the bounds of this grid will be removed.
      Specified by:
      setSize in interface DynamicGrid<E>
      Parameters:
      rows - The number rows to resize to
      columns - The number of columns to resize to
    • resize

      @Nonnull public Grid<E> 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 Grid<E>
      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 Grid<E> 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 Grid<E>
      Returns:
      The transpose of this grid
    • map

      @Nonnull public <F> Grid<F> map(@Nonnull Function<? super E,? 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:
      map in interface Grid<E>
      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
    • mapToDouble

      @Nonnull public DoubleGrid mapToDouble(@Nonnull ToDoubleFunction<? super E> f)
      Applies the provided mapper function f to every element of this grid, then returns a new double grid whose values are populated from that of the provided function's return values.
      Specified by:
      mapToDouble in interface Grid<E>
      Parameters:
      f - The function of which to apply to each element of this grid
      Returns:
      The resulting double grid
    • mapToFloat

      @Nonnull public FloatGrid mapToFloat(@Nonnull ToFloatFunction<? super E> f)
      Applies the provided mapper function f to every element of this grid, then returns a new float grid whose values are populated from that of the provided function's return values.
      Specified by:
      mapToFloat in interface Grid<E>
      Parameters:
      f - The function of which to apply to each element of this grid
      Returns:
      The resulting float grid
    • mapToLong

      @Nonnull public LongGrid mapToLong(@Nonnull ToLongFunction<? super E> f)
      Applies the provided mapper function f to every element of this grid, then returns a new long grid whose values are populated from that of the provided function's return values.
      Specified by:
      mapToLong in interface Grid<E>
      Parameters:
      f - The function of which to apply to each element of this grid
      Returns:
      The resulting long grid
    • mapToInt

      @Nonnull public IntGrid mapToInt(@Nonnull ToIntFunction<? super E> f)
      Applies the provided mapper function f to every element of this grid, then returns a new integer grid whose values are populated from that of the provided function's return values.
      Specified by:
      mapToInt in interface Grid<E>
      Parameters:
      f - The function of which to apply to each element of this grid
      Returns:
      The resulting integer grid
    • merge

      @Nonnull public <F, G> Grid<G> merge(@Nonnull Grid<F> g, @Nonnull BiFunction<? super E,? super F,G> 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 Grid<E>
      Type Parameters:
      F - The type of element to merge this grid with
      G - The type of element to merge the two grids to
      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
    • array

      @Nonnull public E[] 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 Grid<E>
      Returns:
      The array representation of this grid
    • stream

      @Nonnull public Stream<E> stream()
      Returns a stream whose source is the elements of this grid.
      Specified by:
      stream in interface Grid<E>
      Returns:
      A stream whose source is the elements of this grid
      See Also:
    • collect

      @Nonnull public Collection<E> 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<E>
      Specified by:
      collect in interface Grid<E>
      Returns:
      The collection representation of this grid
      See Also:
    • set

      @Nonnull public Set<E> 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<E>
      Specified by:
      set in interface Grid<E>
      Returns:
      The set representation of this grid
      See Also:
    • iterator

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

      public void forEach(@Nonnull Consumer<? super E> 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<E>
      Specified by:
      forEach in interface Grid<E>
      Specified by:
      forEach in interface Iterable<E>
      Parameters:
      a - The action of which to execute for each element of this grid
    • forEach

      public void forEach(@Nonnull TriConsumer<Integer,Integer,? super E> 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<E>
      Specified by:
      forEach in interface Grid<E>
      Parameters:
      a - The action of which to execute for each element of this grid
    • equals

      public boolean equals(Object obj)
      Checks for equality between this grid and the provided object obj.
      Specified by:
      equals in interface Grid<E>
      Overrides:
      equals in class Object
      Parameters:
      obj - The object to compare to
      Returns:
      true if the other object is also a grid, and the dimensions, order of elements, and composition are all equal to this grid
    • toString

      @Nonnull public String toString()
      Serializes this grid into a string.
      Specified by:
      toString in interface Grid<E>
      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