Overview
Lists that efficiently handle fixed sized data (for example, unsigned 8 byte integers) and SIMD numeric types.
To use this library in your code:
import 'raya:typed_data';Classes
ByteBuffer
abstract final class ByteBuffer
A sequence of bytes underlying a typed data object.
Used to process large quantities of binary or numerical data more efficiently using a typed view.
It is a compile-time error for a class to attempt to extend or implement ByteBuffer.
Properties
Methods
Uint8List asUint8List([int offsetInBytes = 0, int? length])Creates a Uint8List view of a region of this byte buffer.
The view is backed by the bytes of this byte buffer. Any changes made to the Uint8List will also change the buffer, and vice versa.
The viewed region start at offsetInBytes and contains length bytes. If length is omitted, the range extends to the end of the buffer.
The start index and length must describe a valid range of the buffer:
offsetInBytesmust not be negative,lengthmust not be negative, andoffsetInBytes + lengthmust not be greater thanlengthInBytes.
import 'raya:typed_data';
void main() {
final words = Uint32List.fromList([0x04030201, 0x08070605]);
final buf = words.buffer;
print('all bytes: ${buf.asUint8List()}');
print('from offset 4: ${buf.asUint8List(4)}');
print('2 bytes at 2: ${buf.asUint8List(2, 2)}');
// A view shares memory: writing a byte changes the word.
buf.asUint8List()[0] = 0xFF;
print('word 0 now: 0x${words[0].toRadixString(16)}');
}all bytes: [1, 2, 3, 4, 5, 6, 7, 8] from offset 4: [5, 6, 7, 8] 2 bytes at 2: [3, 4] word 0 now: 0x40302ff
Int8List asInt8List([int offsetInBytes = 0, int? length])Creates a Int8List view of a region of this byte buffer.
The view is backed by the bytes of this byte buffer. Any changes made to the Int8List will also change the buffer, and vice versa.
The viewed region start at offsetInBytes and contains length bytes. If length is omitted, the range extends to the end of the buffer.
The start index and length must describe a valid range of the buffer:
offsetInBytesmust not be negative,lengthmust not be negative, andoffsetInBytes + lengthmust not be greater thanlengthInBytes.
import 'raya:typed_data';
void main() {
final bytes = Uint8List.fromList([0, 1, 127, 128, 200, 255]);
final signed = bytes.buffer.asInt8List();
print('unsigned: $bytes');
print('signed: $signed');
print('middle 2: ${bytes.buffer.asInt8List(2, 2)}');
}unsigned: [0, 1, 127, 128, 200, 255] signed: [0, 1, 127, -128, -56, -1] middle 2: [127, -128]
Uint8ClampedList asUint8ClampedList([int offsetInBytes = 0, int? length])Creates a Uint8ClampedList view of a region of this byte buffer.
The view is backed by the bytes of this byte buffer. Any changes made to the Uint8ClampedList will also change the buffer, and vice versa.
The viewed region start at offsetInBytes and contains length bytes. If length is omitted, the range extends to the end of the buffer.
The start index and length must describe a valid range of the buffer:
offsetInBytesmust not be negative,lengthmust not be negative, andoffsetInBytes + lengthmust not be greater thanlengthInBytes.
import 'raya:typed_data';
void main() {
final buf = Uint8List(4).buffer;
final clamped = buf.asUint8ClampedList();
clamped[0] = 300; // clamps to 255
clamped[1] = -5; // clamps to 0
clamped[2] = 128;
print('clamped view: $clamped');
print('same bytes: ${buf.asUint8List()}');
}clamped view: [255, 0, 128, 0] same bytes: [255, 0, 128, 0]
Uint16List asUint16List([int offsetInBytes = 0, int? length])Creates a Uint16List view of a region of this byte buffer.
The view is backed by the bytes of this byte buffer. Any changes made to the Uint16List will also change the buffer, and vice versa.
The viewed region start at offsetInBytes, which must be 16-bit aligned, and contains length 16-bit integers with the same endianness as the host (Endian.host). If length is omitted, the range extends as far towards the end of the buffer as possible - if lengthInBytes is not even, the last byte can't be part of the view.
The start index and length must describe a valid 16-bit aligned range of the buffer:
offsetInBytesmust not be negative,offsetInBytesmust be divisible by two,lengthmust not be negative, andoffsetInBytes + length * 2must not be greater thanlengthInBytes.
import 'raya:typed_data';
void main() {
final bytes = Uint8List.fromList([0x01, 0x00, 0xFF, 0xFF, 0x34, 0x12]);
final halves = bytes.buffer.asUint16List();
// Host byte order (little-endian on common machines).
print('uint16 lanes: ${halves.map((v) => '0x${v.toRadixString(16)}').toList()}');
print('from byte 2: ${bytes.buffer.asUint16List(2, 1)}');
}uint16 lanes: [0x1, 0xffff, 0x1234] from byte 2: [65535]
Int16List asInt16List([int offsetInBytes = 0, int? length])Creates a Int16List view of a region of this byte buffer.
The view is backed by the bytes of this byte buffer. Any changes made to the Int16List will also change the buffer, and vice versa.
The viewed region start at offsetInBytes, which must be 16-bit aligned, and contains length 16-bit integers with the same endianness as the host (Endian.host). If length is omitted, the range extends as far towards the end of the buffer as possible - if lengthInBytes is not even, the last byte can't be part of the view.
The start index and length must describe a valid 16-bit aligned range of the buffer:
offsetInBytesmust not be negative,offsetInBytesmust be divisible by two,lengthmust not be negative, andoffsetInBytes + length * 2must not be greater thanlengthInBytes.
import 'raya:typed_data';
void main() {
final raw = Uint16List.fromList([1, 32767, 32768, 65535]);
final signed = raw.buffer.asInt16List();
print('unsigned: $raw');
print('signed: $signed');
}unsigned: [1, 32767, 32768, 65535] signed: [1, 32767, -32768, -1]
Uint32List asUint32List([int offsetInBytes = 0, int? length])Creates a Uint32List view of a region of this byte buffer.
The view is backed by the bytes of this byte buffer. Any changes made to the Uint32List will also change the buffer, and vice versa.
The viewed region start at offsetInBytes, which must be 32-bit aligned, and contains length 32-bit integers with the same endianness as the host (Endian.host). If length is omitted, the range extends as far towards the end of the buffer as possible - if lengthInBytes is not divisible by four, the last bytes can't be part of the view.
The start index and length must describe a valid 32-bit aligned range of the buffer:
offsetInBytesmust not be negative,offsetInBytesmust be divisible by four,lengthmust not be negative, andoffsetInBytes + length * 4must not be greater thanlengthInBytes.
import 'raya:typed_data';
void main() {
final f = Float32List.fromList([1.0, -2.0, 0.5]);
final bits = f.buffer.asUint32List();
for (var i = 0; i < f.length; i++) {
print('${f[i]} -> bits 0x${bits[i].toRadixString(16).padLeft(8, '0')}');
}
}1.0 -> bits 0x3f800000 -2.0 -> bits 0xc0000000 0.5 -> bits 0x3f000000
Int32List asInt32List([int offsetInBytes = 0, int? length])Creates a Int32List view of a region of this byte buffer.
The view is backed by the bytes of this byte buffer. Any changes made to the Int32List will also change the buffer, and vice versa.
The viewed region start at offsetInBytes, which must be 32-bit aligned, and contains length 32-bit integers with the same endianness as the host (Endian.host). If length is omitted, the range extends as far towards the end of the buffer as possible - if lengthInBytes is not divisible by four, the last bytes can't be part of the view.
The start index and length must describe a valid 32-bit aligned range of the buffer:
offsetInBytesmust not be negative,offsetInBytesmust be divisible by four,lengthmust not be negative, andoffsetInBytes + length * 4must not be greater thanlengthInBytes.
import 'raya:typed_data';
void main() {
final buf = Uint8List(12).buffer;
final ints = buf.asInt32List();
ints[0] = -1;
ints[2] = 1 << 30;
print('int32 view: $ints');
print('bytes: ${buf.asUint8List()}');
print('last word: ${buf.asInt32List(8)}');
}int32 view: [-1, 0, 1073741824] bytes: [255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 64] last word: [1073741824]
Uint64List asUint64List([int offsetInBytes = 0, int? length])Creates a Uint64List view of a region of this byte buffer.
The view is backed by the bytes of this byte buffer. Any changes made to the Uint64List will also change the buffer, and vice versa.
The viewed region start at offsetInBytes, which must be 64-bit aligned, and contains length 64-bit integers with the same endianness as the host (Endian.host). If length is omitted, the range extends as far towards the end of the buffer as possible - if lengthInBytes is not divisible by eight, the last bytes can't be part of the view.
The start index and length must describe a valid 64-bit aligned range of the buffer:
offsetInBytesmust not be negative,offsetInBytesmust be divisible by eight,lengthmust not be negative, andoffsetInBytes + length * 8must not be greater thanlengthInBytes.
import 'raya:typed_data';
void main() {
final bytes = Uint8List.fromList([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
final words = bytes.buffer.asUint64List();
print('length: ${words.length}');
print('word 0: ${words[0]}');
print('word 1: 0x${words[1].toRadixString(16)}');
}length: 2 word 0: 1 word 1: 0x100000000000000
Int64List asInt64List([int offsetInBytes = 0, int? length])Creates a Int64List view of a region of this byte buffer.
The view is backed by the bytes of this byte buffer. Any changes made to the Int64List will also change the buffer, and vice versa.
The viewed region start at offsetInBytes, which must be 64-bit aligned, and contains length 64-bit integers with the same endianness as the host (Endian.host). If length is omitted, the range extends as far towards the end of the buffer as possible - if lengthInBytes is not divisible by eight, the last bytes can't be part of the view.
The start index and length must describe a valid 64-bit aligned range of the buffer:
offsetInBytesmust not be negative,offsetInBytesmust be divisible by eight,lengthmust not be negative, andoffsetInBytes + length * 8must not be greater thanlengthInBytes.
import 'raya:typed_data';
void main() {
final buf = Uint8List(16).buffer;
final longs = buf.asInt64List();
longs[0] = -2;
longs[1] = 1 << 40;
print('int64 view: $longs');
print('first 8 bytes: ${buf.asUint8List(0, 8)}');
}int64 view: [-2, 1099511627776] first 8 bytes: [254, 255, 255, 255, 255, 255, 255, 255]
Int32x4List asInt32x4List([int offsetInBytes = 0, int? length])Creates a Int32x4List view of a region of this byte buffer.
The view is backed by the bytes of this byte buffer. Any changes made to the Int32x4List will also change the buffer, and vice versa.
The viewed region start at offsetInBytes, which must be 128-bit aligned, and contains length 128-bit integers. If length is omitted, the range extends as far towards the end of the buffer as possible - if lengthInBytes is not divisible by 16, the last bytes can't be part of the view.
The start index and length must describe a valid 128-bit aligned range of the buffer:
offsetInBytesmust not be negative,offsetInBytesmust be divisible by sixteen,lengthmust not be negative, andoffsetInBytes + length * 16must not be greater thanlengthInBytes.
import 'raya:typed_data';
void main() {
final ints = Int32List.fromList([1, 2, 3, 4, 5, 6, 7, 8]);
final lanes = ints.buffer.asInt32x4List();
print('vectors: ${lanes.length}');
for (final v in lanes) {
print('x=${v.x} y=${v.y} z=${v.z} w=${v.w}');
}
}vectors: 2 x=1 y=2 z=3 w=4 x=5 y=6 z=7 w=8
Float32List asFloat32List([int offsetInBytes = 0, int? length])Creates a Float32List view of a region of this byte buffer.
The view is backed by the bytes of this byte buffer. Any changes made to the Float32List will also change the buffer, and vice versa.
The viewed region start at offsetInBytes, which must be 32-bit aligned, and contains length 32-bit integers. If length is omitted, the range extends as far towards the end of the buffer as possible - if lengthInBytes is not divisible by four, the last bytes can't be part of the view.
The start index and length must describe a valid 32-bit aligned range of the buffer:
offsetInBytesmust not be negative,offsetInBytesmust be divisible by four,lengthmust not be negative, andoffsetInBytes + length * 4must not be greater thanlengthInBytes.
import 'raya:typed_data';
void main() {
final bits = Uint32List.fromList([0x3F800000, 0x40490FDB, 0x7F800000]);
final floats = bits.buffer.asFloat32List();
print('1.0 bits -> ${floats[0]}');
print('pi bits -> ${floats[1]}');
print('infinity bits-> ${floats[2]}');
}1.0 bits -> 1.0 pi bits -> 3.1415927410125732 infinity bits-> Infinity
Float64List asFloat64List([int offsetInBytes = 0, int? length])Creates a Float64List view of a region of this byte buffer.
The view is backed by the bytes of this byte buffer. Any changes made to the Float64List will also change the buffer, and vice versa.
The viewed region start at offsetInBytes, which must be 64-bit aligned, and contains length 64-bit integers. If length is omitted, the range extends as far towards the end of the buffer as possible - if lengthInBytes is not divisible by eight, the last bytes can't be part of the view.
The start index and length must describe a valid 64-bit aligned range of the buffer:
offsetInBytesmust not be negative,offsetInBytesmust be divisible by eight,lengthmust not be negative, andoffsetInBytes + length * 8must not be greater thanlengthInBytes.
import 'raya:typed_data';
void main() {
final buf = Uint8List(24).buffer;
final d = buf.asFloat64List();
d[0] = 0.1;
d[1] = 1e300;
print('values: ${d.sublist(0, 2)}');
print('second double from offset 8: ${buf.asFloat64List(8, 1)}');
print('bytes of 0.1: ${buf.asUint8List(0, 8)}');
}values: [0.1, 1e+300] second double from offset 8: [1e+300] bytes of 0.1: [154, 153, 153, 153, 153, 153, 185, 63]
Float32x4List asFloat32x4List([int offsetInBytes = 0, int? length])Creates a Float32x4List view of a region of this byte buffer.
The view is backed by the bytes of this byte buffer. Any changes made to the Float32x4List will also change the buffer, and vice versa.
The viewed region start at offsetInBytes, which must be 128-bit aligned, and contains length 128-bit integers. If length is omitted, the range extends as far towards the end of the buffer as possible - if lengthInBytes is not divisible by 16, the last bytes can't be part of the view.
The start index and length must describe a valid 128-bit aligned range of the buffer:
offsetInBytesmust not be negative,offsetInBytesmust be divisible by sixteen,lengthmust not be negative, andoffsetInBytes + length * 16must not be greater thanlengthInBytes.
import 'raya:typed_data';
void main() {
final f = Float32List.fromList([1, 2, 3, 4, 10, 20, 30, 40]);
final v = f.buffer.asFloat32x4List();
final sum = v[0] + v[1];
print('vectors: ${v.length}');
print('sum lanes: ${sum.x} ${sum.y} ${sum.z} ${sum.w}');
}vectors: 2 sum lanes: 11.0 22.0 33.0 44.0
Float64x2List asFloat64x2List([int offsetInBytes = 0, int? length])Creates a Float64x2List view of a region of this byte buffer.
The view is backed by the bytes of this byte buffer. Any changes made to the Float64x2List will also change the buffer, and vice versa.
The viewed region start at offsetInBytes, which must be 128-bit aligned, and contains length 128-bit integers. If length is omitted, the range extends as far towards the end of the buffer as possible - if lengthInBytes is not divisible by 16, the last bytes can't be part of the view.
The start index and length must describe a valid 128-bit aligned range of the buffer:
offsetInBytesmust not be negative,offsetInBytesmust be divisible by sixteen,lengthmust not be negative, andoffsetInBytes + length * 16must not be greater thanlengthInBytes.
import 'raya:typed_data';
void main() {
final d = Float64List.fromList([1.5, 2.5, 3.0, 4.0]);
final pairs = d.buffer.asFloat64x2List();
for (var i = 0; i < pairs.length; i++) {
print('pair $i: (${pairs[i].x}, ${pairs[i].y})');
}
}pair 0: (1.5, 2.5) pair 1: (3.0, 4.0)
ByteData asByteData([int offsetInBytes = 0, int? length])Creates a ByteData view of a region of this byte buffer.
The view is backed by the bytes of this byte buffer. Any changes made to the ByteData will also change the buffer, and vice versa.
The viewed region start at offsetInBytes and contains length bytes. If length is omitted, the range extends to the end of the buffer.
The start index and length must describe a valid range of the buffer:
offsetInBytesmust not be negative,lengthmust not be negative, andoffsetInBytes + lengthmust not be greater thanlengthInBytes.
import 'raya:typed_data';
void main() {
final bytes = Uint8List.fromList([0x12, 0x34, 0x56, 0x78]);
final view = bytes.buffer.asByteData();
print('big-endian u32: 0x${view.getUint32(0).toRadixString(16)}');
print('little-endian u32: 0x${view.getUint32(0, Endian.little).toRadixString(16)}');
print('u16 at 2 (big): 0x${bytes.buffer.asByteData(2).getUint16(0).toRadixString(16)}');
}big-endian u32: 0x12345678 little-endian u32: 0x78563412 u16 at 2 (big): 0x5678
TypedData
abstract final class TypedData
A typed view of a sequence of bytes.
It is a compile-time error for a class to attempt to extend or implement TypedData.
Properties
int get elementSizeInBytesThe number of bytes in the representation of each element in this list.
int get offsetInBytesThe offset of this view into the underlying byte buffer, in bytes.
TypedDataList
abstract final class TypedDataList<E> implements TypedData, List<E>
Endian
final class Endian
Endianness of number representation.
The order of bytes in memory of a number representation, with little endian having the least significant byte first, and big endian (aka. Network byte order) having the most significant byte first.
The host endian is the native endianness of the underlying platform, and the default endianness used by typed-data lists, like Uint16List, on this platform. Always one of little or big endian.
Can be specified when accessing or updating a sequence of bytes using a ByteData view. The host endianness can be used if accessing larger numbers by their bytes, for example through a Uint8List view on a buffer written using an Int64List view of the same buffer..
It is a compile-time error for a class to attempt to extend or implement Endian.
import 'raya:typed_data';
void main() {
final b = ByteData(4)..setInt32(0, 1, Endian.little);
print('1 little-endian: ${b.buffer.asUint8List()}');
b.setInt32(0, 1, Endian.big);
print('1 big-endian: ${b.buffer.asUint8List()}');
}1 little-endian: [1, 0, 0, 0] 1 big-endian: [0, 0, 0, 1]
Static
static const Endian big = Endian._(false)static const Endian little = Endian._(true)static final Endian hostByteData
abstract final class ByteData implements TypedData
A fixed-length, random-access sequence of bytes that also provides random and unaligned access to the fixed-width integers and floating point numbers represented by those bytes.
ByteData may be used to pack and unpack data from external sources (such as networks or files systems), and to process large quantities of numerical data more efficiently than would be possible with ordinary List implementations. ByteData can save space, by eliminating the need for object headers, and time, by eliminating the need for data copies.
If data comes in as bytes, they can be converted to ByteData by sharing the same buffer.
Uint8List bytes = ...;
var blob = ByteData.sublistView(bytes);
if (blob.getUint32(0, Endian.little) == 0x04034b50) { // Zip file marker
...
}
Finally, ByteData may be used to intentionally reinterpret the bytes representing one arithmetic type as another. For example this code fragment determine what 32-bit signed integer is represented by the bytes of a 32-bit floating point number (both stored as big endian):
var bdata = ByteData(8);
bdata.setFloat32(0, 3.04);
int huh = bdata.getInt32(0); // 0x40428f5c
It is a compile-time error for a class to attempt to extend or implement ByteData.
import 'raya:typed_data';
void main() {
final b = ByteData(8);
b.setUint16(0, 0xCAFE, Endian.big);
b.setUint16(2, 0xCAFE, Endian.little);
b.setFloat32(4, 1.5);
final bytes = b.buffer.asUint8List();
print('bytes: ${bytes.map((x) => x.toRadixString(16).padLeft(2, '0')).join(' ')}');
print('read big-endian: 0x${b.getUint16(0).toRadixString(16)}');
print('read little-endian: 0x${b.getUint16(2, Endian.little).toRadixString(16)}');
print('float32 back: ${b.getFloat32(4)}');
}bytes: ca fe fe ca 3f c0 00 00 read big-endian: 0xcafe read little-endian: 0xcafe float32 back: 1.5
Constructors
external factory ByteData(int length)Creates a ByteData of the specified length (in elements), all of whose bytes are initially zero.
import 'raya:typed_data';
void main() {
final b = ByteData(6);
print('lengthInBytes: ${b.lengthInBytes}');
b.setUint8(0, 7);
b.setUint16(1, 0x0102);
print('bytes: ${b.buffer.asUint8List()}');
}lengthInBytes: 6 bytes: [7, 1, 2, 0, 0, 0]
factory ByteData.view(ByteBuffer buffer, [int offsetInBytes = 0, int? length])Creates an ByteData view of the specified region in buffer.
Changes in the ByteData will be visible in the byte buffer and vice versa. If the offsetInBytes index of the region is not specified, it defaults to zero (the first byte in the byte buffer). If the length is not provided, the view extends to the end of the byte buffer.
The offsetInBytes and length must be non-negative, and offsetInBytes + length must be less than or equal to the length of buffer.
Note that when creating a view from a TypedData list or byte data, that list or byte data may itself be a view on a larger buffer with a TypedData.offsetInBytes greater than zero. Merely doing ByteData.view(other.buffer, 0, count) may not point to the bytes you intended. Instead you may need to do:
ByteData.view(other.buffer, other.offsetInBytes, count)
Alternatively, use ByteData.sublistView which includes this computation:
ByteData.sublistView(other, 0, count);
(The third argument is an end index rather than a length, so if you start from a position greater than zero, you need not reduce the count correspondingly).
import 'raya:typed_data';
void main() {
final bytes = Uint8List.fromList([0, 0, 0x01, 0x00, 0, 0]);
final mid = ByteData.view(bytes.buffer, 2, 2);
print('view length: ${mid.lengthInBytes}');
print('u16 big: ${mid.getUint16(0)}');
mid.setUint8(1, 0xFF);
print('backing: $bytes');
}view length: 2 u16 big: 256 backing: [0, 0, 1, 255, 0, 0]
factory ByteData.sublistView(TypedData data, [int start = 0, int? end])Creates a ByteData view on a range of elements of data.
Creates a view on the range of data.buffer which corresponds to the elements of data from start until end. If data is a typed data list, like Uint16List, then the view is on the bytes of the elements with indices from start until end. If data is a ByteData, it's treated like a list of bytes.
If provided, start and end must satisfy
0 ≤ start ≤ end ≤ elementCount
Where elementCount is the number of elements in data, which is the same as the List.length of a typed data list.
If omitted, start defaults to zero and end to elementCount.
import 'raya:typed_data';
void main() {
final samples = Int16List.fromList([100, -200, 300]);
// Elements 1..3 of an Int16List = bytes 2..6.
final view = ByteData.sublistView(samples, 1);
print('view bytes: ${view.lengthInBytes}');
print('sample 1 LE: ${view.getInt16(0, Endian.little)}');
print('sample 2 LE: ${view.getInt16(2, Endian.little)}');
}view bytes: 4 sample 1 LE: -200 sample 2 LE: 300
Methods
ByteData asUnmodifiableView()A read-only view of this ByteData.
import 'raya:typed_data';
void main() {
final b = ByteData(2)..setUint16(0, 513);
final ro = b.asUnmodifiableView();
print('read: ${ro.getUint16(0)}');
try {
ro.setUint8(0, 1);
} on UnsupportedError catch (e) {
print('write refused: ${e.runtimeType}');
}
b.setUint16(0, 7);
print('sees owner change: ${ro.getUint16(0)}');
}read: 513 write refused: UnsupportedError sees owner change: 7
int getInt8(int byteOffset)The (possibly negative) integer represented by the byte at the specified byteOffset in this object, in two's complement binary representation.
The return value will be between -128 and 127, inclusive.
The byteOffset must be non-negative, and less than the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData.view(Uint8List.fromList([5, 127, 128, 255]).buffer);
for (var i = 0; i < 4; i++) {
print('byte $i: uint8 ${b.getUint8(i)} int8 ${b.getInt8(i)}');
}
}byte 0: uint8 5 int8 5 byte 1: uint8 127 int8 127 byte 2: uint8 128 int8 -128 byte 3: uint8 255 int8 -1
void setInt8(int byteOffset, int value)Sets the byte at the specified byteOffset in this object to the two's complement binary representation of the specified value, which must fit in a single byte.
In other words, value must be between -128 and 127, inclusive.
The byteOffset must be non-negative, and less than the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData(3);
b.setInt8(0, -1);
b.setInt8(1, -128);
b.setInt8(2, 130); // only the low 8 bits are kept
print('bytes: ${b.buffer.asUint8List()}');
print('as int8: ${[b.getInt8(0), b.getInt8(1), b.getInt8(2)]}');
}bytes: [255, 128, 130] as int8: [-1, -128, -126]
int getUint8(int byteOffset)The positive integer represented by the byte at the specified byteOffset in this object, in unsigned binary form.
The return value will be between 0 and 255, inclusive.
The byteOffset must be non-negative, and less than the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData(4)..setInt32(0, -2);
print('bytes of -2 (big-endian): ${[for (var i = 0; i < 4; i++) b.getUint8(i)]}');
print('checksum: ${[for (var i = 0; i < 4; i++) b.getUint8(i)].reduce((a, c) => a ^ c)}');
}bytes of -2 (big-endian): [255, 255, 255, 254] checksum: 1
void setUint8(int byteOffset, int value)Sets the byte at the specified byteOffset in this object to the unsigned binary representation of the specified value, which must fit in a single byte.
In other words, value must be between 0 and 255, inclusive.
The byteOffset must be non-negative, and less than the length of this object.
import 'raya:typed_data';
void main() {
final header = ByteData(4);
header.setUint8(0, 0x89);
header.setUint8(1, 0x50); // 'P'
header.setUint8(2, 0x4E); // 'N'
header.setUint8(3, 0x47 + 256); // wraps to 0x47 'G'
print('header: ${header.buffer.asUint8List().map((x) => x.toRadixString(16)).join(' ')}');
print('text: ${String.fromCharCodes(header.buffer.asUint8List(1))}');
}header: 89 50 4e 47 text: PNG
int getInt16(int byteOffset, [Endian endian = Endian.big])The (possibly negative) integer represented by the two bytes at the specified byteOffset in this object, in two's complement binary form.
The return value will be between -2<sup>15</sup> and 2<sup>15</sup> - 1, inclusive.
The byteOffset must be non-negative, and byteOffset + 2 must be less than or equal to the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData.view(Uint8List.fromList([0xFF, 0xFE, 0x00, 0x80]).buffer);
print('big @0: ${b.getInt16(0)}');
print('little @0: ${b.getInt16(0, Endian.little)}');
print('big @2: ${b.getInt16(2)}');
print('little @2: ${b.getInt16(2, Endian.little)}');
}big @0: -2 little @0: -257 big @2: 128 little @2: -32768
void setInt16(int byteOffset, int value, [Endian endian = Endian.big])Sets the two bytes starting at the specified byteOffset in this object to the two's complement binary representation of the specified value, which must fit in two bytes.
In other words, value must lie between -2<sup>15</sup> and 2<sup>15</sup> - 1, inclusive.
The byteOffset must be non-negative, and byteOffset + 2 must be less than or equal to the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData(4);
b.setInt16(0, -300);
b.setInt16(2, -300, Endian.little);
print('bytes: ${b.buffer.asUint8List()}');
print('read back: ${b.getInt16(0)}, ${b.getInt16(2, Endian.little)}');
}bytes: [254, 212, 212, 254] read back: -300, -300
int getUint16(int byteOffset, [Endian endian = Endian.big])The positive integer represented by the two bytes starting at the specified byteOffset in this object, in unsigned binary form.
The return value will be between 0 and 2<sup>16</sup> - 1, inclusive.
The byteOffset must be non-negative, and byteOffset + 2 must be less than or equal to the length of this object.
import 'raya:typed_data';
void main() {
// A 4-byte record: port (big-endian) then a little-endian counter.
final b = ByteData.view(Uint8List.fromList([0x1F, 0x90, 0x2A, 0x00]).buffer);
print('port: ${b.getUint16(0)}');
print('counter: ${b.getUint16(2, Endian.little)}');
}port: 8080 counter: 42
void setUint16(int byteOffset, int value, [Endian endian = Endian.big])Sets the two bytes starting at the specified byteOffset in this object to the unsigned binary representation of the specified value, which must fit in two bytes.
In other words, value must be between 0 and 2<sup>16</sup> - 1, inclusive.
The byteOffset must be non-negative, and byteOffset + 2 must be less than or equal to the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData(4);
b.setUint16(0, 8080);
b.setUint16(2, 70000); // keeps the low 16 bits
print('bytes: ${b.buffer.asUint8List()}');
print('70000 stored as ${b.getUint16(2)} (= 70000 - 65536)');
}bytes: [31, 144, 17, 112] 70000 stored as 4464 (= 70000 - 65536)
int getInt32(int byteOffset, [Endian endian = Endian.big])The (possibly negative) integer represented by the four bytes at the specified byteOffset in this object, in two's complement binary form.
The return value will be between -2<sup>31</sup> and 2<sup>31</sup> - 1, inclusive.
The byteOffset must be non-negative, and byteOffset + 4 must be less than or equal to the length of this object.
import 'raya:typed_data';
void main() {
// Parse a little binary header: magic, version, count.
final data = Uint8List.fromList([0x52, 0x41, 0x59, 0x41, 2, 0, 0xE8, 0x03, 0, 0]);
final b = ByteData.sublistView(data);
final magic = String.fromCharCodes(data.sublist(0, 4));
print('magic: $magic');
print('version: ${b.getUint16(4, Endian.little)}');
print('count: ${b.getInt32(6, Endian.little)}');
}magic: RAYA version: 2 count: 1000
void setInt32(int byteOffset, int value, [Endian endian = Endian.big])Sets the four bytes starting at the specified byteOffset in this object to the two's complement binary representation of the specified value, which must fit in four bytes.
In other words, value must lie between -2<sup>31</sup> and 2<sup>31</sup> - 1, inclusive.
The byteOffset must be non-negative, and byteOffset + 4 must be less than or equal to the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData(8);
b.setInt32(0, -123456);
b.setInt32(4, -123456, Endian.little);
print('big: ${b.buffer.asUint8List(0, 4)}');
print('little: ${b.buffer.asUint8List(4, 4)}');
print('back: ${b.getInt32(0)} ${b.getInt32(4, Endian.little)}');
}big: [255, 254, 29, 192] little: [192, 29, 254, 255] back: -123456 -123456
int getUint32(int byteOffset, [Endian endian = Endian.big])The positive integer represented by the four bytes starting at the specified byteOffset in this object, in unsigned binary form.
The return value will be between 0 and 2<sup>32</sup> - 1, inclusive.
The byteOffset must be non-negative, and byteOffset + 4 must be less than or equal to the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData(4)..setInt32(0, -1);
print('as int32: ${b.getInt32(0)}');
print('as uint32: ${b.getUint32(0)}');
final magic = ByteData.view(Uint8List.fromList([0xCA, 0xFE, 0xBA, 0xBE]).buffer);
print('magic: 0x${magic.getUint32(0).toRadixString(16)}');
}as int32: -1 as uint32: 4294967295 magic: 0xcafebabe
void setUint32(int byteOffset, int value, [Endian endian = Endian.big])Sets the four bytes starting at the specified byteOffset in this object to the unsigned binary representation of the specified value, which must fit in four bytes.
In other words, value must be between 0 and 2<sup>32</sup> - 1, inclusive.
The byteOffset must be non-negative, and byteOffset + 4 must be less than or equal to the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData(8);
b.setUint32(0, 0xDEADBEEF);
b.setUint32(4, 0xDEADBEEF, Endian.little);
String hex(int x) => x.toRadixString(16).padLeft(2, '0');
print('big: ${b.buffer.asUint8List(0, 4).map(hex).join(' ')}');
print('little: ${b.buffer.asUint8List(4, 4).map(hex).join(' ')}');
}big: de ad be ef little: ef be ad de
int getInt64(int byteOffset, [Endian endian = Endian.big])The (possibly negative) integer represented by the eight bytes at the specified byteOffset in this object, in two's complement binary form.
The return value will be between -2<sup>63</sup> and 2<sup>63</sup> - 1, inclusive.
The byteOffset must be non-negative, and byteOffset + 8 must be less than or equal to the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData(8);
b.setUint32(0, 0xFFFFFFFF);
b.setUint32(4, 0xFFFFFFFE);
print('int64 big-endian: ${b.getInt64(0)}');
print('as two uint32: ${b.getUint32(0)}, ${b.getUint32(4)}');
}int64 big-endian: -2 as two uint32: 4294967295, 4294967294
void setInt64(int byteOffset, int value, [Endian endian = Endian.big])Sets the eight bytes starting at the specified byteOffset in this object to the two's complement binary representation of the specified value, which must fit in eight bytes.
In other words, value must lie between -2<sup>63</sup> and 2<sup>63</sup> - 1, inclusive.
The byteOffset must be non-negative, and byteOffset + 8 must be less than or equal to the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData(16);
b.setInt64(0, 1 << 40);
b.setInt64(8, -5, Endian.little);
print('1<<40 big: ${b.buffer.asUint8List(0, 8)}');
print('-5 little: ${b.buffer.asUint8List(8, 8)}');
print('read back: ${b.getInt64(0)}, ${b.getInt64(8, Endian.little)}');
}1<<40 big: [0, 0, 1, 0, 0, 0, 0, 0] -5 little: [251, 255, 255, 255, 255, 255, 255, 255] read back: 1099511627776, -5
int getUint64(int byteOffset, [Endian endian = Endian.big])The positive integer represented by the eight bytes starting at the specified byteOffset in this object, in unsigned binary form.
The return value will be between 0 and 2<sup>64</sup> - 1, inclusive.
The byteOffset must be non-negative, and byteOffset + 8 must be less than or equal to the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData(8)..setUint64(0, 0x0102030405060708);
print('big-endian read: 0x${b.getUint64(0).toRadixString(16)}');
print('little-endian read: 0x${b.getUint64(0, Endian.little).toRadixString(16)}');
}big-endian read: 0x102030405060708 little-endian read: 0x807060504030201
void setUint64(int byteOffset, int value, [Endian endian = Endian.big])Sets the eight bytes starting at the specified byteOffset in this object to the unsigned binary representation of the specified value, which must fit in eight bytes.
In other words, value must be between 0 and 2<sup>64</sup> - 1, inclusive.
The byteOffset must be non-negative, and byteOffset + 8 must be less than or equal to the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData(8);
b.setUint64(0, 0x0102030405060708);
print('big-endian bytes: ${b.buffer.asUint8List()}');
b.setUint64(0, 0x0102030405060708, Endian.little);
print('little-endian: ${b.buffer.asUint8List()}');
}big-endian bytes: [1, 2, 3, 4, 5, 6, 7, 8] little-endian: [8, 7, 6, 5, 4, 3, 2, 1]
double getFloat32(int byteOffset, [Endian endian = Endian.big])The floating point number represented by the four bytes at the specified byteOffset in this object, in IEEE 754 single-precision binary floating-point format (binary32).
The byteOffset must be non-negative, and byteOffset + 4 must be less than or equal to the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData(4)..setUint32(0, 0x40490FDB);
print('float32 from bits 0x40490fdb: ${b.getFloat32(0)}');
b.setFloat32(0, 0.1);
print('0.1 stored as float32 reads: ${b.getFloat32(0)}');
}float32 from bits 0x40490fdb: 3.1415927410125732 0.1 stored as float32 reads: 0.10000000149011612
void setFloat32(int byteOffset, double value, [Endian endian = Endian.big])Sets the four bytes starting at the specified byteOffset in this object to the IEEE 754 single-precision binary floating-point (binary32) representation of the specified value.
Note that this method can lose precision. The input value is a 64-bit floating point value, which will be converted to 32-bit floating point value by IEEE 754 rounding rules before it is stored. If value cannot be represented exactly as a binary32, it will be converted to the nearest binary32 value. If two binary32 values are equally close, the one whose least significant bit is zero will be used. Note that finite (but large) values can be converted to infinity, and small non-zero values can be converted to zero.
The byteOffset must be non-negative, and byteOffset + 4 must be less than or equal to the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData(8);
b.setFloat32(0, 1.0);
b.setFloat32(4, -2.5, Endian.little);
print('1.0 big: 0x${b.getUint32(0).toRadixString(16)}');
print('-2.5 little: ${b.buffer.asUint8List(4, 4)}');
print('back: ${b.getFloat32(0)} ${b.getFloat32(4, Endian.little)}');
}1.0 big: 0x3f800000 -2.5 little: [0, 0, 32, 192] back: 1.0 -2.5
double getFloat64(int byteOffset, [Endian endian = Endian.big])The floating point number represented by the eight bytes at the specified byteOffset in this object, in IEEE 754 double-precision binary floating-point format (binary64).
The byteOffset must be non-negative, and byteOffset + 8 must be less than or equal to the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData(8);
b.setUint32(0, 0x3FF00000); // high word of 1.0
print('1.0 from bits: ${b.getFloat64(0)}');
b.setFloat64(0, 0.1, Endian.little);
print('little read: ${b.getFloat64(0, Endian.little)}');
print('wrong order: ${b.getFloat64(0)}');
}1.0 from bits: 1.0 little read: 0.1 wrong order: -1.5423487136675799e-180
void setFloat64(int byteOffset, double value, [Endian endian = Endian.big])Sets the eight bytes starting at the specified byteOffset in this object to the IEEE 754 double-precision binary floating-point (binary64) representation of the specified value.
The byteOffset must be non-negative, and byteOffset + 8 must be less than or equal to the length of this object.
import 'raya:typed_data';
void main() {
final b = ByteData(8)..setFloat64(0, 1.0);
print('1.0 as IEEE-754 bits: ${b.buffer.asUint8List().map((x) => x.toRadixString(16).padLeft(2, '0')).join()}');
b.setFloat64(0, -0.5);
print('-0.5: ${b.buffer.asUint8List().map((x) => x.toRadixString(16).padLeft(2, '0')).join()}');
}1.0 as IEEE-754 bits: 3ff0000000000000 -0.5: bfe0000000000000
Int8List
abstract final class Int8List implements _TypedIntList
A fixed-length list of 8-bit signed integers.
For long lists, this implementation can be considerably more space- and time-efficient than the default List implementation.
Integers stored in the list are truncated to their low eight bits, interpreted as a signed 8-bit two's complement integer with values in the range -128 to +127.
It is a compile-time error for a class to attempt to extend or implement Int8List.
Constructors
external factory Int8List(int length)Creates an Int8List of the specified length (in elements), all of whose elements are initially zero.
The list is backed by a ByteBuffer containing precisely length bytes.
import 'raya:typed_data';
void main() {
final a = Int8List(4);
a[0] = 100;
a[1] = 127 + 1; // wraps to -128
a[2] = 200; // 200 - 256 = -56
a[3] = -129; // wraps to 127
print('Int8List: $a');
print('range: -128..127, ${a.elementSizeInBytes} byte per element');
}Int8List: [100, -128, -56, 127] range: -128..127, 1 byte per element
external factory Int8List.fromList(List<int> elements)Creates a Int8List with the same length as the elements list and copies over the elements.
Values are truncated to fit in the list when they are copied, the same way storing values truncates them.
The list is backed by a ByteBuffer containing precisely elements.length bytes.
import 'raya:typed_data';
void main() {
final deltas = Int8List.fromList([5, -3, 0, 250, -7]);
print('stored: $deltas');
var level = 0;
final levels = [for (final d in deltas) level += d];
print('running: $levels');
}stored: [5, -3, 0, -6, -7] running: [5, 2, 2, -4, -11]
factory Int8List.view(ByteBuffer buffer, [int offsetInBytes = 0, int? length])Creates an Int8List view of the specified region in buffer.
Changes in the Int8List will be visible in the byte buffer and vice versa. If the offsetInBytes index of the region is not specified, it defaults to zero (the first byte in the byte buffer). If the length is not provided, the view extends to the end of the byte buffer.
The offsetInBytes and length must be non-negative, and offsetInBytes + (length * bytesPerElement) must be less than or equal to the length of buffer.
Note that when creating a view from a TypedData list or byte data, that list or byte data may itself be a view on a larger buffer with a TypedData.offsetInBytes greater than zero. Merely doing Int8List.view(other.buffer, 0, count) may not point to the bytes you intended. Instead you may need to do:
Int8List.view(other.buffer, other.offsetInBytes, count)
Alternatively, use Int8List.sublistView which includes this computation:
Int8List.sublistView(other, 0, count);
(The third argument is an end index rather than a length, so if you start from a position greater than zero, you need not reduce the count correspondingly).
import 'raya:typed_data';
void main() {
final bytes = Uint8List.fromList([10, 246, 128, 1]);
final signed = Int8List.view(bytes.buffer);
print('unsigned: $bytes');
print('signed: $signed');
print('last two: ${Int8List.view(bytes.buffer, 2, 2)}');
}unsigned: [10, 246, 128, 1] signed: [10, -10, -128, 1] last two: [-128, 1]
factory Int8List.sublistView(TypedData data, [int start = 0, int? end])Creates an Int8List view on a range of elements of data.
Creates a view on the range of data.buffer which corresponds to the elements of data from start until end. If data is a typed data list, like Uint16List, then the view is on the bytes of the elements with indices from start until end. If data is a ByteData, it's treated like a list of bytes.
If provided, start and end must satisfy
0 ≤ start ≤ end ≤ elementCount
Where elementCount is the number of elements in data, which is the same as the List.length of a typed data list.
If omitted, start defaults to zero and end to elementCount.
import 'raya:typed_data';
void main() {
final pcm = Int16List.fromList([0x0102, -2]);
// Look at the 16-bit samples as signed bytes (host order).
final bytes = Int8List.sublistView(pcm);
print('bytes of both samples: $bytes');
print('bytes of sample 1: ${Int8List.sublistView(pcm, 1, 2)}');
}bytes of both samples: [2, 1, -2, -1] bytes of sample 1: [-2, -1]
Static
static const int bytesPerElement = 1Methods
Int8List asUnmodifiableView()A read-only view of this Int8List;
import 'raya:typed_data';
void main() {
final a = Int8List.fromList([1, -1]);
final ro = a.asUnmodifiableView();
try {
ro[0] = 5;
} on UnsupportedError {
print('cannot write through the view');
}
a[0] = -100;
print('view follows owner: $ro');
}cannot write through the view view follows owner: [-100, -1]
Int8List sublist(int start, [int? end])Creates a new list containing the elements between start and end.
The new list is an Int8List containing the elements of this list at positions greater than or equal to start and less than end in the same order as they occur in this list.
var numbers = Int8List.fromList([0, 1, 2, 3, 4]);
print(numbers.sublist(1, 3)); // [1, 2]
print(numbers.sublist(1, 3).runtimeType); // Int8List
If end is omitted, it defaults to the length of this list.
print(numbers.sublist(1)); // [1, 2, 3, 4]
The start and end positions must satisfy the relations 0 ≤ start ≤ end ≤ this.length. If end is equal to start, then the returned list is empty.
import 'raya:typed_data';
void main() {
final a = Int8List.fromList([-3, -2, -1, 0, 1, 2, 3]);
final neg = a.sublist(0, 3);
neg[0] = 99;
print('negatives (copy): $neg');
print('original intact: $a');
print('from index 4: ${a.sublist(4)}');
}negatives (copy): [99, -2, -1] original intact: [-3, -2, -1, 0, 1, 2, 3] from index 4: [1, 2, 3]
Uint8List
abstract final class Uint8List implements _TypedIntList
A fixed-length list of 8-bit unsigned integers.
For long lists, this implementation can be considerably more space- and time-efficient than the default List implementation.
Integers stored in the list are truncated to their low eight bits, interpreted as an unsigned 8-bit integer with values in the range 0 to 255.
It is a compile-time error for a class to attempt to extend or implement Uint8List.
import 'raya:typed_data';
void main() {
final bytes = Uint8List(4);
bytes[0] = 255;
bytes[1] = 256; // wraps: only the low 8 bits are kept
bytes[2] = -1;
print('bytes: $bytes');
final copy = Uint8List.fromList([1, 2, 3, 4, 5]);
print('sublist: ${copy.sublist(1, 4)}');
print('lengthInBytes: ${copy.lengthInBytes}');
}bytes: [255, 0, 255, 0] sublist: [2, 3, 4] lengthInBytes: 5
Constructors
external factory Uint8List(int length)Creates a Uint8List of the specified length (in elements), all of whose elements are initially zero.
The list is backed by a ByteBuffer containing precisely length bytes.
import 'raya:typed_data';
void main() {
final pixel = Uint8List(3);
pixel[0] = 255;
pixel[1] = 128;
pixel[2] = 300; // 300 & 0xFF = 44
print('rgb: $pixel');
print('hex: #${pixel.map((c) => c.toRadixString(16).padLeft(2, '0')).join()}');
}rgb: [255, 128, 44] hex: #ff802c
external factory Uint8List.fromList(List<int> elements)Creates a Uint8List with the same length as the elements list and copies over the elements.
Values are truncated to fit in the list when they are copied, the same way storing values truncates them.
The list is backed by a ByteBuffer containing precisely elements.length bytes.
import 'raya:typed_data';
void main() {
final ascii = Uint8List.fromList('Raya!'.codeUnits);
print('bytes: $ascii');
print('sum mod 256: ${ascii.fold<int>(0, (s, b) => (s + b) & 0xFF)}');
print('negative wraps: ${Uint8List.fromList([-1, -2])}');
}bytes: [82, 97, 121, 97, 33] sum mod 256: 174 negative wraps: [255, 254]
factory Uint8List.view(ByteBuffer buffer, [int offsetInBytes = 0, int? length])Creates a Uint8List view of the specified region in buffer.
Changes in the Uint8List will be visible in the byte buffer and vice versa. If the offsetInBytes index of the region is not specified, it defaults to zero (the first byte in the byte buffer). If the length is not provided, the view extends to the end of the byte buffer.
The offsetInBytes and length must be non-negative, and offsetInBytes + (length * bytesPerElement) must be less than or equal to the length of buffer.
Note that when creating a view from a TypedData list or byte data, that list or byte data may itself be a view on a larger buffer with a TypedData.offsetInBytes greater than zero. Merely doing Uint8List.view(other.buffer, 0, count) may not point to the bytes you intended. Instead you may need to do:
Uint8List.view(other.buffer, other.offsetInBytes, count)
Alternatively, use Uint8List.sublistView which includes this computation:
Uint8List.sublistView(other, 0, count);
(The third argument is an end index rather than a length, so if you start from a position greater than zero, you need not reduce the count correspondingly).
import 'raya:typed_data';
void main() {
final words = Uint32List.fromList([0x04030201, 0x08070605]);
final bytes = Uint8List.view(words.buffer);
print('the same memory as bytes: $bytes');
bytes[0] = 0xFF;
print('first word now: 0x${words[0].toRadixString(16)}');
}the same memory as bytes: [1, 2, 3, 4, 5, 6, 7, 8] first word now: 0x40302ff
factory Uint8List.sublistView(TypedData data, [int start = 0, int? end])Creates a Uint8List view on a range of elements of data.
Creates a view on the range of data.buffer which corresponds to the elements of data from start until end. If data is a typed data list, like Uint16List, then the view is on the bytes of the elements with indices from start until end. If data is a ByteData, it's treated like a list of bytes.
If provided, start and end must satisfy
0 ≤ start ≤ end ≤ elementCount
Where elementCount is the number of elements in data, which is the same as the List.length of a typed data list.
If omitted, start defaults to zero and end to elementCount.
import 'raya:typed_data';
void main() {
final words = Uint32List.fromList([0x11223344]);
final bytes = Uint8List.sublistView(words);
print('bytes of the word (host order): ${bytes.map((b) => b.toRadixString(16)).toList()}');
final packet = Uint8List.fromList([9, 8, 7, 6, 5]);
final payload = Uint8List.sublistView(packet, 1, 4);
payload[0] = 0;
print('payload: $payload, packet: $packet');
}bytes of the word (host order): [44, 33, 22, 11] payload: [0, 7, 6], packet: [9, 0, 7, 6, 5]
Static
static const int bytesPerElement = 1Methods
Uint8List asUnmodifiableView()A read-only view of this Uint8List.
import 'raya:typed_data';
void main() {
final key = Uint8List.fromList([1, 2, 3, 4]);
final shared = key.asUnmodifiableView();
try {
shared.fillRange(0, 4, 0);
} on UnsupportedError {
print('readers cannot wipe the key');
}
print('key: $shared');
}readers cannot wipe the key key: [1, 2, 3, 4]
Uint8List sublist(int start, [int? end])Creates a new list containing the elements between start and end.
The new list is a Uint8List containing the elements of this list at positions greater than or equal to start and less than end in the same order as they occur in this list.
var numbers = Uint8List.fromList([0, 1, 2, 3, 4]);
print(numbers.sublist(1, 3)); // [1, 2]
print(numbers.sublist(1, 3).runtimeType); // Uint8List
If end is omitted, it defaults to the length of this list.
print(numbers.sublist(1)); // [1, 2, 3, 4]
The start and end positions must satisfy the relations 0 ≤ start ≤ end ≤ this.length. If end is equal to start, then the returned list is empty.
import 'raya:typed_data';
void main() {
final frame = Uint8List.fromList([0x7E, 3, 0x41, 0x42, 0x43, 0x7E]);
final len = frame[1];
final body = frame.sublist(2, 2 + len);
print('body: ${String.fromCharCodes(body)}');
print('type: ${body.runtimeType}');
}body: ABC type: Uint8List
Operators
List<int> operator +(List<int> other)Returns a concatenation of this list and other.
If other is also a typed-data list, then the return list will be a typed data list capable of holding both unsigned 8-bit integers and the elements of other, otherwise it'll be a normal list of integers.
import 'raya:typed_data';
void main() {
final a = Uint8List.fromList([1, 2]);
final b = Uint8List.fromList([3, 4]);
final joined = a + b;
print('a + b: $joined');
print('is Uint8List: ${joined is Uint8List}');
print('as bytes again: ${Uint8List.fromList(joined)}');
}a + b: [1, 2, 3, 4] is Uint8List: false as bytes again: [1, 2, 3, 4]
Uint8ClampedList
abstract final class Uint8ClampedList implements _TypedIntList
A fixed-length list of 8-bit unsigned integers.
For long lists, this implementation can be considerably more space- and time-efficient than the default List implementation.
Integers stored in the list are clamped to an unsigned eight bit value. That is, all values below zero are stored as zero and all values above 255 are stored as 255.
It is a compile-time error for a class to attempt to extend or implement Uint8ClampedList.
Constructors
external factory Uint8ClampedList(int length)Creates a Uint8ClampedList of the specified length (in elements), all of whose elements are initially zero.
The list is backed by a ByteBuffer containing precisely length bytes.
import 'raya:typed_data';
void main() {
final c = Uint8ClampedList(4);
c[0] = 300; // clamps to 255
c[1] = -40; // clamps to 0
c[2] = 100;
final w = Uint8List(4);
w[0] = 300;
w[1] = -40;
print('clamped: $c');
print('wrapped: $w');
}clamped: [255, 0, 100, 0] wrapped: [44, 216, 0, 0]
external factory Uint8ClampedList.fromList(List<int> elements)Creates a Uint8ClampedList of the same size as the elements list and copies over the values clamping when needed.
Values are clamped to fit in the list when they are copied, the same way storing values clamps them.
The list is backed by a ByteBuffer containing precisely elements.length bytes.
import 'raya:typed_data';
void main() {
final brightened = Uint8ClampedList.fromList([for (final v in [10, 120, 200, 250]) v + 60]);
print('brightened pixels: $brightened');
print('rounding 127.5 -> ${Uint8ClampedList.fromList([127, 128])}');
}brightened pixels: [70, 180, 255, 255] rounding 127.5 -> [127, 128]
factory Uint8ClampedList.view(ByteBuffer buffer, [int offsetInBytes = 0, int? length])Creates a Uint8ClampedList view of the specified region in the specified byte buffer.
Changes in the Uint8List will be visible in the byte buffer and vice versa. If the offsetInBytes index of the region is not specified, it defaults to zero (the first byte in the byte buffer). If the length is not provided, the view extends to the end of the byte buffer.
The offsetInBytes and length must be non-negative, and offsetInBytes + (length * bytesPerElement) must be less than or equal to the length of buffer.
Note that when creating a view from a TypedData list or byte data, that list or byte data may itself be a view on a larger buffer with a TypedData.offsetInBytes greater than zero. Merely doing Uint8ClampedList.view(other.buffer, 0, count) may not point to the bytes you intended. Instead you may need to do:
Uint8ClampedList.view(other.buffer, other.offsetInBytes, count)
Alternatively, use Uint8ClampedList.sublistView which includes this computation:
Uint8ClampedList.sublistView(other, 0, count);
(The third argument is an end index rather than a length, so if you start from a position greater than zero, you need not reduce the count correspondingly).
import 'raya:typed_data';
void main() {
final buf = Uint8List(3).buffer;
final safe = Uint8ClampedList.view(buf);
for (var i = 0; i < 3; i++) {
safe[i] = 100 * i + 100; // 100, 200, 300
}
print('clamped writes: $safe');
print('raw bytes: ${buf.asUint8List()}');
}clamped writes: [100, 200, 255] raw bytes: [100, 200, 255]
factory Uint8ClampedList.sublistView(TypedData data, [int start = 0, int? end])Creates a Uint8ClampedList view on a range of elements of data.
Creates a view on the range of data.buffer which corresponds to the elements of data from start until end. If data is a typed data list, like Uint16List, then the view is on the bytes of the elements with indices from start until end. If data is a ByteData, it's treated like a list of bytes.
If provided, start and end must satisfy
0 ≤ start ≤ end ≤ elementCount
Where elementCount is the number of elements in data, which is the same as the List.length of a typed data list.
If omitted, start defaults to zero and end to elementCount.
import 'raya:typed_data';
void main() {
final image = Uint8ClampedList.fromList([10, 20, 30, 40, 50, 60]); // 2 RGB pixels
final second = Uint8ClampedList.sublistView(image, 3, 6);
for (var i = 0; i < 3; i++) {
second[i] = second[i] * 6;
}
print('pixel 2 x6: $second');
print('image: $image');
}pixel 2 x6: [240, 255, 255] image: [10, 20, 30, 240, 255, 255]
Static
static const int bytesPerElement = 1Methods
Uint8ClampedList asUnmodifiableView()A read-only view of this Uint8ClampedList.
import 'raya:typed_data';
void main() {
final px = Uint8ClampedList.fromList([255, 0, 0]);
final ro = px.asUnmodifiableView();
try {
ro[1] = 255;
} on UnsupportedError {
print('read-only view');
}
print('red pixel: $ro');
}read-only view red pixel: [255, 0, 0]
Uint8ClampedList sublist(int start, [int? end])Creates a new list containing the elements between start and end.
The new list is a Uint8ClampedList containing the elements of this list at positions greater than or equal to start and less than end in the same order as they occur in this list.
var numbers = Uint8ClampedList.fromList([0, 1, 2, 3, 4]);
print(numbers.sublist(1, 3)); // [1, 2]
print(numbers.sublist(1, 3).runtimeType); // Uint8ClampedList
If end is omitted, it defaults to the length of this list.
print(numbers.sublist(1)); // [1, 2, 3, 4]
The start and end positions must satisfy the relations 0 ≤ start ≤ end ≤ this.length. If end is equal to start, then the returned list is empty.
import 'raya:typed_data';
void main() {
final px = Uint8ClampedList.fromList([10, 20, 30, 40]);
final copy = px.sublist(1, 3);
copy[0] = 1000;
print('copy (clamps too): $copy, ${copy.runtimeType}');
print('original: $px');
}copy (clamps too): [255, 30], Uint8ClampedList original: [10, 20, 30, 40]
Int16List
abstract final class Int16List implements _TypedIntList
A fixed-length list of 16-bit signed integers that is viewable as a TypedData.
For long lists, this implementation can be considerably more space- and time-efficient than the default List implementation.
Integers stored in the list are truncated to their low 16 bits, interpreted as a signed 16-bit two's complement integer with values in the range -32768 to +32767.
It is a compile-time error for a class to attempt to extend or implement Int16List.
Constructors
external factory Int16List(int length)Creates an Int16List of the specified length (in elements), all of whose elements are initially zero.
The list is backed by a ByteBuffer containing precisely length times 2 bytes.
import 'raya:typed_data';
void main() {
final samples = Int16List(4);
samples[0] = 32767;
samples[1] = 32768; // wraps to -32768
samples[2] = -1;
samples[3] = 70000; // 70000 - 65536 = 4464
print('samples: $samples');
print('${samples.lengthInBytes} bytes for ${samples.length} samples');
}samples: [32767, -32768, -1, 4464] 8 bytes for 4 samples
external factory Int16List.fromList(List<int> elements)Creates a Int16List with the same length as the elements list and copies over the elements.
Values are truncated to fit in the list when they are copied, the same way storing values truncates them.
The list is backed by a ByteBuffer containing precisely elements.length times 2 bytes.
import 'raya:typed_data';
void main() {
// 8 samples of a square wave at full scale.
final wave = Int16List.fromList([for (var i = 0; i < 8; i++) i < 4 ? 32767 : -32768]);
print('wave: $wave');
final halved = Int16List.fromList([for (final s in wave) s ~/ 2]);
print('-6 dB: $halved');
}wave: [32767, 32767, 32767, 32767, -32768, -32768, -32768, -32768] -6 dB: [16383, 16383, 16383, 16383, -16384, -16384, -16384, -16384]
factory Int16List.view(ByteBuffer buffer, [int offsetInBytes = 0, int? length])Creates an Int16List view of the specified region in buffer.
Changes in the Int16List will be visible in the byte buffer and vice versa. If the offsetInBytes index of the region is not specified, it defaults to zero (the first byte in the byte buffer). If the length is not provided, the view extends to the end of the byte buffer.
The offsetInBytes and length must be non-negative, and offsetInBytes + (length * bytesPerElement) must be less than or equal to the length of buffer.
The offsetInBytes must be a multiple of bytesPerElement.
Note that when creating a view from a TypedData list or byte data, that list or byte data may itself be a view on a larger buffer with a TypedData.offsetInBytes greater than zero. Merely doing Int16List.view(other.buffer, 0, count) may not point to the bytes you intended. Instead you may need to do:
Int16List.view(other.buffer, other.offsetInBytes, count)
Alternatively, use Int16List.sublistView which includes this computation:
Int16List.sublistView(other, 0, count);
(The third argument is an end index rather than a length, so if you start from a position greater than zero, you need not reduce the count correspondingly).
import 'raya:typed_data';
void main() {
final le = Uint8List.fromList([0x00, 0x80, 0xFF, 0x7F]); // little-endian samples
final samples = Int16List.view(le.buffer);
print('samples (host order): $samples');
print('second only: ${Int16List.view(le.buffer, 2, 1)}');
}samples (host order): [-32768, 32767] second only: [32767]
factory Int16List.sublistView(TypedData data, [int start = 0, int? end])Creates an Int16List view on a range of elements of data.
Creates a view on the range of data.buffer which corresponds to the elements of data from start until end. If data is a typed data list, like Uint16List, then the view is on the bytes of the elements with indices from start until end. If data is a ByteData, it's treated like a list of bytes.
If provided, start and end must satisfy
0 ≤ start ≤ end ≤ elementCount
Where elementCount is the number of elements in data, which is the same as the List.length of a typed data list.
If omitted, start defaults to zero and end to elementCount.
The start and end indices of the range of bytes being viewed must be multiples of two.
import 'raya:typed_data';
void main() {
final stereo = Int16List.fromList([1, -1, 2, -2, 3, -3]);
final firstTwoFrames = Int16List.sublistView(stereo, 0, 4);
print('frames 0-1: $firstTwoFrames');
firstTwoFrames[0] = 100;
print('shares memory: ${stereo[0]}');
}frames 0-1: [1, -1, 2, -2] shares memory: 100
Static
static const int bytesPerElement = 2Methods
Int16List asUnmodifiableView()A read-only view of this Int16List.
import 'raya:typed_data';
void main() {
final table = Int16List.fromList([0, 12539, 23170, 30273, 32767]);
final sine = table.asUnmodifiableView();
print('quarter-wave table: $sine');
try {
sine[0] = 1;
} on UnsupportedError {
print('table is read-only');
}
}quarter-wave table: [0, 12539, 23170, 30273, 32767] table is read-only
Int16List sublist(int start, [int? end])Creates a new list containing the elements between start and end.
The new list is an Int16List containing the elements of this list at positions greater than or equal to start and less than end in the same order as they occur in this list.
var numbers = Int16List.fromList([0, 1, 2, 3, 4]);
print(numbers.sublist(1, 3)); // [1, 2]
print(numbers.sublist(1, 3).runtimeType); // Int16List
If end is omitted, it defaults to the length of this list.
print(numbers.sublist(1)); // [1, 2, 3, 4]
The start and end positions must satisfy the relations 0 ≤ start ≤ end ≤ this.length. If end is equal to start, then the returned list is empty.
import 'raya:typed_data';
void main() {
final s = Int16List.fromList([-300, -200, -100, 0, 100]);
final tail = s.sublist(3);
print('tail: $tail (${tail.runtimeType})');
print('peak of first three: ${s.sublist(0, 3).reduce((a, b) => a.abs() > b.abs() ? a : b)}');
}tail: [0, 100] (Int16List) peak of first three: -300
Uint16List
abstract final class Uint16List implements _TypedIntList
A fixed-length list of 16-bit unsigned integers that is viewable as a TypedData.
For long lists, this implementation can be considerably more space- and time-efficient than the default List implementation.
Integers stored in the list are truncated to their low 16 bits, interpreted as an unsigned 16-bit integer with values in the range 0 to 65535.
It is a compile-time error for a class to attempt to extend or implement Uint16List.
Constructors
external factory Uint16List(int length)Creates a Uint16List of the specified length (in elements), all of whose elements are initially zero.
The list is backed by a ByteBuffer containing precisely length times 2 bytes.
import 'raya:typed_data';
void main() {
final units = Uint16List(3);
units[0] = 0x48; // 'H'
units[1] = 0x20AC; // euro sign
units[2] = 0x10041; // keeps low 16 bits: 0x41 'A'
print('code units: $units');
print('string: ${String.fromCharCodes(units)}');
}code units: [72, 8364, 65] string: H€A
external factory Uint16List.fromList(List<int> elements)Creates a Uint16List with the same length as the elements list and copies over the elements.
Values are truncated to fit in the list when they are copied, the same way storing values truncates them.
The list is backed by a ByteBuffer containing precisely elements.length times 2 bytes.
import 'raya:typed_data';
void main() {
final s = 'héllo €';
final units = Uint16List.fromList(s.codeUnits);
print('UTF-16 units: $units');
print('round trip: ${String.fromCharCodes(units)}');
print('-1 stored as: ${Uint16List.fromList([-1])}');
}UTF-16 units: [104, 233, 108, 108, 111, 32, 8364] round trip: héllo € -1 stored as: [65535]
factory Uint16List.view(ByteBuffer buffer, [int offsetInBytes = 0, int? length])Creates a Uint16List view of the specified region in the specified byte buffer.
Changes in the Uint16List will be visible in the byte buffer and vice versa. If the offsetInBytes index of the region is not specified, it defaults to zero (the first byte in the byte buffer). If the length is not provided, the view extends to the end of the byte buffer.
The offsetInBytes and length must be non-negative, and offsetInBytes + (length * bytesPerElement) must be less than or equal to the length of buffer.
The offsetInBytes must be a multiple of bytesPerElement.
Note that when creating a view from a TypedData list or byte data, that list or byte data may itself be a view on a larger buffer with a TypedData.offsetInBytes greater than zero. Merely doing Uint16List.view(other.buffer, 0, count) may not point to the bytes you intended. Instead you may need to do:
Uint16List.view(other.buffer, other.offsetInBytes, count)
Alternatively, use Uint16List.sublistView which includes this computation:
Uint16List.sublistView(other, 0, count);
(The third argument is an end index rather than a length, so if you start from a position greater than zero, you need not reduce the count correspondingly).
import 'raya:typed_data';
void main() {
final bytes = Uint8List.fromList([0xFF, 0xFF, 0x01, 0x00]);
final u16 = Uint16List.view(bytes.buffer);
print('uint16 (host order): $u16');
print('from byte 2: ${Uint16List.view(bytes.buffer, 2)}');
}uint16 (host order): [65535, 1] from byte 2: [1]
factory Uint16List.sublistView(TypedData data, [int start = 0, int? end])Creates a Uint16List view on a range of elements of data.
Creates a view on the range of data.buffer which corresponds to the elements of data from start until end. If data is a typed data list, like Uint16List, then the view is on the bytes of the elements with indices from start until end. If data is a ByteData, it's treated like a list of bytes.
If provided, start and end must satisfy
0 ≤ start ≤ end ≤ elementCount
Where elementCount is the number of elements in data, which is the same as the List.length of a typed data list.
If omitted, start defaults to zero and end to elementCount.
The start and end indices of the range of bytes being viewed must be multiples of two.
import 'raya:typed_data';
void main() {
final text = Uint16List.fromList('Hello, Raya'.codeUnits);
final word = Uint16List.sublistView(text, 7);
print('word: ${String.fromCharCodes(word)}');
word[0] = 'K'.codeUnitAt(0);
print('text now: ${String.fromCharCodes(text)}');
}word: Raya text now: Hello, Kaya
Static
static const int bytesPerElement = 2Methods
Uint16List asUnmodifiableView()A read-only view of this Uint16List.
import 'raya:typed_data';
void main() {
final ports = Uint16List.fromList([22, 80, 443]);
final ro = ports.asUnmodifiableView();
try {
ro.add(8080);
} on UnsupportedError {
print('fixed length and read-only');
}
print('ports: $ro');
}fixed length and read-only ports: [22, 80, 443]
Uint16List sublist(int start, [int? end])Creates a new list containing the elements between start and end.
The new list is a Uint16List containing the elements of this list at positions greater than or equal to start and less than end in the same order as they occur in this list.
var numbers = Uint16List.fromList([0, 1, 2, 3, 4]);
print(numbers.sublist(1, 3)); // [1, 2]
print(numbers.sublist(1, 3).runtimeType); // Uint16List
If end is omitted, it defaults to the length of this list.
print(numbers.sublist(1)); // [1, 2, 3, 4]
The start and end positions must satisfy the relations 0 ≤ start ≤ end ≤ this.length. If end is equal to start, then the returned list is empty.
import 'raya:typed_data';
void main() {
final u = Uint16List.fromList([10, 20, 30, 40]);
final mid = u.sublist(1, 3)..[0] = 65535;
print('copy: $mid');
print('orig: $u');
}copy: [65535, 30] orig: [10, 20, 30, 40]
Int32List
abstract final class Int32List implements _TypedIntList
A fixed-length list of 32-bit signed integers that is viewable as a TypedData.
For long lists, this implementation can be considerably more space- and time-efficient than the default List implementation.
Integers stored in the list are truncated to their low 32 bits, interpreted as a signed 32-bit two's complement integer with values in the range -2147483648 to 2147483647.
It is a compile-time error for a class to attempt to extend or implement Int32List.
Constructors
external factory Int32List(int length)Creates an Int32List of the specified length (in elements), all of whose elements are initially zero.
The list is backed by a ByteBuffer containing precisely length times 4 bytes.
import 'raya:typed_data';
void main() {
final a = Int32List(3);
a[0] = 2147483647;
a[1] = 2147483647 + 1; // wraps to -2147483648
a[2] = 1 << 33; // high bits dropped: 0
print('int32: $a');
}int32: [2147483647, -2147483648, 0]
external factory Int32List.fromList(List<int> elements)Creates a Int32List with the same length as the elements list and copies over the elements.
Values are truncated to fit in the list when they are copied, the same way storing values truncates them.
The list is backed by a ByteBuffer containing precisely elements.length times 4 bytes.
import 'raya:typed_data';
void main() {
final scores = Int32List.fromList([1200, -50, 980, 3000000000]);
print('stored: $scores');
print('3000000000 wrapped to ${scores[3]} (= 3000000000 - 2^32)');
}stored: [1200, -50, 980, -1294967296] 3000000000 wrapped to -1294967296 (= 3000000000 - 2^32)
factory Int32List.view(ByteBuffer buffer, [int offsetInBytes = 0, int? length])Creates an Int32List view of the specified region in buffer.
Changes in the Int32List will be visible in the byte buffer and vice versa. If the offsetInBytes index of the region is not specified, it defaults to zero (the first byte in the byte buffer). If the length is not provided, the view extends to the end of the byte buffer.
The offsetInBytes and length must be non-negative, and offsetInBytes + (length * bytesPerElement) must be less than or equal to the length of buffer.
The offsetInBytes must be a multiple of bytesPerElement.
Note that when creating a view from a TypedData list or byte data, that list or byte data may itself be a view on a larger buffer with a TypedData.offsetInBytes greater than zero. Merely doing Int32List.view(other.buffer, 0, count) may not point to the bytes you intended. Instead you may need to do:
Int32List.view(other.buffer, other.offsetInBytes, count)
Alternatively, use Int32List.sublistView which includes this computation:
Int32List.sublistView(other, 0, count);
(The third argument is an end index rather than a length, so if you start from a position greater than zero, you need not reduce the count correspondingly).
import 'raya:typed_data';
void main() {
final bytes = Uint8List(8);
final ints = Int32List.view(bytes.buffer);
ints[0] = -1;
ints[1] = 256;
print('ints: $ints');
print('bytes: $bytes');
}ints: [-1, 256] bytes: [255, 255, 255, 255, 0, 1, 0, 0]
factory Int32List.sublistView(TypedData data, [int start = 0, int? end])Creates an Int32List view on a range of elements of data.
Creates a view on the range of data.buffer which corresponds to the elements of data from start until end. If data is a typed data list, like Uint16List, then the view is on the bytes of the elements with indices from start until end. If data is a ByteData, it's treated like a list of bytes.
If provided, start and end must satisfy
0 ≤ start ≤ end ≤ elementCount
Where elementCount is the number of elements in data, which is the same as the List.length of a typed data list.
If omitted, start defaults to zero and end to elementCount.
The start and end indices of the range of bytes being viewed must be multiples of four.
import 'raya:typed_data';
void main() {
final f = Float32List.fromList([1.0, -0.0]);
// Reinterpret floats as their raw bits.
final bits = Int32List.sublistView(f);
print('1.0 bits: 0x${bits[0].toRadixString(16)}');
print('-0.0 bits: ${bits[1]} (sign bit only)');
}1.0 bits: 0x3f800000 -0.0 bits: -2147483648 (sign bit only)
Static
static const int bytesPerElement = 4Methods
Int32List asUnmodifiableView()A read-only view of this Int16List.
import 'raya:typed_data';
void main() {
final counts = Int32List.fromList([3, 1, 4]);
final ro = counts.asUnmodifiableView();
try {
ro.sort();
} on UnsupportedError {
print('sort refused on read-only view');
}
counts.sort();
print('owner sorted, view sees: $ro');
}sort refused on read-only view owner sorted, view sees: [1, 3, 4]
Int32List sublist(int start, [int? end])Creates a new list containing the elements between start and end.
The new list is an Int32List containing the elements of this list at positions greater than or equal to start and less than end in the same order as they occur in this list.
var numbers = Int32List.fromList([0, 1, 2, 3, 4]);
print(numbers.sublist(1, 3)); // [1, 2]
print(numbers.sublist(1, 3).runtimeType); // Int32List
If end is omitted, it defaults to the length of this list.
print(numbers.sublist(1)); // [1, 2, 3, 4]
The start and end positions must satisfy the relations 0 ≤ start ≤ end ≤ this.length. If end is equal to start, then the returned list is empty.
import 'raya:typed_data';
void main() {
final a = Int32List.fromList([5, 10, 15, 20, 25]);
final window = a.sublist(1, 4);
print('window: $window, sum ${window.reduce((x, y) => x + y)}');
print('end defaults to length: ${a.sublist(3)}');
}window: [10, 15, 20], sum 45 end defaults to length: [20, 25]
Uint32List
abstract final class Uint32List implements _TypedIntList
A fixed-length list of 32-bit unsigned integers that is viewable as a TypedData.
For long lists, this implementation can be considerably more space- and time-efficient than the default List implementation.
Integers stored in the list are truncated to their low 32 bits, interpreted as an unsigned 32-bit integer with values in the range 0 to 4294967295.
It is a compile-time error for a class to attempt to extend or implement Uint32List.
Constructors
external factory Uint32List(int length)Creates a Uint32List of the specified length (in elements), all of whose elements are initially zero.
The list is backed by a ByteBuffer containing precisely length times 4 bytes.
import 'raya:typed_data';
void main() {
final u = Uint32List(3);
u[0] = 0xFFFFFFFF;
u[1] = -1; // same bits: 4294967295
u[2] = 0x1FFFFFFFF; // high bit dropped
print('uint32: $u');
}uint32: [4294967295, 4294967295, 4294967295]
external factory Uint32List.fromList(List<int> elements)Creates a Uint32List with the same length as the elements list and copies over the elements.
Values are truncated to fit in the list when they are copied, the same way storing values truncates them.
The list is backed by a ByteBuffer containing precisely elements.length times 4 bytes.
import 'raya:typed_data';
void main() {
final colors = Uint32List.fromList([0xFF0000FF, 0x00FF00FF, 0x0000FFFF]);
for (final c in colors) {
print('rgba ${c.toRadixString(16).padLeft(8, '0')}: r=${c >> 24} g=${(c >> 16) & 0xFF} b=${(c >> 8) & 0xFF}');
}
}rgba ff0000ff: r=255 g=0 b=0 rgba 00ff00ff: r=0 g=255 b=0 rgba 0000ffff: r=0 g=0 b=255
factory Uint32List.view(ByteBuffer buffer, [int offsetInBytes = 0, int? length])Creates a Uint32List view of the specified region in the specified byte buffer.
Changes in the Uint32List will be visible in the byte buffer and vice versa. If the offsetInBytes index of the region is not specified, it defaults to zero (the first byte in the byte buffer). If the length is not provided, the view extends to the end of the byte buffer.
The offsetInBytes and length must be non-negative, and offsetInBytes + (length * bytesPerElement) must be less than or equal to the length of buffer.
The offsetInBytes must be a multiple of bytesPerElement.
Note that when creating a view from a TypedData list or byte data, that list or byte data may itself be a view on a larger buffer with a TypedData.offsetInBytes greater than zero. Merely doing Uint32List.view(other.buffer, 0, count) may not point to the bytes you intended. Instead you may need to do:
Uint32List.view(other.buffer, other.offsetInBytes, count)
Alternatively, use Uint32List.sublistView which includes this computation:
Uint32List.sublistView(other, 0, count);
(The third argument is an end index rather than a length, so if you start from a position greater than zero, you need not reduce the count correspondingly).
import 'raya:typed_data';
void main() {
final bytes = Uint8List.fromList([1, 0, 0, 0, 0, 0, 0, 128]);
final u = Uint32List.view(bytes.buffer);
print('uint32 (host order): $u');
print('second word only: ${Uint32List.view(bytes.buffer, 4, 1)}');
}uint32 (host order): [1, 2147483648] second word only: [2147483648]
factory Uint32List.sublistView(TypedData data, [int start = 0, int? end])Creates a Uint32List view on a range of elements of data.
Creates a view on the range of data.buffer which corresponds to the elements of data from start until end. If data is a typed data list, like Uint16List, then the view is on the bytes of the elements with indices from start until end. If data is a ByteData, it's treated like a list of bytes.
If provided, start and end must satisfy
0 ≤ start ≤ end ≤ elementCount
Where elementCount is the number of elements in data, which is the same as the List.length of a typed data list.
If omitted, start defaults to zero and end to elementCount.
The start and end indices of the range of bytes being viewed must be multiples of four.
import 'raya:typed_data';
void main() {
final f = Float32List.fromList([0.1, 0.2, 0.3]);
final bits = Uint32List.sublistView(f, 1);
print('bits of 0.2, 0.3: ${bits.map((b) => '0x${b.toRadixString(16)}').toList()}');
}bits of 0.2, 0.3: [0x3e4ccccd, 0x3e99999a]
Static
static const int bytesPerElement = 4Methods
Uint32List asUnmodifiableView()A read-only view of this Uint32List.
import 'raya:typed_data';
void main() {
final crcTable = Uint32List.fromList([for (var n = 0; n < 4; n++) n * 0x77073096 & 0xFFFFFFFF]);
final ro = crcTable.asUnmodifiableView();
print('table: ${ro.map((v) => v.toRadixString(16)).toList()}');
try {
ro[0] = 1;
} on UnsupportedError {
print('read-only');
}
}table: [0, 77073096, ee0e612c, 651591c2] read-only
Uint32List sublist(int start, [int? end])Creates a new list containing the elements between start and end.
The new list is a Uint32List containing the elements of this list at positions greater than or equal to start and less than end in the same order as they occur in this list.
var numbers = Uint32List.fromList([0, 1, 2, 3, 4]);
print(numbers.sublist(1, 3)); // [1, 2]
print(numbers.sublist(1, 3).runtimeType); // Uint32List
If end is omitted, it defaults to the length of this list.
print(numbers.sublist(1)); // [1, 2, 3, 4]
The start and end positions must satisfy the relations 0 ≤ start ≤ end ≤ this.length. If end is equal to start, then the returned list is empty.
import 'raya:typed_data';
void main() {
final u = Uint32List.fromList([1, 2, 3, 4]);
final s = u.sublist(2);
s[0] = 0xFFFFFFFF;
print('copy: $s');
print('orig: $u');
}copy: [4294967295, 4] orig: [1, 2, 3, 4]
Int64List
abstract final class Int64List implements _TypedIntList
A fixed-length list of 64-bit signed integers that is viewable as a TypedData.
For long lists, this implementation can be considerably more space- and time-efficient than the default List implementation.
Integers stored in the list are truncated to their low 64 bits, interpreted as a signed 64-bit two's complement integer with values in the range -9223372036854775808 to +9223372036854775807.
It is a compile-time error for a class to attempt to extend or implement Int64List.
Constructors
external factory Int64List(int length)Creates an Int64List of the specified length (in elements), all of whose elements are initially zero.
The list is backed by a ByteBuffer containing precisely length times 8 bytes.
import 'raya:typed_data';
void main() {
final a = Int64List(3);
a[0] = 9223372036854775807; // max int64
a[1] = -9223372036854775808; // min int64
a[2] = 1 << 62;
print('int64: $a');
print('${a.elementSizeInBytes} bytes per element');
}int64: [9223372036854775807, -9223372036854775808, 4611686018427387904] 8 bytes per element
external factory Int64List.fromList(List<int> elements)Creates a Int64List with the same length as the elements list and copies over the elements.
Values are truncated to fit in the list when they are copied, the same way storing values truncates them.
The list is backed by a ByteBuffer containing precisely elements.length times 8 bytes.
import 'raya:typed_data';
void main() {
final ns = Int64List.fromList([1000000000000, -1, 1 << 50]);
print('values: $ns');
print('sum: ${ns.reduce((a, b) => a + b)}');
}values: [1000000000000, -1, 1125899906842624] sum: 1126899906842623
factory Int64List.view(ByteBuffer buffer, [int offsetInBytes = 0, int? length])Creates an Int64List view of the specified region in buffer.
Changes in the Int64List will be visible in the byte buffer and vice versa. If the offsetInBytes index of the region is not specified, it defaults to zero (the first byte in the byte buffer). If the length is not provided, the view extends to the end of the byte buffer.
The offsetInBytes and length must be non-negative, and offsetInBytes + (length * bytesPerElement) must be less than or equal to the length of buffer.
The offsetInBytes must be a multiple of bytesPerElement.
Note that when creating a view from a TypedData list or byte data, that list or byte data may itself be a view on a larger buffer with a TypedData.offsetInBytes greater than zero. Merely doing Int64List.view(other.buffer, 0, count) may not point to the bytes you intended. Instead you may need to do:
Int64List.view(other.buffer, other.offsetInBytes, count)
Alternatively, use Int64List.sublistView which includes this computation:
Int64List.sublistView(other, 0, count);
(The third argument is an end index rather than a length, so if you start from a position greater than zero, you need not reduce the count correspondingly).
import 'raya:typed_data';
void main() {
final bytes = Uint8List(16);
final longs = Int64List.view(bytes.buffer);
longs[1] = -1;
print('longs: $longs');
print('bytes: $bytes');
}longs: [0, -1] bytes: [0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255]
factory Int64List.sublistView(TypedData data, [int start = 0, int? end])Creates an Int64List view on a range of elements of data.
Creates a view on the range of data.buffer which corresponds to the elements of data from start until end. If data is a typed data list, like Uint16List, then the view is on the bytes of the elements with indices from start until end. If data is a ByteData, it's treated like a list of bytes.
If provided, start and end must satisfy
0 ≤ start ≤ end ≤ elementCount
Where elementCount is the number of elements in data, which is the same as the List.length of a typed data list.
If omitted, start defaults to zero and end to elementCount.
The start and end indices of the range of bytes being viewed must be multiples of eight.
import 'raya:typed_data';
void main() {
final d = Float64List.fromList([1.0, -2.0]);
final bits = Int64List.sublistView(d);
print('1.0 bits: 0x${bits[0].toRadixString(16)}');
print('-2.0 bits: ${bits[1]} (negative: sign bit set)');
}1.0 bits: 0x3ff0000000000000 -2.0 bits: -4611686018427387904 (negative: sign bit set)
Static
static const int bytesPerElement = 8Methods
Int64List asUnmodifiableView()A read-only view of this Int64List.
import 'raya:typed_data';
void main() {
final ids = Int64List.fromList([90071992547409930, 2]);
final ro = ids.asUnmodifiableView();
try {
ro[1] = 3;
} on UnsupportedError {
print('read-only');
}
print('ids: $ro');
}read-only ids: [90071992547409930, 2]
Int64List sublist(int start, [int? end])Creates a new list containing the elements between start and end.
The new list is an Int64List containing the elements of this list at positions greater than or equal to start and less than end in the same order as they occur in this list.
var numbers = Int64List.fromList([0, 1, 2, 3, 4]);
print(numbers.sublist(1, 3)); // [1, 2]
print(numbers.sublist(1, 3).runtimeType); // Int64List
If end is omitted, it defaults to the length of this list.
print(numbers.sublist(1)); // [1, 2, 3, 4]
The start and end positions must satisfy the relations 0 ≤ start ≤ end ≤ this.length. If end is equal to start, then the returned list is empty.
import 'raya:typed_data';
void main() {
final a = Int64List.fromList([1 << 40, 1 << 41, 1 << 42]);
print('sublist(1): ${a.sublist(1)}');
print('type: ${a.sublist(1).runtimeType}');
}sublist(1): [2199023255552, 4398046511104] type: Int64List
Uint64List
abstract final class Uint64List implements _TypedIntList
A fixed-length list of 64-bit unsigned integers that is viewable as a TypedData.
For long lists, this implementation can be considerably more space- and time-efficient than the default List implementation.
Integers stored in the list are truncated to their low 64 bits, interpreted as an unsigned 64-bit integer with values in the range 0 to 18446744073709551615.
It is a compile-time error for a class to attempt to extend or implement Uint64List.
Constructors
external factory Uint64List(int length)Creates a Uint64List of the specified length (in elements), all of whose elements are initially zero.
The list is backed by a ByteBuffer containing precisely length times 8 bytes.
import 'raya:typed_data';
void main() {
final u = Uint64List(2);
u[0] = 1 << 63; // top bit set
u[1] = 12345;
// Values are read back as 64-bit ints; the top bit shows as the sign.
print('word 0 hex: 0x${u[0].toUnsigned(64).toRadixString(16)}');
print('word 1: ${u[1]}');
}word 0 hex: 0x-8000000000000000 word 1: 12345
external factory Uint64List.fromList(List<int> elements)Creates a Uint64List with the same length as the elements list and copies over the elements.
Values are truncated to fit in the list when they are copied, the same way storing values truncates them.
The list is backed by a ByteBuffer containing precisely elements.length times 8 bytes.
import 'raya:typed_data';
void main() {
final masks = Uint64List.fromList([for (var i = 0; i < 4; i++) (1 << (16 * i)) - 1]);
for (final m in masks) {
print('mask: 0x${m.toRadixString(16)}');
}
}mask: 0x0 mask: 0xffff mask: 0xffffffff mask: 0xffffffffffff
factory Uint64List.view(ByteBuffer buffer, [int offsetInBytes = 0, int? length])Creates an Uint64List view of the specified region in the specified byte buffer.
Changes in the Uint64List will be visible in the byte buffer and vice versa. If the offsetInBytes index of the region is not specified, it defaults to zero (the first byte in the byte buffer). If the length is not provided, the view extends to the end of the byte buffer.
The offsetInBytes and length must be non-negative, and offsetInBytes + (length * bytesPerElement) must be less than or equal to the length of buffer.
The offsetInBytes must be a multiple of bytesPerElement.
Note that when creating a view from a TypedData list or byte data, that list or byte data may itself be a view on a larger buffer with a TypedData.offsetInBytes greater than zero. Merely doing Uint64List.view(other.buffer, 0, count) may not point to the bytes you intended. Instead you may need to do:
Uint64List.view(other.buffer, other.offsetInBytes, count)
Alternatively, use Uint64List.sublistView which includes this computation:
Uint64List.sublistView(other, 0, count);
(The third argument is an end index rather than a length, so if you start from a position greater than zero, you need not reduce the count correspondingly).
import 'raya:typed_data';
void main() {
final bytes = Uint8List.fromList([for (var i = 1; i <= 16; i++) i]);
final words = Uint64List.view(bytes.buffer);
print('words: ${words.length}');
print('second word only: 0x${Uint64List.view(bytes.buffer, 8, 1)[0].toRadixString(16)}');
}words: 2 second word only: 0x100f0e0d0c0b0a09
factory Uint64List.sublistView(TypedData data, [int start = 0, int? end])Creates a Uint64List view on a range of elements of data.
Creates a view on the range of data.buffer which corresponds to the elements of data from start until end. If data is a typed data list, like Uint16List, then the view is on the bytes of the elements with indices from start until end. If data is a ByteData, it's treated like a list of bytes.
If provided, start and end must satisfy
0 ≤ start ≤ end ≤ elementCount
Where elementCount is the number of elements in data, which is the same as the List.length of a typed data list.
If omitted, start defaults to zero and end to elementCount.
The start and end indices of the range of bytes being viewed must be multiples of eight.
import 'raya:typed_data';
void main() {
final u32 = Uint32List.fromList([1, 0, 2, 0]);
final u64 = Uint64List.sublistView(u32);
// Two 32-bit halves make one 64-bit word (little-endian host).
print('as uint64: $u64');
}as uint64: [1, 2]
Static
static const int bytesPerElement = 8Methods
Uint64List asUnmodifiableView()A read-only view of this Uint64List.
import 'raya:typed_data';
void main() {
final bitset = Uint64List(1)..[0] = 0x5;
final ro = bitset.asUnmodifiableView();
print('bit 0 set: ${ro[0] & 1 == 1}, bit 1 set: ${ro[0] & 2 == 2}');
try {
ro[0] = 0;
} on UnsupportedError {
print('read-only');
}
}bit 0 set: true, bit 1 set: false read-only
Uint64List sublist(int start, [int? end])Creates a new list containing the elements between start and end.
The new list is a Uint64List containing the elements of this list at positions greater than or equal to start and less than end in the same order as they occur in this list.
var numbers = Uint64List.fromList([0, 1, 2, 3, 4]);
print(numbers.sublist(1, 3)); // [1, 2]
print(numbers.sublist(1, 3).runtimeType); // Uint64List
If end is omitted, it defaults to the length of this list.
print(numbers.sublist(1)); // [1, 2, 3, 4]
The start and end positions must satisfy the relations 0 ≤ start ≤ end ≤ this.length. If end is equal to start, then the returned list is empty.
import 'raya:typed_data';
void main() {
final u = Uint64List.fromList([10, 20, 30]);
final c = u.sublist(0, 2)..[0] = 99;
print('copy: $c, original: $u');
}copy: [99, 20], original: [10, 20, 30]
Float32List
abstract final class Float32List implements _TypedFloatList
A fixed-length list of IEEE 754 single-precision binary floating-point numbers that is viewable as a TypedData.
For long lists, this implementation can be considerably more space- and time-efficient than the default List implementation.
Double values stored in the list are converted to the nearest single-precision value. Values read are converted to a double value with the same value.
It is a compile-time error for a class to attempt to extend or implement Float32List.
Constructors
external factory Float32List(int length)Creates a Float32List of the specified length (in elements), all of whose elements are initially zero.
The list is backed by a ByteBuffer containing precisely length times 4 bytes.
import 'raya:typed_data';
void main() {
final f = Float32List(3);
f[0] = 0.1;
f[1] = 16777217; // 2^24 + 1 is not representable in float32
f[2] = 1e40; // too large: infinity
print('0.1 -> ${f[0]}');
print('16777217 -> ${f[1]}');
print('1e40 -> ${f[2]}');
}0.1 -> 0.10000000149011612 16777217 -> 16777216.0 1e40 -> Infinity
external factory Float32List.fromList(List<double> elements)Creates a Float32List with the same length as the elements list and copies over the elements.
Values are truncated to fit in the list when they are copied, the same way storing values truncates them.
The list is backed by a ByteBuffer containing precisely elements.length times 4 bytes.
import 'raya:typed_data';
void main() {
final values = [1 / 3, 3.14159265358979, 0.5];
final f = Float32List.fromList(values);
for (var i = 0; i < values.length; i++) {
print('${values[i]} -> ${f[i]} error ${(f[i] - values[i]).abs()}');
}
}0.3333333333333333 -> 0.3333333432674408 error 9.934107481068821e-9 3.14159265358979 -> 3.1415927410125732 error 8.7422783234814e-8 0.5 -> 0.5 error 0.0
factory Float32List.view(ByteBuffer buffer, [int offsetInBytes = 0, int? length])Creates a Float32List view of the specified region in buffer.
Changes in the Float32List will be visible in the byte buffer and vice versa. If the offsetInBytes index of the region is not specified, it defaults to zero (the first byte in the byte buffer). If the length is not provided, the view extends to the end of the byte buffer.
The offsetInBytes and length must be non-negative, and offsetInBytes + (length * bytesPerElement) must be less than or equal to the length of buffer.
The offsetInBytes must be a multiple of bytesPerElement.
Note that when creating a view from a TypedData list or byte data, that list or byte data may itself be a view on a larger buffer with a TypedData.offsetInBytes greater than zero. Merely doing Float32List.view(other.buffer, 0, count) may not point to the bytes you intended. Instead you may need to do:
Float32List.view(other.buffer, other.offsetInBytes, count)
Alternatively, use Float32List.sublistView which includes this computation:
Float32List.sublistView(other, 0, count);
(The third argument is an end index rather than a length, so if you start from a position greater than zero, you need not reduce the count correspondingly).
import 'raya:typed_data';
void main() {
final bytes = Uint8List.fromList([0, 0, 0x80, 0x3F, 0, 0, 0, 0x40]);
final f = Float32List.view(bytes.buffer);
print('floats (little-endian host): $f');
}floats (little-endian host): [1.0, 2.0]
factory Float32List.sublistView(TypedData data, [int start = 0, int? end])Creates an Float32List view on a range of elements of data.
Creates a view on the range of data.buffer which corresponds to the elements of data from start until end. If data is a typed data list, like Uint16List, then the view is on the bytes of the elements with indices from start until end. If data is a ByteData, it's treated like a list of bytes.
If provided, start and end must satisfy
0 ≤ start ≤ end ≤ elementCount
Where elementCount is the number of elements in data, which is the same as the List.length of a typed data list.
If omitted, start defaults to zero and end to elementCount.
The start and end indices of the range of bytes being viewed must be multiples of four.
import 'raya:typed_data';
void main() {
final xyz = Float32List.fromList([1, 2, 3, 4, 5, 6]); // two 3D points
final p1 = Float32List.sublistView(xyz, 3, 6);
for (var i = 0; i < 3; i++) {
p1[i] *= 10;
}
print('point 1: $p1');
print('all: $xyz');
}point 1: [40.0, 50.0, 60.0] all: [1.0, 2.0, 3.0, 40.0, 50.0, 60.0]
Static
static const int bytesPerElement = 4Methods
Float32List asUnmodifiableView()A read-only view of this Float32List.
import 'raya:typed_data';
void main() {
final weights = Float32List.fromList([0.25, 0.5, 0.25]);
final ro = weights.asUnmodifiableView();
print('kernel: $ro, sum ${ro.reduce((a, b) => a + b)}');
try {
ro[1] = 1;
} on UnsupportedError {
print('read-only');
}
}kernel: [0.25, 0.5, 0.25], sum 1.0 read-only
Float32List sublist(int start, [int? end])Creates a new list containing the elements between start and end.
The new list is a Float32List containing the elements of this list at positions greater than or equal to start and less than end in the same order as they occur in this list.
var numbers = Float32List.fromList([0.0, 1.0, 2.0, 3.0, 4.0]);
print(numbers.sublist(1, 3)); // [1.0, 2.0]
print(numbers.sublist(1, 3).runtimeType); // Float32List
If end is omitted, it defaults to the length of this list.
print(numbers.sublist(1)); // [1.0, 2.0, 3.0, 4.0]
The start and end positions must satisfy the relations 0 ≤ start ≤ end ≤ this.length. If end is equal to start, then the returned list is empty.
import 'raya:typed_data';
void main() {
final f = Float32List.fromList([0.1, 0.2, 0.3, 0.4]);
final s = f.sublist(1, 3);
print('sublist: $s (${s.runtimeType})');
print('0.2 + 0.3 in float32 = ${s[0] + s[1]}');
}sublist: [0.20000000298023224, 0.30000001192092896] (Float32List) 0.2 + 0.3 in float32 = 0.5000000149011612
Float64List
abstract final class Float64List implements _TypedFloatList
A fixed-length list of IEEE 754 double-precision binary floating-point numbers that is viewable as a TypedData.
For long lists, this implementation can be considerably more space- and time-efficient than the default List implementation.
It is a compile-time error for a class to attempt to extend or implement Float64List.
import 'raya:typed_data';
void main() {
final xs = Float64List.fromList([0.5, 1.5, 2.5]);
print(xs);
final f32 = Float32List.fromList([0.1]);
print('0.1 stored as float32 reads back as ${f32[0]}');
}[0.5, 1.5, 2.5] 0.1 stored as float32 reads back as 0.10000000149011612
Constructors
external factory Float64List(int length)Creates a Float64List of the specified length (in elements), all of whose elements are initially zero.
The list is backed by a ByteBuffer containing precisely length times 8 bytes.
import 'raya:typed_data';
void main() {
final f = Float64List(3);
f[0] = 0.1;
f[1] = 16777217; // exact in float64
f[2] = 1e300;
print('values: $f');
print('${f.lengthInBytes} bytes');
}values: [0.1, 16777217.0, 1e+300] 24 bytes
external factory Float64List.fromList(List<double> elements)Creates a Float64List with the same length as the elements list and copies over the elements.
The list is backed by a ByteBuffer containing precisely elements.length times 8 bytes.
import 'raya:typed_data';
void main() {
final xs = Float64List.fromList([for (var i = 0; i <= 4; i++) i / 4]);
final ys = Float64List.fromList([for (final x in xs) x * x]);
print('x: $xs');
print('x^2: $ys');
}x: [0.0, 0.25, 0.5, 0.75, 1.0] x^2: [0.0, 0.0625, 0.25, 0.5625, 1.0]
factory Float64List.view(ByteBuffer buffer, [int offsetInBytes = 0, int? length])Creates a Float64List view of the specified region in buffer.
Changes in the Float64List will be visible in the byte buffer and vice versa. If the offsetInBytes index of the region is not specified, it defaults to zero (the first byte in the byte buffer). If the length is not provided, the view extends to the end of the byte buffer.
The offsetInBytes and length must be non-negative, and offsetInBytes + (length * bytesPerElement) must be less than or equal to the length of buffer.
The offsetInBytes must be a multiple of bytesPerElement.
Note that when creating a view from a TypedData list or byte data, that list or byte data may itself be a view on a larger buffer with a TypedData.offsetInBytes greater than zero. Merely doing Float64List.view(other.buffer, 0, count) may not point to the bytes you intended. Instead you may need to do:
Float64List.view(other.buffer, other.offsetInBytes, count)
Alternatively, use Float64List.sublistView which includes this computation:
Float64List.sublistView(other, 0, count);
(The third argument is an end index rather than a length, so if you start from a position greater than zero, you need not reduce the count correspondingly).
import 'raya:typed_data';
void main() {
final bytes = Uint8List(16);
final d = Float64List.view(bytes.buffer);
d[0] = 1.0;
d[1] = -0.0;
print('doubles: $d');
print('bytes: $bytes');
}doubles: [1.0, -0.0] bytes: [0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 0, 128]
factory Float64List.sublistView(TypedData data, [int start = 0, int? end])Creates a Float64List view on a range of elements of data.
Creates a view on the range of data.buffer which corresponds to the elements of data from start until end. If data is a typed data list, like Uint16List, then the view is on the bytes of the elements with indices from start until end. If data is a ByteData, it's treated like a list of bytes.
If provided, start and end must satisfy
0 ≤ start ≤ end ≤ elementCount
Where elementCount is the number of elements in data, which is the same as the List.length of a typed data list.
If omitted, start defaults to zero and end to elementCount.
The start and end indices of the range of bytes being viewed must be multiples of eight.
import 'raya:typed_data';
void main() {
final matrix = Float64List.fromList([1, 2, 3, 4, 5, 6]); // 2 x 3, row-major
final row1 = Float64List.sublistView(matrix, 3, 6);
print('row 1: $row1');
row1[0] = 40;
print('matrix: $matrix');
}row 1: [4.0, 5.0, 6.0] matrix: [1.0, 2.0, 3.0, 40.0, 5.0, 6.0]
Static
static const int bytesPerElement = 8Methods
Float64List asUnmodifiableView()A read-only view of this Float64List.
import 'raya:typed_data';
void main() {
final constants = Float64List.fromList([3.141592653589793, 2.718281828459045]);
final ro = constants.asUnmodifiableView();
print('pi, e: $ro');
try {
ro[0] = 3;
} on UnsupportedError {
print('read-only');
}
}pi, e: [3.141592653589793, 2.718281828459045] read-only
Float64List sublist(int start, [int? end])Creates a new list containing the elements between start and end.
The new list is a Float64List containing the elements of this list at positions greater than or equal to start and less than end in the same order as they occur in this list.
var numbers = Float64List.fromList([0.0, 1.0, 2.0, 3.0, 4.0]);
print(numbers.sublist(1, 3)); // [1.0, 2.0]
print(numbers.sublist(1, 3).runtimeType); // Float64List
If end is omitted, it defaults to the length of this list.
print(numbers.sublist(1)); // [1.0, 2.0, 3.0, 4.0]
The start and end positions must satisfy the relations 0 ≤ start ≤ end ≤ this.length. If end is equal to start, then the returned list is empty.
import 'raya:typed_data';
void main() {
final d = Float64List.fromList([1.5, 2.5, 3.5, 4.5]);
final last2 = d.sublist(2);
print('last two: $last2, mean ${(last2[0] + last2[1]) / 2}');
}last two: [3.5, 4.5], mean 4.0
Float32x4List
abstract final class Float32x4List implements TypedDataList<Float32x4>, TypedData
A fixed-length list of Float32x4 numbers that is viewable as a TypedData.
For long lists, this implementation will be considerably more space- and time-efficient than the default List implementation.
It is a compile-time error for a class to attempt to extend or implement Float32x4List.
import 'raya:typed_data';
void main() {
// Sum 8 floats, 4 lanes at a time.
final data = Float32List.fromList([1, 2, 3, 4, 5, 6, 7, 8]);
final v = Float32x4List.view(data.buffer);
var acc = Float32x4.zero();
for (var i = 0; i < v.length; i++) {
acc += v[i];
}
print('lane sums: $acc');
print('total: ${acc.x + acc.y + acc.z + acc.w}');
}lane sums: [6.000000, 8.000000, 10.000000, 12.000000] total: 36.0
Constructors
external factory Float32x4List(int length)Creates a Float32x4List of the specified length (in elements), all of whose elements are initially zero.
The list is backed by a ByteBuffer containing precisely length times 16 bytes.
import 'raya:typed_data';
void main() {
final v = Float32x4List(2);
v[0] = Float32x4(1, 2, 3, 4);
v[1] = v[0].scale(2);
print('length: ${v.length}, bytes: ${v.lengthInBytes}');
print('v[1]: ${v[1]}');
}length: 2, bytes: 32 v[1]: [2.000000, 4.000000, 6.000000, 8.000000]
external factory Float32x4List.fromList(List<Float32x4> elements)Creates a Float32x4List with the same length as the elements list and copies over the elements.
The list is backed by a ByteBuffer containing precisely elements.length times 16 bytes.
import 'raya:typed_data';
void main() {
final points = Float32x4List.fromList([Float32x4(1, 0, 0, 1), Float32x4(0, 2, 0, 1)]);
var sum = Float32x4.zero();
for (final p in points) {
sum += p;
}
print('sum of points: $sum');
}sum of points: [1.000000, 2.000000, 0.000000, 2.000000]
factory Float32x4List.view(ByteBuffer buffer, [int offsetInBytes = 0, int? length])Creates a Float32x4List view of the specified region in buffer.
Changes in the Float32x4List will be visible in the byte buffer and vice versa. If the offsetInBytes index of the region is not specified, it defaults to zero (the first byte in the byte buffer). If the length is not provided, the view extends to the end of the byte buffer.
The offsetInBytes and length must be non-negative, and offsetInBytes + (length * bytesPerElement) must be less than or equal to the length of buffer.
The offsetInBytes must be a multiple of bytesPerElement.
Note that when creating a view from a TypedData list or byte data, that list or byte data may itself be a view on a larger buffer with a TypedData.offsetInBytes greater than zero. Merely doing Float32x4List.view(other.buffer, 0, count) may not point to the bytes you intended. Instead you may need to do:
Float32x4List.view(other.buffer, other.offsetInBytes, count)
Alternatively, use Float32x4List.sublistView which includes this computation:
Float32x4List.sublistView(other, 0, count);
(The third argument is an end index rather than a length, so if you start from a position greater than zero, you need not reduce the count correspondingly).
import 'raya:typed_data';
void main() {
final flat = Float32List.fromList([1, 2, 3, 4, 5, 6, 7, 8]);
final quads = Float32x4List.view(flat.buffer);
quads[1] = quads[1] * Float32x4.splat(10);
print('flat after x10 on second quad: $flat');
}flat after x10 on second quad: [1.0, 2.0, 3.0, 4.0, 50.0, 60.0, 70.0, 80.0]
factory Float32x4List.sublistView(TypedData data, [int start = 0, int? end])Creates a Float32x4List view on a range of elements of data.
Creates a view on the range of data.buffer which corresponds to the elements of data from start until end. If data is a typed data list, like Uint16List, then the view is on the bytes of the elements with indices from start until end. If data is a ByteData, it's treated like a list of bytes.
If provided, start and end must satisfy
0 ≤ start ≤ end ≤ elementCount
Where elementCount is the number of elements in data, which is the same as the List.length of a typed data list.
If omitted, start defaults to zero and end to elementCount.
The start and end indices of the range of bytes being viewed must be multiples of sixteen.
import 'raya:typed_data';
void main() {
final flat = Float32List.fromList([for (var i = 0; i < 12; i++) i.toDouble()]);
// sublistView start/end count elements of the source (floats here).
final mid = Float32x4List.sublistView(flat, 4, 8);
print('quads: ${mid.length}, value: ${mid[0]}');
}quads: 1, value: [4.000000, 5.000000, 6.000000, 7.000000]
Static
static const int bytesPerElement = 16Methods
Float32x4List asUnmodifiableView()A read-only view of this Float32x4List.
import 'raya:typed_data';
void main() {
final v = Float32x4List.fromList([Float32x4(1, 1, 1, 1)]);
final ro = v.asUnmodifiableView();
try {
ro[0] = Float32x4.zero();
} on UnsupportedError {
print('read-only');
}
print('still: ${ro[0]}');
}read-only still: [1.000000, 1.000000, 1.000000, 1.000000]
Float32x4List sublist(int start, [int? end])Creates a new list containing the elements between start and end.
The new list is a Float32x4List containing the elements of this list at positions greater than or equal to start and less than end in the same order as they occur in this list.
var numbers = Float32x4List.fromList([
Float32x4(0, 1, 2, 3),
Float32x4(1, 2, 3, 4),
Float32x4(2, 3, 4, 5),
Float32x4(3, 4, 5, 6),
Float32x4(4, 5, 6, 7),
]);
print(numbers.sublist(1, 2)); // [Float32x4(1, 2, 3, 4)]
print(numbers.sublist(1, 2).runtimeType); // Float32x4List
If end is omitted, it defaults to the length of this list.
print(numbers.sublist(4)); // [Float32x4(4, 5, 6, 7)]
The start and end positions must satisfy the relations 0 ≤ start ≤ end ≤ this.length. If end is equal to start, then the returned list is empty.
import 'raya:typed_data';
void main() {
final v = Float32x4List.fromList([for (var i = 0; i < 3; i++) Float32x4.splat(i.toDouble())]);
final s = v.sublist(1);
print('copy length ${s.length}: ${s[0]}, ${s[1]}');
}copy length 2: [1.000000, 1.000000, 1.000000, 1.000000], [2.000000, 2.000000, 2.000000, 2.000000]
Operators
List<Float32x4> operator +(List<Float32x4> other)The concatenation of this list and other.
If other is also a Float32x4List, the result is a new Float32x4List, otherwise the result is a normal growable List<Float32x4>.
import 'raya:typed_data';
void main() {
final a = Float32x4List.fromList([Float32x4(1, 2, 3, 4)]);
final b = Float32x4List.fromList([Float32x4(5, 6, 7, 8)]);
final joined = a + b; // concatenation, not lane-wise addition
print('length: ${joined.length}');
print('items: ${joined[0]} ${joined[1]}');
}length: 2 items: [1.000000, 2.000000, 3.000000, 4.000000] [5.000000, 6.000000, 7.000000, 8.000000]
Int32x4List
abstract final class Int32x4List implements TypedDataList<Int32x4>, TypedData
A fixed-length list of Int32x4 numbers that is viewable as a TypedData.
For long lists, this implementation will be considerably more space- and time-efficient than the default List implementation.
Constructors
external factory Int32x4List(int length)Creates a Int32x4List of the specified length (in elements), all of whose elements are initially zero.
The list is backed by a ByteBuffer containing precisely length times 16 bytes.
import 'raya:typed_data';
void main() {
final m = Int32x4List(2);
m[0] = Int32x4(1, 2, 3, 4);
m[1] = m[0] + m[0];
print('length: ${m.length}, bytes: ${m.lengthInBytes}');
print('m[1] lanes: ${m[1].x} ${m[1].y} ${m[1].z} ${m[1].w}');
}length: 2, bytes: 32 m[1] lanes: 2 4 6 8
external factory Int32x4List.fromList(List<Int32x4> elements)Creates a Int32x4List with the same length as the elements list and copies over the elements.
The list is backed by a ByteBuffer containing precisely elements.length times 16 bytes.
import 'raya:typed_data';
void main() {
final masks = Int32x4List.fromList([
Int32x4.bool(true, false, true, false),
Int32x4.bool(false, false, false, true),
]);
for (final m in masks) {
print('flags: ${m.flagX} ${m.flagY} ${m.flagZ} ${m.flagW} signMask ${m.signMask}');
}
}flags: true false true false signMask 5 flags: false false false true signMask 8
factory Int32x4List.view(ByteBuffer buffer, [int offsetInBytes = 0, int? length])Creates a Int32x4List view of the specified region in buffer.
Changes in the Int32x4List will be visible in the byte buffer and vice versa. If the offsetInBytes index of the region is not specified, it defaults to zero (the first byte in the byte buffer). If the length is not provided, the view extends to the end of the byte buffer.
The offsetInBytes and length must be non-negative, and offsetInBytes + (length * bytesPerElement) must be less than or equal to the length of buffer.
The offsetInBytes must be a multiple of bytesPerElement.
Note that when creating a view from a TypedData list or byte data, that list or byte data may itself be a view on a larger buffer with a TypedData.offsetInBytes greater than zero. Merely doing Int32x4List.view(other.buffer, 0, count) may not point to the bytes you intended. Instead you may need to do:
Int32x4List.view(other.buffer, other.offsetInBytes, count)
Alternatively, use Int32x4List.sublistView which includes this computation:
Int32x4List.sublistView(other, 0, count);
(The third argument is an end index rather than a length, so if you start from a position greater than zero, you need not reduce the count correspondingly).
import 'raya:typed_data';
void main() {
final ints = Int32List.fromList([1, 2, 3, 4, 5, 6, 7, 8]);
final quads = Int32x4List.view(ints.buffer);
quads[0] = quads[0] + quads[1];
print('ints after quad add: $ints');
}ints after quad add: [6, 8, 10, 12, 5, 6, 7, 8]
factory Int32x4List.sublistView(TypedData data, [int start = 0, int? end])Creates an Int32x4List view on a range of elements of data.
Creates a view on the range of data.buffer which corresponds to the elements of data from start until end. If data is a typed data list, like Uint16List, then the view is on the bytes of the elements with indices from start until end. If data is a ByteData, it's treated like a list of bytes.
If provided, start and end must satisfy
0 ≤ start ≤ end ≤ elementCount
Where elementCount is the number of elements in data, which is the same as the List.length of a typed data list.
If omitted, start defaults to zero and end to elementCount.
The start and end indices of the range of bytes being viewed must be multiples of sixteen.
import 'raya:typed_data';
void main() {
final ints = Int32List.fromList([for (var i = 0; i < 8; i++) i]);
final second = Int32x4List.sublistView(ints, 4);
print('one quad: ${second[0].x} ${second[0].y} ${second[0].z} ${second[0].w}');
}one quad: 4 5 6 7
Static
static const int bytesPerElement = 16Methods
Int32x4List asUnmodifiableView()A read-only view of this Int32x4List.
import 'raya:typed_data';
void main() {
final q = Int32x4List.fromList([Int32x4(9, 9, 9, 9)]);
final ro = q.asUnmodifiableView();
try {
ro[0] = Int32x4(0, 0, 0, 0);
} on UnsupportedError {
print('read-only');
}
print('x: ${ro[0].x}');
}read-only x: 9
Int32x4List sublist(int start, [int? end])Creates a new list containing the elements between start and end.
The new list is an Int32x4List containing the elements of this list at positions greater than or equal to start and less than end in the same order as they occur in this list.
var numbers = Int32x4List.fromList([
Int32x4(0, 1, 2, 3),
Int32x4(1, 2, 3, 4),
Int32x4(2, 3, 4, 5),
Int32x4(3, 4, 5, 6),
Int32x4(4, 5, 6, 7),
]);
print(numbers.sublist(1, 2)); // [Int32x4(1, 2, 3, 4)]
print(numbers.sublist(1, 2).runtimeType); // Int32x4List
If end is omitted, it defaults to the length of this list.
print(numbers.sublist(3)); // [Int32x4(3, 4, 5, 6), Int32x4(4, 5, 6, 7)]
The start and end positions must satisfy the relations 0 ≤ start ≤ end ≤ this.length. If end is equal to start, then the returned list is empty.
import 'raya:typed_data';
void main() {
final q = Int32x4List.fromList([Int32x4(1, 1, 1, 1), Int32x4(2, 2, 2, 2)]);
final s = q.sublist(1);
print('copy length: ${s.length}, first x: ${s[0].x}');
}copy length: 1, first x: 2
Operators
List<Int32x4> operator +(List<Int32x4> other)The concatenation of this list and other.
If other is also a Int32x4List, the result is a new Int32x4List, otherwise the result is a normal growable List<Int32x4>.
import 'raya:typed_data';
void main() {
final a = Int32x4List.fromList([Int32x4(1, 2, 3, 4)]);
final b = Int32x4List.fromList([Int32x4(10, 20, 30, 40)]);
final both = a + b; // concatenation
print('length: ${both.length}, x values: ${both.map((v) => v.x).toList()}');
}length: 2, x values: [1, 10]
Float64x2List
abstract final class Float64x2List implements TypedDataList<Float64x2>, TypedData
A fixed-length list of Float64x2 numbers that is viewable as a TypedData.
For long lists, this implementation will be considerably more space- and time-efficient than the default List implementation.
It is a compile-time error for a class to attempt to extend or implement Float64x2List.
Constructors
external factory Float64x2List(int length)Creates a Float64x2List of the specified length (in elements), all of whose elements have all lanes set to zero.
The list is backed by a ByteBuffer containing precisely length times 16 bytes.
import 'raya:typed_data';
void main() {
final pts = Float64x2List(2);
pts[0] = Float64x2(1.5, -2);
pts[1] = pts[0].scale(2);
print('length: ${pts.length}, bytes: ${pts.lengthInBytes}');
print('pts[1]: ${pts[1]}');
}length: 2, bytes: 32 pts[1]: [3.000000, -4.000000]
external factory Float64x2List.fromList(List<Float64x2> elements)Creates a Float64x2List with the same length as the elements list and copies over the elements.
The list is backed by a ByteBuffer containing precisely elements.length times 16 bytes.
import 'raya:typed_data';
void main() {
final pts = Float64x2List.fromList([Float64x2(0, 0), Float64x2(4, 0), Float64x2(4, 3)]);
var perimeter = 0.0;
for (var i = 0; i < pts.length; i++) {
final d = pts[(i + 1) % pts.length] - pts[i];
final sq = d * d;
perimeter += (sq.x + sq.y) == 0 ? 0 : (sq.x + sq.y);
}
print('sum of squared side lengths: $perimeter');
}sum of squared side lengths: 50.0
factory Float64x2List.view(ByteBuffer buffer, [int offsetInBytes = 0, int? length])Creates a Float64x2List view of the specified region in buffer.
Changes in the Float64x2List will be visible in the byte buffer and vice versa. If the offsetInBytes index of the region is not specified, it defaults to zero (the first byte in the byte buffer). If the length is not provided, the view extends to the end of the byte buffer.
The offsetInBytes and length must be non-negative, and offsetInBytes + (length * bytesPerElement) must be less than or equal to the length of buffer.
The offsetInBytes must be a multiple of bytesPerElement.
Note that when creating a view from a TypedData list or byte data, that list or byte data may itself be a view on a larger buffer with a TypedData.offsetInBytes greater than zero. Merely doing Float64x2List.view(other.buffer, 0, count) may not point to the bytes you intended. Instead you may need to do:
Float64x2List.view(other.buffer, other.offsetInBytes, count)
Alternatively, use Float64x2List.sublistView which includes this computation:
Float64x2List.sublistView(other, 0, count);
(The third argument is an end index rather than a length, so if you start from a position greater than zero, you need not reduce the count correspondingly).
import 'raya:typed_data';
void main() {
final flat = Float64List.fromList([1, 2, 3, 4]);
final pairs = Float64x2List.view(flat.buffer);
pairs[0] = pairs[0] + pairs[1];
print('flat after pair add: $flat');
}flat after pair add: [4.0, 6.0, 3.0, 4.0]
factory Float64x2List.sublistView(TypedData data, [int start = 0, int? end])Creates an Float64x2List view on a range of elements of data.
Creates a view on the range of data.buffer which corresponds to the elements of data from start until end. If data is a typed data list, like Uint16List, then the view is on the bytes of the elements with indices from start until end. If data is a ByteData, it's treated like a list of bytes.
If provided, start and end must satisfy
0 ≤ start ≤ end ≤ elementCount
Where elementCount is the number of elements in data, which is the same as the List.length of a typed data list.
If omitted, start defaults to zero and end to elementCount.
The start and end indices of the range of bytes being viewed must be multiples of sixteen.
import 'raya:typed_data';
void main() {
final flat = Float64List.fromList([1, 2, 3, 4, 5, 6]);
final last = Float64x2List.sublistView(flat, 4);
print('pairs: ${last.length}, (${last[0].x}, ${last[0].y})');
}pairs: 1, (5.0, 6.0)
Static
static const int bytesPerElement = 16Methods
Float64x2List asUnmodifiableView()A read-only view of this Float64x2List.
import 'raya:typed_data';
void main() {
final p = Float64x2List.fromList([Float64x2(1, 2)]);
final ro = p.asUnmodifiableView();
try {
ro[0] = Float64x2.zero();
} on UnsupportedError {
print('read-only');
}
print('still: ${ro[0]}');
}read-only still: [1.000000, 2.000000]
Float64x2List sublist(int start, [int? end])Creates a new list containing the elements between start and end.
The new list is a Float64x2List containing the elements of this list at positions greater than or equal to start and less than end in the same order as they occur in this list.
var numbers = Float64x2List.fromList([
Float64x2(0, 1),
Float64x2(1, 2),
Float64x2(2, 3),
Float64x2(3, 4),
Float64x2(4, 5),
]);
print(numbers.sublist(1, 3)); // [Float64x2(1, 2), Float64x2(2, 3)]
print(numbers.sublist(1, 3).runtimeType); // Float64x2List
If end is omitted, it defaults to the length of this list.
print(numbers.sublist(3)); // [Float64x2(3, 4), Float64x2(4, 5)]
The start and end positions must satisfy the relations 0 ≤ start ≤ end ≤ this.length. If end is equal to start, then the returned list is empty.
import 'raya:typed_data';
void main() {
final p = Float64x2List.fromList([Float64x2(1, 1), Float64x2(2, 2), Float64x2(3, 3)]);
final s = p.sublist(0, 2);
print('copy: ${s.length} pairs, last ${s[1]}');
}copy: 2 pairs, last [2.000000, 2.000000]
Operators
List<Float64x2> operator +(List<Float64x2> other)The concatenation of this list and other.
If other is also a Float64x2List, the result is a new Float64x2List, otherwise the result is a normal growable List<Float64x2>.
import 'raya:typed_data';
void main() {
final a = Float64x2List.fromList([Float64x2(1, 2)]);
final b = Float64x2List.fromList([Float64x2(3, 4)]);
final both = a + b; // concatenation, not element-wise
print('length: ${both.length}: ${both[0]} ${both[1]}');
}length: 2: [1.000000, 2.000000] [3.000000, 4.000000]
Float32x4
abstract final class Float32x4
Four 32-bit floating point values.
Float32x4 stores four 32-bit floating point values in "lanes". The lanes are named x, y, z, and w respectively.
Single operations can be performed on the multiple values of one or more Float32x4s, which will perform the corresponding operation for each lane of the operands, and provide a new Float32x4 (or similar multi-value) result with the results from each lane.
The Float32x4 class cannot be extended or implemented.
import 'raya:typed_data';
void main() {
final a = Float32x4(1, 2, 3, 4);
final b = Float32x4.splat(10);
final c = a * b + Float32x4(0.5, 0.5, 0.5, 0.5);
print('lanes: ${c.x}, ${c.y}, ${c.z}, ${c.w}');
print('max: ${a.max(Float32x4(2, 2, 2, 2))}');
print('sqrt: ${Float32x4(1, 4, 9, 16).sqrt()}');
print('shuffle wzyx: ${a.shuffle(Float32x4.wzyx)}');
}lanes: 10.5, 20.5, 30.5, 40.5 max: [2.000000, 2.000000, 3.000000, 4.000000] sqrt: [1.000000, 2.000000, 3.000000, 4.000000] shuffle wzyx: [4.000000, 3.000000, 2.000000, 1.000000]
Constructors
external factory Float32x4(double x, double y, double z, double w)Creates a Float32x4 containing the 32-bit float values of the arguments.
The created value has Float32x4.x, Float32x4.y, Float32x4.z and Float32x4.w values that are the 32-bit floating point values created from the x, y, z and w arguments by conversion from 64-bit floating point to 32-bit floating point values.
The conversion from 64-bit float to 32-bit float loses significant precision, and may become zero or infinite even if the original 64-bit floating point value was non-zero and finite.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Float32x4(1, 2.5, -3, 4);
print('lanes: ${lanes(v)}');
// Each lane is a 32-bit float, so 0.1 is stored as its nearest float.
print('0.1 as a float32 lane: ${Float32x4(0.1, 0, 0, 0).x}');
}lanes: [1.0, 2.5, -3.0, 4.0] 0.1 as a float32 lane: 0.10000000149011612
external factory Float32x4.splat(double value)Creates a Float32x4 with the same 32-bit float value four times.
The created value has the same Float32x4.x, Float32x4.y, Float32x4.z and Float32x4.w value, which is the 32-bit floating point value created by converting the 64-bit floating point value to a 32-bit floating point value.
The conversion from 64-bit float to 32-bit float loses significant precision, and may become zero or infinite even if the original 64-bit floating point value was non-zero and finite.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Float32x4.splat(2.5);
print('splat(2.5): ${lanes(v)}');
final scaled = Float32x4(1, 2, 3, 4) * Float32x4.splat(10);
print('times splat(10): ${lanes(scaled)}');
}splat(2.5): [2.5, 2.5, 2.5, 2.5] times splat(10): [10.0, 20.0, 30.0, 40.0]
external factory Float32x4.zero()Creates a Float32x4 with all values being zero.
The created value has the same Float32x4.x, Float32x4.y, Float32x4.z and Float32x4.w value, which is the 32-bit floating point zero value.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final z = Float32x4.zero();
print('zero: ${lanes(z)}');
final v = Float32x4(1, -2, 3, -4);
print('v + zero == v: ${lanes(v + z)}');
}zero: [0.0, 0.0, 0.0, 0.0] v + zero == v: [1.0, -2.0, 3.0, -4.0]
external factory Float32x4.fromInt32x4Bits(Int32x4 bits)Creates a Float32x4 with 32-bit float values from the provided bits.
The created value has Float32x4.x, Float32x4.y, Float32x4.z and Float32x4.w values, which are the 32-bit floating point values of the bit-representations of the corresponding lanes of bits.
The conversion is performed using the platform endianness for both 32-bit integers and 32-bit floating point numbers.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
// Reinterpret the 32-bit patterns; no numeric conversion.
final bits = Int32x4(0x3f800000, 0x40000000, 0xbf800000, 0);
print('bits as floats: ${lanes(Float32x4.fromInt32x4Bits(bits))}');
final back = Int32x4.fromFloat32x4Bits(Float32x4(1, 2, -1, 0));
print('1.0 as bits: 0x${back.x.toRadixString(16)}');
}bits as floats: [1.0, 2.0, -1.0, 0.0] 1.0 as bits: 0x3f800000
external factory Float32x4.fromFloat64x2(Float64x2 xy)Creates a Float32x4 with its x and y lanes set to values from xy.
The created value has Float32x4.x and Float32x4.y values which are the conversions of the Float64x2.x and Float64x2.y lanes of xy to 32-bit floating point values. The Float32x4.z and Float32x4.w lanes are set to the zero value.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final xy = Float64x2(1.5, -2.25);
final v = Float32x4.fromFloat64x2(xy);
print('from Float64x2(1.5, -2.25): ${lanes(v)} (z and w are 0)');
}from Float64x2(1.5, -2.25): [1.5, -2.25, 0.0, 0.0] (z and w are 0)
Static
Properties
int get signMaskMethods
Int32x4 lessThan(Float32x4 other)Lane-wise less-than comparison.
Compares the 32-bit floating point value of each lane of this to the value of the corresponding lane of other, using 32-bit floating point comparison. For floating point comparisons, a comparison with a NaN value is always false, and -0.0 (negative zero) is considered equal to 0.0 (positive zero), and not less strictly than it. The result for a lane is a 32-bit signed integer which is -1 (all bits set) if the value from this object is less than the value from other, and the result is 0 (all bits cleared) if not, including if either value is a NaN value.
Returns four values that are always either 0 or -1.
import 'raya:typed_data';
void main() {
final a = Float32x4(1, 5, 3, 8);
final b = Float32x4(4, 4, 4, 4);
final mask = a.lessThan(b);
print('a < b sign mask: ${mask.signMask.toRadixString(2).padLeft(4, '0')} (lane x is bit 0)');
print('min(a, b) by select: ${mask.select(a, b)}');
}a < b sign mask: 0101 (lane x is bit 0) min(a, b) by select: [1.000000, 4.000000, 3.000000, 4.000000]
Int32x4 lessThanOrEqual(Float32x4 other)Lane-wise less-than-or-equal comparison.
Compares the 32-bit floating point value of each lane of this to the value of the corresponding lane of other, using 32-bit floating point comparison. For floating point comparisons, a comparison with a NaN value is always false, and -0.0 (negative zero) is considered equal to 0.0 (positive zero), and not less strictly than it. The result for a lane is a 32-bit signed integer which is -1 (all bits set) if the value from this object is less than or equal to the value from other, and the result is 0 (all bits cleared) if not, including if either value is a NaN value.
Returns four values that are always either 0 or -1.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
List<int> mask(Int32x4 m) => [m.x, m.y, m.z, m.w];
List<bool> flags(Int32x4 m) => [m.flagX, m.flagY, m.flagZ, m.flagW];
void main() {
final a = Float32x4(1, 5, 3, 9), b = Float32x4(2, 5, 1, 9);
final m = a.lessThanOrEqual(b);
print('a <= b lane flags: ${flags(m)}');
print('as masks: ${mask(m)} (-1 = all bits set)');
print('pick a where true, else 0: ${lanes(m.select(a, Float32x4.zero()))}');
}a <= b lane flags: [true, true, false, true] as masks: [-1, -1, 0, -1] (-1 = all bits set) pick a where true, else 0: [1.0, 5.0, 0.0, 9.0]
Int32x4 greaterThan(Float32x4 other)Lane-wise greater-than comparison.
Compares the 32-bit floating point value of each lane of this to the value of the corresponding lane of other, using 32-bit floating point comparison. For floating point comparisons, a comparison with a NaN value is always false, and -0.0 (negative zero) is considered equal to 0.0 (positive zero), and not less strictly than it. The result for a lane is a 32-bit signed integer which is -1 (all bits set) if the value from this object is greater than the value from other, and the result is 0 (all bits cleared) if not, including if either value is a NaN value.
Returns four values that are always either 0 or -1.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
List<int> mask(Int32x4 m) => [m.x, m.y, m.z, m.w];
List<bool> flags(Int32x4 m) => [m.flagX, m.flagY, m.flagZ, m.flagW];
void main() {
final a = Float32x4(1, 5, 3, 9), b = Float32x4(2, 5, 1, 9);
final m = a.greaterThan(b);
print('a > b lane flags: ${flags(m)}');
print('as masks: ${mask(m)} (-1 = all bits set)');
print('pick a where true, else 0: ${lanes(m.select(a, Float32x4.zero()))}');
}a > b lane flags: [false, false, true, false] as masks: [0, 0, -1, 0] (-1 = all bits set) pick a where true, else 0: [0.0, 0.0, 3.0, 0.0]
Int32x4 greaterThanOrEqual(Float32x4 other)Lane-wise greater-than-or-equal comparison.
Compares the 32-bit floating point value of each lane of this to the value of the corresponding lane of other, using 32-bit floating point comparison. For floating point comparisons, a comparison with a NaN value is always false, and -0.0 (negative zero) is considered equal to 0.0 (positive zero), and not less strictly than it. The result for a lane is a 32-bit signed integer which is -1 (all bits set) if the value from this object is greater than or equal to the value from other, and the result is 0 (all bits cleared) if not, including if either value is a NaN value.
Returns four values that are always either 0 or -1.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
List<int> mask(Int32x4 m) => [m.x, m.y, m.z, m.w];
List<bool> flags(Int32x4 m) => [m.flagX, m.flagY, m.flagZ, m.flagW];
void main() {
final a = Float32x4(1, 5, 3, 9), b = Float32x4(2, 5, 1, 9);
final m = a.greaterThanOrEqual(b);
print('a >= b lane flags: ${flags(m)}');
print('as masks: ${mask(m)} (-1 = all bits set)');
print('pick a where true, else 0: ${lanes(m.select(a, Float32x4.zero()))}');
}a >= b lane flags: [false, true, true, true] as masks: [0, -1, -1, -1] (-1 = all bits set) pick a where true, else 0: [0.0, 5.0, 3.0, 9.0]
Int32x4 equal(Float32x4 other)Lane-wise equals comparison.
Compares the 32-bit floating point value of each lane of this to the value of the corresponding lane of other, using 32-bit floating point comparison. For floating point comparisons, a comparison with a NaN value is always false, and -0.0 (negative zero) is considered equal to 0.0 (positive zero), and not less strictly than it. The result for a lane is a 32-bit signed integer which is -1 (all bits set) if the value from this object is equal to the value from other, and the result is 0 (all bits cleared) if not, including if either value is a NaN value.
Returns four values that are always either 0 or -1.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
List<int> mask(Int32x4 m) => [m.x, m.y, m.z, m.w];
List<bool> flags(Int32x4 m) => [m.flagX, m.flagY, m.flagZ, m.flagW];
void main() {
final a = Float32x4(1, 5, 3, 9), b = Float32x4(2, 5, 1, 9);
final m = a.equal(b);
print('a == b lane flags: ${flags(m)}');
print('as masks: ${mask(m)} (-1 = all bits set)');
print('pick a where true, else 0: ${lanes(m.select(a, Float32x4.zero()))}');
}a == b lane flags: [false, true, false, true] as masks: [0, -1, 0, -1] (-1 = all bits set) pick a where true, else 0: [0.0, 5.0, 0.0, 9.0]
Int32x4 notEqual(Float32x4 other)Lane-wise not-equals comparison.
Compares the 32-bit floating point value of each lane of this to the value of the corresponding lane of other, using 32-bit floating point comparison. For floating point comparisons, a comparison with a NaN value is always false, and -0.0 (negative zero) is considered equal to 0.0 (positive zero), and not less strictly than it. The result for a lane is a 32-bit signed integer which is -1 (all bits set) if the value from this object is not equal to the value from other, and the result is 0 (all bits cleared) if not, including if either value is a NaN value.
Returns four values that are always either 0 or -1.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
List<int> mask(Int32x4 m) => [m.x, m.y, m.z, m.w];
List<bool> flags(Int32x4 m) => [m.flagX, m.flagY, m.flagZ, m.flagW];
void main() {
final a = Float32x4(1, 5, 3, 9), b = Float32x4(2, 5, 1, 9);
final m = a.notEqual(b);
print('a != b lane flags: ${flags(m)}');
print('as masks: ${mask(m)} (-1 = all bits set)');
print('pick a where true, else 0: ${lanes(m.select(a, Float32x4.zero()))}');
}a != b lane flags: [true, false, true, false] as masks: [-1, 0, -1, 0] (-1 = all bits set) pick a where true, else 0: [1.0, 0.0, 3.0, 0.0]
Float32x4 scale(double scale)Lane-wise multiplication by scale.
Returns a result where each lane is the result of multiplying the corresponding lane of this value with scale. This can happen either by converting the lane value to a 64-bit floating point value, multiplying it with scale and converting the result back to a 32-bit floating point value, or by converting scale to a 32-bit floating point value and performing a 32-bit floating point multiplication.
In the latter case it is equivalent to thisValue * Float32x4.splat(s).
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Float32x4(1, -2, 0.5, 4);
print('scale(3): ${lanes(v.scale(3))}');
print('scale(-0.5): ${lanes(v.scale(-0.5))}');
}scale(3): [3.0, -6.0, 1.5, 12.0] scale(-0.5): [-0.5, 1.0, -0.25, -2.0]
Float32x4 abs()Lane-wise conversion to absolute value.
Converts each lane's value to a non-negative value by negating the value if it is negative, and keeping the original value if it is not negative.
Returns the result for each lane.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Float32x4(-1.5, 2, -0.0, -8);
print('abs: ${lanes(v.abs())}');
}abs: [1.5, 2.0, 0.0, 8.0]
Float32x4 clamp(Float32x4 lowerLimit, Float32x4 upperLimit)Lane-wise clamp to a range.
Clamps the value of each lane to a minimum value of the corresponding lane of lowerLimit and a maximum value of the corresponding lane of upperLimit. If the original value is lower than the minimum value, the result is the minimum value, and if original value is greater than the maximum value, the result is the maximum value. The result is unspecified if the maximum value is lower than the minimum value, or if any of the three values is a NaN value, other than that the result will be one of those three values, or possibly a different NaN value if any value is a NaN value.
Returns the result for each lane.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Float32x4(-5, 0.5, 2, 9);
final c = v.clamp(Float32x4.splat(0), Float32x4.splat(1));
print('clamp to [0, 1]: ${lanes(c)}');
}clamp to [0, 1]: [0.0, 0.5, 1.0, 1.0]
Float32x4 shuffle(int mask)Shuffle the lane values based on the mask.
The mask must be one of the 256 shuffle masks from xxxx to wwww.
Creates a new Float32x4 whose lane values are taken from the lanes of this value based on the lanes of the shuffle mask, with the result's x lane being taken from the lane of the first letter of the shuffle mask's name, the y lane from the second letter, z lane from the third letter and w lane from the fourth letter.
For example, the shuffle mask wxyz creates a new Float32x4 whose x lane is the w lane of this value, because the first letter of the shuffle mask's name, wxyz is "w". Then the y, z and w lanes of the result are the values of the x, y and z lanes of this value.
The xyzw "identity shuffle" mask gives a result with the same lanes as the original.
Some masks preserve the values of all lanes, but may permute them. Other masks duplicates some lanes and discards the values of others.
For example, doing v1.shuffle(yyyy) is equivalent to Float32x4.splat(v1.y).
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Float32x4(1, 2, 3, 4);
print('wzyx (reverse): ${lanes(v.shuffle(Float32x4.wzyx))}');
print('xxxx (splat x): ${lanes(v.shuffle(Float32x4.xxxx))}');
print('yxwz (swap pairs): ${lanes(v.shuffle(Float32x4.yxwz))}');
}wzyx (reverse): [4.0, 3.0, 2.0, 1.0] xxxx (splat x): [1.0, 1.0, 1.0, 1.0] yxwz (swap pairs): [2.0, 1.0, 4.0, 3.0]
Float32x4 shuffleMix(Float32x4 other, int mask)Mixes lanes chosen from two Float32x4 values using a mask.
Creates a new Float32x4 where the x and y lanes are chosen from the lanes of this value selected by the first two letters of the mask's name, and the z and w lanes are the lanes of other selected by the last two letters of the mask's name.
For example, v1.shuffleMix(v2, Float32x4.xyzw) is equivalent to Float32x4(v1.x, v1.y, v2.z, v2.w).
If other is the same value as this Float32x4, this function is the same as shuffle. That is, doing v1.shuffleMix(v1, mask) is equivalent to v1.shuffle(mask).
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final a = Float32x4(1, 2, 3, 4), b = Float32x4(10, 20, 30, 40);
// x and y come from a, z and w from b.
print('a.shuffleMix(b, xyxy): ${lanes(a.shuffleMix(b, Float32x4.xyxy))}');
print('a.shuffleMix(b, wzzw): ${lanes(a.shuffleMix(b, Float32x4.wzzw))}');
}a.shuffleMix(b, xyxy): [1.0, 2.0, 10.0, 20.0] a.shuffleMix(b, wzzw): [4.0, 3.0, 30.0, 40.0]
Float32x4 withX(double x)This value, but with the value of the Float32x4.x lane set to x.
Returns a new Float32x4 with the same values for the y, z and w lanes as this value, and with a Float32x4.x lane having the value x converted to a 32-bit floating point number.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Float32x4(1, 2, 3, 4);
final w = v.withX(99);
print('withX(99): ${lanes(w)}');
print('original unchanged: ${lanes(v)}');
}withX(99): [99.0, 2.0, 3.0, 4.0] original unchanged: [1.0, 2.0, 3.0, 4.0]
Float32x4 withY(double y)This value, but with the value of the Float32x4.y lane set to y.
Returns a new Float32x4 with the same values for the x, z and w lanes as this value, and with a Float32x4.y lane having the value y converted to a 32-bit floating point number.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Float32x4(1, 2, 3, 4);
final w = v.withY(99);
print('withY(99): ${lanes(w)}');
print('original unchanged: ${lanes(v)}');
}withY(99): [1.0, 99.0, 3.0, 4.0] original unchanged: [1.0, 2.0, 3.0, 4.0]
Float32x4 withZ(double z)This value, but with the value of the Float32x4.z lane set to z.
Returns a new Float32x4 with the same values for the x, y and w lanes as this value, and with a Float32x4.z lane having the value z converted to a 32-bit floating point number.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Float32x4(1, 2, 3, 4);
final w = v.withZ(99);
print('withZ(99): ${lanes(w)}');
print('original unchanged: ${lanes(v)}');
}withZ(99): [1.0, 2.0, 99.0, 4.0] original unchanged: [1.0, 2.0, 3.0, 4.0]
Float32x4 withW(double w)This value, but with the value of the Float32x4.w lane set to w.
Returns a new Float32x4 with the same values for the x, y and z lanes as this value, and with a Float32x4.w lane having the value w converted to a 32-bit floating point number.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Float32x4(1, 2, 3, 4);
final w = v.withW(99);
print('withW(99): ${lanes(w)}');
print('original unchanged: ${lanes(v)}');
}withW(99): [1.0, 2.0, 3.0, 99.0] original unchanged: [1.0, 2.0, 3.0, 4.0]
Float32x4 min(Float32x4 other)Lane-wise minimum.
For each lane select the lesser of the lane value of this and other.
The result is the lesser of the two lane values if either is lesser. The result is unspecified if either lane contains a NaN value or if the values are -0.0 and 0.0, so that neither value is smaller or greater than the other. Different platforms may give different results in those cases, but always one of the lane values.
Returns the result for each lane.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final a = Float32x4(1, 8, -3, 4), b = Float32x4(2, 5, -7, 4);
print('lane-wise min: ${lanes(a.min(b))}');
}lane-wise min: [1.0, 5.0, -7.0, 4.0]
Float32x4 max(Float32x4 other)Lane-wise maximum.
For each lane select the greater of the lane value of this and other.
The result is the greater of the two lane values if either is greater. The result is unspecified if either lane contains a NaN value or if the values are -0.0 and 0.0, so that neither value is smaller or greater than the other. Different platforms may give different results in those cases, but always one of the lane values.
Returns the result for each lane.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final a = Float32x4(1, 8, -3, 4), b = Float32x4(2, 5, -7, 4);
print('lane-wise max: ${lanes(a.max(b))}');
}lane-wise max: [2.0, 8.0, -3.0, 4.0]
Float32x4 sqrt()Lane-wise square root.
For each lane compute the 32-bit floating point square root of the lane's value.
The result for a lane is a NaN value if the original value is less than zero or if it is a NaN value. The result for a negative zero, -0.0, is the same value again. The result for positive infinity is positive infinity. Otherwise the result is a positive value which approximates the mathematical square root of the original value.
Returns the result for each lane.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Float32x4(4, 9, 16, 2);
final r = v.sqrt();
print('sqrt: ${lanes(r).map((d) => d.toStringAsFixed(4)).toList()}');
}sqrt: [2.0000, 3.0000, 4.0000, 1.4142]
Float32x4 reciprocal()Lane-wise reciprocal.
For each lane compute the result of dividing 1.0 by the lane's value.
If the value is a NaN value, so is the result. If the value is infinite, the result is a zero value with the same sign. If the value is zero, the result is infinite with the same sign. Otherwise the result is an approximation of the mathematical result of dividing 1 by the (finite, non-zero) value of the lane.
Returns the result for each lane.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Float32x4(1, 2, 4, 8);
final r = v.reciprocal();
print('1/v: ${lanes(r).map((d) => d.toStringAsFixed(3)).toList()}');
}1/v: [1.000, 0.500, 0.250, 0.125]
Float32x4 reciprocalSqrt()Lane-wise approximation of reciprocal square root.
Approximates the same result as reciprocal followed by sqrt, or sqrt followed by reciprocal, but may be more precise and/or efficient due to computing the result directly, rather than not creating a an intermediate result, and possibly by working entirely at a reduced precision.
The result can differ between platforms due to differences in approximation and precision, and for values where the order of sqrt and reciprocal makes a difference. The latter applies specifically to -0.0 where sqrt(-0.0) is defined to be -0.0, and reciprocal of that is -Infinity. In the opposite order it computes sqrt of -Infinity which is NaN.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Float32x4(1, 4, 16, 64);
final r = v.reciprocalSqrt();
print('1/sqrt(v): ${lanes(r).map((d) => d.toStringAsFixed(3)).toList()}');
}1/sqrt(v): [1.000, 0.500, 0.250, 0.125]
Operators
Float32x4 operator +(Float32x4 other)Lane-wise addition.
Adds the value of each lane of this value to the value of the corresponding lane of other.
Returns the result for each lane.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final a = Float32x4(1, 2, 3, 4), b = Float32x4(10, 20, 30, 40);
print('a + b: ${lanes(a + b)}');
}a + b: [11.0, 22.0, 33.0, 44.0]
Float32x4 operator -()Lane-wise negation.
Returns a result where each lane is the negation of the corresponding lane of this value.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Float32x4(1, -2, 3, 0);
print('-v: ${lanes(-v)}');
}-v: [-1.0, 2.0, -3.0, -0.0]
Float32x4 operator -(Float32x4 other)Lane-wise subtraction.
Subtracts the value of each lane of other from the value of the corresponding lane of this value.
Returns the result for each lane.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final a = Float32x4(10, 20, 30, 40), b = Float32x4(1, 2, 3, 4);
print('a - b: ${lanes(a - b)}');
}a - b: [9.0, 18.0, 27.0, 36.0]
Float32x4 operator *(Float32x4 other)Lane-wise multiplication.
Multiplies the value of each lane of this value with the value of the corresponding lane of other.
Returns the result for each lane.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final a = Float32x4(1, 2, 3, 4), b = Float32x4(2, 2, 0.5, -1);
print('a * b: ${lanes(a * b)}');
// A 4-lane dot product: multiply, then add the lanes.
final p = a * a;
print('|a|^2 = ${p.x + p.y + p.z + p.w}');
}a * b: [2.0, 4.0, 1.5, -4.0] |a|^2 = 30.0
Float32x4 operator /(Float32x4 other)Lane-wise division.
Divides the value of each lane of this value with the value of the corresponding lane of other.
Returns the result for each lane.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final a = Float32x4(1, 9, 5, -8), b = Float32x4(2, 3, 0.5, 4);
print('a / b: ${lanes(a / b)}');
}a / b: [0.5, 3.0, 10.0, -2.0]
Int32x4
abstract final class Int32x4
Int32x4 and operations.
Int32x4 stores 4 32-bit bit-masks in "lanes". The lanes are "x", "y", "z", and "w" respectively.
import 'raya:typed_data';
void main() {
final a = Int32x4(1, 2, 3, 4);
final b = Int32x4(10, 20, 30, 40);
print('a + b: ${a + b}');
print('a & b: ${a & b}');
print('lanes: ${(a + b).x}, ${(a + b).w}');
}a + b: [0000000b, 00000016, 00000021, 0000002c] a & b: [00000000, 00000000, 00000002, 00000000] lanes: 11, 44
Constructors
external factory Int32x4(int x, int y, int z, int w)import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Int32x4(1, -2, 3, 0x7fffffff);
print('lanes: ${lanes(v)}');
// Lanes are 32 bits: larger values wrap.
print('2^32 + 5 -> ${Int32x4(0x100000005, 0, 0, 0).x}');
}lanes: [1, -2, 3, 2147483647] 2^32 + 5 -> 5
external factory Int32x4.bool(bool x, bool y, bool z, bool w)import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
List<bool> flags(Int32x4 m) => [m.flagX, m.flagY, m.flagZ, m.flagW];
void main() {
final m = Int32x4.bool(true, false, true, false);
print('lanes: ${lanes(m)} (true = -1, all bits set)');
print('flags: ${flags(m)}');
}lanes: [-1, 0, -1, 0] (true = -1, all bits set) flags: [true, false, true, false]
external factory Int32x4.fromFloat32x4Bits(Float32x4 x)import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final bits = Int32x4.fromFloat32x4Bits(Float32x4(1, -1, 0.5, 0));
print('float bit patterns: ${lanes(bits).map((b) => '0x${b.toUnsigned(32).toRadixString(16)}').toList()}');
}float bit patterns: [0x3f800000, 0xbf800000, 0x3f000000, 0x0]
Static
static const int xxxy = 0x40static const int xxxz = 0x80static const int xxxw = 0xC0static const int xxyx = 0x10static const int xxyy = 0x50static const int xxyz = 0x90static const int xxyw = 0xD0static const int xxzx = 0x20static const int xxzy = 0x60static const int xxzz = 0xA0static const int xxzw = 0xE0static const int xxwx = 0x30static const int xxwy = 0x70static const int xxwz = 0xB0static const int xxww = 0xF0static const int xyxx = 0x04static const int xyxy = 0x44static const int xyxz = 0x84static const int xyxw = 0xC4static const int xyyx = 0x14static const int xyyy = 0x54static const int xyyz = 0x94static const int xyyw = 0xD4static const int xyzx = 0x24static const int xyzy = 0x64static const int xyzz = 0xA4static const int xyzw = 0xE4static const int xywx = 0x34static const int xywy = 0x74static const int xywz = 0xB4static const int xyww = 0xF4static const int xzxx = 0x08static const int xzxy = 0x48static const int xzxz = 0x88static const int xzxw = 0xC8static const int xzyx = 0x18static const int xzyy = 0x58static const int xzyz = 0x98static const int xzyw = 0xD8static const int xzzx = 0x28static const int xzzy = 0x68static const int xzzz = 0xA8static const int xzzw = 0xE8static const int xzwx = 0x38static const int xzwy = 0x78static const int xzwz = 0xB8static const int xzww = 0xF8static const int xwxx = 0x0Cstatic const int xwxy = 0x4Cstatic const int xwxz = 0x8Cstatic const int xwxw = 0xCCstatic const int xwyx = 0x1Cstatic const int xwyy = 0x5Cstatic const int xwyz = 0x9Cstatic const int xwyw = 0xDCstatic const int xwzx = 0x2Cstatic const int xwzy = 0x6Cstatic const int xwzz = 0xACstatic const int xwzw = 0xECstatic const int xwwx = 0x3Cstatic const int xwwy = 0x7Cstatic const int xwwz = 0xBCstatic const int xwww = 0xFCstatic const int yxxx = 0x01static const int yxxy = 0x41static const int yxxz = 0x81static const int yxxw = 0xC1static const int yxyx = 0x11static const int yxyy = 0x51static const int yxyz = 0x91static const int yxyw = 0xD1static const int yxzx = 0x21static const int yxzy = 0x61static const int yxzz = 0xA1static const int yxzw = 0xE1static const int yxwx = 0x31static const int yxwy = 0x71static const int yxwz = 0xB1static const int yxww = 0xF1static const int yyxx = 0x05static const int yyxy = 0x45static const int yyxz = 0x85static const int yyxw = 0xC5static const int yyyx = 0x15static const int yyyy = 0x55static const int yyyz = 0x95static const int yyyw = 0xD5static const int yyzx = 0x25static const int yyzy = 0x65static const int yyzz = 0xA5static const int yyzw = 0xE5static const int yywx = 0x35static const int yywy = 0x75static const int yywz = 0xB5static const int yyww = 0xF5static const int yzxx = 0x09static const int yzxy = 0x49static const int yzxz = 0x89static const int yzxw = 0xC9static const int yzyx = 0x19static const int yzyy = 0x59static const int yzyz = 0x99static const int yzyw = 0xD9static const int yzzx = 0x29static const int yzzy = 0x69static const int yzzz = 0xA9static const int yzzw = 0xE9static const int yzwx = 0x39static const int yzwy = 0x79static const int yzwz = 0xB9static const int yzww = 0xF9static const int ywxx = 0x0Dstatic const int ywxy = 0x4Dstatic const int ywxz = 0x8Dstatic const int ywxw = 0xCDstatic const int ywyx = 0x1Dstatic const int ywyy = 0x5Dstatic const int ywyz = 0x9Dstatic const int ywyw = 0xDDstatic const int ywzx = 0x2Dstatic const int ywzy = 0x6Dstatic const int ywzz = 0xADstatic const int ywzw = 0xEDstatic const int ywwx = 0x3Dstatic const int ywwy = 0x7Dstatic const int ywwz = 0xBDstatic const int ywww = 0xFDstatic const int zxxx = 0x02static const int zxxy = 0x42static const int zxxz = 0x82static const int zxxw = 0xC2static const int zxyx = 0x12static const int zxyy = 0x52static const int zxyz = 0x92static const int zxyw = 0xD2static const int zxzx = 0x22static const int zxzy = 0x62static const int zxzz = 0xA2static const int zxzw = 0xE2static const int zxwx = 0x32static const int zxwy = 0x72static const int zxwz = 0xB2static const int zxww = 0xF2static const int zyxx = 0x06static const int zyxy = 0x46static const int zyxz = 0x86static const int zyxw = 0xC6static const int zyyx = 0x16static const int zyyy = 0x56static const int zyyz = 0x96static const int zyyw = 0xD6static const int zyzx = 0x26static const int zyzy = 0x66static const int zyzz = 0xA6static const int zyzw = 0xE6static const int zywx = 0x36static const int zywy = 0x76static const int zywz = 0xB6static const int zyww = 0xF6static const int zzxx = 0x0Astatic const int zzxy = 0x4Astatic const int zzxz = 0x8Astatic const int zzxw = 0xCAstatic const int zzyx = 0x1Astatic const int zzyy = 0x5Astatic const int zzyz = 0x9Astatic const int zzyw = 0xDAstatic const int zzzx = 0x2Astatic const int zzzy = 0x6Astatic const int zzzz = 0xAAstatic const int zzzw = 0xEAstatic const int zzwx = 0x3Astatic const int zzwy = 0x7Astatic const int zzwz = 0xBAstatic const int zzww = 0xFAstatic const int zwxx = 0x0Estatic const int zwxy = 0x4Estatic const int zwxz = 0x8Estatic const int zwxw = 0xCEstatic const int zwyx = 0x1Estatic const int zwyy = 0x5Estatic const int zwyz = 0x9Estatic const int zwyw = 0xDEstatic const int zwzx = 0x2Estatic const int zwzy = 0x6Estatic const int zwzz = 0xAEstatic const int zwzw = 0xEEstatic const int zwwx = 0x3Estatic const int zwwy = 0x7Estatic const int zwwz = 0xBEstatic const int zwww = 0xFEstatic const int wxxx = 0x03static const int wxxy = 0x43static const int wxxz = 0x83static const int wxxw = 0xC3static const int wxyx = 0x13static const int wxyy = 0x53static const int wxyz = 0x93static const int wxyw = 0xD3static const int wxzx = 0x23static const int wxzy = 0x63static const int wxzz = 0xA3static const int wxzw = 0xE3static const int wxwx = 0x33static const int wxwy = 0x73static const int wxwz = 0xB3static const int wxww = 0xF3static const int wyxx = 0x07static const int wyxy = 0x47static const int wyxz = 0x87static const int wyxw = 0xC7static const int wyyx = 0x17static const int wyyy = 0x57static const int wyyz = 0x97static const int wyyw = 0xD7static const int wyzx = 0x27static const int wyzy = 0x67static const int wyzz = 0xA7static const int wyzw = 0xE7static const int wywx = 0x37static const int wywy = 0x77static const int wywz = 0xB7static const int wyww = 0xF7static const int wzxx = 0x0Bstatic const int wzxy = 0x4Bstatic const int wzxz = 0x8Bstatic const int wzxw = 0xCBstatic const int wzyx = 0x1Bstatic const int wzyy = 0x5Bstatic const int wzyz = 0x9Bstatic const int wzyw = 0xDBstatic const int wzzx = 0x2Bstatic const int wzzy = 0x6Bstatic const int wzzz = 0xABstatic const int wzzw = 0xEBstatic const int wzwx = 0x3Bstatic const int wzwy = 0x7Bstatic const int wzwz = 0xBBstatic const int wzww = 0xFBstatic const int wwxx = 0x0Fstatic const int wwxy = 0x4Fstatic const int wwxz = 0x8Fstatic const int wwxw = 0xCFstatic const int wwyx = 0x1Fstatic const int wwyy = 0x5Fstatic const int wwyz = 0x9Fstatic const int wwyw = 0xDFstatic const int wwzx = 0x2Fstatic const int wwzy = 0x6Fstatic const int wwzz = 0xAFstatic const int wwzw = 0xEFstatic const int wwwx = 0x3Fstatic const int wwwy = 0x7Fstatic const int wwwz = 0xBFstatic const int wwww = 0xFFProperties
int get signMaskExtract the top bit from each lane return them in the first 4 bits. "x" lane is bit 0. "y" lane is bit 1. "z" lane is bit 2. "w" lane is bit 3.
Methods
Int32x4 shuffle(int mask)Shuffle the lane values. mask must be one of the 256 shuffle constants.
import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Int32x4(1, 2, 3, 4);
print('wzyx: ${lanes(v.shuffle(Int32x4.wzyx))}');
print('zzzz: ${lanes(v.shuffle(Int32x4.zzzz))}');
}wzyx: [4, 3, 2, 1] zzzz: [3, 3, 3, 3]
Int32x4 shuffleMix(Int32x4 other, int mask)Shuffle the lane values in this Int32x4 and other. The returned Int32x4 will have XY lanes from this Int32x4 and ZW lanes from other. Uses the same mask as shuffle.
import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final a = Int32x4(1, 2, 3, 4), b = Int32x4(10, 20, 30, 40);
print('a.shuffleMix(b, xyzw): ${lanes(a.shuffleMix(b, Int32x4.xyzw))}');
print('a.shuffleMix(b, wwxx): ${lanes(a.shuffleMix(b, Int32x4.wwxx))}');
}a.shuffleMix(b, xyzw): [1, 2, 30, 40] a.shuffleMix(b, wwxx): [4, 4, 10, 10]
Int32x4 withX(int x)import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Int32x4(1, 2, 3, 4);
print('withX(-7): ${lanes(v.withX(-7))}');
print('original: ${lanes(v)}');
}withX(-7): [-7, 2, 3, 4] original: [1, 2, 3, 4]
Int32x4 withY(int y)import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Int32x4(1, 2, 3, 4);
print('withY(-7): ${lanes(v.withY(-7))}');
print('original: ${lanes(v)}');
}withY(-7): [1, -7, 3, 4] original: [1, 2, 3, 4]
Int32x4 withZ(int z)import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Int32x4(1, 2, 3, 4);
print('withZ(-7): ${lanes(v.withZ(-7))}');
print('original: ${lanes(v)}');
}withZ(-7): [1, 2, -7, 4] original: [1, 2, 3, 4]
Int32x4 withW(int w)import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final v = Int32x4(1, 2, 3, 4);
print('withW(-7): ${lanes(v.withW(-7))}');
print('original: ${lanes(v)}');
}withW(-7): [1, 2, 3, -7] original: [1, 2, 3, 4]
Int32x4 withFlagX(bool x)import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
List<bool> flags(Int32x4 m) => [m.flagX, m.flagY, m.flagZ, m.flagW];
void main() {
final m = Int32x4.bool(false, false, false, false);
final m2 = m.withFlagX(true);
print('withFlagX(true): ${flags(m2)}');
print('lane values: ${lanes(m2)}');
}withFlagX(true): [true, false, false, false] lane values: [-1, 0, 0, 0]
Int32x4 withFlagY(bool y)import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
List<bool> flags(Int32x4 m) => [m.flagX, m.flagY, m.flagZ, m.flagW];
void main() {
final m = Int32x4.bool(false, false, false, false);
final m2 = m.withFlagY(true);
print('withFlagY(true): ${flags(m2)}');
print('lane values: ${lanes(m2)}');
}withFlagY(true): [false, true, false, false] lane values: [0, -1, 0, 0]
Int32x4 withFlagZ(bool z)import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
List<bool> flags(Int32x4 m) => [m.flagX, m.flagY, m.flagZ, m.flagW];
void main() {
final m = Int32x4.bool(false, false, false, false);
final m2 = m.withFlagZ(true);
print('withFlagZ(true): ${flags(m2)}');
print('lane values: ${lanes(m2)}');
}withFlagZ(true): [false, false, true, false] lane values: [0, 0, -1, 0]
Int32x4 withFlagW(bool w)import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
List<bool> flags(Int32x4 m) => [m.flagX, m.flagY, m.flagZ, m.flagW];
void main() {
final m = Int32x4.bool(false, false, false, false);
final m2 = m.withFlagW(true);
print('withFlagW(true): ${flags(m2)}');
print('lane values: ${lanes(m2)}');
}withFlagW(true): [false, false, false, true] lane values: [0, 0, 0, -1]
Float32x4 select(Float32x4 trueValue, Float32x4 falseValue)Merge trueValue and falseValue based on this Int32x4 bit mask: Select bit from trueValue when bit in this Int32x4 is on. Select bit from falseValue when bit in this Int32x4 is off.
import 'raya:typed_data';
List<double> lanes(Float32x4 v) => [v.x, v.y, v.z, v.w];
List<int> mask(Int32x4 m) => [m.x, m.y, m.z, m.w];
List<bool> flags(Int32x4 m) => [m.flagX, m.flagY, m.flagZ, m.flagW];
void main() {
final a = Float32x4(1, 2, 3, 4), b = Float32x4(-1, -2, -3, -4);
final m = Int32x4.bool(true, false, false, true);
print('select(a, b): ${lanes(m.select(a, b))}');
// Branch-free max: pick a where a > b.
print('max via select: ${lanes(a.greaterThan(b).select(a, b))}');
}select(a, b): [1.0, -2.0, -3.0, 4.0] max via select: [1.0, 2.0, 3.0, 4.0]
Operators
Int32x4 operator |(Int32x4 other)The bit-wise or operator.
import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final a = Int32x4(0x0f, 0, 1, 8), b = Int32x4(0xf0, 0, 2, 8);
print('a | b: ${lanes(a | b)}');
}a | b: [255, 0, 3, 8]
Int32x4 operator &(Int32x4 other)The bit-wise and operator.
import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final a = Int32x4(0xff, 6, 1, -1), b = Int32x4(0x0f, 3, 2, 42);
print('a & b: ${lanes(a & b)}');
}a & b: [15, 2, 0, 42]
Int32x4 operator ^(Int32x4 other)The bit-wise xor operator.
import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final a = Int32x4(5, 6, 0, -1), b = Int32x4(3, 6, 7, 0);
print('a ^ b: ${lanes(a ^ b)}');
}a ^ b: [6, 0, 7, -1]
Int32x4 operator +(Int32x4 other)Addition operator.
import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final a = Int32x4(1, 2, 0x7fffffff, -1), b = Int32x4(10, 20, 1, 1);
print('a + b: ${lanes(a + b)} (the third lane wraps)');
}a + b: [11, 22, -2147483648, 0] (the third lane wraps)
Int32x4 operator -(Int32x4 other)Subtraction operator.
import 'raya:typed_data';
List<int> lanes(Int32x4 v) => [v.x, v.y, v.z, v.w];
void main() {
final a = Int32x4(10, 20, -0x80000000, 0), b = Int32x4(1, 2, 1, 5);
print('a - b: ${lanes(a - b)} (the third lane wraps)');
}a - b: [9, 18, 2147483647, -5] (the third lane wraps)
Float64x2
abstract final class Float64x2
Float64x2 immutable value type and operations.
Float64x2 stores 2 64-bit floating point values in "lanes". The lanes are "x" and "y" respectively.
It is a compile-time error for a class to attempt to extend or implement Float64x2.
Constructors
external factory Float64x2(double x, double y)import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
final v = Float64x2(0.1, -2.5);
print('lanes: ${lanes(v)}');
print('full double precision kept: ${v.x == 0.1}');
}lanes: [0.1, -2.5] full double precision kept: true
external factory Float64x2.splat(double v)import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
print('splat(3.5): ${lanes(Float64x2.splat(3.5))}');
}splat(3.5): [3.5, 3.5]
external factory Float64x2.zero()import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
final z = Float64x2.zero();
print('zero: ${lanes(z)}');
print('v + zero: ${lanes(Float64x2(1, 2) + z)}');
}zero: [0.0, 0.0] v + zero: [1.0, 2.0]
external factory Float64x2.fromFloat32x4(Float32x4 v)Uses the "x" and "y" lanes from v.
import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
final v = Float64x2.fromFloat32x4(Float32x4(1.5, -2, 3, 4));
print('first two lanes: ${lanes(v)}');
}first two lanes: [1.5, -2.0]
Properties
int get signMaskExtract the sign bits from each lane return them in the first 2 bits. "x" lane is bit 0. "y" lane is bit 1.
Methods
Float64x2 scale(double s)Returns a copy of this Float64x2 each lane being scaled by s. Equivalent to this * new Float64x2.splat(s)
import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
print('scale(4): ${lanes(Float64x2(0.25, -1).scale(4))}');
}scale(4): [1.0, -4.0]
Float64x2 abs()The lane-wise absolute value of this Float64x2.
import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
print('abs: ${lanes(Float64x2(-3.5, 2).abs())}');
}abs: [3.5, 2.0]
Float64x2 clamp(Float64x2 lowerLimit, Float64x2 upperLimit)Lane-wise clamp this Float64x2 to be in the range lowerLimit-upperLimit.
import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
final c = Float64x2(-1, 7).clamp(Float64x2(0, 0), Float64x2(5, 5));
print('clamp to [0, 5]: ${lanes(c)}');
}clamp to [0, 5]: [0.0, 5.0]
Float64x2 withX(double x)import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
final v = Float64x2(1, 2);
print('withX(9): ${lanes(v.withX(9))}, original ${lanes(v)}');
}withX(9): [9.0, 2.0], original [1.0, 2.0]
Float64x2 withY(double y)import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
final v = Float64x2(1, 2);
print('withY(9): ${lanes(v.withY(9))}, original ${lanes(v)}');
}withY(9): [1.0, 9.0], original [1.0, 2.0]
Float64x2 min(Float64x2 other)The lane-wise minimum value in this Float64x2 or other.
import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
print('min: ${lanes(Float64x2(1, 8).min(Float64x2(3, 2)))}');
}min: [1.0, 2.0]
Float64x2 max(Float64x2 other)The lane-wise maximum value in this Float64x2 or other.
import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
print('max: ${lanes(Float64x2(1, 8).max(Float64x2(3, 2)))}');
}max: [3.0, 8.0]
Float64x2 sqrt()The lane-wise square root of this Float64x2.
import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
final r = Float64x2(2, 9).sqrt();
print('sqrt: ${lanes(r)}');
}sqrt: [1.4142135623730951, 3.0]
Operators
Float64x2 operator +(Float64x2 other)Addition operator.
import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
print('(1, 2) + (0.5, 0.25): ${lanes(Float64x2(1, 2) + Float64x2(0.5, 0.25))}');
}(1, 2) + (0.5, 0.25): [1.5, 2.25]
Float64x2 operator -()Negate operator.
import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
print('-(1, -2): ${lanes(-Float64x2(1, -2))}');
}-(1, -2): [-1.0, 2.0]
Float64x2 operator -(Float64x2 other)Subtraction operator.
import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
print('(5, 5) - (1, 2): ${lanes(Float64x2(5, 5) - Float64x2(1, 2))}');
}(5, 5) - (1, 2): [4.0, 3.0]
Float64x2 operator *(Float64x2 other)Multiplication operator.
import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
final p = Float64x2(3, 4) * Float64x2(3, 4);
print('(3, 4) * (3, 4): ${lanes(p)}, sum ${p.x + p.y}');
}(3, 4) * (3, 4): [9.0, 16.0], sum 25.0
Float64x2 operator /(Float64x2 other)Division operator.
import 'raya:typed_data';
List<double> lanes(Float64x2 v) => [v.x, v.y];
void main() {
print('(1, 9) / (4, 3): ${lanes(Float64x2(1, 9) / Float64x2(4, 3))}');
}(1, 9) / (4, 3): [0.25, 3.0]
BytesBuilder
abstract interface class BytesBuilder
Builds a list of bytes, allowing bytes and lists of bytes to be added at the end.
Used to efficiently collect bytes and lists of bytes.
import 'raya:typed_data';
void main() {
final bb = BytesBuilder();
bb.add([1, 2]);
bb.addByte(3);
bb.add(Uint8List.fromList([4, 5]));
print('length ${bb.length}');
print('bytes: ${bb.takeBytes()}');
print('after takeBytes: ${bb.length}');
}length 5 bytes: [1, 2, 3, 4, 5] after takeBytes: 0
Constructors
factory BytesBuilder({bool copy = true})Construct a new empty BytesBuilder.
If copy is true (the default), the created builder is a copying builder. A copying builder maintains its own internal buffer and copies the bytes added to it eagerly.
If copy set to false, the created builder assumes that lists added to it will not change. Any Uint8List added using add is kept until toBytes or takeBytes is called, and only then are their contents copied. A non-Uint8List may be copied eagerly. If only a single Uint8List is added to the builder, that list is returned by toBytes or takeBytes directly, without any copying. A list added to a non-copying builder should not change its content after being added, and it must not change its length after being added. (Normal Uint8Lists are fixed length lists, but growing lists implementing Uint8List exist.)
import 'raya:typed_data';
void main() {
final copying = BytesBuilder();
final noCopy = BytesBuilder(copy: false);
final chunk = Uint8List.fromList([1, 2, 3]);
copying.add(chunk);
noCopy.add(chunk);
print('copy: ${copying.toBytes()} length ${copying.length}');
print('noCopy: ${noCopy.toBytes()} isEmpty ${noCopy.isEmpty}');
}copy: [1, 2, 3] length 3 noCopy: [1, 2, 3] isEmpty false
Properties
Methods
void add(List<int> bytes)Appends bytes to the current contents of this builder.
Each value of bytes will be truncated to an 8-bit value in the range 0.. 255.
import 'raya:typed_data';
void main() {
final b = BytesBuilder();
b.add([0x48, 0x69]);
b.add(' there'.codeUnits);
b.add(Uint8List.fromList([33]));
print('length: ${b.length}');
print('text: ${String.fromCharCodes(b.toBytes())}');
}length: 9 text: Hi there!
void addByte(int byte)Appends byte to the current contents of this builder.
The byte will be truncated to an 8-bit value in the range 0.. 255.
import 'raya:typed_data';
void main() {
final b = BytesBuilder();
for (var i = 0; i < 5; i++) {
b.addByte(i * i);
}
b.addByte(257); // low 8 bits: 1
print('bytes: ${b.toBytes()}');
}bytes: [0, 1, 4, 9, 16, 1]
Uint8List takeBytes()Returns the bytes currently contained in this builder and clears it.
The returned list may be a view of a larger buffer.
import 'raya:typed_data';
void main() {
final b = BytesBuilder()..add([1, 2])..add([3]);
final taken = b.takeBytes();
print('taken: $taken');
print('builder empty: ${b.isEmpty}');
b.addByte(9);
print('next message: ${b.takeBytes()}');
}taken: [1, 2, 3] builder empty: true next message: [9]
Uint8List toBytes()Returns a copy of the current byte contents of this builder.
Leaves the contents of this builder intact.
import 'raya:typed_data';
void main() {
final b = BytesBuilder()..add([10, 20]);
final first = b.toBytes();
b.addByte(30);
print('snapshot: $first');
print('now: ${b.toBytes()}');
print('still full: ${b.length}');
}snapshot: [10, 20] now: [10, 20, 30] still full: 3
void clear()Clears the contents of this builder.
The current contents are discarded and this builder becomes empty.
import 'raya:typed_data';
void main() {
final b = BytesBuilder()..add([1, 2, 3]);
print('before: ${b.length}');
b.clear();
print('after: ${b.length}, bytes ${b.toBytes()}');
}before: 3 after: 0, bytes []