| 1 |
// Copyright 2007 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Datastructure: Trie. |
| 17 |
* |
| 18 |
* |
| 19 |
* This file provides the implementation of a trie data structure. A trie is a |
| 20 |
* data structure that stores key/value pairs in a prefix tree. See: |
| 21 |
* http://en.wikipedia.org/wiki/Trie |
| 22 |
*/ |
| 23 |
|
| 24 |
|
| 25 |
goog.provide('goog.structs.Trie'); |
| 26 |
|
| 27 |
goog.require('goog.object'); |
| 28 |
goog.require('goog.structs'); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* Class for a Trie datastructure. Trie data structures are made out of trees |
| 34 |
* of Trie classes. |
| 35 |
* |
| 36 |
* @param {goog.structs.Trie<VALUE>|Object<string, VALUE>=} opt_trie Optional |
| 37 |
* goog.structs.Trie or Object to initialize trie with. |
| 38 |
* @constructor |
| 39 |
* @template VALUE |
| 40 |
*/ |
| 41 |
goog.structs.Trie = function(opt_trie) { |
| 42 |
/** |
| 43 |
* This trie's value. For the base trie, this will be the value of the |
| 44 |
* empty key, if defined. |
| 45 |
* @private {VALUE} |
| 46 |
*/ |
| 47 |
this.value_ = undefined; |
| 48 |
|
| 49 |
/** |
| 50 |
* This trie's child nodes. |
| 51 |
* @private {!Object<!goog.structs.Trie<VALUE>>} |
| 52 |
*/ |
| 53 |
this.childNodes_ = {}; |
| 54 |
|
| 55 |
if (opt_trie) { |
| 56 |
this.setAll(opt_trie); |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* Sets the given key/value pair in the trie. O(L), where L is the length |
| 63 |
* of the key. |
| 64 |
* @param {string} key The key. |
| 65 |
* @param {VALUE} value The value. |
| 66 |
*/ |
| 67 |
goog.structs.Trie.prototype.set = function(key, value) { |
| 68 |
this.setOrAdd_(key, value, false); |
| 69 |
}; |
| 70 |
|
| 71 |
|
| 72 |
/** |
| 73 |
* Adds the given key/value pair in the trie. Throw an exception if the key |
| 74 |
* already exists in the trie. O(L), where L is the length of the key. |
| 75 |
* @param {string} key The key. |
| 76 |
* @param {VALUE} value The value. |
| 77 |
*/ |
| 78 |
goog.structs.Trie.prototype.add = function(key, value) { |
| 79 |
this.setOrAdd_(key, value, true); |
| 80 |
}; |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* Helper function for set and add. Adds the given key/value pair to |
| 85 |
* the trie, or, if the key already exists, sets the value of the key. If |
| 86 |
* opt_add is true, then throws an exception if the key already has a value in |
| 87 |
* the trie. O(L), where L is the length of the key. |
| 88 |
* @param {string} key The key. |
| 89 |
* @param {VALUE} value The value. |
| 90 |
* @param {boolean=} opt_add Throw exception if key is already in the trie. |
| 91 |
* @private |
| 92 |
*/ |
| 93 |
goog.structs.Trie.prototype.setOrAdd_ = function(key, value, opt_add) { |
| 94 |
var node = this; |
| 95 |
for (var characterPosition = 0; characterPosition < key.length; |
| 96 |
characterPosition++) { |
| 97 |
var currentCharacter = key.charAt(characterPosition); |
| 98 |
if (!node.childNodes_[currentCharacter]) { |
| 99 |
node.childNodes_[currentCharacter] = new goog.structs.Trie(); |
| 100 |
} |
| 101 |
node = node.childNodes_[currentCharacter]; |
| 102 |
} |
| 103 |
if (opt_add && node.value_ !== undefined) { |
| 104 |
throw new Error('The collection already contains the key "' + key + '"'); |
| 105 |
} else { |
| 106 |
node.value_ = value; |
| 107 |
} |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
/** |
| 112 |
* Adds multiple key/value pairs from another goog.structs.Trie or Object. |
| 113 |
* O(N) where N is the number of nodes in the trie. |
| 114 |
* @param {!Object<string, VALUE>|!goog.structs.Trie<VALUE>} trie Object |
| 115 |
* containing the data to add. |
| 116 |
*/ |
| 117 |
goog.structs.Trie.prototype.setAll = function(trie) { |
| 118 |
var keys = goog.structs.getKeys(trie); |
| 119 |
var values = goog.structs.getValues(trie); |
| 120 |
|
| 121 |
for (var i = 0; i < keys.length; i++) { |
| 122 |
this.set(keys[i], values[i]); |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* Traverse along the given path, returns the child node at ending. |
| 129 |
* Returns undefined if node for the path doesn't exist. |
| 130 |
* @param {string} path The path to traverse. |
| 131 |
* @return {!goog.structs.Trie<VALUE>|undefined} |
| 132 |
* @private |
| 133 |
*/ |
| 134 |
goog.structs.Trie.prototype.getChildNode_ = function(path) { |
| 135 |
var node = this; |
| 136 |
for (var characterPosition = 0; characterPosition < path.length; |
| 137 |
characterPosition++) { |
| 138 |
var currentCharacter = path.charAt(characterPosition); |
| 139 |
node = node.childNodes_[currentCharacter]; |
| 140 |
if (!node) { |
| 141 |
return undefined; |
| 142 |
} |
| 143 |
} |
| 144 |
return node; |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
/** |
| 149 |
* Retrieves a value from the trie given a key. O(L), where L is the length of |
| 150 |
* the key. |
| 151 |
* @param {string} key The key to retrieve from the trie. |
| 152 |
* @return {VALUE|undefined} The value of the key in the trie, or undefined if |
| 153 |
* the trie does not contain this key. |
| 154 |
*/ |
| 155 |
goog.structs.Trie.prototype.get = function(key) { |
| 156 |
var node = this.getChildNode_(key); |
| 157 |
return node ? node.value_ : undefined; |
| 158 |
}; |
| 159 |
|
| 160 |
|
| 161 |
/** |
| 162 |
* Retrieves all values from the trie that correspond to prefixes of the given |
| 163 |
* input key. O(L), where L is the length of the key. |
| 164 |
* |
| 165 |
* @param {string} key The key to use for lookup. The given key as well as all |
| 166 |
* prefixes of the key are retrieved. |
| 167 |
* @param {?number=} opt_keyStartIndex Optional position in key to start lookup |
| 168 |
* from. Defaults to 0 if not specified. |
| 169 |
* @return {!Object<string, VALUE>} Map of end index of matching prefixes and |
| 170 |
* corresponding values. Empty if no match found. |
| 171 |
*/ |
| 172 |
goog.structs.Trie.prototype.getKeyAndPrefixes = function( |
| 173 |
key, opt_keyStartIndex) { |
| 174 |
/** @type {!goog.structs.Trie<VALUE>} */ |
| 175 |
var node = this; |
| 176 |
var matches = {}; |
| 177 |
var characterPosition = opt_keyStartIndex || 0; |
| 178 |
|
| 179 |
if (node.value_ !== undefined) { |
| 180 |
matches[characterPosition] = node.value_; |
| 181 |
} |
| 182 |
|
| 183 |
for (; characterPosition < key.length; characterPosition++) { |
| 184 |
var currentCharacter = key.charAt(characterPosition); |
| 185 |
if (!(currentCharacter in node.childNodes_)) { |
| 186 |
break; |
| 187 |
} |
| 188 |
node = node.childNodes_[currentCharacter]; |
| 189 |
if (/** @type {VALUE} */ (node.value_) !== undefined) { |
| 190 |
matches[characterPosition] = node.value_; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return matches; |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* Gets the values of the trie. Not returned in any reliable order. O(N) where |
| 200 |
* N is the number of nodes in the trie. Calls getValuesInternal_. |
| 201 |
* @return {!Array<VALUE>} The values in the trie. |
| 202 |
*/ |
| 203 |
goog.structs.Trie.prototype.getValues = function() { |
| 204 |
var allValues = []; |
| 205 |
this.getValuesInternal_(allValues); |
| 206 |
return allValues; |
| 207 |
}; |
| 208 |
|
| 209 |
|
| 210 |
/** |
| 211 |
* Gets the values of the trie. Not returned in any reliable order. O(N) where |
| 212 |
* N is the number of nodes in the trie. Builds the values as it goes. |
| 213 |
* @param {!Array<VALUE>} allValues Array to place values into. |
| 214 |
* @private |
| 215 |
*/ |
| 216 |
goog.structs.Trie.prototype.getValuesInternal_ = function(allValues) { |
| 217 |
if (this.value_ !== undefined) { |
| 218 |
allValues.push(this.value_); |
| 219 |
} |
| 220 |
for (var childNode in this.childNodes_) { |
| 221 |
this.childNodes_[childNode].getValuesInternal_(allValues); |
| 222 |
} |
| 223 |
}; |
| 224 |
|
| 225 |
|
| 226 |
/** |
| 227 |
* Gets the keys of the trie. Not returned in any reliable order. O(N) where |
| 228 |
* N is the number of nodes in the trie (or prefix subtree). |
| 229 |
* @param {string=} opt_prefix Find only keys with this optional prefix. |
| 230 |
* @return {!Array<string>} The keys in the trie. |
| 231 |
*/ |
| 232 |
goog.structs.Trie.prototype.getKeys = function(opt_prefix) { |
| 233 |
var allKeys = []; |
| 234 |
if (opt_prefix) { |
| 235 |
// Traverse to the given prefix, then call getKeysInternal_ to dump the |
| 236 |
// keys below that point. |
| 237 |
var node = this; |
| 238 |
for (var characterPosition = 0; characterPosition < opt_prefix.length; |
| 239 |
characterPosition++) { |
| 240 |
var currentCharacter = opt_prefix.charAt(characterPosition); |
| 241 |
if (!node.childNodes_[currentCharacter]) { |
| 242 |
return []; |
| 243 |
} |
| 244 |
node = node.childNodes_[currentCharacter]; |
| 245 |
} |
| 246 |
node.getKeysInternal_(opt_prefix, allKeys); |
| 247 |
} else { |
| 248 |
this.getKeysInternal_('', allKeys); |
| 249 |
} |
| 250 |
return allKeys; |
| 251 |
}; |
| 252 |
|
| 253 |
|
| 254 |
/** |
| 255 |
* Private method to get keys from the trie. Builds the keys as it goes. |
| 256 |
* @param {string} keySoFar The partial key (prefix) traversed so far. |
| 257 |
* @param {!Array<string>} allKeys The partially built array of keys seen so |
| 258 |
* far. |
| 259 |
* @private |
| 260 |
*/ |
| 261 |
goog.structs.Trie.prototype.getKeysInternal_ = function(keySoFar, allKeys) { |
| 262 |
if (this.value_ !== undefined) { |
| 263 |
allKeys.push(keySoFar); |
| 264 |
} |
| 265 |
for (var childNode in this.childNodes_) { |
| 266 |
this.childNodes_[childNode].getKeysInternal_(keySoFar + childNode, allKeys); |
| 267 |
} |
| 268 |
}; |
| 269 |
|
| 270 |
|
| 271 |
/** |
| 272 |
* Checks to see if a certain key is in the trie. O(L), where L is the length |
| 273 |
* of the key. |
| 274 |
* @param {string} key A key that may be in the trie. |
| 275 |
* @return {boolean} Whether the trie contains key. |
| 276 |
*/ |
| 277 |
goog.structs.Trie.prototype.containsKey = function(key) { |
| 278 |
return this.get(key) !== undefined; |
| 279 |
}; |
| 280 |
|
| 281 |
|
| 282 |
/** |
| 283 |
* Checks to see if a certain prefix is in the trie. O(L), where L is the length |
| 284 |
* of the prefix. |
| 285 |
* @param {string} prefix A prefix that may be in the trie. |
| 286 |
* @return {boolean} Whether any key of the trie has the prefix. |
| 287 |
*/ |
| 288 |
goog.structs.Trie.prototype.containsPrefix = function(prefix) { |
| 289 |
// Empty string is any key's prefix. |
| 290 |
if (prefix.length == 0) { |
| 291 |
return !this.isEmpty(); |
| 292 |
} |
| 293 |
return !!this.getChildNode_(prefix); |
| 294 |
}; |
| 295 |
|
| 296 |
|
| 297 |
/** |
| 298 |
* Checks to see if a certain value is in the trie. Worst case is O(N) where |
| 299 |
* N is the number of nodes in the trie. |
| 300 |
* @param {VALUE} value A value that may be in the trie. |
| 301 |
* @return {boolean} Whether the trie contains the value. |
| 302 |
*/ |
| 303 |
goog.structs.Trie.prototype.containsValue = function(value) { |
| 304 |
if (this.value_ === value) { |
| 305 |
return true; |
| 306 |
} |
| 307 |
for (var childNode in this.childNodes_) { |
| 308 |
if (this.childNodes_[childNode].containsValue(value)) { |
| 309 |
return true; |
| 310 |
} |
| 311 |
} |
| 312 |
return false; |
| 313 |
}; |
| 314 |
|
| 315 |
|
| 316 |
/** |
| 317 |
* Completely empties a trie of all keys and values. ~O(1) |
| 318 |
*/ |
| 319 |
goog.structs.Trie.prototype.clear = function() { |
| 320 |
this.childNodes_ = {}; |
| 321 |
this.value_ = undefined; |
| 322 |
}; |
| 323 |
|
| 324 |
|
| 325 |
/** |
| 326 |
* Removes a key from the trie or throws an exception if the key is not in the |
| 327 |
* trie. O(L), where L is the length of the key. |
| 328 |
* @param {string} key A key that should be removed from the trie. |
| 329 |
* @return {VALUE} The value whose key was removed. |
| 330 |
*/ |
| 331 |
goog.structs.Trie.prototype.remove = function(key) { |
| 332 |
var node = this; |
| 333 |
var parents = []; |
| 334 |
for (var characterPosition = 0; characterPosition < key.length; |
| 335 |
characterPosition++) { |
| 336 |
var currentCharacter = key.charAt(characterPosition); |
| 337 |
if (!node.childNodes_[currentCharacter]) { |
| 338 |
throw new Error('The collection does not have the key "' + key + '"'); |
| 339 |
} |
| 340 |
|
| 341 |
// Archive the current parent and child name (key in childNodes_) so that |
| 342 |
// we may remove the following node and its parents if they are empty. |
| 343 |
parents.push([node, currentCharacter]); |
| 344 |
|
| 345 |
node = node.childNodes_[currentCharacter]; |
| 346 |
} |
| 347 |
var oldValue = node.value_; |
| 348 |
delete node.value_; |
| 349 |
|
| 350 |
while (parents.length > 0) { |
| 351 |
var currentParentAndCharacter = parents.pop(); |
| 352 |
var currentParent = currentParentAndCharacter[0]; |
| 353 |
var currentCharacter = currentParentAndCharacter[1]; |
| 354 |
if (currentParent.childNodes_[currentCharacter].isEmpty()) { |
| 355 |
// If the child is empty, then remove it. |
| 356 |
delete currentParent.childNodes_[currentCharacter]; |
| 357 |
} else { |
| 358 |
// No point of traversing back any further, since we can't remove this |
| 359 |
// path. |
| 360 |
break; |
| 361 |
} |
| 362 |
} |
| 363 |
return oldValue; |
| 364 |
}; |
| 365 |
|
| 366 |
|
| 367 |
/** |
| 368 |
* Clones a trie and returns a new trie. O(N), where N is the number of nodes |
| 369 |
* in the trie. |
| 370 |
* @return {!goog.structs.Trie<VALUE>} A new goog.structs.Trie with the same |
| 371 |
* key value pairs. |
| 372 |
*/ |
| 373 |
goog.structs.Trie.prototype.clone = function() { |
| 374 |
return new goog.structs.Trie(this); |
| 375 |
}; |
| 376 |
|
| 377 |
|
| 378 |
/** |
| 379 |
* Returns the number of key value pairs in the trie. O(N), where N is the |
| 380 |
* number of nodes in the trie. |
| 381 |
* TODO: This could be optimized by storing a weight (count below) in every |
| 382 |
* node. |
| 383 |
* @return {number} The number of pairs. |
| 384 |
*/ |
| 385 |
goog.structs.Trie.prototype.getCount = function() { |
| 386 |
return goog.structs.getCount(this.getValues()); |
| 387 |
}; |
| 388 |
|
| 389 |
|
| 390 |
/** |
| 391 |
* Returns true if this trie contains no elements. ~O(1). |
| 392 |
* @return {boolean} True iff this trie contains no elements. |
| 393 |
*/ |
| 394 |
goog.structs.Trie.prototype.isEmpty = function() { |
| 395 |
return this.value_ === undefined && goog.object.isEmpty(this.childNodes_); |
| 396 |
}; |