1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'bitfield.dart' as bitfield;
6
7/// The dart:io implementation of [bitfield.kMaxUnsignedSMI].
8const int kMaxUnsignedSMI = 0x3FFFFFFFFFFFFFFF; // ignore: avoid_js_rounded_ints, (VM-only code)
9
10/// The dart:io implementation of [bitfield.Bitfield].
11class BitField<T extends dynamic> implements bitfield.BitField<T> {
12 /// The dart:io implementation of [bitfield.Bitfield()].
13 BitField(this._length)
14 : assert(_length <= _smiBits),
15 _bits = _allZeros;
16
17 /// The dart:io implementation of [bitfield.Bitfield.filled].
18 BitField.filled(this._length, bool value)
19 : assert(_length <= _smiBits),
20 _bits = value ? _allOnes : _allZeros;
21
22 final int _length;
23 int _bits;
24
25 static const int _smiBits = 62; // see https://www.dartlang.org/articles/numeric-computation/#smis-and-mints
26 static const int _allZeros = 0;
27 static const int _allOnes = kMaxUnsignedSMI; // 2^(_kSMIBits+1)-1
28
29 @override
30 bool operator [](T index) {
31 final int intIndex = index.index as int;
32 assert(intIndex < _length);
33 return (_bits & 1 << intIndex) > 0;
34 }
35
36 @override
37 void operator []=(T index, bool value) {
38 final int intIndex = index.index as int;
39 assert(intIndex < _length);
40 if (value) {
41 _bits = _bits | (1 << intIndex);
42 } else {
43 _bits = _bits & ~(1 << intIndex);
44 }
45 }
46
47 @override
48 void reset([ bool value = false ]) {
49 _bits = value ? _allOnes : _allZeros;
50 }
51}
52