blob: ff18033c641a99ee1ba0090f386c23665351444b [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"
Mårten Kongstad5c541f62018-06-20 08:46:41 +020022#include "androidfw/ConfigDescription.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023
Mårten Kongstad5c541f62018-06-20 08:46:41 +020024using ::android::ConfigDescription;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070026namespace aapt {
27
28DominatorTree::DominatorTree(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070029 const std::vector<std::unique_ptr<ResourceConfigValue>>& configs) {
30 for (const auto& config : configs) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031 product_roots_[config->product].TryAddChild(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 util::make_unique<Node>(config.get(), nullptr));
33 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070034}
35
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036void DominatorTree::Accept(Visitor* visitor) {
37 for (auto& entry : product_roots_) {
38 visitor->VisitTree(entry.first, &entry.second);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070040}
41
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042bool 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())) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 // This is not the root and the child dominates us.
46 return false;
47 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 return AddChild(std::move(new_child));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070049}
50
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051bool DominatorTree::Node::AddChild(std::unique_ptr<Node> new_child) {
52 bool has_dominated_children = false;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 // Demote children dominated by the new config.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 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));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 child = {};
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 has_dominated_children = true;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070060 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 }
62 // Remove dominated children.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 if (has_dominated_children) {
64 children_.erase(
65 std::remove_if(children_.begin(), children_.end(),
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 [](const std::unique_ptr<Node>& child) -> bool {
67 return child == nullptr;
68 }),
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 children_.end());
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 }
71 // Add the new config to a child if a child dominates the new config.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 for (auto& child : children_) {
73 if (child->Dominates(new_child.get())) {
74 child->AddChild(std::move(new_child));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 return true;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070076 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 }
78 // The new config is not dominated by a child, so add it here.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 new_child->parent_ = this;
80 children_.push_back(std::move(new_child));
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 return true;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070082}
83
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084bool DominatorTree::Node::Dominates(const Node* other) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 // Check root node dominations.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 if (other->is_root_node()) {
87 return is_root_node();
88 } else if (is_root_node()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 return true;
90 }
91 // Neither node is a root node; compare the configurations.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 return value_->config.Dominates(other->value_->config);
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070093}
94
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095} // namespace aapt