Package civitas.celestis.util.grid
Class LongArrayGrid
java.lang.Object
civitas.celestis.util.grid.LongArrayGrid
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 -
Constructor Summary
ConstructorsConstructorDescriptionLongArrayGrid(int rows, int columns) Creates a new array grid.Creates a new array grid. -
Method Summary
Modifier and TypeMethodDescriptionlong[]array()Returns an array containing every element within this grid.boxed()Returns a new grid whose values are populated from that of this grid's values, encapsulated inside aLongobject.collect()Returns a collection containing every element within this grid.intcolumns()Returns the number of columns (the width) of this grid.booleancontains(long v) Checks if this grid contains the provided valuev.booleancontainsAll(Iterable<Long> i) Checks if this grid contains multiple values.booleanChecks for equality between this grid and the provided objectobj.voidfill(long v) Fills this grid, populating every index with the provided valuev.voidfillRange(int r1, int c1, int r2, int c2, long v) Fills this grid, but only does so within the specified range.voidforEach(TriConsumer<Integer, Integer, ? super Long> a) Executes the provided action for each element of this grid.voidExecutes the provided action for each element of this grid.longget(int r, int c) Returns the element at the specified index.inthashCode()Returns the hash code of this instance.iterator()Returns an iterator of every element of this grid.Applies the provided mapper functionfto 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(LongFunction<? extends F> f) Applies the provided mapper functionfto every element of this grid, then returns a new grid whose values are populated from that of the provided function's return values.merge(LongGrid g, LongBinaryOperator f) Between this grid and the provided gridg, this applies the merger functionffor each corresponding pair of elements, then returns a new grid whose elements are populated from that of the return values of the merger functionf.static LongArrayGridof(long[][] values) Creates a new array grid from a 2D array of values.voidreplaceAll(long oldValue, long 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.introws()Returns the number of rows (the height) of this grid.set()Returns a set containing every element of this grid.voidset(int r, int c, long v) Sets the value of the specified index.voidSets a sub-grid of this grid, copying values from the provided sub-gridg.intsize()Returns the size (the geometric area) of this grid.stream()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.toString()Serializes this grid into a string.Returns the transpose of this grid.voidApplies the provided update functionfto every element of this grid, assigning the return value of the function to the corresponding index of this grid.voidApplies the provided update functionfto 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, waitMethods inherited from interface java.lang.Iterable
spliterator
-
Field Details
-
values
protected final long[][] valuesThe internal 2D array of values. -
rows
protected final int rowsThe number of rows this grid has. -
columns
protected final int columnsThe number of columns this grid has.
-
-
Constructor Details
-
LongArrayGrid
public LongArrayGrid(int rows, int columns) Creates a new array grid.- Parameters:
rows- The number of rows to initializecolumns- The number of columns to initialize
-
LongArrayGrid
Creates a new array grid.- Parameters:
g- The grid of which to copy component values from
-
-
Method Details
-
of
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. -
columns
public int columns()Returns the number of columns (the width) of this grid. -
size
public int size()Returns the size (the geometric area) of this grid. In other words, this return the maximum capacity of this grid. -
contains
public boolean contains(long v) Checks if this grid contains the provided valuev. -
containsAll
Checks if this grid contains multiple values.- Specified by:
containsAllin interfaceLongGrid- Parameters:
i- The iterable object of which to check for containment- Returns:
trueif this grid contains every value of the iterable object
-
get
Returns the element at the specified index.- Specified by:
getin interfaceLongGrid- Parameters:
r- The index of the row to getc- The index of the column to get- Returns:
- The element at the specified index
- Throws:
IndexOutOfBoundsException- When the indices are out of bounds
-
set
Sets the value of the specified index.- Specified by:
setin interfaceLongGrid- Parameters:
r- The index of the row to setc- The index of the column to setv- The value of which to assign to the specified index- Throws:
IndexOutOfBoundsException- When the indices are out of bounds
-
fill
public void fill(long v) Fills this grid, populating every index with the provided valuev. -
fillRange
Fills this grid, but only does so within the specified range.- Specified by:
fillRangein interfaceLongGrid- Parameters:
r1- The starting position's row indexc1- The starting position's column indexr2- The ending position's row indexc2- The ending position's column indexv- The value of which to fill the specified range with- Throws:
IndexOutOfBoundsException- When the range is out of bounds
-
update
Applies the provided update functionfto every element of this grid, assigning the return value of the function to the corresponding index of this grid. -
update
Applies the provided update functionfto 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. -
replaceAll
public void replaceAll(long oldValue, long newValue) Replaces all instances of the old value with the new value within this grid.- Specified by:
replaceAllin interfaceLongGrid- Parameters:
oldValue- The old value of which to replacenewValue- The new value of which to replace to
-
subGrid
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:
subGridin interfaceLongGrid- Parameters:
r1- The starting position's row indexc1- The starting position's column indexr2- The ending position's row indexc2- 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 LongGrid g) throws IndexOutOfBoundsException Sets a sub-grid of this grid, copying values from the provided sub-gridg. 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:
setRangein interfaceLongGrid- Parameters:
r1- The starting position's row indexc1- The starting position's column indexr2- The ending position's row indexc2- The ending position's column indexg- The sub-grid of this grid containing the values to assign- Throws:
IndexOutOfBoundsException- When the range is out of bounds
-
resize
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 asnull. -
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 functionf({r, c}) -> {c, r}, whererrepresents the row index, andcrepresents the column index. In simple terms, the rows and columns are inverted. -
map
Applies the provided mapper functionfto every element of this grid, then returns a new grid whose values are populated from that of the provided function's return values. -
mapToObj
Applies the provided mapper functionfto every element of this grid, then returns a new grid whose values are populated from that of the provided function's return values. -
merge
@Nonnull public LongGrid merge(@Nonnull LongGrid g, @Nonnull LongBinaryOperator f) throws IllegalArgumentException Between this grid and the provided gridg, this applies the merger functionffor each corresponding pair of elements, then returns a new grid whose elements are populated from that of the return values of the merger functionf.- Specified by:
mergein interfaceLongGrid- Parameters:
g- The grid of which to merge this grid withf- The merger function to handle the merging of the two grids- Returns:
- The resulting grid
- Throws:
IllegalArgumentException- When the provided gridg's dimensions are different from that of this grid's dimensions
-
iterator
Returns an iterator of every element of this grid. -
forEach
Executes the provided action for each element of this grid. The order of execution is not guaranteed to be consistent. -
forEach
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. -
array
@Nonnull public long[] 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. -
stream
Returns a stream whose source is the elements of this grid. -
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. -
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. -
boxed
Returns a new grid whose values are populated from that of this grid's values, encapsulated inside aLongobject. -
equals
Checks for equality between this grid and the provided objectobj. -
toString
Serializes this grid into a string. -
hashCode
public int hashCode()Returns the hash code of this instance.
-