Game.Rendering.Utilities.HeapBlock
Assembly: Assembly-CSharp
Namespace: Game.Rendering.Utilities
Type: struct
Base: System.ValueType (implements IComparable
Summary:
HeapBlock is a simple value-type representing a contiguous range on a heap or buffer using inclusive begin and exclusive end indices (both ulong). It exposes quick helpers for length and emptiness checks, a factory to create a block by size, and basic ordering/equality semantics based solely on the begin field. Note: equality and comparison are based only on the begin value — two blocks with the same begin but different end are considered equal by Equals and CompareTo, which may be surprising and should be considered when using HeapBlock in collections or sets.
Fields
-
public ulong begin
Represents the starting index of the block (inclusive). Used by CompareTo and Equals. Changing this field changes ordering/equality behaviour. -
public ulong end
Represents the ending index of the block (exclusive). The Length property is computed as end - begin. Beware of overflow if end < begin (since values are unsigned, subtraction will wrap).
Properties
-
public ulong Length { get; }
Returns the length of the block as end - begin. Because both are ulongs, if end < begin this will underflow and produce a large value. Use care to ensure begin <= end. -
public bool Empty { get; }
True when Length == 0 (i.e., end == begin). This is a quick check to determine if the block contains any bytes/elements.
Constructors
public HeapBlock(ulong begin, ulong end)
Creates a HeapBlock with the given begin (inclusive) and end (exclusive). No validation is performed; begin may be greater than end which will affect Length calculation (causing underflow).
Methods
-
public static HeapBlock OfSize(ulong begin, ulong size)
Factory that creates a HeapBlock starting at begin and spanning size elements (end = begin + size). Note: addition may overflow/wrap if begin + size exceeds ulong.MaxValue. Consider validating size or using checked arithmetic if overflow must be detected. -
public int CompareTo(HeapBlock other)
Implements IComparable. Compares this block to other by comparing the begin fields only (begin.CompareTo(other.begin)). This provides ordering by start position, but does NOT consider end — blocks with identical begins are considered equal in ordering even if their ends differ. -
public bool Equals(HeapBlock other)
Implements IEquatable. Returns true if CompareTo(other) == 0 — i.e., equality is determined solely by the begin value. Two blocks that start at the same position but have different lengths will be treated as equal by this method.
Remarks: - The DebuggerDisplay attribute displays the block as "(begin, end), Length = {Length}" in debuggers. - Because CompareTo/Equals use only begin, do not rely on Equals to determine full-range equality. If full-range equality is required, compare both begin and end manually.
Usage Example
// Create a block from 100 to 200
var b1 = new HeapBlock(100u, 200u);
Console.WriteLine(b1.Length); // 100
Console.WriteLine(b1.Empty); // False
// Create a block by size
var b2 = HeapBlock.OfSize(300u, 50u); // begin=300, end=350
// Sorting by begin
var arr = new HeapBlock[] { b2, b1 };
Array.Sort(arr); // sorts by begin (100 then 300)
// Equality is based on begin only
var b3 = new HeapBlock(100u, 150u);
Console.WriteLine(b1.Equals(b3)); // True (same begin 100, different end)
// If you need to check both begin and end:
bool sameRange = (b1.begin == b3.begin) && (b1.end == b3.end); // False