|
1 |
| -import RabinFingerprint from '../../../utils/hash/rolling/Rabin_Fingerprint'; |
| 1 | +import PolynomialHash from '../../cryptography/polynomial-hash/PolynomialHash'; |
2 | 2 |
|
3 | 3 | /**
|
4 |
| - * @param {string} text |
5 |
| - * @param {string} word |
6 |
| - * @return {number} |
| 4 | + * Checks if two strings are equal. |
| 5 | + * |
| 6 | + * We may simply compare (string1 === string2) but for the |
| 7 | + * purpose of analyzing algorithm time complexity let's do |
| 8 | + * it character by character. |
| 9 | + * |
| 10 | + * @param {string} string1 |
| 11 | + * @param {string} string2 |
7 | 12 | */
|
8 |
| -export default function rabinKarp(text, word) { |
9 |
| - const toNum = function toNum(character) { |
10 |
| - const surrogate = character.codePointAt(1); |
11 |
| - return ((surrogate === undefined) ? 0 : surrogate) + (character.codePointAt(0) * (2 ** 16)); |
12 |
| - }; |
13 |
| - const arrEq = (a1, a2) => ((a1.length === a2.length) && a1.every((val, idx) => val === a2[idx])); |
14 |
| - |
15 |
| - const wordArr = [...word].map(toNum); |
16 |
| - const textArr = [...text].map(toNum); |
17 |
| - |
18 |
| - // The prime generation function could depend on the inputs for collision guarantees. |
19 |
| - const hasher = new RabinFingerprint(() => 229); |
20 |
| - const cmpVal = hasher.init(wordArr); |
21 |
| - |
22 |
| - let currHash = hasher.init(textArr.slice(0, wordArr.length)); |
23 |
| - if ((currHash === cmpVal) && arrEq(wordArr, textArr.slice(0, wordArr.length))) { |
24 |
| - return 0; |
| 13 | +function stringsAreEqual(string1, string2) { |
| 14 | + if (string1.length !== string2.length) { |
| 15 | + return false; |
| 16 | + } |
| 17 | + |
| 18 | + for (let charIndex = 0; charIndex < string1.length; charIndex += 1) { |
| 19 | + if (string1[charIndex] !== string2[charIndex]) { |
| 20 | + return false; |
| 21 | + } |
25 | 22 | }
|
26 | 23 |
|
27 |
| - for (let i = 0; i < (textArr.length - wordArr.length); i += 1) { |
28 |
| - currHash = hasher.roll(textArr[i], textArr[i + wordArr.length]); |
29 |
| - if ((currHash === cmpVal) && arrEq(wordArr, textArr.slice(i + 1, i + wordArr.length + 1))) { |
30 |
| - return i + 1; |
| 24 | + return true; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * @param {string} text - Text that may contain the searchable word. |
| 29 | + * @param {string} word - Word that is being searched in text. |
| 30 | + * @return {number} - Position of the word in text. |
| 31 | + */ |
| 32 | +export default function rabinKarp(text, word) { |
| 33 | + const hasher = new PolynomialHash(); |
| 34 | + |
| 35 | + // Calculate word hash that we will use for comparison with other substring hashes. |
| 36 | + const wordHash = hasher.hash(word); |
| 37 | + |
| 38 | + let prevFrame = null; |
| 39 | + let currentFrameHash = null; |
| 40 | + |
| 41 | + // Go through all substring of the text that may match. |
| 42 | + for (let charIndex = 0; charIndex <= (text.length - word.length); charIndex += 1) { |
| 43 | + const currentFrame = text.substring(charIndex, charIndex + word.length); |
| 44 | + |
| 45 | + // Calculate the hash of current substring. |
| 46 | + if (currentFrameHash === null) { |
| 47 | + currentFrameHash = hasher.hash(currentFrame); |
| 48 | + } else { |
| 49 | + currentFrameHash = hasher.roll(currentFrameHash, prevFrame, currentFrame); |
| 50 | + } |
| 51 | + |
| 52 | + prevFrame = currentFrame; |
| 53 | + |
| 54 | + // Compare the hash of current substring and seeking string. |
| 55 | + // In case if hashes match let's check substring char by char. |
| 56 | + if ( |
| 57 | + wordHash === currentFrameHash |
| 58 | + && stringsAreEqual(text.substr(charIndex, word.length), word) |
| 59 | + ) { |
| 60 | + return charIndex; |
31 | 61 | }
|
32 | 62 | }
|
33 | 63 |
|
|
0 commit comments