blob: 5ad67cf926c553f2594108e9d5194ddee2b9aff0 [file] [log] [blame]
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -08001// Copyright (c) 2009 The Chromium 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/graph_utils.h"
6#include "base/basictypes.h"
7
8using std::vector;
9
10namespace chromeos_update_engine {
11
12namespace graph_utils {
13
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070014uint64_t EdgeWeight(const Graph& graph, const Edge& edge) {
15 uint64_t weight = 0;
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080016 const vector<Extent>& extents =
17 graph[edge.first].out_edges.find(edge.second)->second.extents;
18 for (vector<Extent>::const_iterator it = extents.begin();
19 it != extents.end(); ++it) {
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070020 if (it->start_block() != kSparseHole)
21 weight += it->num_blocks();
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080022 }
23 return weight;
24}
25
Andrew de los Reyes09e56d62010-04-23 13:45:53 -070026void AppendBlockToExtents(vector<Extent>* extents, uint64_t block) {
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080027 if (!extents->empty()) {
28 Extent& extent = extents->back();
Andrew de los Reyesb10320d2010-03-31 16:44:44 -070029 if (block == kSparseHole) {
30 if (extent.start_block() == kSparseHole) {
31 // Extend sparse hole extent
32 extent.set_num_blocks(extent.num_blocks() + 1);
33 return;
34 } else {
35 // Create new extent below outer 'if'
36 }
37 } else if (extent.start_block() + extent.num_blocks() == block) {
Andrew de los Reyes0ce161b2010-02-22 15:27:01 -080038 extent.set_num_blocks(extent.num_blocks() + 1);
39 return;
40 }
41 }
42 Extent new_extent;
43 new_extent.set_start_block(block);
44 new_extent.set_num_blocks(1);
45 extents->push_back(new_extent);
46}
47
48} // namespace graph_utils
49
50} // namespace chromeos_update_engine