OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ | 5 #ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ |
6 #define CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ | 6 #define CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <list> | 9 #include <list> |
10 #include <map> | 10 #include <map> |
11 #include <set> | 11 #include <set> |
12 #include <string> | |
13 #include <vector> | 12 #include <vector> |
14 | 13 |
15 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/string16.h" |
16 | 16 |
17 class BookmarkNode; | 17 class BookmarkNode; |
18 class Profile; | 18 class Profile; |
19 class QueryNode; | 19 class QueryNode; |
20 class QueryParser; | 20 class QueryParser; |
21 | 21 |
22 namespace bookmark_utils { | 22 namespace bookmark_utils { |
23 struct TitleMatch; | 23 struct TitleMatch; |
24 } | 24 } |
25 | 25 |
(...skipping 14 matching lines...) Expand all Loading... |
40 explicit BookmarkIndex(Profile* profile) : profile_(profile) {} | 40 explicit BookmarkIndex(Profile* profile) : profile_(profile) {} |
41 | 41 |
42 // Invoked when a bookmark has been added to the model. | 42 // Invoked when a bookmark has been added to the model. |
43 void Add(const BookmarkNode* node); | 43 void Add(const BookmarkNode* node); |
44 | 44 |
45 // Invoked when a bookmark has been removed from the model. | 45 // Invoked when a bookmark has been removed from the model. |
46 void Remove(const BookmarkNode* node); | 46 void Remove(const BookmarkNode* node); |
47 | 47 |
48 // Returns up to |max_count| of bookmarks containing the text |query|. | 48 // Returns up to |max_count| of bookmarks containing the text |query|. |
49 void GetBookmarksWithTitlesMatching( | 49 void GetBookmarksWithTitlesMatching( |
50 const std::wstring& query, | 50 const string16& query, |
51 size_t max_count, | 51 size_t max_count, |
52 std::vector<bookmark_utils::TitleMatch>* results); | 52 std::vector<bookmark_utils::TitleMatch>* results); |
53 | 53 |
54 private: | 54 private: |
55 typedef std::set<const BookmarkNode*> NodeSet; | 55 typedef std::set<const BookmarkNode*> NodeSet; |
56 typedef std::map<std::wstring, NodeSet> Index; | 56 typedef std::map<string16, NodeSet> Index; |
57 | 57 |
58 // Used when finding the set of bookmarks that match a query. Each match | 58 // Used when finding the set of bookmarks that match a query. Each match |
59 // represents a set of terms (as an interator into the Index) matching the | 59 // represents a set of terms (as an interator into the Index) matching the |
60 // query as well as the set of nodes that contain those terms in their titles. | 60 // query as well as the set of nodes that contain those terms in their titles. |
61 struct Match { | 61 struct Match { |
62 // List of terms matching the query. | 62 // List of terms matching the query. |
63 std::list<Index::const_iterator> terms; | 63 std::list<Index::const_iterator> terms; |
64 | 64 |
65 // The set of nodes matching the terms. As an optimization this is empty | 65 // The set of nodes matching the terms. As an optimization this is empty |
66 // when we match only one term, and is filled in when we get more than one | 66 // when we match only one term, and is filled in when we get more than one |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 | 109 |
110 // Add |node| to |results| if the node matches the query. | 110 // Add |node| to |results| if the node matches the query. |
111 void AddMatchToResults(const BookmarkNode* node, | 111 void AddMatchToResults(const BookmarkNode* node, |
112 QueryParser* parser, | 112 QueryParser* parser, |
113 const std::vector<QueryNode*>& query_nodes, | 113 const std::vector<QueryNode*>& query_nodes, |
114 std::vector<bookmark_utils::TitleMatch>* results); | 114 std::vector<bookmark_utils::TitleMatch>* results); |
115 | 115 |
116 // Populates |matches| for the specified term. If |first_term| is true, this | 116 // Populates |matches| for the specified term. If |first_term| is true, this |
117 // is the first term in the query. Returns true if there is at least one node | 117 // is the first term in the query. Returns true if there is at least one node |
118 // matching the term. | 118 // matching the term. |
119 bool GetBookmarksWithTitleMatchingTerm(const std::wstring& term, | 119 bool GetBookmarksWithTitleMatchingTerm(const string16& term, |
120 bool first_term, | 120 bool first_term, |
121 Matches* matches); | 121 Matches* matches); |
122 | 122 |
123 // Iterates over |matches| updating each Match's nodes to contain the | 123 // Iterates over |matches| updating each Match's nodes to contain the |
124 // intersection of the Match's current nodes and the nodes at |index_i|. | 124 // intersection of the Match's current nodes and the nodes at |index_i|. |
125 // If the intersection is empty, the Match is removed. | 125 // If the intersection is empty, the Match is removed. |
126 // | 126 // |
127 // This is invoked from GetBookmarksWithTitleMatchingTerm. | 127 // This is invoked from GetBookmarksWithTitleMatchingTerm. |
128 void CombineMatchesInPlace(const Index::const_iterator& index_i, | 128 void CombineMatchesInPlace(const Index::const_iterator& index_i, |
129 Matches* matches); | 129 Matches* matches); |
130 | 130 |
131 // Iterates over |current_matches| calculating the intersection between the | 131 // Iterates over |current_matches| calculating the intersection between the |
132 // Match's nodes and the nodes at |index_i|. If the intersection between the | 132 // Match's nodes and the nodes at |index_i|. If the intersection between the |
133 // two is non-empty, a new match is added to |result|. | 133 // two is non-empty, a new match is added to |result|. |
134 // | 134 // |
135 // This differs from CombineMatchesInPlace in that if the intersection is | 135 // This differs from CombineMatchesInPlace in that if the intersection is |
136 // non-empty the result is added to result, not combined in place. This | 136 // non-empty the result is added to result, not combined in place. This |
137 // variant is used for prefix matching. | 137 // variant is used for prefix matching. |
138 // | 138 // |
139 // This is invoked from GetBookmarksWithTitleMatchingTerm. | 139 // This is invoked from GetBookmarksWithTitleMatchingTerm. |
140 void CombineMatches(const Index::const_iterator& index_i, | 140 void CombineMatches(const Index::const_iterator& index_i, |
141 const Matches& current_matches, | 141 const Matches& current_matches, |
142 Matches* result); | 142 Matches* result); |
143 | 143 |
144 // Returns the set of query words from |query|. | 144 // Returns the set of query words from |query|. |
145 std::vector<std::wstring> ExtractQueryWords(const std::wstring& query); | 145 std::vector<string16> ExtractQueryWords(const string16& query); |
146 | 146 |
147 // Adds |node| to |index_|. | 147 // Adds |node| to |index_|. |
148 void RegisterNode(const std::wstring& term, const BookmarkNode* node); | 148 void RegisterNode(const string16& term, const BookmarkNode* node); |
149 | 149 |
150 // Removes |node| from |index_|. | 150 // Removes |node| from |index_|. |
151 void UnregisterNode(const std::wstring& term, const BookmarkNode* node); | 151 void UnregisterNode(const string16& term, const BookmarkNode* node); |
152 | 152 |
153 Index index_; | 153 Index index_; |
154 | 154 |
155 Profile* profile_; | 155 Profile* profile_; |
156 | 156 |
157 DISALLOW_COPY_AND_ASSIGN(BookmarkIndex); | 157 DISALLOW_COPY_AND_ASSIGN(BookmarkIndex); |
158 }; | 158 }; |
159 | 159 |
160 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ | 160 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_INDEX_H_ |
OLD | NEW |