bitfield.dart 1.78 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8 9
import '_bitfield_io.dart'
  if (dart.library.html) '_bitfield_web.dart' as _bitfield;

10 11
/// The largest SMI value.
///
12
/// See <https://www.dartlang.org/articles/numeric-computation/#smis-and-mints>
13 14 15
///
/// When compiling to JavaScript, this value is not supported since it is
/// larger than the maximum safe 32bit integer.
16
const int kMaxUnsignedSMI = _bitfield.kMaxUnsignedSMI;
17 18 19 20 21

/// A BitField over an enum (or other class whose values implement "index").
/// Only the first 62 values of the enum can be used as indices.
///
/// When compiling to JavaScript, this class is not supported.
22
abstract class BitField<T extends dynamic> {
23 24 25
  /// Creates a bit field of all zeros.
  ///
  /// The given length must be at most 62.
26
  factory BitField(int length) = _bitfield.BitField<T>;
27 28 29 30 31 32 33

  /// Creates a bit field filled with a particular value.
  ///
  /// If the value argument is true, the bits are filled with ones. Otherwise,
  /// the bits are filled with zeros.
  ///
  /// The given length must be at most 62.
34
  factory BitField.filled(int length, bool value) = _bitfield.BitField<T>.filled;
35 36

  /// Returns whether the bit with the given index is set to one.
37
  bool operator [](T index);
38 39 40 41 42

  /// Sets the bit with the given index to the given value.
  ///
  /// If value is true, the bit with the given index is set to one. Otherwise,
  /// the bit is set to zero.
43
  void operator []=(T index, bool value);
44 45 46 47 48

  /// Sets all the bits to the given value.
  ///
  /// If the value is true, the bits are all set to one. Otherwise, the bits are
  /// all set to zero. Defaults to setting all the bits to zero.
49
  void reset([ bool value = false ]);
50
}