blob: ff18033c641a99ee1ba0090f386c23665351444b (
plain)
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
|
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "DominatorTree.h"
#include <algorithm>
#include "android-base/logging.h"
#include "androidfw/ConfigDescription.h"
using ::android::ConfigDescription;
namespace aapt {
DominatorTree::DominatorTree(
const std::vector<std::unique_ptr<ResourceConfigValue>>& configs) {
for (const auto& config : configs) {
product_roots_[config->product].TryAddChild(
util::make_unique<Node>(config.get(), nullptr));
}
}
void DominatorTree::Accept(Visitor* visitor) {
for (auto& entry : product_roots_) {
visitor->VisitTree(entry.first, &entry.second);
}
}
bool DominatorTree::Node::TryAddChild(std::unique_ptr<Node> new_child) {
CHECK(new_child->value_) << "cannot add a root or empty node as a child";
if (value_ && !Dominates(new_child.get())) {
// This is not the root and the child dominates us.
return false;
}
return AddChild(std::move(new_child));
}
bool DominatorTree::Node::AddChild(std::unique_ptr<Node> new_child) {
bool has_dominated_children = false;
// Demote children dominated by the new config.
for (auto& child : children_) {
if (new_child->Dominates(child.get())) {
child->parent_ = new_child.get();
new_child->children_.push_back(std::move(child));
child = {};
has_dominated_children = true;
}
}
// Remove dominated children.
if (has_dominated_children) {
children_.erase(
std::remove_if(children_.begin(), children_.end(),
[](const std::unique_ptr<Node>& child) -> bool {
return child == nullptr;
}),
children_.end());
}
// Add the new config to a child if a child dominates the new config.
for (auto& child : children_) {
if (child->Dominates(new_child.get())) {
child->AddChild(std::move(new_child));
return true;
}
}
// The new config is not dominated by a child, so add it here.
new_child->parent_ = this;
children_.push_back(std::move(new_child));
return true;
}
bool DominatorTree::Node::Dominates(const Node* other) const {
// Check root node dominations.
if (other->is_root_node()) {
return is_root_node();
} else if (is_root_node()) {
return true;
}
// Neither node is a root node; compare the configurations.
return value_->config.Dominates(other->value_->config);
}
} // namespace aapt
|