Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2010 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 | // |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 16 | #include "update_engine/payload_generator/tarjan.h" |
Andrew de los Reyes | 81ebcd8a | 2010-03-09 15:56:18 -0800 | [diff] [blame] | 17 | |
| 18 | #include <algorithm> |
| 19 | #include <vector> |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 20 | |
| 21 | #include <base/logging.h> |
Eric Caruso | c0cfb7d | 2018-01-23 16:04:06 -0800 | [diff] [blame] | 22 | #include <base/stl_util.h> |
Andrew de los Reyes | 81ebcd8a | 2010-03-09 15:56:18 -0800 | [diff] [blame] | 23 | |
| 24 | using std::min; |
| 25 | using std::vector; |
| 26 | |
| 27 | namespace chromeos_update_engine { |
| 28 | |
| 29 | namespace { |
| 30 | const vector<Vertex>::size_type kInvalidIndex = -1; |
| 31 | } |
| 32 | |
| 33 | void TarjanAlgorithm::Execute(Vertex::Index vertex, |
| 34 | Graph* graph, |
| 35 | vector<Vertex::Index>* out) { |
| 36 | stack_.clear(); |
| 37 | components_.clear(); |
| 38 | index_ = 0; |
| 39 | for (Graph::iterator it = graph->begin(); it != graph->end(); ++it) |
| 40 | it->index = it->lowlink = kInvalidIndex; |
| 41 | required_vertex_ = vertex; |
| 42 | |
| 43 | Tarjan(vertex, graph); |
| 44 | if (!components_.empty()) |
| 45 | out->swap(components_[0]); |
| 46 | } |
| 47 | |
| 48 | void TarjanAlgorithm::Tarjan(Vertex::Index vertex, Graph* graph) { |
| 49 | CHECK_EQ((*graph)[vertex].index, kInvalidIndex); |
| 50 | (*graph)[vertex].index = index_; |
| 51 | (*graph)[vertex].lowlink = index_; |
| 52 | index_++; |
| 53 | stack_.push_back(vertex); |
| 54 | for (Vertex::EdgeMap::iterator it = (*graph)[vertex].out_edges.begin(); |
Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 55 | it != (*graph)[vertex].out_edges.end(); |
| 56 | ++it) { |
Andrew de los Reyes | 81ebcd8a | 2010-03-09 15:56:18 -0800 | [diff] [blame] | 57 | Vertex::Index vertex_next = it->first; |
| 58 | if ((*graph)[vertex_next].index == kInvalidIndex) { |
| 59 | Tarjan(vertex_next, graph); |
Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 60 | (*graph)[vertex].lowlink = |
| 61 | min((*graph)[vertex].lowlink, (*graph)[vertex_next].lowlink); |
Eric Caruso | c0cfb7d | 2018-01-23 16:04:06 -0800 | [diff] [blame] | 62 | } else if (base::ContainsValue(stack_, vertex_next)) { |
Amin Hassani | 232f8f9 | 2019-01-14 16:15:31 -0800 | [diff] [blame] | 63 | (*graph)[vertex].lowlink = |
| 64 | min((*graph)[vertex].lowlink, (*graph)[vertex_next].index); |
Andrew de los Reyes | 81ebcd8a | 2010-03-09 15:56:18 -0800 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | if ((*graph)[vertex].lowlink == (*graph)[vertex].index) { |
| 68 | vector<Vertex::Index> component; |
| 69 | Vertex::Index other_vertex; |
| 70 | do { |
| 71 | other_vertex = stack_.back(); |
| 72 | stack_.pop_back(); |
| 73 | component.push_back(other_vertex); |
| 74 | } while (other_vertex != vertex && !stack_.empty()); |
Alex Deymo | 161c4a1 | 2014-05-16 15:56:21 -0700 | [diff] [blame] | 75 | |
Eric Caruso | c0cfb7d | 2018-01-23 16:04:06 -0800 | [diff] [blame] | 76 | if (base::ContainsValue(component, required_vertex_)) { |
Andrew de los Reyes | 81ebcd8a | 2010-03-09 15:56:18 -0800 | [diff] [blame] | 77 | components_.resize(components_.size() + 1); |
| 78 | component.swap(components_.back()); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | } // namespace chromeos_update_engine |