Skip to content

Bytes final

finalclassBytesextendsBitVector

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(ByteVectorunderlying,intsize)

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

ByteVectorgetbytes

Returns the underlying bytes of this bit vector.

Inherited from BitVector.

Implementation
dart
ByteVector get bytes => toByteVector();

hashCode no setter inherited

intgethashCode

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

boolgethead

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

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

BitVectorgetinit

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

boolgetisEmpty

Returns true if this vector has no bits.

Inherited from BitVector.

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

last no setter inherited

boolgetlast

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

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

intgetlength

Alias for size.

Inherited from BitVector.

Implementation
dart
int get length => size;

nonEmpty no setter inherited

boolgetnonEmpty

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

BitVectorgetnot

Returns a bitwise complement of this BitVector.

Inherited from BitVector.

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

reverse no setter inherited

BitVectorgetreverse

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

TypegetruntimeType

A representation of the runtime type of the object.

Inherited from Object.

Implementation
dart
external Type get runtimeType;

size final

finalintsize

Returns number of bits in this vector.

Implementation
dart
@override
final int size;

tail no setter inherited

BitVectorgettail

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

finalByteVectorunderlying

The underlying byte storage.

Implementation
dart
final ByteVector underlying;

Methods

acquire() inherited

Either<String,BitVector>acquire(intn)

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

RacquireThen<R>(intn,RFunction(String)err,RFunction(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

Bytesalign()

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

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

BitVectorappend(boolb)

Returns a new vector with the specified bit appended.

Inherited from BitVector.

Implementation
dart
BitVector append(bool b) => concat(BitVector.bit(b));

bufferBy() inherited

BitVectorbufferBy([intchunkSizeInBits=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

boolcall(intn)

Alias for get.

Inherited from BitVector.

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

checkBounds()

voidcheckBounds(intn)
Implementation
dart
void checkBounds(int n) {
  if (!sizeGreaterThan(n)) outOfBounds(n);
}

clear() inherited

BitVectorclear(intn)

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()

Bytescombine(Bytesother)
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

Bytescompact()

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

intcompareTo(BitVectorthat)

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

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>(intn,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

RconsumeThen<R>(intn,RFunction(String)err,RFunction(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

boolcontainsSlice(BitVectorslice)

Determines if the specified slice is in this vector.

Inherited from BitVector.

Implementation
dart
bool containsSlice(BitVector slice) => indexOfSlice(slice) >= 0;

copy() inherited

Bytescopy()

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

BitVectordrop(intn)

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

BitVectordropRight(intn)

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

BitVectordropWhile(boolFunction(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

boolendsWith(BitVectorb)

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

BitVectorforce()

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

boolget(intn)

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

intgetByte(intn)

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

intindexOfSlice(BitVectorslice, [intfrom=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

BitVectorinsert(intidx,boolb)

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()

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

invertReverseByteOrder() inherited

BitVectorinvertReverseByteOrder()

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(intn)

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

dynamicnoSuchMethod(Invocationinvocation)

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()

NeveroutOfBounds(intn)
Implementation
dart
Never outOfBounds(int n) => throw RangeError('invalid index: $n of $size');

padLeft() inherited

BitVectorpadLeft(intn)

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

BitVectorpadRight(intn)

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

BitVectorpadTo(intn)

Alias for padRight.

Inherited from BitVector.

Implementation
dart
BitVector padTo(int n) => padRight(n);

patch() inherited

BitVectorpatch(intidx,BitVectorb)

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

intpopulationCount()

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

BitVectorprepend(boolb)

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

voidprintHexDump()

Prints a colorized hex dump of this bit vector to stdout.

Inherited from BitVector.

Implementation
dart
void printHexDump() => HexDumpFormat.defaultFormat.printBits(this);

reverseBitOrder() inherited

BitVectorreverseBitOrder()

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

BitVectorreverseByteOrder()

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

BitVectorrotateLeft(intn)

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

BitVectorrotateRight(intn)

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

BitVectorset(intn)

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

BitVectorshiftLeft(intn)

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

BitVectorshiftRight(intn,boolsignExtension)

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

boolsizeGreaterThan(intn)

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

boolsizeGreaterThanOrEqual(intn)

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

boolsizeLessThan(intn)

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

boolsizeLessThanOrEqual(intn)

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

BitVectorslice(intfrom,intuntil)

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(intn, [intstep=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

BitVectorsplice(intidx,BitVectorb)

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

RecordsplitAt(intn)

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

boolstartsWith(BitVectorb)

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

BitVectortake(intn)

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

BitVectortakeRight(intn)

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

StringtoBase16([HexAlphabetalphabet= Alphabets.hexLower])

Alias for toHex.

Inherited from BitVector.

Implementation
dart
String toBase16([HexAlphabet alphabet = Alphabets.hexLower]) => toHex(alphabet);

toBase32() inherited

StringtoBase32([Base32Alphabetalphabet= 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

StringtoBase64([Base64Alphabetalphabet= 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

StringtoBase64NoPad()

Encodes as base-64 without padding characters.

Inherited from BitVector.

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

toBase64Url() inherited

StringtoBase64Url()

Encodes as URL-safe base-64.

Inherited from BitVector.

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

toBase64UrlNoPad() inherited

StringtoBase64UrlNoPad()

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

Inherited from BitVector.

Implementation
dart
String toBase64UrlNoPad() => toBase64(Alphabets.base64UrlNoPad);

toBigInt() inherited

BigInttoBigInt({boolsigned=true,Endianordering= 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

StringtoBin([BinaryAlphabetalphabet= 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

Uint8ListtoByteArray()

Returns the contents of this bit vector as a Uint8List.

Inherited from BitVector.

Implementation
dart
Uint8List toByteArray() => bytes.toByteArray();

toByteVector() inherited

ByteVectortoByteVector()

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

StringtoHex([HexAlphabetalphabet= 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

StringtoHexDump()

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

StringtoHexDumpColorized()

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

inttoInt({boolsigned=true,Endianordering= 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

StringtoString()

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

BitVectorunbuffer()

Materializes any buffered appends, returning an unbuffered bit vector.

Inherited from BitVector.

Implementation
dart
BitVector unbuffer() => this;

update() override

BitVectorupdate(intn,boolhigh)

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

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

BitVectoroperator <<(intn)

Left shift by n bits.

Inherited from BitVector.

Implementation
dart
BitVector operator <<(int n) => shiftLeft(n);

operator ==() inherited

booloperator ==(Objectother)

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

BitVectoroperator >>(intn)

Arithmetic right shift by n bits (sign-extending).

Inherited from BitVector.

Implementation
dart
BitVector operator >>(int n) => shiftRight(n, true);

operator >>>() inherited

BitVectoroperator >>>(intn)

Logical right shift by n bits (zero-filling).

Inherited from BitVector.

Implementation
dart
BitVector operator >>>(int n) => shiftRight(n, false);

operator ^() inherited

BitVectoroperator ^(BitVectorother)

Bitwise XOR of this vector and other.

Inherited from BitVector.

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

operator |() inherited

BitVectoroperator |(BitVectorother)

Bitwise OR of this vector and other.

Inherited from BitVector.

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

operator ~() inherited

BitVectoroperator ~()

Bitwise NOT. Returns the complement of this vector.

Inherited from BitVector.

Implementation
dart
BitVector operator ~() => not;