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

Unified Diff: third_party/WebKit/Source/build/scripts/trie_builder.py

Issue 1978703002: Factor and simplify generation of the lookup trie used for HTMLLookupTrie. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove newlines Created 4 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/build/scripts/trie_builder.py
diff --git a/third_party/WebKit/Source/build/scripts/trie_builder.py b/third_party/WebKit/Source/build/scripts/trie_builder.py
new file mode 100644
index 0000000000000000000000000000000000000000..fe4eb9cb7d963b3f0b1749ffaef9851364fe3feb
--- /dev/null
+++ b/third_party/WebKit/Source/build/scripts/trie_builder.py
@@ -0,0 +1,57 @@
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+from collections import defaultdict
+
+
+def _single_trie(string_to_value_pairs, index):
+ """Build a single trie from a dict of input string to output values.
+
+ This function assumes that all of the strings in
+ string_to_value_pairs have the same length.
+ The input
+ {'abcd': 'ABCD', 'adef': 'ADEF', 'adeg': 'ADEG'}
+ creates a trie like this:
+ {
+ 'a' : {
+ 'b': {'cd' : "ABCD"},
+ 'd' : {
+ 'e' : {
+ 'f' : {'': "ADEF"},
+ 'g' : {'': "ADEG"},
+ },
+ },
+ },
+ }
+ """
+ dicts_by_indexed_letter = defaultdict(list)
+ for string, value in string_to_value_pairs:
+ dicts_by_indexed_letter[string[index]].append((string, value))
+
+ output = {}
+ for char, d in dicts_by_indexed_letter.items():
+ if len(d) == 1:
+ string = d[0][0]
+ value = d[0][1]
+ output[char] = {string[index + 1:]: value}
+ else:
+ output[char] = _single_trie(d, index + 1)
+
+ return output
+
+
+def trie_list_by_str_length(str_to_return_value_dict):
+ """Make a list of tries from a dict of input string to output value.
+
+ All strings should be all lower case.
+ """
+ dicts_by_length = defaultdict(list)
+ for string, value in str_to_return_value_dict.items():
+ dicts_by_length[len(string)].append((string, value))
+
+ output = []
+ for length, pairs in dicts_by_length.items():
+ output.append((length, _single_trie(sorted(pairs), 0)))
+
+ return output
« no previous file with comments | « third_party/WebKit/Source/build/scripts/templates/macros.tmpl ('k') | third_party/WebKit/Source/core/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698