Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [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 | // Based on system/update_engine/payload_generator/tarjan.cc |
| 18 | |
| 19 | #ifndef LIBMEMUNREACHABLE_TARJAN_H_ |
| 20 | #define LIBMEMUNREACHABLE_TARJAN_H_ |
| 21 | |
Dan Albert | 8b31627 | 2016-09-21 16:21:52 -0700 | [diff] [blame] | 22 | #include <assert.h> |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 23 | #include <algorithm> |
| 24 | |
| 25 | #include "Allocator.h" |
| 26 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 27 | template <class T> |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 28 | class Node { |
| 29 | public: |
| 30 | allocator::set<Node<T>*> references_in; |
| 31 | allocator::set<Node<T>*> references_out; |
| 32 | size_t index; |
| 33 | size_t lowlink; |
| 34 | |
| 35 | T* ptr; |
| 36 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 37 | Node(T* ptr, Allocator<Node> allocator) |
| 38 | : references_in(allocator), references_out(allocator), ptr(ptr){}; |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 39 | Node(Node&& rhs) = default; |
| 40 | void Edge(Node<T>* ref) { |
| 41 | references_out.emplace(ref); |
| 42 | ref->references_in.emplace(this); |
| 43 | } |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 44 | template <class F> |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 45 | void Foreach(F&& f) { |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 46 | for (auto& node : references_out) { |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 47 | f(node->ptr); |
| 48 | } |
| 49 | } |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 50 | |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 51 | private: |
| 52 | DISALLOW_COPY_AND_ASSIGN(Node<T>); |
| 53 | }; |
| 54 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 55 | template <class T> |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 56 | using Graph = allocator::vector<Node<T>*>; |
| 57 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 58 | template <class T> |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 59 | using SCC = allocator::vector<Node<T>*>; |
| 60 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 61 | template <class T> |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 62 | using SCCList = allocator::vector<SCC<T>>; |
| 63 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 64 | template <class T> |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 65 | class TarjanAlgorithm { |
| 66 | public: |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 67 | explicit TarjanAlgorithm(Allocator<void> allocator) |
| 68 | : index_(0), stack_(allocator), components_(allocator) {} |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 69 | |
| 70 | void Execute(Graph<T>& graph, SCCList<T>& out); |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 71 | |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 72 | private: |
| 73 | static constexpr size_t UNDEFINED_INDEX = static_cast<size_t>(-1); |
| 74 | void Tarjan(Node<T>* vertex, Graph<T>& graph); |
| 75 | |
| 76 | size_t index_; |
| 77 | allocator::vector<Node<T>*> stack_; |
| 78 | SCCList<T> components_; |
| 79 | }; |
| 80 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 81 | template <class T> |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 82 | void TarjanAlgorithm<T>::Execute(Graph<T>& graph, SCCList<T>& out) { |
| 83 | stack_.clear(); |
| 84 | components_.clear(); |
| 85 | index_ = 0; |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 86 | for (auto& it : graph) { |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 87 | it->index = UNDEFINED_INDEX; |
| 88 | it->lowlink = UNDEFINED_INDEX; |
| 89 | } |
| 90 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 91 | for (auto& it : graph) { |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 92 | if (it->index == UNDEFINED_INDEX) { |
| 93 | Tarjan(it, graph); |
| 94 | } |
| 95 | } |
| 96 | out.swap(components_); |
| 97 | } |
| 98 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 99 | template <class T> |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 100 | void TarjanAlgorithm<T>::Tarjan(Node<T>* vertex, Graph<T>& graph) { |
| 101 | assert(vertex->index == UNDEFINED_INDEX); |
| 102 | vertex->index = index_; |
| 103 | vertex->lowlink = index_; |
| 104 | index_++; |
| 105 | stack_.push_back(vertex); |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 106 | for (auto& it : vertex->references_out) { |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 107 | Node<T>* vertex_next = it; |
| 108 | if (vertex_next->index == UNDEFINED_INDEX) { |
| 109 | Tarjan(vertex_next, graph); |
| 110 | vertex->lowlink = std::min(vertex->lowlink, vertex_next->lowlink); |
| 111 | } else if (std::find(stack_.begin(), stack_.end(), vertex_next) != stack_.end()) { |
| 112 | vertex->lowlink = std::min(vertex->lowlink, vertex_next->index); |
| 113 | } |
| 114 | } |
| 115 | if (vertex->lowlink == vertex->index) { |
| 116 | SCC<T> component{components_.get_allocator()}; |
| 117 | Node<T>* other_vertex; |
| 118 | do { |
| 119 | other_vertex = stack_.back(); |
| 120 | stack_.pop_back(); |
| 121 | component.push_back(other_vertex); |
| 122 | } while (other_vertex != vertex && !stack_.empty()); |
| 123 | |
| 124 | components_.emplace_back(component); |
| 125 | } |
| 126 | } |
| 127 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 128 | template <class T> |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 129 | void Tarjan(Graph<T>& graph, SCCList<T>& out) { |
| 130 | TarjanAlgorithm<T> tarjan{graph.get_allocator()}; |
| 131 | tarjan.Execute(graph, out); |
| 132 | } |
| 133 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 134 | #endif // LIBMEMUNREACHABLE_TARJAN_H_ |