Skip to content

Bytes final

final class Bytes extends BitVector

A 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 → BitVectorBytes

Constructors

Bytes()

Bytes(ByteVector underlying, int size)

Creates a Bytes from the given underlying byte vector and a size in bits.

Implementation
dart
Bytes(this.underlying, this.size);

Properties

bytes no setter inherited

ByteVector get bytes

Returns the underlying bytes of this bit vector.

Inherited from BitVector.

Implementation
dart
ByteVector get bytes => toByteVector();

hashCode no setter inherited

int get hashCode

The 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
dart
@override
int get hashCode => Object.hash(size, bytes);

head no setter inherited

bool get head

Returns the first bit of this vector or throws if vector is emtpy.

Inherited from BitVector.

Implementation
dart
bool get head => get(0);

headOption no setter inherited

Option<bool> get headOption

Returns the first bit of this vector or None if vector is emtpy.

Inherited from BitVector.

Implementation
dart
Option<bool> get headOption => lift(0);

init no setter inherited

BitVector get init

Returns a vector of all bits in this vector except the last bit.

Inherited from BitVector.

Implementation
dart
BitVector get init => dropRight(1);

isEmpty no setter inherited

bool get isEmpty

Returns true if this vector has no bits.

Inherited from BitVector.

Implementation
dart
bool get isEmpty => sizeLessThan(1);

last no setter inherited

bool get last

Returns the last bit in this vector or throws if vector is empty.

Inherited from BitVector.

Implementation
dart
bool get last => get(size - 1);

lastOption no setter inherited

Option<bool> get lastOption

Returns the last bit in this vector or returns None if vector is empty.

Inherited from BitVector.

Implementation
dart
Option<bool> get lastOption => lift(size - 1);

length no setter inherited

int get length

Alias for size.

Inherited from BitVector.

Implementation
dart
int get length => size;

nonEmpty no setter inherited

bool get nonEmpty

Returns true if this vector has a non-zero number of bits.

Inherited from BitVector.

Implementation
dart
bool get nonEmpty => !isEmpty;

not no setter inherited

BitVector get not

Returns a bitwise complement of this BitVector.

Inherited from BitVector.

Implementation
dart
BitVector get not => _mapBytes((b) => b.not);

reverse no setter inherited

BitVector get reverse

Returns a bit vector with the bits in reverse order.

Inherited from BitVector.

Implementation
dart
BitVector get reverse => BitVector.fromByteVector(
  compact().underlying.reverse.map(_reverseBitsInByte),
).drop(8 - _validBitsInLastByte(size));

runtimeType no setter inherited

Type get runtimeType

A representation of the runtime type of the object.

Inherited from Object.

Implementation
dart
external Type get runtimeType;

size final

final int size

Returns number of bits in this vector.

Implementation
dart
@override
final int size;

tail no setter inherited

BitVector get tail

Returns a vector of all bits in this vector except the first bit.

Inherited from BitVector.

Implementation
dart
BitVector get tail => drop(1);

underlying final

final ByteVector underlying

The underlying byte storage.

Implementation
dart
final ByteVector underlying;

Methods

acquire() inherited

Either<String, BitVector> acquire(int n)

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
dart
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
dart
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
dart
@override
Bytes align() => this;

and() inherited

BitVector and(BitVector other)

Returns a bitwise AND of this BitVector with the specified BitVector.

Inherited from BitVector.

Implementation
dart
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
dart
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
dart
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) ~&#47; 8), 0, _BufferState(0));
  }
}

call() inherited

bool call(int n)

Alias for get.

Inherited from BitVector.

Implementation
dart
bool call(int n) => get(n);

checkBounds()

void checkBounds(int n)
Implementation
dart
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
dart
BitVector clear(int n) => update(n, false);

combine()

Bytes combine(Bytes other)
Implementation
dart
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
dart
Bytes compact() {
  if (_bytesNeededForBits(size) > Integer.maxValue) {
    throw ArgumentError('cannot compact bit vector of size ${size.toDouble() &#47; 8 &#47; 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
dart
@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

BitVector concat(BitVector b2)

Returns a new bit vector representing this vector's contents followed by the specified vector's contents.

Inherited from BitVector.

Implementation
dart
BitVector concat(BitVector b2) {
  return isEmpty ? b2 : _Chunks(_Append(this, b2));
}

consume() inherited

Either<String, Record> consume<A>(
  int n,
  Either<String, A> Function(BitVector) decode,
)

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
dart
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

R consumeThen<R>(
  int n,
  R Function(String) err,
  R Function(BitVector, BitVector) f,
)

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
dart
R consumeThen<R>(
  int n,
  Function1<String, R> err,
  Function2<BitVector, BitVector, R> f,
) {
  if (sizeGreaterThanOrEqual(n)) {
    return f(take(n), drop(n)); &#47;&#47; 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
dart
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
dart
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
dart
@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 ~&#47; 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
dart
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
dart
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
dart
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
dart
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
dart
@override
bool get(int n) {
  checkBounds(n);
  return _getBit(underlying.get(n ~&#47; 8), n % 8);
}

getByte() override

int getByte(int n)

Returns the nth byte, 0-indexed.

Implementation
dart
@override
int getByte(int n) {
  if (n < underlying.size - 1) {
    return underlying.get(n);
  } else {
    &#47;&#47; 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
dart
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
dart
BitVector insert(int idx, bool b) => take(idx).append(b).concat(drop(idx));

invalidBits()

int invalidBits()
Implementation
dart
int invalidBits() => 8 - _validBitsInLastByte(size);

invertReverseByteOrder() inherited

BitVector invertReverseByteOrder()

Inverse of reverseByteOrder.

Inherited from BitVector.

Implementation
dart
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
dart
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:

dart
dynamic object = 1;
object.add(42); // Statically allowed, run-time error

This 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:

dart
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
dart
@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
dart
BitVector or(BitVector other) => _zipBytesWith(other, (a, b) => a | b);

outOfBounds()

Never outOfBounds(int n)
Implementation
dart
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
dart
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
dart
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
dart
BitVector padTo(int n) => padRight(n);

patch() inherited

BitVector patch(int idx, BitVector b)

Returns a vector with the specified bit vector replacing bits [idx, idx + b.size].

Inherited from BitVector.

Implementation
dart
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
dart
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
dart
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
dart
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
dart
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
dart
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
dart
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
dart
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
dart
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
dart
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
dart
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
dart
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
dart
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
dart
@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
dart
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
dart
BitVector slice(int from, int until) => drop(from).take(until - max(from, 0));

sliding() inherited

RIterator<BitVector> sliding(int n, [int step = 1])

Returns an iterator of n-bit sliding windows over this vector, advancing step bits between consecutive windows.

Inherited from BitVector.

Implementation
dart
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

BitVector splice(int idx, BitVector b)

Returns a vector with the specified bit vector inserted at the specified index.

Inherited from BitVector.

Implementation
dart
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
dart
(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
dart
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
dart
@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
dart
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
dart
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
dart
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
dart
String toBase64([Base64Alphabet alphabet = Alphabets.base64]) => bytes.toBase64(alphabet);

toBase64NoPad() inherited

String toBase64NoPad()

Encodes as base-64 without padding characters.

Inherited from BitVector.

Implementation
dart
String toBase64NoPad() => toBase64(Alphabets.base64NoPad);

toBase64Url() inherited

String toBase64Url()

Encodes as URL-safe base-64.

Inherited from BitVector.

Implementation
dart
String toBase64Url() => toBase64(Alphabets.base64Url);

toBase64UrlNoPad() inherited

String toBase64UrlNoPad()

Encodes as URL-safe base-64 without padding characters.

Inherited from BitVector.

Implementation
dart
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
dart
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
dart
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
dart
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
dart
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
dart
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
dart
String toHexDump() => HexDumpFormat.noAnsi.renderBits(this);

toHexDumpColorized() inherited

String toHexDumpColorized()

Returns a colorized hex dump of this bit vector.

Inherited from BitVector.

Implementation
dart
String toHexDumpColorized() => HexDumpFormat.defaultFormat.renderBits(this);

toIList() inherited

IList<bool> toIList()

Returns an IList of booleans, one per bit.

Inherited from BitVector.

Implementation
dart
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
dart
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
dart
@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
dart
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
dart
@override
BitVector update(int n, bool high) {
  checkBounds(n);

  final b2 = underlying.update(
    n ~&#47; 8,
    underlying.lift(n ~&#47; 8).map((a) => _setBit(a, n % 8, high)).getOrElse(() => outOfBounds(n)),
  );

  return Bytes(b2, size);
}

xor() inherited

BitVector xor(BitVector other)

Returns a bitwise XOR of this BitVector with the specified BitVector.

Inherited from BitVector.

Implementation
dart
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
dart
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 == o must be true.

  • Symmetric: For all objects o1 and o2, o1 == o2 and o2 == o1 must either both be true, or both be false.

  • Transitive: For all objects o1, o2, and o3, if o1 == o2 and o2 == o3 are true, then o1 == o3 must 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
dart
@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
dart
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
dart
BitVector operator >>>(int n) => shiftRight(n, false);

operator ^() inherited

BitVector operator ^(BitVector other)

Bitwise XOR of this vector and other.

Inherited from BitVector.

Implementation
dart
BitVector operator ^(BitVector other) => xor(other);

operator |() inherited

BitVector operator |(BitVector other)

Bitwise OR of this vector and other.

Inherited from BitVector.

Implementation
dart
BitVector operator |(BitVector other) => or(other);

operator ~() inherited

BitVector operator ~()

Bitwise NOT. Returns the complement of this vector.

Inherited from BitVector.

Implementation
dart
BitVector operator ~() => not;