blob: 2d4ca3160762ee0ed3e0df4d1cda3f910b4a15fd [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Deymo161c4a12014-05-16 15:56:21 -070016#include "update_engine/payload_generator/tarjan.h"
Andrew de los Reyes81ebcd8a2010-03-09 15:56:18 -080017
18#include <algorithm>
19#include <vector>
Alex Deymo161c4a12014-05-16 15:56:21 -070020
21#include <base/logging.h>
Eric Carusoc0cfb7d2018-01-23 16:04:06 -080022#include <base/stl_util.h>
Andrew de los Reyes81ebcd8a2010-03-09 15:56:18 -080023
24using std::min;
25using std::vector;
26
27namespace chromeos_update_engine {
28
29namespace {
30const vector<Vertex>::size_type kInvalidIndex = -1;
31}
32
33void 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
48void 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 Hassani232f8f92019-01-14 16:15:31 -080055 it != (*graph)[vertex].out_edges.end();
56 ++it) {
Andrew de los Reyes81ebcd8a2010-03-09 15:56:18 -080057 Vertex::Index vertex_next = it->first;
58 if ((*graph)[vertex_next].index == kInvalidIndex) {
59 Tarjan(vertex_next, graph);
Amin Hassani232f8f92019-01-14 16:15:31 -080060 (*graph)[vertex].lowlink =
61 min((*graph)[vertex].lowlink, (*graph)[vertex_next].lowlink);
Eric Carusoc0cfb7d2018-01-23 16:04:06 -080062 } else if (base::ContainsValue(stack_, vertex_next)) {
Amin Hassani232f8f92019-01-14 16:15:31 -080063 (*graph)[vertex].lowlink =
64 min((*graph)[vertex].lowlink, (*graph)[vertex_next].index);
Andrew de los Reyes81ebcd8a2010-03-09 15:56:18 -080065 }
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 Deymo161c4a12014-05-16 15:56:21 -070075
Eric Carusoc0cfb7d2018-01-23 16:04:06 -080076 if (base::ContainsValue(component, required_vertex_)) {
Andrew de los Reyes81ebcd8a2010-03-09 15:56:18 -080077 components_.resize(components_.size() + 1);
78 component.swap(components_.back());
79 }
80 }
81}
82
83} // namespace chromeos_update_engine