Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: chrome/browser/ui/title_prefix_matcher.cc

Issue 6579050: Elides the beginning of tab titles that have common prefixes. ... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Elides the beginning of tab titles that have common prefixes. ... Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 "chrome/browser/ui/title_prefix_matcher.h"
6
7 #include "base/hash_tables.h"
8 #include "base/i18n/break_iterator.h"
9 #include "base/logging.h"
10
11 namespace {
12 // We use this value to identify that we have already seen the title associated
13 // to this value in the duplicate_titles hash_set, ans marked it as a duplicate.
14 const size_t kPreviouslySeenIndex = 0xFFFFFFFF;
15 }
16
17 TitlePrefixMatcher::TitleInfo::TitleInfo(const string16* title,
18 int caller_value)
19 : title(title),
20 prefix_length(0),
21 caller_value(caller_value) {
22 DCHECK(title != NULL);
23 }
24
25 // static
26 void TitlePrefixMatcher::CalculatePrefixLengths(
27 std::vector<TitleInfo>* title_infos) {
28 DCHECK(title_infos != NULL);
29 // This set will contain the indexes of the TitleInfo objects in the vector
30 // that have a duplicate.
31 base::hash_set<size_t> duplicate_titles;
32 // This map is used to identify duplicates by remembering the vector indexes
33 // we have seen with a given title string. The vector index is set to
34 // kPreviouslySeenIndex once we identified duplicates and placed their
35 // indices in duplicate_titles.
36 base::hash_map<string16, size_t> existing_title;
37 // We identify if there are identical titles upfront,
38 // because we don't want to remove prefixes for those at all.
39 // We do it as a separate pass so that we don't need to remove
40 // previously parsed titles when we find a duplicate title later on.
41 for (size_t i = 0; i < title_infos->size(); ++i) {
42 // We use pairs to test existence and insert in one shot.
43 std::pair<base::hash_map<string16, size_t>::iterator, bool> insert_result =
44 existing_title.insert(std::make_pair(*title_infos->at(i).title, i));
45 if (!insert_result.second) {
46 // insert_result.second is false when we insert a duplicate in the set.
47 // insert_result.first is a map iterator and thus
48 // insert_result.first->first is the string title key of the map.
49 DCHECK(*title_infos->at(i).title == insert_result.first->first);
50 duplicate_titles.insert(i);
51 // insert_result.first->second is the value of the title index and if it's
52 // not kPreviouslySeenIndex yet, we must remember it as a duplicate too.
53 if (insert_result.first->second != kPreviouslySeenIndex) {
54 duplicate_titles.insert(insert_result.first->second);
55 insert_result.first->second = kPreviouslySeenIndex;
56 }
57 }
58 }
59
60 // This next loop accumulates all the potential prefixes,
61 // and remember on which titles we saw them.
62 base::hash_map<string16, std::vector<size_t> > prefixes;
63 for (size_t i = 0; i < title_infos->size(); ++i) {
64 // Duplicate titles are not to be included in this process.
65 if (duplicate_titles.find(i) != duplicate_titles.end())
66 continue;
67 const string16* title = title_infos->at(i).title;
68 // We only create prefixes at word boundaries.
69 base::BreakIterator iter(title, base::BreakIterator::BREAK_WORD);
70 // We ignore this title if we can't break it into words, or if it only
71 // contains a single word.
72 if (!iter.Init() || !iter.Advance())
73 continue;
74 // We continue advancing even though we already advanced to the first
75 // word above, so that we can use iter.prev() to identify the end of the
76 // previous word and more easily ignore the last word while iterating.
77 while (iter.Advance()) {
78 if (iter.IsWord())
79 prefixes[title->substr(0, iter.prev())].push_back(i);
80 }
81 }
82
83 // Now we parse the map to find common prefixes
84 // and keep the largest per title.
85 for (base::hash_map<string16, std::vector<size_t> >::iterator iter =
86 prefixes.begin(); iter != prefixes.end(); ++iter) {
87 // iter->first is the prefix string, iter->second is a vector of indices.
88 if (iter->second.size() > 1) {
89 size_t prefix_length = iter->first.size();
90 for (size_t i = 0; i < iter->second.size(); ++i){
91 if (title_infos->at(iter->second[i]).prefix_length < prefix_length)
92 title_infos->at(iter->second[i]).prefix_length = prefix_length;
93 }
94 }
95 }
96 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/title_prefix_matcher.h ('k') | chrome/browser/ui/title_prefix_matcher_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698