OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/devtools/serialize_host_descriptions.h" | 5 #include "chrome/browser/devtools/serialize_host_descriptions.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <unordered_set> | |
8 #include <utility> | 9 #include <utility> |
9 | 10 |
10 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
11 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 // Returns the serialization of |root|. It expects |children[x]| to be the | 16 // Returns the serialization of |root|. It expects |children[x]| to be the |
16 // vector of child nodes for all descendants |x| of |root|. The serialization | 17 // vector of child nodes for all descendants |x| of |root|. The serialization |
17 // consists of taking the |representation| value of each node, starting in | 18 // consists of taking the |representation| value of each node, starting in |
(...skipping 15 matching lines...) Expand all Loading... | |
33 } | 34 } |
34 | 35 |
35 if (!children_list->empty()) | 36 if (!children_list->empty()) |
36 root->Set(child_key, std::move(children_list)); | 37 root->Set(child_key, std::move(children_list)); |
37 return std::move(*root); | 38 return std::move(*root); |
38 } | 39 } |
39 | 40 |
40 // Takes a vector of host description and converts it into: | 41 // Takes a vector of host description and converts it into: |
41 // |children|: a map from a host's representation to representations of its | 42 // |children|: a map from a host's representation to representations of its |
42 // children, | 43 // children, |
43 // |roots|: a vector of representations of hosts with no parents, and | 44 // |roots|: a set of representations of hosts with no parents, and |
44 // |representations|: a vector actually storing all those representations to | 45 // |representations|: a vector actually storing all those representations to |
45 // which the rest just points. | 46 // which the rest just points. |
46 void CreateDictionaryForest( | 47 void CreateDictionaryForest( |
47 std::vector<HostDescriptionNode> hosts, | 48 std::vector<HostDescriptionNode> hosts, |
48 std::map<base::DictionaryValue*, std::vector<base::DictionaryValue*>>* | 49 std::map<base::DictionaryValue*, std::vector<base::DictionaryValue*>>* |
49 children, | 50 children, |
50 std::vector<base::DictionaryValue*>* roots, | 51 std::unordered_set<base::DictionaryValue*>* roots, |
vabr (Chromium)
2017/04/23 11:47:32
I chose unordered_set, because I don't care about
| |
51 std::vector<base::DictionaryValue>* representations) { | 52 std::vector<base::DictionaryValue>* representations) { |
52 representations->reserve(hosts.size()); | 53 representations->reserve(hosts.size()); |
53 children->clear(); | 54 children->clear(); |
54 roots->clear(); | 55 roots->clear(); |
55 representations->clear(); | 56 representations->clear(); |
56 | 57 |
57 std::map<base::StringPiece, base::DictionaryValue*> name_to_representation; | 58 std::map<base::StringPiece, base::DictionaryValue*> name_to_representation; |
58 | 59 |
59 // First move the representations and map the names to them. | 60 // First move the representations and map the names to them. |
60 for (HostDescriptionNode& node : hosts) { | 61 for (HostDescriptionNode& node : hosts) { |
61 representations->push_back(std::move(node.representation)); | 62 representations->push_back(std::move(node.representation)); |
62 name_to_representation[node.name] = &representations->back(); | 63 // If there are multiple nodes with the same name, subsequent insertions |
64 // will be ignored, so only the first node with a given name will be | |
65 // referenced by |roots| and |children|. | |
66 // | |
67 // Note that explicitly creating a StringPiece from |node.name| is | |
68 // necessary, otherwise std::make_pair first creates a pair of a temporary | |
69 // std::string copied from |node.name|, then the pair gets implicitly cast, | |
70 // and the key in the map will be a StringPiece referencing the temporary | |
71 // copied string instead of the one in |node|. | |
72 name_to_representation.insert( | |
73 std::make_pair(base::StringPiece(node.name), &representations->back())); | |
jdoerrie
2017/04/23 18:16:11
Recently the Linux sysroot got upgraded (crbug.com
vabr (Chromium)
2017/04/23 18:52:13
Thanks for the information!
However, looking at h
jdoerrie
2017/04/24 09:09:54
Fair point, that sentence probably should be updat
vabr (Chromium)
2017/04/24 14:51:54
Thanks! I actually like how using emplace simplifi
| |
63 } | 74 } |
64 | 75 |
65 // Now compute children. | 76 // Now compute children. |
66 for (HostDescriptionNode& node : hosts) { | 77 for (HostDescriptionNode& node : hosts) { |
67 base::DictionaryValue* node_rep = name_to_representation[node.name]; | 78 base::DictionaryValue* node_rep = name_to_representation[node.name]; |
68 base::StringPiece parent_name = node.parent_name; | 79 base::StringPiece parent_name = node.parent_name; |
69 if (parent_name.empty()) { | 80 if (parent_name.empty()) { |
70 roots->push_back(node_rep); | 81 roots->insert(node_rep); |
71 continue; | 82 continue; |
72 } | 83 } |
73 auto node_it = name_to_representation.find(parent_name); | 84 auto node_it = name_to_representation.find(parent_name); |
74 if (node_it == name_to_representation.end()) { | 85 if (node_it == name_to_representation.end()) { |
75 roots->push_back(node_rep); | 86 roots->insert(node_rep); |
76 continue; | 87 continue; |
77 } | 88 } |
78 (*children)[name_to_representation[parent_name]].push_back(node_rep); | 89 (*children)[name_to_representation[parent_name]].push_back(node_rep); |
79 } | 90 } |
80 } | 91 } |
81 | 92 |
82 } // namespace | 93 } // namespace |
83 | 94 |
84 base::ListValue SerializeHostDescriptions( | 95 base::ListValue SerializeHostDescriptions( |
85 std::vector<HostDescriptionNode> hosts, | 96 std::vector<HostDescriptionNode> hosts, |
86 base::StringPiece child_key) { | 97 base::StringPiece child_key) { |
87 // |representations| must outlive |children| and |roots|, which contain | 98 // |representations| must outlive |children| and |roots|, which contain |
88 // pointers to objects in |representations|. | 99 // pointers to objects in |representations|. |
89 std::vector<base::DictionaryValue> representations; | 100 std::vector<base::DictionaryValue> representations; |
90 std::map<base::DictionaryValue*, std::vector<base::DictionaryValue*>> | 101 std::map<base::DictionaryValue*, std::vector<base::DictionaryValue*>> |
91 children; | 102 children; |
92 std::vector<base::DictionaryValue*> roots; | 103 std::unordered_set<base::DictionaryValue*> roots; |
93 | 104 |
94 CreateDictionaryForest(std::move(hosts), &children, &roots, &representations); | 105 CreateDictionaryForest(std::move(hosts), &children, &roots, &representations); |
95 | 106 |
96 base::ListValue list_value; | 107 base::ListValue list_value; |
97 for (auto* root : roots) { | 108 for (auto* root : roots) { |
98 list_value.base::Value::GetList().push_back( | 109 list_value.base::Value::GetList().push_back( |
99 Serialize(child_key, root, children)); | 110 Serialize(child_key, root, children)); |
100 } | 111 } |
101 return list_value; | 112 return list_value; |
102 } | 113 } |
OLD | NEW |