blob: 118a385e2253a742241fed84dc21f3aa44050c53 [file] [log] [blame]
Alexandria Cornwall77788eb2016-09-06 15:16:49 -07001/*
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
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070017#include "DominatorTree.h"
18
19#include <algorithm>
20
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021#include "android-base/logging.h"
22
23#include "ConfigDescription.h"
24
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070025namespace aapt {
26
27DominatorTree::DominatorTree(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070028 const std::vector<std::unique_ptr<ResourceConfigValue>>& configs) {
29 for (const auto& config : configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030 product_roots_[config->product].TryAddChild(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070031 util::make_unique<Node>(config.get(), nullptr));
32 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070033}
34
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035void DominatorTree::Accept(Visitor* visitor) {
36 for (auto& entry : product_roots_) {
37 visitor->VisitTree(entry.first, &entry.second);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070039}
40
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041bool DominatorTree::Node::TryAddChild(std::unique_ptr<Node> new_child) {
42 CHECK(new_child->value_) << "cannot add a root or empty node as a child";
43 if (value_ && !Dominates(new_child.get())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 // This is not the root and the child dominates us.
45 return false;
46 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 return AddChild(std::move(new_child));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070048}
49
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050bool DominatorTree::Node::AddChild(std::unique_ptr<Node> new_child) {
51 bool has_dominated_children = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070052 // Demote children dominated by the new config.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 for (auto& child : children_) {
54 if (new_child->Dominates(child.get())) {
55 child->parent_ = new_child.get();
56 new_child->children_.push_back(std::move(child));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 child = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 has_dominated_children = true;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070059 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 }
61 // Remove dominated children.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 if (has_dominated_children) {
63 children_.erase(
64 std::remove_if(children_.begin(), children_.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 [](const std::unique_ptr<Node>& child) -> bool {
66 return child == nullptr;
67 }),
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 children_.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 }
70 // Add the new config to a child if a child dominates the new config.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 for (auto& child : children_) {
72 if (child->Dominates(new_child.get())) {
73 child->AddChild(std::move(new_child));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 return true;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070075 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 }
77 // The new config is not dominated by a child, so add it here.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 new_child->parent_ = this;
79 children_.push_back(std::move(new_child));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 return true;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070081}
82
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083bool DominatorTree::Node::Dominates(const Node* other) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 // Check root node dominations.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 if (other->is_root_node()) {
86 return is_root_node();
87 } else if (is_root_node()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 return true;
89 }
90 // Neither node is a root node; compare the configurations.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 return value_->config.Dominates(other->value_->config);
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070092}
93
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094} // namespace aapt