1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "DominatorTree.h"
18
19 #include <algorithm>
20
21 #include "android-base/logging.h"
22 #include "androidfw/ConfigDescription.h"
23
24 using ::android::ConfigDescription;
25
26 namespace aapt {
27
DominatorTree(const std::vector<std::unique_ptr<ResourceConfigValue>> & configs)28 DominatorTree::DominatorTree(
29 const std::vector<std::unique_ptr<ResourceConfigValue>>& configs) {
30 for (const auto& config : configs) {
31 product_roots_[config->product].TryAddChild(
32 util::make_unique<Node>(config.get(), nullptr));
33 }
34 }
35
Accept(Visitor * visitor)36 void DominatorTree::Accept(Visitor* visitor) {
37 for (auto& entry : product_roots_) {
38 visitor->VisitTree(entry.first, &entry.second);
39 }
40 }
41
TryAddChild(std::unique_ptr<Node> new_child)42 bool DominatorTree::Node::TryAddChild(std::unique_ptr<Node> new_child) {
43 CHECK(new_child->value_) << "cannot add a root or empty node as a child";
44 if (value_ && !Dominates(new_child.get())) {
45 // This is not the root and the child dominates us.
46 return false;
47 }
48 return AddChild(std::move(new_child));
49 }
50
AddChild(std::unique_ptr<Node> new_child)51 bool DominatorTree::Node::AddChild(std::unique_ptr<Node> new_child) {
52 bool has_dominated_children = false;
53 // Demote children dominated by the new config.
54 for (auto& child : children_) {
55 if (new_child->Dominates(child.get())) {
56 child->parent_ = new_child.get();
57 new_child->children_.push_back(std::move(child));
58 child = {};
59 has_dominated_children = true;
60 }
61 }
62 // Remove dominated children.
63 if (has_dominated_children) {
64 children_.erase(
65 std::remove_if(children_.begin(), children_.end(),
66 [](const std::unique_ptr<Node>& child) -> bool {
67 return child == nullptr;
68 }),
69 children_.end());
70 }
71 // Add the new config to a child if a child dominates the new config.
72 for (auto& child : children_) {
73 if (child->Dominates(new_child.get())) {
74 child->AddChild(std::move(new_child));
75 return true;
76 }
77 }
78 // The new config is not dominated by a child, so add it here.
79 new_child->parent_ = this;
80 children_.push_back(std::move(new_child));
81 return true;
82 }
83
Dominates(const Node * other) const84 bool DominatorTree::Node::Dominates(const Node* other) const {
85 // Check root node dominations.
86 if (other->is_root_node()) {
87 return is_root_node();
88 } else if (is_root_node()) {
89 return true;
90 }
91 // Neither node is a root node; compare the configurations.
92 return value_->config.Dominates(other->value_->config);
93 }
94
95 } // namespace aapt
96