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

Side by Side 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 some unused imports 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 unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2016 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 from collections import defaultdict
6
7
8 def _single_trie(string_to_value_pairs, index):
9 """Build a single trie from a dict of input string to output values.
10
11 This function assumes that all of the strings in
12 string_to_value_pairs have the same length.
13 The input
14 {'abcd': 'ABCD', 'adef': 'ADEF', 'adeg': 'ADEG'}
15 creates a trie like this:
16 {
17 'a' : {
18 'b': {'cd' : "ABCD"},
19 'd' : {
20 'e' : {
21 'f' : {'': "ADEF"},
22 'g' : {'': "ADEG"},
23 },
24 },
25 },
26 }
27 """
28 dicts_by_indexed_letter = defaultdict(list)
29 for string, value in string_to_value_pairs:
30 dicts_by_indexed_letter[string[index]].append((string, value))
31
32 output = {}
33 for char, d in dicts_by_indexed_letter.items():
34 if len(d) == 1:
35 string = d[0][0]
36 value = d[0][1]
37 output[char] = {string[index + 1:]: value}
38 else:
39 output[char] = _single_trie(d, index + 1)
40
41 return output
42
43
44 def trie_list_by_str_length(str_to_return_value_dict):
45 """Make a list of tries from a dict of input string to output value.
46
47 All strings should be all lower case.
48 """
49 dicts_by_length = defaultdict(list)
50 for string, value in str_to_return_value_dict.items():
51 dicts_by_length[len(string)].append((string, value))
52
53 output = []
54 for length, pairs in dicts_by_length.items():
55 output.append((length, _single_trie(sorted(pairs), 0)))
56
57 return output
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698