OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/accessibility/tree_generator.h" | 5 #include "ui/accessibility/tree_generator.h" |
6 | 6 |
7 #include "ui/accessibility/ax_serializable_tree.h" | 7 #include "ui/accessibility/ax_serializable_tree.h" |
8 #include "ui/accessibility/ax_tree.h" | 8 #include "ui/accessibility/ax_tree.h" |
9 | 9 |
10 namespace ui { | 10 namespace ui { |
11 | 11 |
12 TreeGenerator::TreeGenerator(int node_count) | 12 static int UniqueTreeCountForNodeCount(int node_count, |
13 : node_count_(node_count), unique_tree_count_(1) { | 13 bool permutations) { |
| 14 int unique_tree_count = 1; |
| 15 |
14 // (n-1)! for the possible trees. | 16 // (n-1)! for the possible trees. |
15 for (int i = 2; i < node_count_; i++) | 17 for (int i = 2; i < node_count; ++i) |
16 unique_tree_count_ *= i; | 18 unique_tree_count *= i; |
| 19 |
17 // n! for the permutations of ids. | 20 // n! for the permutations of ids. |
18 for (int i = 2; i <= node_count_; i++) | 21 if (permutations) |
19 unique_tree_count_ *= i; | 22 unique_tree_count = unique_tree_count * unique_tree_count * node_count; |
20 }; | 23 |
| 24 return unique_tree_count; |
| 25 } |
| 26 |
| 27 TreeGenerator::TreeGenerator(int max_node_count, bool permutations) |
| 28 : max_node_count_(max_node_count), |
| 29 permutations_(permutations), |
| 30 total_unique_tree_count_(0) { |
| 31 unique_tree_count_by_size_.push_back(0); |
| 32 for (int i = 1; i <= max_node_count; ++i) { |
| 33 int unique_tree_count = UniqueTreeCountForNodeCount(i, permutations); |
| 34 unique_tree_count_by_size_.push_back(unique_tree_count); |
| 35 total_unique_tree_count_ += unique_tree_count; |
| 36 } |
| 37 } |
| 38 |
| 39 TreeGenerator::~TreeGenerator() { |
| 40 } |
21 | 41 |
22 int TreeGenerator::UniqueTreeCount() const { | 42 int TreeGenerator::UniqueTreeCount() const { |
23 return unique_tree_count_; | 43 return total_unique_tree_count_; |
24 }; | 44 } |
25 | 45 |
26 void TreeGenerator::BuildUniqueTree(int tree_index, AXTree* out_tree) const { | 46 void TreeGenerator::BuildUniqueTree(int tree_index, AXTree* out_tree) const { |
| 47 CHECK_LT(tree_index, total_unique_tree_count_); |
| 48 |
| 49 int unique_tree_count_so_far = 0; |
| 50 for (int node_count = 1; node_count <= max_node_count_; ++node_count) { |
| 51 int unique_tree_count = unique_tree_count_by_size_[node_count]; |
| 52 if (tree_index - unique_tree_count_so_far < unique_tree_count) { |
| 53 BuildUniqueTreeWithSize(node_count, |
| 54 tree_index - unique_tree_count_so_far, |
| 55 out_tree); |
| 56 return; |
| 57 } |
| 58 unique_tree_count_so_far += unique_tree_count; |
| 59 } |
| 60 } |
| 61 |
| 62 void TreeGenerator::BuildUniqueTreeWithSize( |
| 63 int node_count, int tree_index, AXTree* out_tree) const { |
27 std::vector<int> indices; | 64 std::vector<int> indices; |
28 std::vector<int> permuted; | 65 std::vector<int> permuted; |
29 CHECK(tree_index <= unique_tree_count_); | 66 int unique_tree_count = unique_tree_count_by_size_[node_count]; |
| 67 CHECK_LT(tree_index, unique_tree_count); |
30 | 68 |
31 // Use the first few bits of |tree_index| to permute the indices. | 69 if (permutations_) { |
32 for (int i = 0; i < node_count_; i++) | 70 // Use the first few bits of |tree_index| to permute the indices. |
33 indices.push_back(i + 1); | 71 for (int i = 0; i < node_count; ++i) |
34 for (int i = 0; i < node_count_; i++) { | 72 indices.push_back(i + 1); |
35 int p = (node_count_ - i); | 73 for (int i = 0; i < node_count; ++i) { |
36 int index = tree_index % p; | 74 int p = (node_count - i); |
37 tree_index /= p; | 75 int index = tree_index % p; |
38 permuted.push_back(indices[index]); | 76 tree_index /= p; |
39 indices.erase(indices.begin() + index); | 77 permuted.push_back(indices[index]); |
| 78 indices.erase(indices.begin() + index); |
| 79 } |
| 80 } else { |
| 81 for (int i = 0; i < node_count; ++i) |
| 82 permuted.push_back(i + 1); |
40 } | 83 } |
41 | 84 |
42 // Build an AXTreeUpdate. The first two nodes of the tree always | 85 // Build an AXTreeUpdate. The first two nodes of the tree always |
43 // go in the same place. | 86 // go in the same place. |
44 AXTreeUpdate update; | 87 AXTreeUpdate update; |
45 update.nodes.resize(node_count_); | 88 update.nodes.resize(node_count); |
46 update.nodes[0].id = permuted[0]; | 89 update.nodes[0].id = permuted[0]; |
47 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA; | 90 update.nodes[0].role = AX_ROLE_ROOT_WEB_AREA; |
48 update.nodes[0].state = AX_STATE_NONE; | 91 update.nodes[0].state = AX_STATE_NONE; |
49 update.nodes[0].child_ids.push_back(permuted[1]); | 92 if (node_count > 1) { |
50 update.nodes[1].id = permuted[1]; | 93 update.nodes[0].child_ids.push_back(permuted[1]); |
51 update.nodes[1].state = AX_STATE_NONE; | 94 update.nodes[1].id = permuted[1]; |
| 95 update.nodes[1].state = AX_STATE_NONE; |
| 96 } |
52 | 97 |
53 // The remaining nodes are assigned based on their parent | 98 // The remaining nodes are assigned based on their parent |
54 // selected from the next bits from |tree_index|. | 99 // selected from the next bits from |tree_index|. |
55 for (int i = 2; i < node_count_; i++) { | 100 for (int i = 2; i < node_count; ++i) { |
56 update.nodes[i].id = permuted[i]; | 101 update.nodes[i].id = permuted[i]; |
57 update.nodes[i].state = AX_STATE_NONE; | 102 update.nodes[i].state = AX_STATE_NONE; |
58 int parent_index = (tree_index % i); | 103 int parent_index = (tree_index % i); |
59 tree_index /= i; | 104 tree_index /= i; |
60 update.nodes[parent_index].child_ids.push_back(permuted[i]); | 105 update.nodes[parent_index].child_ids.push_back(permuted[i]); |
61 } | 106 } |
62 | 107 |
63 // Unserialize the tree update into the destination tree. | 108 // Unserialize the tree update into the destination tree. |
64 CHECK(out_tree->Unserialize(update)) << out_tree->error(); | 109 CHECK(out_tree->Unserialize(update)) << out_tree->error(); |
65 }; | 110 }; |
66 | 111 |
67 } // namespace ui | 112 } // namespace ui |
OLD | NEW |