static IEnumerable <CellMap <T> > GridCells <T>(this GridSpec <T> spec) where T : struct { var bit = 0; var seg = 0; var segbits = spec.CellSize; for (int row = 0, rowbit = 0; row < spec.RowCount; row++) { for (var col = 0; col < spec.ColCount; col++, bit++, rowbit++, segbits--) { if (segbits == 0) { seg++; segbits = spec.CellSize; } var offset = (byte)(spec.CellSize - segbits).Bits; var pos = CellIndex <T> .Define((ushort)seg, offset); yield return(new CellMap <T>(pos.Segment, pos.Offset, pos.LinearIndex, row, col)); } seg++; segbits = spec.CellSize; } }
public Bit this[Index r] { [MethodImpl(Inline)] get { var pos = CellIndex <ulong> .FromIndex((uint)r.Value); return(part <ulong>(pos.Segment).TestBit(pos.Offset)); } }
/// <summary> /// Calculates a canonical bijection from a contiguous sequence of bits onto a contiguous sequence of segments /// </summary> /// <param name="bitcount">The total number of bits to distribute over one or more segments</param> public static CellIndex <T>[] BitMap <T>(BitSize bitcount) where T : struct { var dst = new CellIndex <T> [bitcount]; var capacity = bitsize <T>(); ushort seg = 0; byte offset = 0; for (var i = 0; i < bitcount; i++) { if (i != 0) { if ((i % capacity) == 0) { seg++; offset = 0; } } dst[i] = (seg, offset++); } return(dst); }