blob: 8f6f7832755909f45d5a4a7fd5d6447bccd5a9d6 [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"
Adam Lesinskicacb28f2016-10-19 12:18:14 -070018#include "ConfigDescription.h"
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070019
20#include <algorithm>
21
22namespace aapt {
23
24DominatorTree::DominatorTree(
Adam Lesinskicacb28f2016-10-19 12:18:14 -070025 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 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070030}
31
32void DominatorTree::accept(Visitor* visitor) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 for (auto& entry : mProductRoots) {
34 visitor->visitTree(entry.first, &entry.second);
35 }
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070036}
37
38bool DominatorTree::Node::tryAddChild(std::unique_ptr<Node> newChild) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 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));
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070045}
46
47bool DominatorTree::Node::addChild(std::unique_ptr<Node> newChild) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 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;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070056 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 }
58 // Remove dominated children.
59 if (hasDominatedChildren) {
60 mChildren.erase(
61 std::remove_if(mChildren.begin(), mChildren.end(),
62 [](const std::unique_ptr<Node>& child) -> bool {
63 return child == nullptr;
64 }),
65 mChildren.end());
66 }
67 // Add the new config to a child if a child dominates the new config.
68 for (auto& child : mChildren) {
69 if (child->dominates(newChild.get())) {
70 child->addChild(std::move(newChild));
71 return true;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070072 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 }
74 // The new config is not dominated by a child, so add it here.
75 newChild->mParent = this;
76 mChildren.push_back(std::move(newChild));
77 return true;
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070078}
79
80bool DominatorTree::Node::dominates(const Node* other) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 // Check root node dominations.
82 if (other->isRootNode()) {
83 return isRootNode();
84 } else if (isRootNode()) {
85 return true;
86 }
87 // Neither node is a root node; compare the configurations.
88 return mValue->config.dominates(other->mValue->config);
Alexandria Cornwall77788eb2016-09-06 15:16:49 -070089}
90
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091} // namespace aapt