2024-04-06 13:46:34 +02:00
|
|
|
using System.Collections;
|
|
|
|
|
2024-04-09 22:38:56 +02:00
|
|
|
namespace TanksServer.ServicePointDisplay;
|
2024-04-06 13:46:34 +02:00
|
|
|
|
2024-04-10 22:39:33 +02:00
|
|
|
internal sealed class FixedSizeBitRowView(Memory<byte> data) : IList<bool>
|
2024-04-06 13:46:34 +02:00
|
|
|
{
|
|
|
|
public int Count => data.Length * 8;
|
|
|
|
public bool IsReadOnly => false;
|
|
|
|
|
2024-04-06 16:38:26 +02:00
|
|
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
2024-04-09 22:38:56 +02:00
|
|
|
|
2024-04-06 13:46:34 +02:00
|
|
|
public IEnumerator<bool> GetEnumerator()
|
|
|
|
{
|
|
|
|
return Enumerable().GetEnumerator();
|
|
|
|
|
|
|
|
IEnumerable<bool> Enumerable()
|
|
|
|
{
|
|
|
|
for (var i = 0; i < Count; i++)
|
|
|
|
yield return this[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
{
|
|
|
|
var span = data.Span;
|
|
|
|
for (var i = 0; i < data.Length; i++)
|
|
|
|
span[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void CopyTo(bool[] array, int arrayIndex)
|
|
|
|
{
|
|
|
|
for (var i = 0; i < Count && i + arrayIndex < array.Length; i++)
|
|
|
|
array[i + arrayIndex] = this[i];
|
|
|
|
}
|
|
|
|
|
2024-04-09 22:38:56 +02:00
|
|
|
private (int byteIndex, int bitInByteIndex) GetIndexes(int bitIndex)
|
2024-04-06 13:46:34 +02:00
|
|
|
{
|
|
|
|
var byteIndex = bitIndex / 8;
|
|
|
|
var bitInByteIndex = 7 - bitIndex % 8;
|
2024-04-09 22:38:56 +02:00
|
|
|
if (byteIndex >= data.Length)
|
|
|
|
{
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(bitIndex),
|
|
|
|
$"accessing this bit field at position {bitIndex} would result in an access to byte " +
|
|
|
|
$"{byteIndex} but byte length is {data.Length}");
|
|
|
|
}
|
|
|
|
|
2024-04-06 13:46:34 +02:00
|
|
|
return (byteIndex, bitInByteIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool this[int bitIndex]
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
var (byteIndex, bitInByteIndex) = GetIndexes(bitIndex);
|
|
|
|
var bitInByteMask = (byte)(1 << bitInByteIndex);
|
|
|
|
return (data.Span[byteIndex] & bitInByteMask) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
var (byteIndex, bitInByteIndex) = GetIndexes(bitIndex);
|
|
|
|
var bitInByteMask = (byte)(1 << bitInByteIndex);
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
data.Span[byteIndex] |= bitInByteMask;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var withoutBitMask = (byte)(ushort.MaxValue ^ bitInByteMask);
|
|
|
|
data.Span[byteIndex] &= withoutBitMask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-06 16:38:26 +02:00
|
|
|
public void Add(bool item) => throw new NotSupportedException();
|
|
|
|
public bool Contains(bool item) => throw new NotSupportedException();
|
|
|
|
public bool Remove(bool item) => throw new NotSupportedException();
|
|
|
|
public int IndexOf(bool item) => throw new NotSupportedException();
|
|
|
|
public void Insert(int index, bool item) => throw new NotSupportedException();
|
|
|
|
public void RemoveAt(int index) => throw new NotSupportedException();
|
2024-04-09 22:38:56 +02:00
|
|
|
}
|