blob: 2e341448312ea05098ece360dbc0522ead6c5ad7 [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>
8
9using std::set;
10using std::vector;
11
12namespace chromeos_update_engine {
13
14namespace {
15void TopologicalSortVisit(const Graph& graph,
16 set<Vertex::Index>* visited_nodes,
17 vector<Vertex::Index>* nodes,
18 Vertex::Index node) {
19 if (visited_nodes->find(node) != visited_nodes->end())
20 return;
21
22 visited_nodes->insert(node);
23 // Visit all children.
24 for (Vertex::EdgeMap::const_iterator it = graph[node].out_edges.begin();
25 it != graph[node].out_edges.end(); ++it) {
26 TopologicalSortVisit(graph, visited_nodes, nodes, it->first);
27 }
28 // Visit this node.
29 nodes->push_back(node);
30}
31} // namespace {}
32
33void TopologicalSort(const Graph& graph, vector<Vertex::Index>* out) {
34 set<Vertex::Index> visited_nodes;
35
36 for (Vertex::Index i = 0; i < graph.size(); i++) {
37 TopologicalSortVisit(graph, &visited_nodes, out, i);
38 }
39}
40
41} // namespace chromeos_update_engine