Class BitArrayView
A grid view that wraps a C# BitArray into a settable grid view of boolean values.
Inherited Members
Namespace: SadRogue.Primitives.GridViews
Assembly: TheSadRogue.Primitives.dll
Syntax
public sealed class BitArrayView : SettableGridView1DIndexBase<bool>, ISettableGridView<bool>, IGridView<bool>, ICloneable, IMatchable<BitArrayView>
Remarks
This grid view con be useful to represent a region or area of a 2d grid where points are either "on" or "off". HashSet<Point> can work for this purpose, but hashing can be slow. bool[] or ArrayView<bool> are other options, but this class uses approximately 8x less memory than those options, and is only very slightly slower (less than 0.5ns) in terms of index access. The Fill operation is actually much faster than the corresponding operation for a boolean array, which can make it a very useful alternative as a "visited" array when iterating over a grid.
Constructors
BitArrayView(BitArray, Int32)
Constructor. Takes an existing 1D array to use as the underlying array, and the width of the 2D grid represented by that array.
Declaration
public BitArrayView(BitArray existingArray, int width)
Parameters
Type | Name | Description |
---|---|---|
BitArray | existingArray | Existing 1D array to use as the underlying array. |
Int32 | width | The width of the 2D grid represented by |
BitArrayView(Int32, Int32)
Constructor. Takes width and height of array to create.
Declaration
public BitArrayView(int width, int height)
Parameters
Type | Name | Description |
---|---|---|
Int32 | width | Width of array. |
Int32 | height | Height of array. |
Properties
Height
The height of the grid being represented.
Declaration
public override int Height { get; }
Property Value
Type | Description |
---|---|
Int32 |
Overrides
Item[Int32]
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 bool this[int index1D] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
Int32 | index1D | 1D index of location to get/set the "value" for. |
Property Value
Type | Description |
---|---|
Boolean | The "value" associated with the given 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
Clone()
Performs deep copy of bit-array view.
Declaration
public object Clone()
Returns
Type | Description |
---|---|
Object | The cloned BitArrayView. |
Fill(Boolean)
Sets every location within the grid view to the given value.
Declaration
public override void Fill(bool value)
Parameters
Type | Name | Description |
---|---|---|
Boolean | value |
Overrides
Matches(BitArrayView)
Compares the current BitArrayView to the one given.
Declaration
public bool Matches(BitArrayView other)
Parameters
Type | Name | Description |
---|---|---|
BitArrayView | other |
Returns
Type | Description |
---|---|
Boolean | True if the given BitArrayView references the same underlying bit-array, false otherwise. |
ToBitArray()
Converts to BitArray, without copying the values. Typically using this method is unnecessary and you can use the implicit conversion defined for this type instead.
Declaration
public BitArray ToBitArray()
Returns
Type | Description |
---|---|
BitArray | The underlying BitArray data as a 1D array. |
ToString()
Returns a string representation of the grid values.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
String | A string representation of the grid values. |
ToString(Func<Boolean, String>)
Returns a string representation of the grid values, using elementStringifier
to determine what string represents each value.
Declaration
public string ToString(Func<bool, string> elementStringifier)
Parameters
Type | Name | Description |
---|---|---|
Func<Boolean, String> | elementStringifier | Function determining the string representation of each value. |
Returns
Type | Description |
---|---|
String | A string representation of the grid values. |
ToString(Int32, Func<Boolean, String>)
Returns a string representation of the grid values using the given parameters.
Declaration
public string ToString(int fieldSize, Func<bool, string> elementStringifier = null)
Parameters
Type | Name | Description |
---|---|---|
Int32 | fieldSize | The size of the field to give each value. A positive-number right-aligns the text within the field, while a negative number left-aligns the text. |
Func<Boolean, String> | elementStringifier | Function to use to convert each value to a string. null defaults to the ToString function of type T. |
Returns
Type | Description |
---|---|
String | A string representation of the grid values. |
Remarks
Each element will have spaces added to cause it to take up exactly
fieldSize
characters, provided fieldSize
is less than the length of the value's string representation.
Operators
Implicit(BitArrayView to BitArray)
Allows implicit conversion to BitArray. Does not copy the underlying values.
Declaration
public static implicit operator BitArray(BitArrayView arrayView)
Parameters
Type | Name | Description |
---|---|---|
BitArrayView | arrayView | BitArrayView to convert. |
Returns
Type | Description |
---|---|
BitArray |