OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/spellchecker/feedback.h" | 5 #include "chrome/browser/spellchecker/feedback.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <set> | |
10 | 9 |
11 namespace spellcheck { | 10 namespace spellcheck { |
12 | 11 |
12 namespace { | |
13 | |
14 typedef std::set<uint32> HashCollection; | |
palmer
2013/05/21 23:06:28
Would it make sense to use these typedefs in feedb
please use gerrit instead
2013/05/21 23:09:08
Sure, why not.
| |
15 typedef std::map<int, HashCollection> RendererHashesMap; | |
16 typedef std::map<uint32, Misspelling> HashMisspellingMap; | |
17 | |
18 } // namespace | |
19 | |
13 Feedback::Feedback() { | 20 Feedback::Feedback() { |
14 } | 21 } |
15 | 22 |
16 Feedback::~Feedback() { | 23 Feedback::~Feedback() { |
17 } | 24 } |
18 | 25 |
19 Misspelling* Feedback::GetMisspelling(uint32 hash) { | 26 Misspelling* Feedback::GetMisspelling(uint32 hash) { |
20 std::map<uint32, Misspelling>::iterator it = misspellings_.find(hash); | 27 HashMisspellingMap::iterator it = misspellings_.find(hash); |
21 if (it == misspellings_.end()) | 28 if (it == misspellings_.end()) |
22 return NULL; | 29 return NULL; |
23 return &it->second; | 30 return &it->second; |
24 } | 31 } |
25 | 32 |
26 void Feedback::FinalizeRemovedMisspellings( | 33 void Feedback::FinalizeRemovedMisspellings( |
27 int renderer_process_id, | 34 int renderer_process_id, |
28 const std::vector<uint32>& remaining_markers) { | 35 const std::vector<uint32>& remaining_markers) { |
29 std::map<int, std::vector<uint32> >::iterator i = | 36 RendererHashesMap::iterator i = hashes_.find(renderer_process_id); |
30 hashes_.find(renderer_process_id); | |
31 if (i == hashes_.end() || i->second.empty()) | 37 if (i == hashes_.end() || i->second.empty()) |
32 return; | 38 return; |
33 std::vector<uint32> remaining_copy(remaining_markers); | 39 std::set<uint32> remaining_set(remaining_markers.begin(), |
34 std::sort(remaining_copy.begin(), remaining_copy.end()); | 40 remaining_markers.end()); |
35 std::sort(i->second.begin(), i->second.end()); | |
36 std::vector<uint32> removed_markers; | 41 std::vector<uint32> removed_markers; |
37 std::set_difference(i->second.begin(), | 42 std::set_difference(i->second.begin(), |
38 i->second.end(), | 43 i->second.end(), |
39 remaining_copy.begin(), | 44 remaining_set.begin(), |
40 remaining_copy.end(), | 45 remaining_set.end(), |
41 std::back_inserter(removed_markers)); | 46 std::back_inserter(removed_markers)); |
42 for (std::vector<uint32>::const_iterator j = removed_markers.begin(); | 47 for (std::vector<uint32>::const_iterator j = removed_markers.begin(); |
43 j != removed_markers.end(); | 48 j != removed_markers.end(); |
44 ++j) { | 49 ++j) { |
45 std::map<uint32, Misspelling>::iterator k = misspellings_.find(*j); | 50 HashMisspellingMap::iterator k = misspellings_.find(*j); |
46 if (k != misspellings_.end() && !k->second.action.IsFinal()) | 51 if (k != misspellings_.end() && !k->second.action.IsFinal()) |
47 k->second.action.Finalize(); | 52 k->second.action.Finalize(); |
48 } | 53 } |
49 } | 54 } |
50 | 55 |
51 bool Feedback::RendererHasMisspellings(int renderer_process_id) const { | 56 bool Feedback::RendererHasMisspellings(int renderer_process_id) const { |
52 std::map<int, std::vector<uint32> >::const_iterator it = | 57 RendererHashesMap::const_iterator it = hashes_.find(renderer_process_id); |
53 hashes_.find(renderer_process_id); | |
54 return it != hashes_.end() && !it->second.empty(); | 58 return it != hashes_.end() && !it->second.empty(); |
55 } | 59 } |
56 | 60 |
57 std::vector<Misspelling> Feedback::GetMisspellingsInRenderer( | 61 std::vector<Misspelling> Feedback::GetMisspellingsInRenderer( |
58 int renderer_process_id) const { | 62 int renderer_process_id) const { |
59 std::vector<Misspelling> result; | 63 std::vector<Misspelling> result; |
60 std::map<int, std::vector<uint32> >::const_iterator i = | 64 RendererHashesMap::const_iterator i = hashes_.find(renderer_process_id); |
61 hashes_.find(renderer_process_id); | |
62 if (i == hashes_.end() || i->second.empty()) | 65 if (i == hashes_.end() || i->second.empty()) |
63 return result; | 66 return result; |
64 for (std::vector<uint32>::const_iterator j = i->second.begin(); | 67 for (HashCollection::const_iterator j = i->second.begin(); |
65 j != i->second.end(); | 68 j != i->second.end(); |
66 ++j) { | 69 ++j) { |
67 std::map<uint32, Misspelling>::const_iterator k = misspellings_.find(*j); | 70 HashMisspellingMap::const_iterator k = misspellings_.find(*j); |
68 if (k != misspellings_.end()) | 71 if (k != misspellings_.end()) |
69 result.push_back(k->second); | 72 result.push_back(k->second); |
70 } | 73 } |
71 return result; | 74 return result; |
72 } | 75 } |
73 | 76 |
74 void Feedback::EraseFinalizedMisspellings(int renderer_process_id) { | 77 void Feedback::EraseFinalizedMisspellings(int renderer_process_id) { |
75 std::map<int, std::vector<uint32> >::iterator i = | 78 RendererHashesMap::iterator i = hashes_.find(renderer_process_id); |
76 hashes_.find(renderer_process_id); | |
77 if (i == hashes_.end() || i->second.empty()) | 79 if (i == hashes_.end() || i->second.empty()) |
78 return; | 80 return; |
79 std::vector<uint32> pending; | 81 HashCollection pending; |
80 for (std::vector<uint32>::const_iterator j = i->second.begin(); | 82 for (HashCollection::const_iterator j = i->second.begin(); |
81 j != i->second.end(); | 83 j != i->second.end(); |
82 ++j) { | 84 ++j) { |
83 std::map<uint32, Misspelling>::iterator k = misspellings_.find(*j); | 85 HashMisspellingMap::iterator k = misspellings_.find(*j); |
84 if (k != misspellings_.end()) { | 86 if (k != misspellings_.end()) { |
85 if (k->second.action.IsFinal()) | 87 if (k->second.action.IsFinal()) |
86 misspellings_.erase(k); | 88 misspellings_.erase(k); |
87 else | 89 else |
88 pending.push_back(*j); | 90 pending.insert(*j); |
89 } | 91 } |
90 } | 92 } |
91 i->second.swap(pending); | 93 i->second.swap(pending); |
92 } | 94 } |
93 | 95 |
94 bool Feedback::HasMisspelling(uint32 hash) const { | 96 bool Feedback::HasMisspelling(uint32 hash) const { |
95 return !!misspellings_.count(hash); | 97 return !!misspellings_.count(hash); |
96 } | 98 } |
97 | 99 |
98 void Feedback::AddMisspelling(int renderer_process_id, | 100 void Feedback::AddMisspelling(int renderer_process_id, |
99 const Misspelling& misspelling) { | 101 const Misspelling& misspelling) { |
100 misspellings_[misspelling.hash] = misspelling; | 102 misspellings_[misspelling.hash] = misspelling; |
101 hashes_[renderer_process_id].push_back(misspelling.hash); | 103 hashes_[renderer_process_id].insert(misspelling.hash); |
102 } | 104 } |
103 | 105 |
104 bool Feedback::Empty() const { | 106 bool Feedback::Empty() const { |
105 return misspellings_.empty(); | 107 return misspellings_.empty(); |
106 } | 108 } |
107 | 109 |
108 std::vector<int> Feedback::GetRendersWithMisspellings() const { | 110 std::vector<int> Feedback::GetRendersWithMisspellings() const { |
109 std::vector<int> result; | 111 std::vector<int> result; |
110 for (std::map<int, std::vector<uint32> >::const_iterator it = hashes_.begin(); | 112 for (RendererHashesMap::const_iterator it = hashes_.begin(); |
111 it != hashes_.end(); | 113 it != hashes_.end(); |
112 ++it) { | 114 ++it) { |
113 if (!it->second.empty()) | 115 if (!it->second.empty()) |
114 result.push_back(it->first); | 116 result.push_back(it->first); |
115 } | 117 } |
116 return result; | 118 return result; |
117 } | 119 } |
118 | 120 |
119 void Feedback::FinalizeAllMisspellings() { | 121 void Feedback::FinalizeAllMisspellings() { |
120 for (std::map<uint32, Misspelling>::iterator it = misspellings_.begin(); | 122 for (HashMisspellingMap::iterator it = misspellings_.begin(); |
121 it != misspellings_.end(); | 123 it != misspellings_.end(); |
122 ++it) { | 124 ++it) { |
123 if (!it->second.action.IsFinal()) | 125 if (!it->second.action.IsFinal()) |
124 it->second.action.Finalize(); | 126 it->second.action.Finalize(); |
125 } | 127 } |
126 } | 128 } |
127 | 129 |
128 std::vector<Misspelling> Feedback::GetAllMisspellings() const { | 130 std::vector<Misspelling> Feedback::GetAllMisspellings() const { |
129 std::vector<Misspelling> result; | 131 std::vector<Misspelling> result; |
130 for (std::map<uint32, Misspelling>::const_iterator it = misspellings_.begin(); | 132 for (HashMisspellingMap::const_iterator it = misspellings_.begin(); |
131 it != misspellings_.end(); | 133 it != misspellings_.end(); |
132 ++it) { | 134 ++it) { |
133 result.push_back(it->second); | 135 result.push_back(it->second); |
134 } | 136 } |
135 return result; | 137 return result; |
136 } | 138 } |
137 | 139 |
138 void Feedback::Clear() { | 140 void Feedback::Clear() { |
139 misspellings_.clear(); | 141 misspellings_.clear(); |
140 hashes_.clear(); | 142 hashes_.clear(); |
141 } | 143 } |
142 | 144 |
143 } // namespace spellcheck | 145 } // namespace spellcheck |
OLD | NEW |