GUI: util... added createTrie() function to fn.js
Change-Id: I112fd9bf0dbd4eea078453447fc648c61a665e66
Showing
1 changed file
with
40 additions
and
1 deletions
| ... | @@ -287,6 +287,44 @@ | ... | @@ -287,6 +287,44 @@ |
| 287 | } | 287 | } |
| 288 | } | 288 | } |
| 289 | 289 | ||
| 290 | + // generate a trie structure from the given array of strings | ||
| 291 | + // if ignoreCase is true, all words are converted to uppercase first | ||
| 292 | + // note: each letter in each string must be valid as an object property key | ||
| 293 | + function createTrie(words, ignoreCase) { | ||
| 294 | + var trie = {}; | ||
| 295 | + | ||
| 296 | + function add(c) { | ||
| 297 | + var q = c.s.shift(), | ||
| 298 | + np = c.p[q]; | ||
| 299 | + | ||
| 300 | + if (!np) { | ||
| 301 | + c.p[q] = {}; | ||
| 302 | + np = c.p[q]; | ||
| 303 | + } | ||
| 304 | + | ||
| 305 | + return { | ||
| 306 | + p: np, | ||
| 307 | + s: c.s | ||
| 308 | + } | ||
| 309 | + } | ||
| 310 | + | ||
| 311 | + words.forEach(function (word) { | ||
| 312 | + var p = trie, | ||
| 313 | + w = ignoreCase ? word.toUpperCase() : word, | ||
| 314 | + s = w.split(''), | ||
| 315 | + c = { | ||
| 316 | + p: p, | ||
| 317 | + s: s | ||
| 318 | + }; | ||
| 319 | + | ||
| 320 | + while (c.s.length) { | ||
| 321 | + c = add(c); | ||
| 322 | + } | ||
| 323 | + }); | ||
| 324 | + | ||
| 325 | + return trie; | ||
| 326 | + } | ||
| 327 | + | ||
| 290 | angular.module('onosUtil') | 328 | angular.module('onosUtil') |
| 291 | .factory('FnService', | 329 | .factory('FnService', |
| 292 | ['$window', '$location', '$log', function (_$window_, $loc, _$log_) { | 330 | ['$window', '$location', '$log', function (_$window_, $loc, _$log_) { |
| ... | @@ -321,7 +359,8 @@ | ... | @@ -321,7 +359,8 @@ |
| 321 | noPx: noPx, | 359 | noPx: noPx, |
| 322 | noPxStyle: noPxStyle, | 360 | noPxStyle: noPxStyle, |
| 323 | endsWith: endsWith, | 361 | endsWith: endsWith, |
| 324 | - parseBitRate: parseBitRate | 362 | + parseBitRate: parseBitRate, |
| 363 | + createTrie: createTrie | ||
| 325 | }; | 364 | }; |
| 326 | }]); | 365 | }]); |
| 327 | 366 | ... | ... |
-
Please register or login to post a comment