blob: 8422849c7d84e92813c45a7adea674da5e5090bd [file] [log] [blame]
Andrew de los Reyes58151552010-03-10 20:07:08 -08001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "update_engine/topological_sort.h"
6#include <set>
7#include <vector>
Andrew de los Reyesb10320d2010-03-31 16:44:44 -07008#include "base/logging.h"
Andrew de los Reyes58151552010-03-10 20:07:08 -08009
10using std::set;
11using std::vector;
12
13namespace chromeos_update_engine {
14
15namespace {
16void TopologicalSortVisit(const Graph& graph,
17 set<Vertex::Index>* visited_nodes,
18 vector<Vertex::Index>* nodes,
19 Vertex::Index node) {
20 if (visited_nodes->find(node) != visited_nodes->end())
21 return;
22
23 visited_nodes->insert(node);
24 // Visit all children.
25 for (Vertex::EdgeMap::const_iterator it = graph[node].out_edges.begin();
26 it != graph[node].out_edges.end(); ++it) {
27 TopologicalSortVisit(graph, visited_nodes, nodes, it->first);
28 }
29 // Visit this node.
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070030 LOG(INFO) << graph[node].file_name << " " << graph[node].op.type() << " "
31 << graph[node].op.data_length();
Andrew de los Reyes58151552010-03-10 20:07:08 -080032 nodes->push_back(node);
33}
34} // namespace {}
35
36void TopologicalSort(const Graph& graph, vector<Vertex::Index>* out) {
37 set<Vertex::Index> visited_nodes;
38
39 for (Vertex::Index i = 0; i < graph.size(); i++) {
40 TopologicalSortVisit(graph, &visited_nodes, out, i);
41 }
42}
43
44} // namespace chromeos_update_engine