Class ScyllaHexGrid<TCell>.SynchronizedScyllaHexGrid
Thread-safe wrapper around ScyllaHexGrid<TCell> that serializes all read and write operations using a single monitor lock. Obtained via AsSynchronized(object) or the Synchronized(object) builder option.
Inherited Members
Namespace: Scylla.Core.Structures
Assembly: ScyllaCore.dll
Syntax
public sealed class ScyllaHexGrid<TCell>.SynchronizedScyllaHexGrid : IScyllaGrid<HexCoord, TCell>, IScyllaCollection
Remarks
Layout parameters (Width, Height, QMin,
RMin, and the internal layout) are cached at construction time and
read lock-free. Mutation methods (Fill(TCell), this[coord] = value,
Clear()) and all read methods take the lock for every call.
Individual property reads are each independently locked. Check-then-act sequences (e.g., IsInBounds(HexCoord) followed by the indexer) are not atomic and remain subject to TOCTOU races in concurrent scenarios. Use TryGet(HexCoord, out TCell) or TrySet(HexCoord, TCell) for atomic conditional access.
Constructors
SynchronizedScyllaHexGrid(ScyllaHexGrid<TCell>, object)
Initializes a new synchronized wrapper around the specified grid.
Declaration
public SynchronizedScyllaHexGrid(ScyllaHexGrid<TCell> inner, object syncRoot)
Parameters
| Type | Name | Description |
|---|---|---|
| ScyllaHexGrid<TCell> | inner | The grid to wrap. Must not be null. |
| object | syncRoot | The lock object to use for synchronization. If null, a new private lock object is created. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
Properties
Capabilities
Gets the feature and capability flags supported by this collection instance.
Declaration
public ScyllaCollectionCapabilities Capabilities { get; }
Property Value
| Type | Description |
|---|---|
| ScyllaCollectionCapabilities | A bitfield of ScyllaCollectionCapabilities flags describing which optional operations the concrete instance supports. The value None indicates that only the base contract (Count, IsEmpty, Clear()) is available. |
Remarks
Use Capabilities for feature detection in place of type-casting. For example:
if ((collection.Capabilities & ScyllaCollectionCapabilities.SupportsPeek) != 0)
{
// safe to call TryPeek through IScyllaCollection<T>
}
Synchronized wrappers cache the capability flags of their inner collection at construction time, so the value returned is stable and does not require synchronization.
See Also
CellCount
Gets the number of populated (logically active) cells in the grid.
Declaration
public int CellCount { get; }
Property Value
| Type | Description |
|---|---|
| int | For dense grids, this always equals Count - every cell slot is pre-allocated and considered populated regardless of its stored value. The value is fixed at construction and never changes. For sparse grids, this equals the number of cells that have been explicitly written via TrySet(TCoord, TCell) and not yet removed. The value may be zero on a freshly created or cleared sparse grid. |
See Also
Count
Gets the number of items currently contained in the collection.
Declaration
public int Count { get; }
Property Value
| Type | Description |
|---|---|
| int | A non-negative integer representing the current element count.
Returns |
Remarks
On unsynchronized collections, Count is not thread-safe and may return a stale
value when accessed concurrently. On synchronized wrappers, reading Count is
protected by the internal lock.
Height
Gets the grid height.
Declaration
public int Height { get; }
Property Value
| Type | Description |
|---|---|
| int |
IsEmpty
Always false. A dense grid pre-allocates all cells at construction time and is never considered logically empty.
Declaration
public bool IsEmpty { get; }
Property Value
| Type | Description |
|---|---|
| bool |
this[HexCoord]
Gets or sets the cell value at the specified coordinate.
Declaration
public TCell this[HexCoord coord] { get; set; }
Parameters
| Type | Name | Description |
|---|---|---|
| HexCoord | coord | The hex coordinate. |
Property Value
| Type | Description |
|---|---|
| TCell |
Exceptions
| Type | Condition |
|---|---|
| IndexOutOfRangeException | Thrown when the coordinate is out of bounds. |
QMin
Gets the minimum Q value in the grid bounds.
Declaration
public int QMin { get; }
Property Value
| Type | Description |
|---|---|
| int |
RMin
Gets the minimum R value in the grid bounds.
Declaration
public int RMin { get; }
Property Value
| Type | Description |
|---|---|
| int |
SyncRoot
Gets the synchronization root object used to externally coordinate multi-step operations with this collection.
Declaration
public object SyncRoot { get; }
Property Value
| Type | Description |
|---|---|
| object | A non-null object suitable for use with |
Remarks
When this collection is a synchronized wrapper, SyncRoot must be non-null and
stable for the entire lifetime of the wrapper. All internal operations on the wrapper
must lock on this same object. This allows callers to perform atomic multi-step
sequences:
lock (collection.SyncRoot)
{
if (!collection.IsEmpty)
typedCollection.TryRemove(out var item);
}
For unsynchronized collections (SyncRoot == null), callers are responsible for
supplying and consistently applying their own external synchronization mechanism.
A null SyncRoot does not mean the collection is thread-safe;
consult ThreadSafety for the authoritative guarantee.
See Also
ThreadSafety
Gets the thread-safety guarantees provided by this collection instance.
Declaration
public ScyllaCollectionThreadSafety ThreadSafety { get; }
Property Value
| Type | Description |
|---|---|
| ScyllaCollectionThreadSafety | A ScyllaCollectionThreadSafety value indicating whether the collection is unsynchronized, synchronized via a lock, or safe for lock-free concurrent access. Most core implementations return Unsynchronized. Synchronized wrappers return Synchronized. |
Remarks
Always check this property (or SyncRoot) before assuming a collection is
safe for concurrent use. Do not rely solely on the absence of a non-null SyncRoot
to infer thread safety; use this property as the authoritative source.
See Also
Width
Gets the grid width.
Declaration
public int Width { get; }
Property Value
| Type | Description |
|---|---|
| int |
Methods
Clear()
Removes all items from the collection and resets it to an empty state.
Declaration
public void Clear()
Remarks
Implementations that store reference-type elements should null out internal slots after
clearing to avoid retaining object references and preventing garbage collection.
After Clear returns, Count must be 0 and
IsEmpty must be true.
On synchronized wrappers this operation is protected by the collection's internal lock. On unsynchronized collections, callers are responsible for external coordination.
Contains(HexCoord)
Determines whether the specified coordinate is addressable and populated in this grid.
Declaration
public bool Contains(HexCoord coord)
Parameters
| Type | Name | Description |
|---|---|---|
| HexCoord | coord | The coordinate to test. |
Returns
| Type | Description |
|---|---|
| bool |
For dense grids, returns
For sparse grids, returns |
Remarks
This method is equivalent to checking whether TryGet(TCoord, out TCell) would return
true for the same coordinate. Use it when you only need the existence check
and do not need the value.
See Also
Fill(TCell)
Fills all cells with the specified value.
Declaration
public void Fill(TCell value)
Parameters
| Type | Name | Description |
|---|---|---|
| TCell | value | The value to fill with. |
GetNeighborsNonAlloc(HexCoord, Span<HexCoord>)
Writes the in-bounds neighbors of the specified coordinate into the provided buffer.
Declaration
public int GetNeighborsNonAlloc(HexCoord coord, Span<HexCoord> buffer)
Parameters
| Type | Name | Description |
|---|---|---|
| HexCoord | coord | The coordinate to find neighbors for. |
| Span<HexCoord> | buffer | A span of at least 6 elements to receive neighbor coordinates. |
Returns
| Type | Description |
|---|---|
| int | The number of neighbors written (up to 6). |
GridToWorld(HexCoord)
Converts a hex coordinate to world-space position using the grid's layout.
Declaration
public Vector2 GridToWorld(HexCoord coord)
Parameters
| Type | Name | Description |
|---|---|---|
| HexCoord | coord | The hex coordinate. |
Returns
| Type | Description |
|---|---|
| Vector2 | The world-space center of the hex. |
IsInBounds(HexCoord)
Determines whether the specified coordinate is within the grid bounds.
Declaration
public bool IsInBounds(HexCoord coord)
Parameters
| Type | Name | Description |
|---|---|---|
| HexCoord | coord | The hex coordinate. |
Returns
| Type | Description |
|---|---|
| bool | True if the coordinate is within bounds. |
TryGet(HexCoord, out TCell)
Attempts to retrieve the value stored at the specified coordinate without throwing on failure.
Declaration
public bool TryGet(HexCoord coord, out TCell value)
Parameters
| Type | Name | Description |
|---|---|---|
| HexCoord | coord | The grid coordinate to look up. |
| TCell | value | When this method returns |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
Prefer this method over direct indexer access when the coordinate's validity is not guaranteed, as it avoids the cost of exception handling on out-of-bounds access. For dense grids, "within bounds" means the coordinate maps to a valid flat-array index. For sparse grids, "within bounds" additionally requires that the cell has been set.
See Also
TrySet(HexCoord, TCell)
Attempts to store a value at the specified coordinate without throwing on failure.
Declaration
public bool TrySet(HexCoord coord, TCell value)
Parameters
| Type | Name | Description |
|---|---|---|
| HexCoord | coord | The grid coordinate to write to. |
| TCell | value | The value to store at |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
For dense grids, this writes to the pre-allocated cell at
coord and returns false if the coordinate falls outside
the grid's dimensions.
For sparse grids, this inserts or updates the entry at
coord and returns false only when the grid is bounded
and coord falls outside the defined bounds. On unbounded sparse
grids this method always returns true.
A successful call increments CellCount on sparse grids when
coord is newly written for the first time.
See Also
WorldToGrid(Vector2)
Converts a world-space position to a hex coordinate using the grid's layout.
Declaration
public HexCoord WorldToGrid(Vector2 worldPos)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | worldPos | The world-space position. |
Returns
| Type | Description |
|---|---|
| HexCoord | The nearest hex coordinate. |