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