blob: 86c73605f23f7fa7b870724841318185d59a7be6 [file] [log] [blame]
Colin Crosseccc5e42016-03-02 17:53:39 -08001/*
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 Albert8e310e82016-09-21 16:21:52 -070022#include <assert.h>
Colin Crosseccc5e42016-03-02 17:53:39 -080023#include <algorithm>
24
25#include "Allocator.h"
26
Colin Cross401319a2017-06-22 10:50:05 -070027template <class T>
Colin Crosseccc5e42016-03-02 17:53:39 -080028class 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 Cross401319a2017-06-22 10:50:05 -070037 Node(T* ptr, Allocator<Node> allocator)
38 : references_in(allocator), references_out(allocator), ptr(ptr){};
Colin Crosseccc5e42016-03-02 17:53:39 -080039 Node(Node&& rhs) = default;
40 void Edge(Node<T>* ref) {
41 references_out.emplace(ref);
42 ref->references_in.emplace(this);
43 }
Colin Cross401319a2017-06-22 10:50:05 -070044 template <class F>
Colin Crosseccc5e42016-03-02 17:53:39 -080045 void Foreach(F&& f) {
Colin Cross401319a2017-06-22 10:50:05 -070046 for (auto& node : references_out) {
Colin Crosseccc5e42016-03-02 17:53:39 -080047 f(node->ptr);
48 }
49 }
Colin Cross401319a2017-06-22 10:50:05 -070050
Colin Crosseccc5e42016-03-02 17:53:39 -080051 private:
52 DISALLOW_COPY_AND_ASSIGN(Node<T>);
53};
54
Colin Cross401319a2017-06-22 10:50:05 -070055template <class T>
Colin Crosseccc5e42016-03-02 17:53:39 -080056using Graph = allocator::vector<Node<T>*>;
57
Colin Cross401319a2017-06-22 10:50:05 -070058template <class T>
Colin Crosseccc5e42016-03-02 17:53:39 -080059using SCC = allocator::vector<Node<T>*>;
60
Colin Cross401319a2017-06-22 10:50:05 -070061template <class T>
Colin Crosseccc5e42016-03-02 17:53:39 -080062using SCCList = allocator::vector<SCC<T>>;
63
Colin Cross401319a2017-06-22 10:50:05 -070064template <class T>
Colin Crosseccc5e42016-03-02 17:53:39 -080065class TarjanAlgorithm {
66 public:
Colin Cross401319a2017-06-22 10:50:05 -070067 explicit TarjanAlgorithm(Allocator<void> allocator)
68 : index_(0), stack_(allocator), components_(allocator) {}
Colin Crosseccc5e42016-03-02 17:53:39 -080069
70 void Execute(Graph<T>& graph, SCCList<T>& out);
Colin Cross401319a2017-06-22 10:50:05 -070071
Colin Crosseccc5e42016-03-02 17:53:39 -080072 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 Cross401319a2017-06-22 10:50:05 -070081template <class T>
Colin Crosseccc5e42016-03-02 17:53:39 -080082void TarjanAlgorithm<T>::Execute(Graph<T>& graph, SCCList<T>& out) {
83 stack_.clear();
84 components_.clear();
85 index_ = 0;
Colin Cross401319a2017-06-22 10:50:05 -070086 for (auto& it : graph) {
Colin Crosseccc5e42016-03-02 17:53:39 -080087 it->index = UNDEFINED_INDEX;
88 it->lowlink = UNDEFINED_INDEX;
89 }
90
Colin Cross401319a2017-06-22 10:50:05 -070091 for (auto& it : graph) {
Colin Crosseccc5e42016-03-02 17:53:39 -080092 if (it->index == UNDEFINED_INDEX) {
93 Tarjan(it, graph);
94 }
95 }
96 out.swap(components_);
97}
98
Colin Cross401319a2017-06-22 10:50:05 -070099template <class T>
Colin Crosseccc5e42016-03-02 17:53:39 -0800100void 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 Cross401319a2017-06-22 10:50:05 -0700106 for (auto& it : vertex->references_out) {
Colin Crosseccc5e42016-03-02 17:53:39 -0800107 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 Cross401319a2017-06-22 10:50:05 -0700128template <class T>
Colin Crosseccc5e42016-03-02 17:53:39 -0800129void Tarjan(Graph<T>& graph, SCCList<T>& out) {
130 TarjanAlgorithm<T> tarjan{graph.get_allocator()};
131 tarjan.Execute(graph, out);
132}
133
Colin Cross401319a2017-06-22 10:50:05 -0700134#endif // LIBMEMUNREACHABLE_TARJAN_H_