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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/platform/Trie.js

Issue 2385093002: DevTools: introduce Trie data structure (Closed)
Patch Set: rebaseline Created 4 years, 2 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 /**
6 * @constructor
7 */
8 function Trie()
9 {
10 this.clear();
11 }
12
13 Trie.prototype = {
14 /**
15 * @param {string} word
16 */
17 add: function(word)
18 {
19 var node = this._root;
20 for (var i = 0; i < word.length; ++i) {
21 var edge = word[i];
22 var next = this._edges[node][edge];
23 if (!next) {
24 next = this._size++;
25 this._marks.push(false);
26 this._edges.push({ __proto__: null });
27 this._edges[node][edge] = next;
28 }
29 node = next;
30 }
31 this._marks[node] = true;
32 },
33
34 /**
35 * @param {string} word
36 * @return {boolean}
37 */
38 remove: function(word)
39 {
40 var node = this._root;
41 for (var i = 0; i < word.length; ++i) {
42 node = this._edges[node][word[i]];
43 if (!node)
44 return false;
45 }
46 if (!this._marks[node])
47 return false;
48 this._marks[node] = false;
dgozman 2016/10/03 20:32:57 Why not remove edges which do not lead to leafs?
lushnikov 2016/10/03 22:56:22 Done.
49 return true;
50 },
51
52 /**
53 * @param {string} word
54 * @return {boolean}
55 */
56 has: function(word)
57 {
58 var node = this._root;
59 for (var i = 0; i < word.length; ++i) {
60 node = this._edges[node][word[i]];
61 if (!node)
62 return false;
63 }
64 return this._marks[node];
65 },
66
67 /**
68 * @param {string=} prefix
69 * @return {!Array<string>}
70 */
71 words: function(prefix)
72 {
73 prefix = prefix || "";
74 var node = this._root;
75 for (var i = 0; i < prefix.length; ++i) {
76 var next = this._edges[node][prefix[i]];
77 if (!next)
78 return [];
79 node = next;
dgozman 2016/10/03 20:32:57 Can avoid |next| variable similar to has() functio
lushnikov 2016/10/03 22:56:22 Nice, thanks
80 }
81 var results = [];
82 this._dfs(node, prefix, results);
83 return results;
84 },
85
86 /**
87 * @param {number} node
88 * @param {string} prefix
89 * @param {!Array<string>} results
90 */
91 _dfs: function(node, prefix, results)
92 {
93 if (this._marks[node])
94 results.push(prefix);
95 var edges = this._edges[node];
96 for (var edge in edges)
97 this._dfs(edges[edge], prefix + edge, results);
98 },
99
100 /**
101 * @param {string} word
102 * @param {boolean} fullWordOnly
103 * @return {string}
104 */
105 longestPrefix: function(word, fullWordOnly)
106 {
107 var node = this._root;
108 var wordIndex = 0;
109 for (var i = 0; i < word.length; ++i) {
110 if (!fullWordOnly || this._marks[node])
111 wordIndex = i;
112 node = this._edges[node][word[i]];
113 if (!node)
114 break;
115 }
116 return word.substring(0, wordIndex);
117 },
118
119 clear: function()
120 {
121 this._size = 1;
122 this._root = 0;
123 /** @type {!Array<!Object<string, number>>} */
124 this._edges = [{ __proto__: null }];
125 /** @type {!Array<boolean>} */
126 this._marks = [false];
127 }
128 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698