Class DiffAwareGridView<T>
A grid view wrapper useful for recording diffs (change-sets) of changes to a grid view, and applying/removing those change-sets of values from the grid view. Only works with grid views of value types.
Inherited Members
Namespace: SadRogue.Primitives.GridViews
Assembly: TheSadRogue.Primitives.dll
Syntax
public class DiffAwareGridView<T> : SettableGridViewBase<T>, ISettableGridView<T>, IGridView<T> where T : struct, ValueType
Type Parameters
Name | Description |
---|---|
T | Type of value in the grid view. Must be a value type. |
Remarks
Generally, this class is useful with values types/primitive types wherein values are completely replaced when they are modified. It allows applying a series of change sets in forward or reverse order; and as such can be extremely useful for debugging or situations where you want to record intermediate states of an algorithm.
Constructors
DiffAwareGridView(ISettableGridView<T>, Boolean)
Constructs a diff-aware grid view that wraps around an existing grid view.
Declaration
public DiffAwareGridView(ISettableGridView<T> baseGrid, bool autoCompress = true)
Parameters
Type | Name | Description |
---|---|---|
ISettableGridView<T> | baseGrid | The grid view whose changes are to be recorded in diffs. |
Boolean | autoCompress | Whether or not to automatically compress diffs when the currently applied diff is changed. |
DiffAwareGridView(Int32, Int32, Boolean)
Constructs a diff-aware grid view, whose base grid will be a new ArrayView<T>.
Declaration
public DiffAwareGridView(int width, int height, bool autoCompress = true)
Parameters
Type | Name | Description |
---|---|---|
Int32 | width | Width of the base grid view that will be created. |
Int32 | height | Height of the base grid view that will be created. |
Boolean | autoCompress | Whether or not to automatically compress diffs when the currently applied diff is changed. |
Fields
AutoCompress
Whether or not to automatically compress diffs when the currently applied diff is changed.
Declaration
public bool AutoCompress
Field Value
Type | Description |
---|---|
Boolean |
Properties
BaseGrid
The grid view whose changes are being recorded in diffs.
Declaration
public IGridView<T> BaseGrid { get; }
Property Value
Type | Description |
---|---|
IGridView<T> |
CurrentDiffIndex
The index of the diff whose ending state is currently reflected in BaseGrid. Returns -1 if none of the diffs in the list have been applied (eg. the grid view is in the state it was in at the DiffAwareGridView<T>'s creation.
Declaration
public int CurrentDiffIndex { get; }
Property Value
Type | Description |
---|---|
Int32 |
Diffs
All diffs recorded for the current grid view, and their changes.
Declaration
public IReadOnlyList<Diff<T>> Diffs { get; }
Property Value
Type | Description |
---|---|
IReadOnlyList<Diff<T>> |
Height
The height of the grid being represented.
Declaration
public override int Height { get; }
Property Value
Type | Description |
---|---|
Int32 |
Overrides
Item[Point]
Interface designed to act as a standardized input/output format that defines minimal required data for algorithms that operate and make changes to a grid of some sort. For a concrete implementation to subclass for custom implementations, see SettableGridViewBase<T>.
Declaration
public override T this[Point pos] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
Point | pos | Location to get/set the value for. |
Property Value
Type | Description |
---|---|
T | The "value" associated with the provided location. |
Overrides
Remarks
See IGridView<T>. This interface serves the same purpose, but for cases when it is also necessary for an algorithm to be able to change the value at each location.
Like IGridView, a number of implementations of this interface to cover common needs are provided. For example, ArrayView<T> defines the interface such that the data is retrieved from and set to an array, and LambdaSettableGridView<T> defines the interface such that arbitrary callbacks are used to retrieve and set the data.
Width
The width of the grid being represented.
Declaration
public override int Width { get; }
Property Value
Type | Description |
---|---|
Int32 |
Overrides
Methods
ApplyNextDiff()
Applies the next recorded diff, or throws exception if there is no future diffs recorded.
Declaration
public void ApplyNextDiff()
ApplyNextDiffOrFinalize()
Convenience method that calls ApplyNextDiff() if there are existing diffs to apply, and FinalizeCurrentDiff() if there are no existing diffs to apply. Returns whether or not an existing diff was applied.
Declaration
public bool ApplyNextDiffOrFinalize()
Returns
Type | Description |
---|---|
Boolean | True if an existing diff is applied, false if a new one was created. |
ClearHistory()
Erase recorded diffs without modifying state of grid view.
Declaration
public void ClearHistory()
FinalizeCurrentDiff()
Finalizes the current diff so that no more changes can be added to it; future changes will create a new diff. Throws exceptions if there are diffs that are not currently applied.
Declaration
public void FinalizeCurrentDiff()
RevertToPreviousDiff()
Reverts the current diff's changes, so that the grid view will be in the state it was in at the end of the previous diff. Throws exception if no diffs are applied.
Declaration
public void RevertToPreviousDiff()
SetBaseline(IGridView<T>)
Sets the baseline values (eg. values before any diffs are recorded) to the values from the given grid view. Only valid to do before any diffs are recorded.
Declaration
public void SetBaseline(IGridView<T> baseline)
Parameters
Type | Name | Description |
---|---|---|
IGridView<T> | baseline | Baseline values to use. Must have same width/height as BaseGrid. |
SetHistory(IEnumerable<Diff<T>>)
Overwrite any history present and replace it with the history given. This does not modify the underlying grid view; the history must be valid with respect to its current state.
Declaration
public void SetHistory(IEnumerable<Diff<T>> history)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<Diff<T>> | history | The history to apply. |
Remarks
Generally, you will not call this function, however it can be useful for serialization and copying histories between objects. Assumes the grid view is in a state that reflects all of the diffs in the history being applied.
SetHistory(IEnumerable<Diff<T>>, Int32)
Overwrite any history present and replace it with the history given. This does not modify the underlying grid view; the history must be valid with respect to its current state.
Declaration
public void SetHistory(IEnumerable<Diff<T>> history, int currentIndex)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<Diff<T>> | history | The history to apply. |
Int32 | currentIndex | The index of the given history that is applied to the BaseGrid. Set to the length of the list - 1 if all diffs have been applied, or -1 if none of them have. |
Remarks
Generally, you will not call this function, however it can be useful for serialization and copying histories between objects.