File: ax_bit_map.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (110 lines) | stat: -rw-r--r-- 3,698 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_ACCESSIBILITY_AX_BIT_MAP_H_
#define UI_ACCESSIBILITY_AX_BIT_MAP_H_

#include "base/check.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

#include <memory>
#include <string>
#include <tuple>

namespace ui {

// A helper class to store AX-related boolean enums.
template <typename T>
class AXBitMap {
 public:
  static const size_t kElementsPerMapBucket = 64;

  AXBitMap() {
    for (size_t i = 0;
         i <= static_cast<size_t>(T::kMaxValue) / kElementsPerMapBucket; ++i) {
      true_map_[i] = 0;
      false_map_[i] = 0;
    }
  }
  ~AXBitMap() = default;

  // Returns whether enum T at |value| is set to true, false or unset.
  absl::optional<bool> Has(const T enum_value) {
    auto [value_position, true_map, false_map] = GetPositionAndMaps(enum_value);
    const bool is_in_true_map = (*true_map) >> value_position & 1ull;
    const bool is_in_false_map = (*false_map) >> value_position & 1ull;

    CHECK(!(is_in_true_map && is_in_false_map))
        << std::string("A value can't be true and false at the same time.");

    if (is_in_true_map) {
      return true;
    }

    if (is_in_false_map) {
      return false;
    }

    return absl::nullopt;
  }

  // Sets the enum T at |enum_value| to true or false.
  void Set(const T enum_value, const bool bool_value) {
    auto [value_position, true_map, false_map] = GetPositionAndMaps(enum_value);
    uint64_t* map_to_set_true;
    uint64_t* map_to_set_false;
    if (bool_value) {
      map_to_set_true = true_map;
      map_to_set_false = false_map;
    } else {
      map_to_set_true = false_map;
      map_to_set_false = true_map;
    }

    *map_to_set_true |= 1ull << value_position;
    *map_to_set_false &= ~(1ull << value_position);
  }

  // Unsets the enum T at |enum_value|. If it is not set, this is a no-op.
  void Unset(const T enum_value) {
    auto [value_position, true_map, false_map] = GetPositionAndMaps(enum_value);
    (*true_map) &= ~(1ull << value_position);
    (*false_map) &= ~(1ull << value_position);
  }

 private:
  // Helper function that returns a tuple with the position of the value in the
  // maps, a pointer to the correct bucket in |true_map_| and |false_map_|.
  std::tuple<uint64_t, uint64_t*, uint64_t*> GetPositionAndMaps(const T value) {
    uint64_t absolute_value_position = static_cast<uint64_t>(value);
    const size_t map_bucket = absolute_value_position / kElementsPerMapBucket;
    uint64_t* true_map = &(true_map_[map_bucket]);
    uint64_t* false_map = &(false_map_[map_bucket]);

    // Subtracts map_bucket * 64 from |value_position| so that it references the
    // correct place in the map variables.
    uint64_t relative_value_position =
        absolute_value_position - map_bucket * kElementsPerMapBucket;

    return {relative_value_position, true_map, false_map};
  }

  // Indicates that the enum T is true at the bit shifted value. This array
  // holds 64 enum values per position, and will contains as many entries to
  // hold all enum possible values.
  uint64_t true_map_[static_cast<size_t>(
      static_cast<size_t>(T::kMaxValue) / kElementsPerMapBucket + 1)];

  // Indicates that the enum T is false at the bit shifted value. This array
  // holds 64 enum values per position, and will contains as many entries to
  // hold all enum possible values.
  uint64_t false_map_[static_cast<size_t>(
      static_cast<size_t>(T::kMaxValue) / kElementsPerMapBucket + 1)];

  // Undefined/unset implied by not in *_true and *_false;
};

}  // namespace ui

#endif  // UI_ACCESSIBILITY_AX_BIT_MAP_H_