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

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

Issue 2385093002: DevTools: introduce Trie data structure (Closed)
Patch Set: address comments 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 WebInspector.Trie = function()
9 {
10 this.clear();
11 }
12
13 WebInspector.Trie.prototype = {
14 /**
15 * @param {string} word
16 */
17 add: function(word)
18 {
19 var node = this._root;
20 ++this._subtreeMarks[this._root];
21 for (var i = 0; i < word.length; ++i) {
22 var edge = word[i];
23 var next = this._edges[node][edge];
24 if (!next) {
25 if (this._freeNodes.length) {
26 next = this._freeNodes.pop();
dgozman 2016/10/03 23:08:34 Let's comment that we don't clear all the fields b
lushnikov 2016/10/04 00:17:59 Done.
27 } else {
28 next = this._size++;
29 this._marks.push(false);
30 this._subtreeMarks.push(0);
31 this._edges.push({ __proto__: null });
32 }
33 this._edges[node][edge] = next;
34 }
35 ++this._subtreeMarks[next];
36 node = next;
37 }
38 this._marks[node] = true;
39 },
40
41 /**
42 * @param {string} word
43 * @return {boolean}
44 */
45 remove: function(word)
46 {
47 if (!this.has(word))
48 return false;
49 var node = this._root;
50 --this._subtreeMarks[this._root];
51 for (var i = 0; i < word.length; ++i) {
52 var edge = word[i];
53 var next = this._edges[node][edge];
54 if (!--this._subtreeMarks[next]) {
55 delete this._edges[node][edge];
56 this._freeNodes.push(next);
dgozman 2016/10/03 23:08:34 Let's exercise add->remove->add in the test.
lushnikov 2016/10/04 00:17:59 Done.
57 }
58 node = next;
59 }
60 this._marks[node] = false;
61 return true;
62 },
63
64 /**
65 * @param {string} word
66 * @return {boolean}
67 */
68 has: function(word)
69 {
70 var node = this._root;
71 for (var i = 0; i < word.length; ++i) {
72 node = this._edges[node][word[i]];
73 if (!node)
74 return false;
75 }
76 return this._marks[node];
77 },
78
79 /**
80 * @param {string=} prefix
81 * @return {!Array<string>}
82 */
83 words: function(prefix)
84 {
85 prefix = prefix || "";
86 var node = this._root;
87 for (var i = 0; i < prefix.length; ++i) {
88 node = this._edges[node][prefix[i]];
89 if (!node)
90 return [];
91 }
92 var results = [];
93 this._dfs(node, prefix, results);
94 return results;
95 },
96
97 /**
98 * @param {number} node
99 * @param {string} prefix
100 * @param {!Array<string>} results
101 */
102 _dfs: function(node, prefix, results)
103 {
104 if (this._marks[node])
105 results.push(prefix);
106 var edges = this._edges[node];
107 for (var edge in edges)
108 this._dfs(edges[edge], prefix + edge, results);
109 },
110
111 /**
112 * @param {string} word
113 * @param {boolean} fullWordOnly
114 * @return {string}
115 */
116 longestPrefix: function(word, fullWordOnly)
117 {
118 var node = this._root;
119 var wordIndex = 0;
120 for (var i = 0; i < word.length; ++i) {
121 if (!fullWordOnly || this._marks[node])
122 wordIndex = i;
123 node = this._edges[node][word[i]];
124 if (!node)
125 break;
126 }
127 return word.substring(0, wordIndex);
128 },
129
130 clear: function()
131 {
132 this._size = 1;
133 this._root = 0;
134 /** @type {!Array<!Object<string, number>>} */
135 this._edges = [{ __proto__: null }];
136 /** @type {!Array<boolean>} */
137 this._marks = [false];
dgozman 2016/10/03 23:08:34 _isWord or something like that?
lushnikov 2016/10/04 00:17:59 Done.
138 /** @type {!Array<number>} */
139 this._subtreeMarks = [0];
dgozman 2016/10/03 23:08:34 _wordsInSubtree
lushnikov 2016/10/04 00:17:59 Done.
140 /** @type {!Array<number>} */
141 this._freeNodes = [];
142 }
143 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698