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 <vector> |
| 6 |
5 namespace ui { | 7 namespace ui { |
6 | 8 |
7 class AXTree; | 9 class AXTree; |
8 | 10 |
9 // A class to create all possible trees with <n> nodes and the ids [1...n]. | 11 // A class to create all possible trees with up to <n> nodes and the |
| 12 // ids [1...n]. |
10 // | 13 // |
11 // There are two parts to the algorithm: | 14 // There are two parts to the algorithm: |
12 // | 15 // |
13 // The tree structure is formed as follows: without loss of generality, | 16 // The tree structure is formed as follows: without loss of generality, |
14 // the first node becomes the root and the second node becomes its | 17 // the first node becomes the root and the second node becomes its |
15 // child. Thereafter, choose every possible parent for every other node. | 18 // child. Thereafter, choose every possible parent for every other node. |
16 // | 19 // |
17 // So for node i in (3...n), there are (i - 1) possible choices for its | 20 // So for node i in (3...n), there are (i - 1) possible choices for its |
18 // parent, for a total of (n-1)! (n minus 1 factorial) possible trees. | 21 // parent, for a total of (n-1)! (n minus 1 factorial) possible trees. |
19 // | 22 // |
20 // The second part is the assignment of ids to the nodes in the tree. | 23 // The second optional part is the assignment of ids to the nodes in the tree. |
21 // There are exactly n! (n factorial) permutations of the sequence 1...n, | 24 // There are exactly n! (n factorial) permutations of the sequence 1...n, |
22 // and each of these is assigned to every node in every possible tree. | 25 // and each of these is assigned to every node in every possible tree. |
23 // | 26 // |
24 // The total number of trees returned for a given <n>, then, is | 27 // The total number of trees for a given <n>, including permutations of ids, is |
25 // n! * (n-1)! | 28 // n! * (n-1)! |
26 // | 29 // |
27 // n = 2: 2 trees | 30 // n = 2: 2 trees |
28 // n = 3: 12 trees | 31 // n = 3: 12 trees |
29 // n = 4: 144 trees | 32 // n = 4: 144 trees |
30 // n = 5: 2880 trees | 33 // n = 5: 2880 trees |
31 // | 34 // |
32 // This grows really fast! Luckily it's very unlikely that there'd be | 35 // Note that the generator returns all trees with sizes *up to* <n>, which |
33 // bugs that affect trees with >4 nodes that wouldn't affect a smaller tree | 36 // is a bit larger. |
34 // too. | 37 // |
| 38 // This grows really fast! Still, it's very helpful for exhaustively testing |
| 39 // tree code on smaller trees at least. |
35 class TreeGenerator { | 40 class TreeGenerator { |
36 public: | 41 public: |
37 TreeGenerator(int node_count); | 42 // Will generate all trees with up to |max_node_count| nodes. |
| 43 // If |permutations| is true, will return every possible permutation of |
| 44 // ids, otherwise the root will always have id 1, and so on. |
| 45 TreeGenerator(int max_node_count, bool permutations); |
| 46 ~TreeGenerator(); |
38 | 47 |
39 int UniqueTreeCount() const; | 48 int UniqueTreeCount() const; |
40 | 49 |
41 void BuildUniqueTree(int tree_index, AXTree* out_tree) const; | 50 void BuildUniqueTree(int tree_index, AXTree* out_tree) const; |
42 | 51 |
43 private: | 52 private: |
44 int node_count_; | 53 void BuildUniqueTreeWithSize( |
45 int unique_tree_count_; | 54 int node_count, int tree_index, AXTree* out_tree) const; |
| 55 |
| 56 int max_node_count_; |
| 57 bool permutations_; |
| 58 int total_unique_tree_count_; |
| 59 std::vector<int> unique_tree_count_by_size_; |
46 }; | 60 }; |
47 | 61 |
48 } // namespace ui | 62 } // namespace ui |
OLD | NEW |