bitfield.dart 1.77 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
import '_bitfield_io.dart'
6
  if (dart.library.js_util) '_bitfield_web.dart' as bitfield;
7

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

/// 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.
20
abstract class BitField<T extends dynamic> {
21 22 23
  /// Creates a bit field of all zeros.
  ///
  /// The given length must be at most 62.
24
  factory BitField(int length) = bitfield.BitField<T>;
25 26 27 28 29 30 31

  /// 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.
32
  factory BitField.filled(int length, bool value) = bitfield.BitField<T>.filled;
33 34

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

  /// 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.
41
  void operator []=(T index, bool value);
42 43 44 45 46

  /// 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.
47
  void reset([ bool value = false ]);
48
}