Package civitas.celestis.util.grid
Interface Grid<E>
- Type Parameters:
E- The type of element this grid should hold
- All Superinterfaces:
BaseGrid<E>,Iterable<E>,Serializable
- All Known Subinterfaces:
DynamicGrid<E>
- All Known Implementing Classes:
ArrayGrid,AtomicGrid,HashGrid,SyncGrid
A two-dimensional structure of elements. Grids can be traversed by providing
two indices: one for the row, and one for the column.
Static grids which use two-dimensional arrays as their underlying data structure are fixed-size, meaning the dimensions cannot be changed without re-instantiation.
On the other hand, dynamic grids use a more flexible underlying
data structure, allowing the resizing of the grid without re-instantiation. Note
that dynamic grids have more overhead in terms of memory consumption, and primitive
types are not supported. (the boxed object types such as Doubles are used)
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionE[]array()Returns an array containing every element within this grid.static <E> Grid<E>atomicCopyOf(Grid<? extends E> g) Creates an atomic copy of an existing grid.static <E> Grid<E>atomicOf(E[][] values) Creates a new atomic grid form a two-dimensional array of values.collect()Returns a collection containing every element within this grid.intcolumns()Returns the number of columns (the width) of this grid.booleanChecks if this grid contains the provided objectobj.booleancontainsAll(Iterable<?> i) Checks if this grid contains multiple objects.static <E> Grid<E>Creates a copy of an existing grid.static <E> Grid<E>dynamicCopyOf(Grid<? extends E> g) Creates a dynamic copy of an existing grid.static <E> Grid<E>dynamicOf(E[][] values) Creates a new dynamic grid from a two-dimensional array of values.booleanChecks for equality between this grid and the provided objectobj.voidFills this grid, populating every index with the provided valuev.voidFills this grid, but only does so if the original element currently occupying the corresponding slot isnull.voidFills this grid, but only does so within the specified range.voidforEach(TriConsumer<Integer, Integer, ? super E> a) Executes the provided action for each element of this grid.voidExecutes 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 isnull.iterator()Returns an iterator of every element of this grid.<F> Grid<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.mapToDouble(ToDoubleFunction<? super E> f) Applies the provided mapper functionfto every element of this grid, then returns a new double grid whose values are populated from that of the provided function's return values.mapToFloat(ToFloatFunction<? super E> f) Applies the provided mapper functionfto 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 functionfto every element of this grid, then returns a new integer grid whose values are populated from that of the provided function's return values.mapToLong(ToLongFunction<? super E> f) Applies the provided mapper functionfto 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 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 <E> Grid<E>of(E[][] values) Creates a new grid from a two-dimensional array of values.voidreplaceAll(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.introws()Returns the number of rows (the height) of this grid.set()Returns a set containing every element of this grid.voidSets 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.static <E> Grid<E>syncCopyOf(Grid<? extends E> g) Creates a thread-safe copy of an existing grid.static <E> Grid<E>syncOf(E[][] values) Creates a new thread-safe grid from a two-dimensional array of values.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 interface java.lang.Iterable
spliterator
-
Method Details
-
of
Creates a new grid from a two-dimensional 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 instance
-
dynamicOf
Creates a new dynamic grid from a two-dimensional 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 dynamic grid instance
- See Also:
-
syncOf
Creates a new thread-safe grid from a two-dimensional 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 thread-safe grid instance
- See Also:
-
atomicOf
Creates a new atomic grid form a two-dimensional 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 atomic grid instance
- See Also:
-
copyOf
Creates a copy of an existing grid.- Type Parameters:
E- The type of element to contain- Parameters:
g- The grid of which to copy elements from- Returns:
- A shallow copy of the provided grid
g
-
dynamicCopyOf
Creates a dynamic copy of an existing grid.- Type Parameters:
E- The type of element to contain- Parameters:
g- The grid of which to copy elements from- Returns:
- A dynamic shallow copy of the provided grid
g - See Also:
-
syncCopyOf
Creates a thread-safe copy of an existing grid.- Type Parameters:
E- The type of element to contain- Parameters:
g- The grid of which to copy elements from- Returns:
- A thread-safe shallow copy of the provided grid
g - See Also:
-
atomicCopyOf
Creates an atomic copy of an existing grid.- Type Parameters:
E- The type of element to contain- Parameters:
g- The grid of which to copy elements from- Returns:
- An atomic shallow copy of the provided grid
g - See Also:
-
rows
int rows()Returns the number of rows (the height) of this grid. -
columns
int columns()Returns the number of columns (the width) of this grid. -
size
int size()Returns the size (the geometric area) of this grid. In other words, this return the maximum capacity of this grid. -
contains
Checks if this grid contains the provided objectobj.- Parameters:
obj- The object of which to check for containment- Returns:
trueif there is at least one element within this grid which is equal to that of the provided objectobj
-
containsAll
Checks if this grid contains multiple objects.- Parameters:
i- The iterable object of which to check for containment- Returns:
trueif this grid contains every element of the iterable object
-
get
Returns the element at the specified index.- 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
-
getOrDefault
Returns the element at the specified index, but returns the fallback value if the existing element isnull.- Parameters:
r- The index of the row to getc- The index of the column to getfallback- The fallback value of which to return if the element isnull- 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
Sets the value of the specified index.- 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
Fills this grid, populating every index with the provided valuev.- Parameters:
v- The value of which to fill this grid with
-
fillEmpty
Fills this grid, but only does so if the original element currently occupying the corresponding slot isnull.- Parameters:
v- The values of which to selectively fill this grid with
-
fillRange
Fills this grid, but only does so within the specified range.- 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.- Parameters:
f- The function of which to apply to each element 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.- Parameters:
f- The function of which to apply to each element of this grid
-
replaceAll
Replaces all instances of the old value with the new value within this grid.- 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.- 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
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-gridg. This is not a direct reference to this grid, and changes in the sub-grid will not be reflected to the original grid.- 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.- Parameters:
rows- The number of rows the resized grid should havecolumns- The number of columns the resized grid should have- Returns:
- A new grid with the specified dimensions
-
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.- Returns:
- The transpose of this grid
-
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.- 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
Applies the provided mapper functionfto every element of this grid, then returns a new double grid whose values are populated from that of the provided function's return values.- Parameters:
f- The function of which to apply to each element of this grid- Returns:
- The resulting double grid
-
mapToFloat
Applies the provided mapper functionfto every element of this grid, then returns a new float grid whose values are populated from that of the provided function's return values.- Parameters:
f- The function of which to apply to each element of this grid- Returns:
- The resulting float grid
-
mapToLong
Applies the provided mapper functionfto every element of this grid, then returns a new long grid whose values are populated from that of the provided function's return values.- Parameters:
f- The function of which to apply to each element of this grid- Returns:
- The resulting long grid
-
mapToInt
Applies the provided mapper functionfto every element of this grid, then returns a new integer grid whose values are populated from that of the provided function's return values.- Parameters:
f- The function of which to apply to each element of this grid- Returns:
- The resulting integer grid
-
merge
@Nonnull <F,G> Grid<G> merge(@Nonnull Grid<F> g, @Nonnull BiFunction<? super E, ? super F, throws IllegalArgumentExceptionG> 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.- Type Parameters:
F- The type of element to merge this grid withG- The type of element to merge the two grids to- 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
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.- Returns:
- The array representation of this grid
-
stream
Returns a stream whose source is the elements of this grid.- Returns:
- A stream whose source is the elements of this grid
- See Also:
-
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. -
equals
Checks for equality between this grid and the provided objectobj. -
toString
Serializes this grid into a string.
-