Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/common/diff/defaultLinesDiffComputer/utils.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { CharCode } from '../../../../base/common/charCode.js';
7
import { LineRange } from '../../core/ranges/lineRange.js';
8
import { DetailedLineRangeMapping } from '../rangeMapping.js';
9
10
export class Array2D<T> {
11
private readonly array: T[] = [];
12
13
constructor(public readonly width: number, public readonly height: number) {
14
this.array = new Array<T>(width * height);
15
}
16
17
get(x: number, y: number): T {
18
return this.array[x + y * this.width];
19
}
20
21
set(x: number, y: number, value: T): void {
22
this.array[x + y * this.width] = value;
23
}
24
}
25
26
export function isSpace(charCode: number): boolean {
27
return charCode === CharCode.Space || charCode === CharCode.Tab;
28
}
29
30
export class LineRangeFragment {
31
private static chrKeys = new Map<string, number>();
32
33
private static getKey(chr: string): number {
34
let key = this.chrKeys.get(chr);
35
if (key === undefined) {
36
key = this.chrKeys.size;
37
this.chrKeys.set(chr, key);
38
}
39
return key;
40
}
41
42
private readonly totalCount: number;
43
private readonly histogram: number[] = [];
44
constructor(
45
public readonly range: LineRange,
46
public readonly lines: string[],
47
public readonly source: DetailedLineRangeMapping,
48
) {
49
let counter = 0;
50
for (let i = range.startLineNumber - 1; i < range.endLineNumberExclusive - 1; i++) {
51
const line = lines[i];
52
for (let j = 0; j < line.length; j++) {
53
counter++;
54
const chr = line[j];
55
const key = LineRangeFragment.getKey(chr);
56
this.histogram[key] = (this.histogram[key] || 0) + 1;
57
}
58
counter++;
59
const key = LineRangeFragment.getKey('\n');
60
this.histogram[key] = (this.histogram[key] || 0) + 1;
61
}
62
63
this.totalCount = counter;
64
}
65
66
public computeSimilarity(other: LineRangeFragment): number {
67
let sumDifferences = 0;
68
const maxLength = Math.max(this.histogram.length, other.histogram.length);
69
for (let i = 0; i < maxLength; i++) {
70
sumDifferences += Math.abs((this.histogram[i] ?? 0) - (other.histogram[i] ?? 0));
71
}
72
return 1 - (sumDifferences / (this.totalCount + other.totalCount));
73
}
74
}
75
76