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 | |
| 17 | #include "ConfigDescription.h" |
| 18 | #include "DominatorTree.h" |
| 19 | |
| 20 | #include <algorithm> |
| 21 | |
| 22 | namespace aapt { |
| 23 | |
| 24 | DominatorTree::DominatorTree( |
| 25 | const std::vector<std::unique_ptr<ResourceConfigValue>>& configs) { |
| 26 | for (const auto& config : configs) { |
| 27 | mProductRoots[config->product].tryAddChild( |
| 28 | util::make_unique<Node>(config.get(), nullptr)); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | void DominatorTree::accept(Visitor* visitor) { |
| 33 | for (auto& entry : mProductRoots) { |
| 34 | visitor->visitTree(entry.first, &entry.second); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | bool DominatorTree::Node::tryAddChild(std::unique_ptr<Node> newChild) { |
| 39 | assert(newChild->mValue && "cannot add a root or empty node as a child"); |
| 40 | if (mValue && !dominates(newChild.get())) { |
| 41 | // This is not the root and the child dominates us. |
| 42 | return false; |
| 43 | } |
| 44 | return addChild(std::move(newChild)); |
| 45 | } |
| 46 | |
| 47 | bool DominatorTree::Node::addChild(std::unique_ptr<Node> newChild) { |
| 48 | bool hasDominatedChildren = false; |
| 49 | // Demote children dominated by the new config. |
| 50 | for (auto& child : mChildren) { |
| 51 | if (newChild->dominates(child.get())) { |
| 52 | child->mParent = newChild.get(); |
| 53 | newChild->mChildren.push_back(std::move(child)); |
| 54 | child = {}; |
| 55 | hasDominatedChildren = true; |
| 56 | } |
| 57 | } |
| 58 | // Remove dominated children. |
| 59 | if (hasDominatedChildren) { |
| 60 | mChildren.erase(std::remove_if(mChildren.begin(), mChildren.end(), |
| 61 | [](const std::unique_ptr<Node>& child) -> bool { |
| 62 | return child == nullptr; |
| 63 | }), mChildren.end()); |
| 64 | } |
| 65 | // Add the new config to a child if a child dominates the new config. |
| 66 | for (auto& child : mChildren) { |
| 67 | if (child->dominates(newChild.get())) { |
| 68 | child->addChild(std::move(newChild)); |
| 69 | return true; |
| 70 | } |
| 71 | } |
| 72 | // The new config is not dominated by a child, so add it here. |
| 73 | newChild->mParent = this; |
| 74 | mChildren.push_back(std::move(newChild)); |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | bool DominatorTree::Node::dominates(const Node* other) const { |
| 79 | // Check root node dominations. |
| 80 | if (other->isRootNode()) { |
| 81 | return isRootNode(); |
| 82 | } else if (isRootNode()) { |
| 83 | return true; |
| 84 | } |
| 85 | // Neither node is a root node; compare the configurations. |
| 86 | return mValue->config.dominates(other->mValue->config); |
| 87 | } |
| 88 | |
| 89 | } // namespace aapt |