BitVector640
struct BitVector640
This type effectively stores 640 bool values, but compresses their storage
into ten ulongs, where each bit of the ulong represents a single bool value.
It exposes a public indexer so that the bool values can be accessed as a
standard .NET collection API.
It is used in System.ClientModel and Azure.Core to implement response
classifiers, where each true bit represents a status code that the
classifier considers a success code.
using System.Runtime.CompilerServices;
namespace System.ClientModel.Internal
{
internal struct BitVector640
{
private ulong _bits0;
private ulong _bits1;
private ulong _bits2;
private ulong _bits3;
private ulong _bits4;
private ulong _bits5;
private ulong _bits6;
private ulong _bits7;
private ulong _bits8;
private ulong _bits9;
public bool this[int i] {
[System.Runtime.CompilerServices.IsReadOnly]
get {
int index = i >> 6;
int num = i & 63;
ulong num2 = (ulong)(1 << num);
return (Get(index) & num2) == num2;
}
set {
int index = i >> 6;
int num = i & 63;
ulong mask = (ulong)(1 << num);
Set(index, mask, value);
}
}
[System.Runtime.CompilerServices.IsReadOnly]
private ulong Get(int index)
{
switch (index) {
case 0:
return _bits0;
case 1:
return _bits1;
case 2:
return _bits2;
case 3:
return _bits3;
case 4:
return _bits4;
case 5:
return _bits5;
case 6:
return _bits6;
case 7:
return _bits7;
case 8:
return _bits8;
case 9:
return _bits9;
default:
throw new InvalidOperationException();
}
}
private void Set(int index, ulong mask, bool value)
{
if (!value) {
switch (index) {
case 0:
_bits0 &= ~mask;
break;
case 1:
_bits1 &= ~mask;
break;
case 2:
_bits2 &= ~mask;
break;
case 3:
_bits3 &= ~mask;
break;
case 4:
_bits4 &= ~mask;
break;
case 5:
_bits5 &= ~mask;
break;
case 6:
_bits6 &= ~mask;
break;
case 7:
_bits7 &= ~mask;
break;
case 8:
_bits8 &= ~mask;
break;
case 9:
_bits9 &= ~mask;
break;
default:
throw new InvalidOperationException();
}
} else {
switch (index) {
case 0:
_bits0 |= mask;
break;
case 1:
_bits1 |= mask;
break;
case 2:
_bits2 |= mask;
break;
case 3:
_bits3 |= mask;
break;
case 4:
_bits4 |= mask;
break;
case 5:
_bits5 |= mask;
break;
case 6:
_bits6 |= mask;
break;
case 7:
_bits7 |= mask;
break;
case 8:
_bits8 |= mask;
break;
case 9:
_bits9 |= mask;
break;
default:
throw new InvalidOperationException();
}
}
}
}
}