Make composebox_typeahead.seen_topics be a Dict of Dicts.

The new implementation makes add_topic() be O(1).  We incur
the cost of sort() in topics_seen_for(), but that's only called
in the typeahead widget, and I think the typical number of topics
should be manageable here.

(imported from commit 0e332301b2e44b4465bf7a1d93ae525a8d17a6b6)
This commit is contained in:
Steve Howell 2013-08-14 16:11:54 -04:00
parent a4af6c7062
commit dc19f15b1f
1 changed files with 12 additions and 14 deletions

View File

@ -28,30 +28,28 @@ exports.get_cleaned_pm_recipients = function (query_string) {
return recipients; return recipients;
}; };
function case_insensitive_find(term, array) {
var lowered_term = term.toLowerCase();
return _.filter(array, function (elt) {
return elt.toLowerCase() === lowered_term;
}).length !== 0;
}
var seen_topics = new Dict(); var seen_topics = new Dict();
exports.add_topic = function (stream, topic) { exports.add_topic = function (uc_stream, uc_topic) {
stream = stream.toLowerCase(); // For Denmark/FooBar, we set
// seen_topics['denmark']['foobar'] to 'FooBar',
// where seen_topics is a Dict of Dicts
var stream = uc_stream.toLowerCase();
var topic = uc_topic.toLowerCase();
if (! seen_topics.has(stream)) { if (! seen_topics.has(stream)) {
seen_topics.set(stream, []); seen_topics.set(stream, new Dict());
} }
if (! case_insensitive_find(topic, seen_topics.get(stream))) { var topic_dict = seen_topics.get(stream);
seen_topics.get(stream).push(topic); if (! topic_dict.has(topic)) {
seen_topics.get(stream).sort(); topic_dict.set(topic, uc_topic);
} }
}; };
exports.topics_seen_for = function (stream) { exports.topics_seen_for = function (stream) {
stream = stream.toLowerCase(); stream = stream.toLowerCase();
if (seen_topics.has(stream)) { if (seen_topics.has(stream)) {
return seen_topics.get(stream); return seen_topics.get(stream).values().sort();
} }
return []; return [];
}; };