Skip to content

Commit

Permalink
Compute LCS lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
rien committed Sep 18, 2024
1 parent 57cfcc6 commit 3313bc0
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions core/src/algorithm/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ export class Pair extends Identifiable {
public readonly rightCovered;
public readonly leftTotal;
public readonly rightTotal;
public readonly longest;
public readonly similarity;
public readonly leftIgnored;
public readonly rightIgnored;

private longestValue: number;

constructor(
public readonly leftEntry: FileEntry,
public readonly rightEntry: FileEntry,
Expand All @@ -50,12 +51,38 @@ export class Pair extends Identifiable {
large = leftEntry;
}

for (const fingeprint of small.shared) {
if (large.shared.has(fingeprint)) {
this.shared.push(fingeprint);
this.leftCovered = 0;
this.rightCovered = 0;
for (const fingerprint of small.shared) {
if (large.shared.has(fingerprint)) {
this.shared.push(fingerprint);
this.leftCovered += fingerprint.occurrencesOf(this.leftFile).length;
this.rightCovered += fingerprint.occurrencesOf(this.rightFile).length;
}
}

this.longestValue = -1;

this.leftIgnored = leftEntry.ignored.size;
this.rightIgnored = leftEntry.ignored.size;
this.leftTotal = leftEntry.kgrams.length;
this.rightTotal = rightEntry.kgrams.length;
const denominator = this.leftTotal + this.rightTotal - this.leftIgnored - this.rightIgnored;
if (denominator > 0) {
this.similarity = (this.leftCovered + this.rightCovered) / denominator;
} else {
this.similarity = 0;
}
}

get longest(): number {
if (this.longestValue < 0) {
this.calculateLongestFragment();
}
return this.longestValue;
}

public calculateLongestFragment() {
const left: Kgram[] = [];
const right: Kgram[] = [];
for (const fingerprint of this.shared) {
Expand All @@ -69,20 +96,8 @@ export class Pair extends Identifiable {
left.sort((a, b) => a.index - b.index);
right.sort((a, b) => a.index - b.index);

this.longest = this.longestCommonSubstring(left, right);

this.leftCovered = left.length;
this.rightCovered = right.length;
this.leftIgnored = leftEntry.ignored.size;
this.rightIgnored = leftEntry.ignored.size;
this.leftTotal = leftEntry.kgrams.length;
this.rightTotal = rightEntry.kgrams.length;
const denominator = this.leftTotal + this.rightTotal - this.leftIgnored - this.rightIgnored;
if (denominator > 0) {
this.similarity = (this.leftCovered + this.rightCovered) / denominator;
} else {
this.similarity = 0;
}
this.longestValue = this.longestCommonSubstring(left, right);
return this.longestValue;
}

private longestCommonSubstring(l: Kgram[], r: Kgram[]): number {
Expand Down

0 comments on commit 3313bc0

Please sign in to comment.