OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 17 matching lines...) Expand all Loading... |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 /** | 31 /** |
32 * @constructor | 32 * @constructor |
33 */ | 33 */ |
34 WebInspector.TextDictionary = function() | 34 WebInspector.TextDictionary = function() |
35 { | 35 { |
36 /** @type {!Map<string, number>} */ | 36 /** @type {!Map<string, number>} */ |
37 this._words = new Map(); | 37 this._words = new Map(); |
| 38 this._index = new Trie(); |
38 } | 39 } |
39 | 40 |
40 WebInspector.TextDictionary.prototype = { | 41 WebInspector.TextDictionary.prototype = { |
41 /** | 42 /** |
42 * @param {string} word | 43 * @param {string} word |
43 */ | 44 */ |
44 addWord: function(word) | 45 addWord: function(word) |
45 { | 46 { |
46 var count = this._words.get(word) || 0; | 47 var count = this._words.get(word) || 0; |
47 ++count; | 48 ++count; |
48 this._words.set(word, count); | 49 this._words.set(word, count); |
| 50 this._index.add(word); |
49 }, | 51 }, |
50 | 52 |
51 /** | 53 /** |
52 * @param {string} word | 54 * @param {string} word |
53 */ | 55 */ |
54 removeWord: function(word) | 56 removeWord: function(word) |
55 { | 57 { |
56 var count = this._words.get(word) || 0; | 58 var count = this._words.get(word) || 0; |
57 if (!count) | 59 if (!count) |
58 return; | 60 return; |
| 61 this._index.remove(word); |
59 if (count === 1) { | 62 if (count === 1) { |
60 this._words.delete(word); | 63 this._words.delete(word); |
61 return; | 64 return; |
62 } | 65 } |
63 --count; | 66 --count; |
64 this._words.set(word, count); | 67 this._words.set(word, count); |
65 }, | 68 }, |
66 | 69 |
67 /** | 70 /** |
68 * @param {string} prefix | 71 * @param {string} prefix |
69 * @return {!Array.<string>} | 72 * @return {!Array.<string>} |
70 */ | 73 */ |
71 wordsWithPrefix: function(prefix) | 74 wordsWithPrefix: function(prefix) |
72 { | 75 { |
73 var words = []; | 76 return this._index.words(prefix); |
74 for (var word of this._words.keys()) { | |
75 if (word.startsWith(prefix)) | |
76 words.push(word); | |
77 } | |
78 return words; | |
79 }, | 77 }, |
80 | 78 |
81 /** | 79 /** |
82 * @param {string} word | 80 * @param {string} word |
83 * @return {boolean} | 81 * @return {boolean} |
84 */ | 82 */ |
85 hasWord: function(word) | 83 hasWord: function(word) |
86 { | 84 { |
87 return this._words.has(word); | 85 return this._words.has(word); |
88 }, | 86 }, |
89 | 87 |
90 /** | 88 /** |
91 * @param {string} word | 89 * @param {string} word |
92 * @return {number} | 90 * @return {number} |
93 */ | 91 */ |
94 wordCount: function(word) | 92 wordCount: function(word) |
95 { | 93 { |
96 return this._words.get(word) || 0; | 94 return this._words.get(word) || 0; |
97 }, | 95 }, |
98 | 96 |
99 reset: function() | 97 reset: function() |
100 { | 98 { |
101 this._words.clear(); | 99 this._words.clear(); |
| 100 this._index.clear(); |
102 } | 101 } |
103 } | 102 } |
OLD | NEW |