OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 "ppapi/shared_impl/var_graph.h" | |
6 | |
7 #include <stack> | |
8 | |
9 #include "base/stl_util.h" | |
10 #include "ppapi/shared_impl/array_var.h" | |
11 #include "ppapi/shared_impl/dictionary_var.h" | |
12 #include "ppapi/shared_impl/ppapi_globals.h" | |
13 #include "ppapi/shared_impl/var.h" | |
14 #include "ppapi/shared_impl/var_tracker.h" | |
15 | |
16 namespace ppapi { | |
17 | |
18 // static | |
19 bool VarGraph::Create(const PP_Var& start_node, VarGraph* var_graph) { | |
20 std::stack<PP_Var> stack; | |
21 stack.push(start_node); | |
22 | |
23 // Traverse the PP_Var graph with DFS and add each reference node to |nodes| | |
24 // exactly once. | |
25 while (!stack.empty()) { | |
26 PP_Var current = stack.top(); | |
27 stack.pop(); | |
28 if (!PpapiGlobals::Get()->GetVarTracker()->GetVar(current.value.as_id) || | |
29 ContainsKey(var_graph->id_map, current.value.as_id)) { | |
30 continue; | |
31 } | |
32 var_graph->nodes.push_back(current); | |
dmichael (off chromium)
2013/04/12 22:40:38
Why have the vector at all? You could just stick P
raymes
2013/04/14 16:32:57
This has changed.
| |
33 var_graph->id_map[current.value.as_id] = var_graph->nodes.size() - 1; | |
34 | |
35 // Add child nodes to the stack. | |
36 if (current.type == PP_VARTYPE_ARRAY) { | |
37 ArrayVar* array_var = ArrayVar::FromPPVar(current); | |
38 if (!array_var) { | |
39 NOTREACHED(); | |
40 return false; | |
41 } | |
42 for (ArrayVar::ElementVector::const_iterator iter = | |
43 array_var->elements().begin(); | |
44 iter != array_var->elements().end(); | |
45 ++iter) { | |
46 stack.push(iter->get()); | |
47 } | |
48 } else if (current.type == PP_VARTYPE_DICTIONARY) { | |
49 DictionaryVar* dict_var = DictionaryVar::FromPPVar(current); | |
50 if (!dict_var) { | |
51 NOTREACHED(); | |
52 return false; | |
53 } | |
54 for (DictionaryVar::KeyValueMap::const_iterator iter = | |
55 dict_var->key_value_map().begin(); | |
56 iter != dict_var->key_value_map().end(); | |
57 ++iter) { | |
58 stack.push(iter->second.get()); | |
59 } | |
60 } | |
61 } | |
62 DCHECK(var_graph->nodes.size() == var_graph->id_map.size()); | |
63 return true; | |
64 } | |
65 | |
66 } // namespace ppapi | |
OLD | NEW |