| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2007 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2012 Google Inc. All rights reserved. | 3 * Copyright (C) 2012 Google Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
| 7 * are met: | 7 * are met: |
| 8 * | 8 * |
| 9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 } | 297 } |
| 298 | 298 |
| 299 /** | 299 /** |
| 300 * @param {string|undefined} string | 300 * @param {string|undefined} string |
| 301 * @return {number} | 301 * @return {number} |
| 302 */ | 302 */ |
| 303 String.hashCode = function(string) | 303 String.hashCode = function(string) |
| 304 { | 304 { |
| 305 if (!string) | 305 if (!string) |
| 306 return 0; | 306 return 0; |
| 307 var result = 0; | 307 // Hash algorithm for substrings is described in "Über die Komplexität der M
ultiplikation in |
| 308 for (var i = 0; i < string.length; ++i) | 308 // eingeschränkten Branchingprogrammmodellen" by Woelfe. |
| 309 result = (result * 31 + string.charCodeAt(i)) | 0; | 309 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#
SECTION00832000000000000000 |
| 310 return Math.abs(result); | 310 var p = ((1 << 30) * 4 - 5); // prime: 2^32 - 5 |
| 311 var z = 0x5033d967; // 32 bits from random.org |
| 312 var z2 = 0x59d2f15d; // random odd 32 bit number |
| 313 var s = 0; |
| 314 var zi = 1; |
| 315 for (var i = 0; i < string.length; i++) { |
| 316 var xi = string.charCodeAt(i) * z2; |
| 317 s = (s + zi * xi) % p; |
| 318 zi = (zi * z) % p; |
| 319 } |
| 320 s = (s + zi * (p - 1)) % p; |
| 321 return Math.abs(s|0); |
| 311 } | 322 } |
| 312 | 323 |
| 313 /** | 324 /** |
| 314 * @param {string} string | 325 * @param {string} string |
| 315 * @param {number} index | 326 * @param {number} index |
| 316 * @return {boolean} | 327 * @return {boolean} |
| 317 */ | 328 */ |
| 318 String.isDigitAt = function(string, index) | 329 String.isDigitAt = function(string, index) |
| 319 { | 330 { |
| 320 var c = string.charCodeAt(index); | 331 var c = string.charCodeAt(index); |
| (...skipping 1324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1645 _tryMerge: function(first, second) | 1656 _tryMerge: function(first, second) |
| 1646 { | 1657 { |
| 1647 var merged = this._mergeCallback && this._mergeCallback(first, second); | 1658 var merged = this._mergeCallback && this._mergeCallback(first, second); |
| 1648 if (!merged) | 1659 if (!merged) |
| 1649 return null; | 1660 return null; |
| 1650 merged.begin = first.begin; | 1661 merged.begin = first.begin; |
| 1651 merged.end = Math.max(first.end, second.end); | 1662 merged.end = Math.max(first.end, second.end); |
| 1652 return merged; | 1663 return merged; |
| 1653 } | 1664 } |
| 1654 } | 1665 } |
| OLD | NEW |