Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 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 | |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 17 | #include "DominatorTree.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 21 | #include "android-base/logging.h" |
| 22 | |
| 23 | #include "ConfigDescription.h" |
| 24 | |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 25 | namespace aapt { |
| 26 | |
| 27 | DominatorTree::DominatorTree( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 28 | const std::vector<std::unique_ptr<ResourceConfigValue>>& configs) { |
| 29 | for (const auto& config : configs) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 30 | product_roots_[config->product].TryAddChild( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 31 | util::make_unique<Node>(config.get(), nullptr)); |
| 32 | } |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 35 | void DominatorTree::Accept(Visitor* visitor) { |
| 36 | for (auto& entry : product_roots_) { |
| 37 | visitor->VisitTree(entry.first, &entry.second); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 38 | } |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | bool 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 44 | // This is not the root and the child dominates us. |
| 45 | return false; |
| 46 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | return AddChild(std::move(new_child)); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 50 | bool DominatorTree::Node::AddChild(std::unique_ptr<Node> new_child) { |
| 51 | bool has_dominated_children = false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 52 | // Demote children dominated by the new config. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 53 | 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 Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 57 | child = {}; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 58 | has_dominated_children = true; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 59 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | } |
| 61 | // Remove dominated children. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | if (has_dominated_children) { |
| 63 | children_.erase( |
| 64 | std::remove_if(children_.begin(), children_.end(), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | [](const std::unique_ptr<Node>& child) -> bool { |
| 66 | return child == nullptr; |
| 67 | }), |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | children_.end()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | } |
| 70 | // Add the new config to a child if a child dominates the new config. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | for (auto& child : children_) { |
| 72 | if (child->Dominates(new_child.get())) { |
| 73 | child->AddChild(std::move(new_child)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | return true; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 75 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 76 | } |
| 77 | // The new config is not dominated by a child, so add it here. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 78 | new_child->parent_ = this; |
| 79 | children_.push_back(std::move(new_child)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 80 | return true; |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | bool DominatorTree::Node::Dominates(const Node* other) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 84 | // Check root node dominations. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 85 | if (other->is_root_node()) { |
| 86 | return is_root_node(); |
| 87 | } else if (is_root_node()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | return true; |
| 89 | } |
| 90 | // Neither node is a root node; compare the configurations. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 91 | return value_->config.Dominates(other->value_->config); |
Alexandria Cornwall | 77788eb | 2016-09-06 15:16:49 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 94 | } // namespace aapt |