using System;
using System.Collections;
using System.Collections.Generic;
namespace Capnp
{
///
/// ListDeserializer specialization for a List(Bool).
///
public class ListOfBitsDeserializer: ListDeserializer, IReadOnlyList
{
readonly bool _defaultValue;
internal ListOfBitsDeserializer(in DeserializerState context, bool defaultValue) :
base(context)
{
_defaultValue = defaultValue;
}
///
/// Always ListKind.ListOfBits
///
public override ListKind Kind => ListKind.ListOfBits;
///
/// Gets the element at given index.
///
/// Element index
/// Element value
/// is out of range.
public bool this[int index]
{
get
{
if (index < 0 || index >= Count)
throw new IndexOutOfRangeException();
int wi = index / 64;
int bi = index % 64;
return ((State.CurrentSegment[State.Offset + wi] >> bi) & 1) !=
(_defaultValue ? 1u : 0);
}
}
IEnumerable Enumerate()
{
for (int i = 0; i < Count; i++)
yield return this[i];
}
///
/// Implements
///
///
public IEnumerator GetEnumerator()
{
return Enumerate().GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
///
/// Return this
///
public override IReadOnlyList CastBool() => this;
///
/// Always throws since it is not intended to represent a list of bits differently.
///
public override IReadOnlyList Cast(Func cons)
{
throw new NotSupportedException("Cannot cast a list of bits to anything else");
}
}
}