Bytes final
final class Bytes extends BitVectorA leaf BitVector node backed by a contiguous ByteVector.
This is the concrete representation that stores actual bit data. Most BitVector operations eventually compact down to a Bytes instance.
Inheritance
Object → BitVector → Bytes
Constructors
Bytes()
Bytes(ByteVector underlying, int size)Creates a Bytes from the given underlying byte vector and a size in bits.
Implementation
Bytes(this.underlying, this.size);Properties
bytes no setter inherited
ByteVector get bytesReturns the underlying bytes of this bit vector.
Inherited from BitVector.
Implementation
ByteVector get bytes => toByteVector();hashCode no setter inherited
int get hashCodeThe hash code for this object.
A hash code is a single integer which represents the state of the object that affects operator == comparisons.
All objects have hash codes. The default hash code implemented by Object represents only the identity of the object, the same way as the default operator == implementation only considers objects equal if they are identical (see identityHashCode).
If operator == is overridden to use the object state instead, the hash code must also be changed to represent that state, otherwise the object cannot be used in hash based data structures like the default Set and Map implementations.
Hash codes must be the same for objects that are equal to each other according to operator ==. The hash code of an object should only change if the object changes in a way that affects equality. There are no further requirements for the hash codes. They need not be consistent between executions of the same program and there are no distribution guarantees.
Objects that are not equal are allowed to have the same hash code. It is even technically allowed that all instances have the same hash code, but if clashes happen too often, it may reduce the efficiency of hash-based data structures like HashSet or HashMap.
If a subclass overrides hashCode, it should override the operator == operator as well to maintain consistency.
Inherited from BitVector.
Implementation
@override
int get hashCode => Object.hash(size, bytes);head no setter inherited
bool get headReturns the first bit of this vector or throws if vector is emtpy.
Inherited from BitVector.
Implementation
bool get head => get(0);headOption no setter inherited
Option<bool> get headOptionReturns the first bit of this vector or None if vector is emtpy.
Inherited from BitVector.
Implementation
Option<bool> get headOption => lift(0);init no setter inherited
BitVector get initReturns a vector of all bits in this vector except the last bit.
Inherited from BitVector.
Implementation
BitVector get init => dropRight(1);isEmpty no setter inherited
bool get isEmptyReturns true if this vector has no bits.
Inherited from BitVector.
Implementation
bool get isEmpty => sizeLessThan(1);last no setter inherited
bool get lastReturns the last bit in this vector or throws if vector is empty.
Inherited from BitVector.
Implementation
bool get last => get(size - 1);lastOption no setter inherited
Option<bool> get lastOptionReturns the last bit in this vector or returns None if vector is empty.
Inherited from BitVector.
Implementation
Option<bool> get lastOption => lift(size - 1);length no setter inherited
int get lengthAlias for size.
Inherited from BitVector.
Implementation
int get length => size;nonEmpty no setter inherited
bool get nonEmptyReturns true if this vector has a non-zero number of bits.
Inherited from BitVector.
Implementation
bool get nonEmpty => !isEmpty;not no setter inherited
BitVector get notReturns a bitwise complement of this BitVector.
Inherited from BitVector.
Implementation
BitVector get not => _mapBytes((b) => b.not);reverse no setter inherited
BitVector get reverseReturns a bit vector with the bits in reverse order.
Inherited from BitVector.
Implementation
BitVector get reverse => BitVector.fromByteVector(
compact().underlying.reverse.map(_reverseBitsInByte),
).drop(8 - _validBitsInLastByte(size));runtimeType no setter inherited
Type get runtimeTypeA representation of the runtime type of the object.
Inherited from Object.
Implementation
external Type get runtimeType;size final
final int sizeReturns number of bits in this vector.
Implementation
@override
final int size;tail no setter inherited
BitVector get tailReturns a vector of all bits in this vector except the first bit.
Inherited from BitVector.
Implementation
BitVector get tail => drop(1);underlying final
final ByteVector underlyingThe underlying byte storage.
Implementation
final ByteVector underlying;Methods
acquire() inherited
Returns a vector whose contents are the results of taking the first n bits of this vector.
If this vector does not contain at least n bits, an error message is returned.
Inherited from BitVector.
Implementation
Either<String, BitVector> acquire(int n) => Either.cond(
() => sizeGreaterThanOrEqual(n),
() => take(n),
() => 'cannot acquire $n bits from a vector that contains $size bits',
);acquireThen() inherited
R acquireThen<R>(int n, R Function(String) err, R Function(BitVector) f)Like aquire, but immediately consumes the Either via the pair of functions err and f.
Inherited from BitVector.
Implementation
R acquireThen<R>(
int n,
Function1<String, R> err,
Function1<BitVector, R> f,
) =>
sizeGreaterThanOrEqual(n)
? f(take(n))
: err('cannot acquire $n bits from a vector that contains $size bits');align() override
Bytes align()Produce a single flat Bytes by interpreting any non-byte-aligned appends or drops. Unlike compact, the underlying ByteVector is not necessarily copied.
Implementation
@override
Bytes align() => this;and() inherited
Returns a bitwise AND of this BitVector with the specified BitVector.
Inherited from BitVector.
Implementation
BitVector and(BitVector other) => _zipBytesWith(other, (a, b) => a & b);append() inherited
BitVector append(bool b)Returns a new vector with the specified bit appended.
Inherited from BitVector.
Implementation
BitVector append(bool b) => concat(BitVector.bit(b));bufferBy() inherited
BitVector bufferBy([int chunkSizeInBits = 8192])Returns a buffered version of this bit vector that amortizes appends by collecting them into chunks of chunkSizeInBits bits.
Inherited from BitVector.
Implementation
BitVector bufferBy([int chunkSizeInBits = 8192]) {
switch (this) {
case final _Buffer b:
if (b.lastChunk.length * 8 >= chunkSizeInBits) {
return b;
} else {
return b.unbuffer().bufferBy(chunkSizeInBits);
}
default:
return _Buffer(this, Uint8List((chunkSizeInBits + 7) ~/ 8), 0, _BufferState(0));
}
}call() inherited
bool call(int n)Alias for get.
Inherited from BitVector.
Implementation
bool call(int n) => get(n);checkBounds()
void checkBounds(int n)Implementation
void checkBounds(int n) {
if (!sizeGreaterThan(n)) outOfBounds(n);
}clear() inherited
BitVector clear(int n)Returns a new bit vector with the nth bit low (and all other bits unmodified).
Inherited from BitVector.
Implementation
BitVector clear(int n) => update(n, false);combine()
Implementation
Bytes combine(Bytes other) {
final nInvalidBits = invalidBits();
if (isEmpty) {
return other;
} else if (other.isEmpty) {
return this;
} else if (nInvalidBits == 0) {
return _toBytes(underlying.concat(other.underlying), size + other.size);
} else {
final bytesCleared = _clearUnneededBits(size, underlying);
final hi = bytesCleared.get(bytesCleared.size - 1);
final lo =
(((other.underlying.head & _topNBits(nInvalidBits)) & 0x000000ff) >>>
_validBitsInLastByte(size)) &
0xff;
final updatedOurBytes = bytesCleared.update(bytesCleared.size - 1, (hi | lo) & 0xff);
final updatedOtherBytes = other.drop(nInvalidBits).bytes;
return _toBytes(
updatedOurBytes.concat(updatedOtherBytes),
size + other.size,
);
}
}compact() inherited
Bytes compact()Compacts this bit vector into a single contiguous Bytes node.
If this vector already consists of a single chunk, the underlying byte vector may be returned without copying. Use copy when a guaranteed fresh copy is required.
Inherited from BitVector.
Implementation
Bytes compact() {
if (_bytesNeededForBits(size) > Integer.maxValue) {
throw ArgumentError('cannot compact bit vector of size ${size.toDouble() / 8 / 1e9} GB');
}
IVector<Bytes> go(IList<BitVector> b, IVector<Bytes> acc) {
var currentB = b;
var currentAcc = acc;
while (currentB.nonEmpty) {
final head = currentB.head;
final rem = currentB.tail;
switch (head) {
case final _Suspend s:
currentB = rem.prepended(s.underlying);
case final Bytes bytesNode:
currentB = rem;
currentAcc = currentAcc.appended(bytesNode);
case final _Drop d:
currentB = rem;
currentAcc = currentAcc.appended(d.interpretDrop());
case _Append(left: final l, right: final r):
currentB = rem.prepended(r).prepended(l);
case final _Chunks c:
currentB = rem.prepended(c.chunks.right).prepended(c.chunks.left);
case final _Buffer b:
currentB = rem;
currentAcc = currentAcc.appended(b.unbuffer().align());
}
}
return currentAcc;
}
switch (this) {
case final Bytes bs:
final b2 = bs.underlying.compact();
return b2 == bs.underlying ? bs : Bytes(b2, bs.size);
case final _Drop d:
final bs = d.interpretDrop();
final b2 = bs.underlying.compact();
return b2 == bs.underlying ? bs : Bytes(b2, bs.size);
default:
final balanced = _reduceBalanced(
go(ilist([this]), IVector.empty()),
(bv) => bv.size,
(x, y) => x.combine(y),
);
return Bytes(balanced.underlying.compact(), balanced.size);
}
}compareTo() inherited
int compareTo(BitVector that)Compares this object to another object.
Returns a value like a Comparator when comparing this to other. That is, it returns a negative integer if this is ordered before other, a positive integer if this is ordered after other, and zero if this and other are ordered together.
The other argument must be a value that is comparable to this object.
Inherited from BitVector.
Implementation
@override
int compareTo(BitVector that) {
if (this == that) {
return 0;
} else {
final thisLength = length;
final thatLength = that.length;
final commonLength = min(thisLength, thatLength);
var i = 0;
while (i < commonLength) {
final thisI = get(i);
final cmp =
thisI == that.get(i)
? 0
: thisI
? 1
: -1;
if (cmp != 0) return cmp;
i = i + 1;
}
if (thisLength < thatLength) {
return -1;
} else if (thisLength > thatLength) {
return 1;
} else {
return 0;
}
}
}concat() inherited
Returns a new bit vector representing this vector's contents followed by the specified vector's contents.
Inherited from BitVector.
Implementation
BitVector concat(BitVector b2) {
return isEmpty ? b2 : _Chunks(_Append(this, b2));
}consume() inherited
Consumes the first n bits of this vector and decodes them with the specified function, resulting in a vector of the remaining bits and the decoded value. If this vector does not have n bits or an error occurs while decoding, an error is returned instead.
Inherited from BitVector.
Implementation
Either<String, (BitVector, A)> consume<A>(
int n,
Function1<BitVector, Either<String, A>> decode,
) => acquire(n).flatMap((toDecode) => decode(toDecode).map((decoded) => (drop(n), decoded)));consumeThen() inherited
If this vector has at least n bits, returns f(take(n),drop(n)), otherwise calls err with a meaningful error message. This function can be used to avoid intermediate allocations of Either objects when using acquire or consume directly.
Inherited from BitVector.
Implementation
R consumeThen<R>(
int n,
Function1<String, R> err,
Function2<BitVector, BitVector, R> f,
) {
if (sizeGreaterThanOrEqual(n)) {
return f(take(n), drop(n)); // todo unsafeTake, unsafeDrop
} else {
return err("cannot acquire $n bits from a vector that contains $size bits");
}
}containsSlice() inherited
bool containsSlice(BitVector slice)Determines if the specified slice is in this vector.
Inherited from BitVector.
Implementation
bool containsSlice(BitVector slice) => indexOfSlice(slice) >= 0;copy() inherited
Bytes copy()Return a BitVector with the same contents as this, but based off a single flat ByteVector. This function is guaranteed to copy all the bytes in this BitVector, unlike compact, which may no-op if this BitVector already consists of a single ByteVector chunk.
Inherited from BitVector.
Implementation
Bytes copy() => switch (this) {
final Bytes b => Bytes(b.underlying.copy(), b.size),
_ => compact(),
};drop() override
BitVector drop(int n)Returns a vector of all bits in this vector except the first n bits.
The resulting vector's size is 0 max (size - n).
Implementation
@override
BitVector drop(int n) {
if (n >= size) {
return BitVector.empty;
} else if (n <= 0) {
return this;
} else if (n % 8 == 0) {
return Bytes(underlying.drop(n ~/ 8), size - n);
} else {
return _Drop(this, n);
}
}dropRight() inherited
BitVector dropRight(int n)Returns a vector of all bits in this vector except the last n bits.
The resulting vector's size is 0 max (size - n).
Inherited from BitVector.
Implementation
BitVector dropRight(int n) {
if (n <= 0) {
return this;
} else if (n >= size) {
return BitVector.empty;
} else {
return take(size - n);
}
}dropWhile() inherited
BitVector dropWhile(bool Function(bool) f)Drops the longest prefix of bits that satisfy f.
Inherited from BitVector.
Implementation
BitVector dropWhile(Function1<bool, bool> f) {
var toDrop = 0;
while (toDrop < size && f(get(toDrop))) {
toDrop += 1;
}
return drop(toDrop);
}endsWith() inherited
bool endsWith(BitVector b)Returns true if this bit vector ends with the specified vector.
Inherited from BitVector.
Implementation
bool endsWith(BitVector b) => takeRight(b.size) == b;force() inherited
BitVector force()Forces any Suspend nodes in this BitVector and ensures the tree is balanced.
Inherited from BitVector.
Implementation
BitVector force() {
BitVector go(IVector<BitVector> cont) {
var currentCont = cont;
while (currentCont.nonEmpty) {
final cur = currentCont.head;
final tail = currentCont.tail;
switch (cur) {
case final Bytes b:
return tail.foldLeft(b, (a, b) => a.concat(b));
case _Append(left: final l, right: final r):
currentCont = tail.prepended(r).prepended(l);
case final _Drop d:
return tail.foldLeft(d, (a, b) => a.concat(b));
case final _Suspend s:
currentCont = tail.prepended(s.underlying);
case final _Chunks c:
currentCont = tail.prepended(c.chunks);
case final _Buffer b:
currentCont = tail.prepended(b.unbuffer());
}
}
return currentCont.foldLeft(BitVector.empty, (a, b) => a.concat(b));
}
return go(ivec([this]));
}get() override
bool get(int n)Returns true if the nth bit is high, false otherwise.
Implementation
@override
bool get(int n) {
checkBounds(n);
return _getBit(underlying.get(n ~/ 8), n % 8);
}getByte() override
int getByte(int n)Returns the nth byte, 0-indexed.
Implementation
@override
int getByte(int n) {
if (n < underlying.size - 1) {
return underlying.get(n);
} else {
// last byte may have some garbage bits, clear these out
final valid = 8 - invalidBits();
return (underlying.get(n) & _topNBits(valid)) & 0xff;
}
}indexOfSlice() inherited
int indexOfSlice(BitVector slice, [int from = 0])Finds the first index after from of the specified bit pattern in this vector.
Inherited from BitVector.
Implementation
int indexOfSlice(BitVector slice, [int from = 0]) {
int go(BitVector b, int idx) {
var b2 = b;
var idx2 = idx;
while (true) {
if (b2.startsWith(slice)) {
return idx2;
} else if (b2.isEmpty) {
return -1;
} else {
b2 = b2.tail;
idx2 += 1;
}
}
}
return go(drop(from), from);
}insert() inherited
BitVector insert(int idx, bool b)Returns a vector with the specified bit inserted at the specified index.
Inherited from BitVector.
Implementation
BitVector insert(int idx, bool b) => take(idx).append(b).concat(drop(idx));invalidBits()
int invalidBits()Implementation
int invalidBits() => 8 - _validBitsInLastByte(size);invertReverseByteOrder() inherited
BitVector invertReverseByteOrder()Inverse of reverseByteOrder.
Inherited from BitVector.
Implementation
BitVector invertReverseByteOrder() {
if (size % 8 == 0) {
return reverseByteOrder();
} else {
final validFinalBits = _validBitsInLastByte(size);
final (init, last) = splitAt(size - validFinalBits);
return last.concat(init.bytes.reverse.bits);
}
}lift() inherited
Option<bool> lift(int n)Returns Some(true) if the nth bit is high, Some(false) if low, and None if n >= size.
Inherited from BitVector.
Implementation
Option<bool> lift(int n) => Option.when(() => sizeGreaterThan(n), () => get(n));noSuchMethod() inherited
dynamic noSuchMethod(Invocation invocation)Invoked when a nonexistent method or property is accessed.
A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:
dynamic object = 1;
object.add(42); // Statically allowed, run-time errorThis invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).
Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.
A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:
class MockList<T> implements List<T> {
noSuchMethod(Invocation invocation) {
log(invocation);
super.noSuchMethod(invocation); // Will throw.
}
}
void main() {
MockList().add(42);
}This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.
If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.
The default behavior is to throw a NoSuchMethodError.
Inherited from Object.
Implementation
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);or() inherited
Returns a bitwise OR of this BitVector with the specified BitVector.
Inherited from BitVector.
Implementation
BitVector or(BitVector other) => _zipBytesWith(other, (a, b) => a | b);outOfBounds()
Never outOfBounds(int n)Implementation
Never outOfBounds(int n) => throw RangeError('invalid index: $n of $size');padLeft() inherited
BitVector padLeft(int n)Returns an n-bit vector whose contents are 0 or more low bits followed by this vector's contents.
Inherited from BitVector.
Implementation
BitVector padLeft(int n) => size < n ? BitVector.low(n - size).concat(this) : this;padRight() inherited
BitVector padRight(int n)Returns an n-bit vector whose contents are 0 or more low bits followed by this vector's contents.
Inherited from BitVector.
Implementation
BitVector padRight(int n) => size < n ? concat(BitVector.low(n - size)) : this;padTo() inherited
BitVector padTo(int n)Alias for padRight.
Inherited from BitVector.
Implementation
BitVector padTo(int n) => padRight(n);patch() inherited
Returns a vector with the specified bit vector replacing bits [idx, idx + b.size].
Inherited from BitVector.
Implementation
BitVector patch(int idx, BitVector b) => take(idx).concat(b).concat(drop(idx + b.size));populationCount() inherited
int populationCount()Returns the number of bits that are high.
Inherited from BitVector.
Implementation
int populationCount() {
var count = 0;
var ix = 0;
while (ix < size) {
if (get(ix)) count++;
ix++;
}
return count;
}prepend() inherited
BitVector prepend(bool b)Returns a new vector with the specified bit prepended.
Inherited from BitVector.
Implementation
BitVector prepend(bool b) => BitVector.bit(b).concat(this);printHexDump() inherited
void printHexDump()Prints a colorized hex dump of this bit vector to stdout.
Inherited from BitVector.
Implementation
void printHexDump() => HexDumpFormat.defaultFormat.printBits(this);reverseBitOrder() inherited
BitVector reverseBitOrder()Returns a new vector of the same size with the bit order reversed.
Inherited from BitVector.
Implementation
BitVector reverseBitOrder() {
final reversed = compact().underlying.map(_reverseBitsInByte);
if (size % 8 == 0) {
return BitVector.fromByteVector(reversed);
} else {
final lastIdx = reversed.size - 1;
final toDrop = 8 - _validBitsInLastByte(size);
return BitVector.fromByteVector(
reversed.update(lastIdx, (reversed.get(lastIdx) << toDrop) & 0xff),
).dropRight(toDrop);
}
}reverseByteOrder() inherited
BitVector reverseByteOrder()Returns a new vector of the same size with the byte order reversed.
Note that reverseByteOrder.reverseByteOrder == identity only when size is evenly divisble by 8. To invert reverseByteOrder for an arbitrary size, use invertReverseByteOrder.
Inherited from BitVector.
Implementation
BitVector reverseByteOrder() {
if (size % 8 == 0) {
return _toBytes(compact().underlying.reverse, size);
} else {
final validFinalBits = _validBitsInLastByte(size);
final last = take(validFinalBits).compact();
final b = drop(validFinalBits).bytes.reverse;
final init = _toBytes(b, size - last.size);
return init.concat(last);
}
}rotateLeft() inherited
BitVector rotateLeft(int n)Returns a BitVector of the same size with each bit circularly shifted to the left n bits.
Inherited from BitVector.
Implementation
BitVector rotateLeft(int n) {
if (n <= 0 || isEmpty) {
return this;
} else {
final n0 = n % size;
return n0 == 0 ? this : drop(n0).concat(take(n0));
}
}rotateRight() inherited
BitVector rotateRight(int n)Returns a BitVector of the same size with each bit circularly shifted to the right n bits.
Inherited from BitVector.
Implementation
BitVector rotateRight(int n) {
if (n <= 0 || isEmpty) {
return this;
} else {
final n0 = n % size;
return n0 == 0 ? this : takeRight(n0).concat(dropRight(n0));
}
}set() inherited
BitVector set(int n)Returns a new bit vector with the nth bit high (and all other bits unmodified).
Inherited from BitVector.
Implementation
BitVector set(int n) => update(n, true);shiftLeft() inherited
BitVector shiftLeft(int n)Returns a BitVector of the same size with each bit shifted to the left n bits.
Inherited from BitVector.
Implementation
BitVector shiftLeft(int n) {
if (n <= 0) {
return this;
} else if (n >= size) {
return BitVector.low(size);
} else {
return drop(n).concat(BitVector.low(n));
}
}shiftRight() inherited
BitVector shiftRight(int n, bool signExtension)Returns a BitVector of the same size with each bit shifted to the right n bits.
Inherited from BitVector.
Implementation
BitVector shiftRight(int n, bool signExtension) {
if (isEmpty || n <= 0) {
return this;
} else {
final extensionHigh = signExtension && get(0);
if (n >= size) {
return extensionHigh ? BitVector.high(size) : BitVector.low(size);
} else {
return (extensionHigh ? BitVector.high(n) : BitVector.low(n)).concat(dropRight(n));
}
}
}sizeGreaterThan() inherited
bool sizeGreaterThan(int n)Returns true if the size of this BitVector is greater than n. Unlike size, this forces this BitVector from left to right, halting as soon as it has a definite answer.
Inherited from BitVector.
Implementation
bool sizeGreaterThan(int n) => n < 0 || !sizeLessThanOrEqual(n);sizeGreaterThanOrEqual() inherited
bool sizeGreaterThanOrEqual(int n)Returns true if the size of this BitVector is greater than or equal to n. Unlike size, this forces this BitVector from left to right, halting as soon as it has a definite answer.
Inherited from BitVector.
Implementation
bool sizeGreaterThanOrEqual(int n) => n < 0 || !sizeLessThanOrEqual(n - 1);sizeLessThan() override
bool sizeLessThan(int n)Returns true if the size of this BitVector is less than n. Unlike size, this forces this BitVector from left to right, halting as soon as it has a definite answer.
Implementation
@override
bool sizeLessThan(int n) => size < n;sizeLessThanOrEqual() inherited
bool sizeLessThanOrEqual(int n)Returns true if the size of this BitVector is less than or equal to n. Unlike size, this forces this BitVector from left to right, halting as soon as it has a definite answer.
Inherited from BitVector.
Implementation
bool sizeLessThanOrEqual(int n) => n == Integer.maxValue || sizeLessThan(n + 1);slice() inherited
BitVector slice(int from, int until)Returns a vector made up of the bits starting at index from up to index until, not including the index until.
Inherited from BitVector.
Implementation
BitVector slice(int from, int until) => drop(from).take(until - max(from, 0));sliding() inherited
Returns an iterator of n-bit sliding windows over this vector, advancing step bits between consecutive windows.
Inherited from BitVector.
Implementation
RIterator<BitVector> sliding(int n, [int step = 1]) {
assert(n > 0 && step > 0, 'both n and step must be positive');
RIterator<int> limit(RIterator<int> itr) =>
(step < n) ? itr.take((size - n) + 1) : itr.takeWhile((i) => i < size);
return limit(RIterator.iterate(0, (x) => x + step)).map((idx) => slice(idx, idx + n));
}splice() inherited
Returns a vector with the specified bit vector inserted at the specified index.
Inherited from BitVector.
Implementation
BitVector splice(int idx, BitVector b) => take(idx).concat(b).concat(drop(idx));splitAt() inherited
Record splitAt(int n)Returns a pair of vectors that is equal to (take(n), drop(n)).
Inherited from BitVector.
Implementation
(BitVector, BitVector) splitAt(int n) => (take(n), drop(n));startsWith() inherited
bool startsWith(BitVector b)Returns true if this bit vector starts with the specified vector.
Inherited from BitVector.
Implementation
bool startsWith(BitVector b) => take(b.size) == b;take() override
BitVector take(int n)Returns a vector of the first n bits of this vector.
The resulting vector's size is n min size.
Note: if an n-bit vector is required, use the acquire method instead.
Implementation
@override
BitVector take(int n) => _toBytes(underlying, max(0, min(size, n)));takeRight() inherited
BitVector takeRight(int n)Returns a vector of the last n bits of this vector.
The resulting vector's size is n min size.
Inherited from BitVector.
Implementation
BitVector takeRight(int n) {
if (n < 0) {
throw ArgumentError('takeRight($n)');
} else if (n >= size) {
return this;
} else {
return drop(size - n);
}
}toBase16() inherited
String toBase16([HexAlphabet alphabet = Alphabets.hexLower])Alias for toHex.
Inherited from BitVector.
Implementation
String toBase16([HexAlphabet alphabet = Alphabets.hexLower]) => toHex(alphabet);toBase32() inherited
String toBase32([Base32Alphabet alphabet = Alphabets.base32])Encodes this bit vector as a base-32 string.
Inherited from BitVector.
Implementation
String toBase32([Base32Alphabet alphabet = Alphabets.base32]) => bytes.toBase32(alphabet);toBase64() inherited
String toBase64([Base64Alphabet alphabet = Alphabets.base64])Encodes this bit vector as a base-64 string.
Inherited from BitVector.
Implementation
String toBase64([Base64Alphabet alphabet = Alphabets.base64]) => bytes.toBase64(alphabet);toBase64NoPad() inherited
String toBase64NoPad()Encodes as base-64 without padding characters.
Inherited from BitVector.
Implementation
String toBase64NoPad() => toBase64(Alphabets.base64NoPad);toBase64Url() inherited
String toBase64Url()Encodes as URL-safe base-64.
Inherited from BitVector.
Implementation
String toBase64Url() => toBase64(Alphabets.base64Url);toBase64UrlNoPad() inherited
String toBase64UrlNoPad()Encodes as URL-safe base-64 without padding characters.
Inherited from BitVector.
Implementation
String toBase64UrlNoPad() => toBase64(Alphabets.base64UrlNoPad);toBigInt() inherited
BigInt toBigInt({bool signed = true, Endian ordering = Endian.big})Converts this bit vector to a BigInt.
If signed is true (the default), the most significant bit is treated as a sign bit. Use ordering to specify byte order.
Inherited from BitVector.
Implementation
BigInt toBigInt({bool signed = true, Endian ordering = Endian.big}) =>
ordering == Endian.little
? invertReverseByteOrder().toBigInt(signed: signed)
: _getBigEndianBigInt(0, size, signed);toBin() inherited
String toBin([BinaryAlphabet alphabet = Alphabets.binary])Encodes this bit vector as a binary string using the given alphabet.
Inherited from BitVector.
Implementation
String toBin([BinaryAlphabet alphabet = Alphabets.binary]) =>
bytes.toBin(alphabet).substring(0, size);toByteArray() inherited
Uint8List toByteArray()Returns the contents of this bit vector as a Uint8List.
Inherited from BitVector.
Implementation
Uint8List toByteArray() => bytes.toByteArray();toByteVector() inherited
ByteVector toByteVector()Converts this bit vector to a ByteVector, clearing any trailing padding bits in the last byte.
Inherited from BitVector.
Implementation
ByteVector toByteVector() => _clearUnneededBits(size, compact().underlying);toHex() inherited
String toHex([HexAlphabet alphabet = Alphabets.hexLower])Encodes this bit vector as a hexadecimal string using the given alphabet.
Inherited from BitVector.
Implementation
String toHex([HexAlphabet alphabet = Alphabets.hexLower]) {
final full = bytes.toHex(alphabet);
if (size % 8 == 0) {
return full;
} else if (size % 8 <= 4) {
return full.init;
} else {
return full;
}
}toHexDump() inherited
String toHexDump()Returns a plain-text hex dump of this bit vector (no ANSI colors).
Inherited from BitVector.
Implementation
String toHexDump() => HexDumpFormat.noAnsi.renderBits(this);toHexDumpColorized() inherited
String toHexDumpColorized()Returns a colorized hex dump of this bit vector.
Inherited from BitVector.
Implementation
String toHexDumpColorized() => HexDumpFormat.defaultFormat.renderBits(this);toIList() inherited
IList<bool> toIList()Returns an IList of booleans, one per bit.
Inherited from BitVector.
Implementation
IList<bool> toIList() => IList.tabulate(size, (ix) => get(ix));toInt() inherited
int toInt({bool signed = true, Endian ordering = Endian.big})Converts this bit vector to a Dart int.
If signed is true (the default), the result is sign-extended. Use ordering to specify byte order (Endian.big by default).
Inherited from BitVector.
Implementation
int toInt({bool signed = true, Endian ordering = Endian.big}) {
return switch (this) {
final Bytes bytes => switch (size) {
32 when signed => ByteData.sublistView(
bytes.underlying.toByteArray(),
).getInt32(0, ordering),
32 when !signed => ByteData.sublistView(
bytes.underlying.toByteArray(),
).getUint32(0, ordering),
16 when signed => ByteData.sublistView(
bytes.underlying.toByteArray(),
).getInt16(0, ordering),
16 when !signed => ByteData.sublistView(
bytes.underlying.toByteArray(),
).getUint16(0, ordering),
8 when signed => ByteData.sublistView(bytes.underlying.toByteArray()).getInt8(0),
8 when !signed => ByteData.sublistView(bytes.underlying.toByteArray()).getUint8(0),
_ =>
ordering == Endian.little
? invertReverseByteOrder().toInt(signed: signed)
: _getBigEndianInt(0, size, signed),
},
_ =>
ordering == Endian.little
? invertReverseByteOrder().toInt(signed: signed)
: _getBigEndianInt(0, size, signed),
};
}toString() inherited
String toString()A string representation of this object.
Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.
Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.
Inherited from BitVector.
Implementation
@override
String toString() {
if (isEmpty) {
return 'BitVector.empty';
} else if (sizeLessThan(513)) {
return 'BitVector(${toHex()})';
} else {
return 'BitVector($size, $hashCode)';
}
}unbuffer() inherited
BitVector unbuffer()Materializes any buffered appends, returning an unbuffered bit vector.
Inherited from BitVector.
Implementation
BitVector unbuffer() => this;update() override
BitVector update(int n, bool high)Returns a new bit vector with the nth bit high if high is true or low if high is false.
Implementation
@override
BitVector update(int n, bool high) {
checkBounds(n);
final b2 = underlying.update(
n ~/ 8,
underlying.lift(n ~/ 8).map((a) => _setBit(a, n % 8, high)).getOrElse(() => outOfBounds(n)),
);
return Bytes(b2, size);
}xor() inherited
Returns a bitwise XOR of this BitVector with the specified BitVector.
Inherited from BitVector.
Implementation
BitVector xor(BitVector other) => _zipBytesWith(other, (a, b) => a ^ b);Operators
operator <<() inherited
BitVector operator <<(int n)Left shift by n bits.
Inherited from BitVector.
Implementation
BitVector operator <<(int n) => shiftLeft(n);operator ==() inherited
bool operator ==(Object other)The equality operator.
The default behavior for all Objects is to return true if and only if this object and other are the same object.
Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:
Total: It must return a boolean for all arguments. It should never throw.
Reflexive: For all objects
o,o == omust be true.Symmetric: For all objects
o1ando2,o1 == o2ando2 == o1must either both be true, or both be false.Transitive: For all objects
o1,o2, ando3, ifo1 == o2ando2 == o3are true, theno1 == o3must be true.
The method should also be consistent over time, so whether two objects are equal should only change if at least one of the objects was modified.
If a subclass overrides the equality operator, it should override the hashCode method as well to maintain consistency.
Inherited from BitVector.
Implementation
@override
bool operator ==(Object other) {
if (other is! BitVector) {
return false;
} else if (other.size != size) {
return false;
} else {
const chunkSize = 8 * 1024 * 64;
var x = this;
var y = other;
while (true) {
if (x.isEmpty) {
return y.isEmpty;
} else {
final chunkX = x.take(chunkSize);
final chunkY = y.take(chunkSize);
if (chunkX.bytes != chunkY.bytes) {
return false;
} else {
x = x.drop(chunkSize);
y = y.drop(chunkSize);
}
}
}
}
}operator >>() inherited
BitVector operator >>(int n)Arithmetic right shift by n bits (sign-extending).
Inherited from BitVector.
Implementation
BitVector operator >>(int n) => shiftRight(n, true);operator >>>() inherited
BitVector operator >>>(int n)Logical right shift by n bits (zero-filling).
Inherited from BitVector.
Implementation
BitVector operator >>>(int n) => shiftRight(n, false);operator ^() inherited
Bitwise XOR of this vector and other.
Inherited from BitVector.
Implementation
BitVector operator ^(BitVector other) => xor(other);operator |() inherited
Bitwise OR of this vector and other.
Inherited from BitVector.
Implementation
BitVector operator |(BitVector other) => or(other);operator ~() inherited
BitVector operator ~()Bitwise NOT. Returns the complement of this vector.
Inherited from BitVector.
Implementation
BitVector operator ~() => not;