Skip to content

Commit

Permalink
chore: v1.0.1
Browse files Browse the repository at this point in the history
move pattern equality array outside function
  • Loading branch information
arugaz committed Sep 22, 2023
1 parent 88a687a commit 19c97b5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hidden-finder/didyoumean",
"version": "1.0.0",
"version": "1.0.1",
"description": "A simple and lightweight matching input to a list of potential matches using the Levenshtein distance algorithm.",
"type": "module",
"source": "./src/index.ts",
Expand Down
52 changes: 31 additions & 21 deletions src/calculate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const patternEqualityArray = new Uint32Array(65536)

export const calculateMyersDistanceShort = (text: string, pattern: string): number => {
const textLength = text.length
const patternLength = pattern.length
const lastSetBit = 1 << (textLength - 1)
const patternEqualityArray = new Uint32Array(65536)

let positiveVector = -1
let negativeVector = 0
Expand Down Expand Up @@ -32,6 +33,10 @@ export const calculateMyersDistanceShort = (text: string, pattern: string): numb
negativeVector &= mixedVector
}

for (let i = textLength - 1; i >= 0; i--) {
patternEqualityArray[text.charCodeAt(i)] = 0
}

return point
}

Expand All @@ -40,16 +45,18 @@ export const calculateMyersDistanceLong = (text: string, pattern: string): numbe
const patternLength = pattern.length
const matchHighBits = new Array<number>(Math.ceil(textLength / 32))
const mismatchHighBits = new Array<number>(Math.ceil(textLength / 32))
const patternEqualityArray = new Uint32Array(65536)

for (let i = 0; i < matchHighBits.length; i++) {
matchHighBits[i] = 0
mismatchHighBits[i] = -1
}

let negativeVector = 0
let positiveVector = -1
let point = patternLength
let patternHighBit: number, textHighBit: number, mixedHighBit: number

for (let j = 0; j < Math.ceil(patternLength / 32); j++) {
let negativeVector = 0
let positiveVector = -1
const start = j * 32
const vectorLength = Math.min(32, patternLength - start) + start

Expand All @@ -59,19 +66,20 @@ export const calculateMyersDistanceLong = (text: string, pattern: string): numbe

for (let i = 0; i < textLength; i++) {
const equality = patternEqualityArray[text.charCodeAt(i)]
const patternBit = (matchHighBits[(i / 32) | 0] >>> i) & 1
const textBit = (mismatchHighBits[(i / 32) | 0] >>> i) & 1
const index = (i / 32) | 0
const patternBit = (matchHighBits[index] >>> i) & 1
const textBit = (mismatchHighBits[index] >>> i) & 1
const mixedVector = equality | negativeVector

const mixedHighBit = ((((equality | textBit) & positiveVector) + positiveVector) ^ positiveVector) | equality | textBit
let patternHighBit = negativeVector | ~(mixedHighBit | positiveVector)
let textHighBit = positiveVector & mixedHighBit
mixedHighBit = ((((equality | textBit) & positiveVector) + positiveVector) ^ positiveVector) | equality | textBit
patternHighBit = negativeVector | ~(mixedHighBit | positiveVector)
textHighBit = positiveVector & mixedHighBit

if ((patternHighBit >>> 31) ^ patternBit) {
matchHighBits[(i / 32) | 0] ^= 1 << i
matchHighBits[index] ^= 1 << i
}
if ((textHighBit >>> 31) ^ textBit) {
mismatchHighBits[(i / 32) | 0] ^= 1 << i
mismatchHighBits[index] ^= 1 << i
}

patternHighBit = (patternHighBit << 1) | patternBit
Expand All @@ -85,34 +93,32 @@ export const calculateMyersDistanceLong = (text: string, pattern: string): numbe
}
}

let negativeVector = 0
let positiveVector = -1
const start = Math.ceil(patternLength / 32) * 32
const vectorLength = Math.min(32, patternLength - start) + start

for (let k = start; k < vectorLength; k++) {
patternEqualityArray[pattern.charCodeAt(k)] |= 1 << k
}

let point = patternLength
for (let i = 0; i < textLength; i++) {
const equality = patternEqualityArray[text.charCodeAt(i)]
const patternBit = (matchHighBits[(i / 32) | 0] >>> i) & 1
const textBit = (mismatchHighBits[(i / 32) | 0] >>> i) & 1
const index = (i / 32) | 0
const patternBit = (matchHighBits[index] >>> i) & 1
const textBit = (mismatchHighBits[index] >>> i) & 1
const mixedVector = equality | negativeVector

const mixedHighBit = ((((equality | textBit) & positiveVector) + positiveVector) ^ positiveVector) | equality | textBit
let patternHighBit = negativeVector | ~(mixedHighBit | positiveVector)
let textHighBit = positiveVector & mixedHighBit
mixedHighBit = ((((equality | textBit) & positiveVector) + positiveVector) ^ positiveVector) | equality | textBit
patternHighBit = negativeVector | ~(mixedHighBit | positiveVector)
textHighBit = positiveVector & mixedHighBit

point += (patternHighBit >>> (patternLength - 1)) & 1
point -= (textHighBit >>> (patternLength - 1)) & 1

if ((patternHighBit >>> 31) ^ patternBit) {
matchHighBits[(i / 32) | 0] ^= 1 << i
matchHighBits[index] ^= 1 << i
}
if ((textHighBit >>> 31) ^ textBit) {
mismatchHighBits[(i / 32) | 0] ^= 1 << i
mismatchHighBits[index] ^= 1 << i
}

patternHighBit = (patternHighBit << 1) | patternBit
Expand All @@ -121,5 +127,9 @@ export const calculateMyersDistanceLong = (text: string, pattern: string): numbe
negativeVector = patternHighBit & mixedVector
}

for (let k = start; k < vectorLength; k++) {
patternEqualityArray[pattern.charCodeAt(k)] = 0
}

return point
}
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import { calculateMyersDistanceShort, calculateMyersDistanceLong } from './calcu
* @returns {number} The Levenshtein distance between the two input strings.
*/
export const calculateDistance = (text: string, pattern: string): number => {
if (text.length < pattern.length) [pattern, text] = [text, pattern]

if (pattern.length === 0) return text.length

if (text.length === 0) return pattern.length

if (text.length < pattern.length) [text, pattern] = [pattern, text]

if (text.length <= 32) return calculateMyersDistanceShort(text, pattern)

return calculateMyersDistanceLong(text, pattern)
Expand Down

0 comments on commit 19c97b5

Please sign in to comment.