Path: blob/main/src/vs/editor/common/diff/defaultLinesDiffComputer/utils.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { CharCode } from '../../../../base/common/charCode.js';6import { LineRange } from '../../core/ranges/lineRange.js';7import { DetailedLineRangeMapping } from '../rangeMapping.js';89export class Array2D<T> {10private readonly array: T[] = [];1112constructor(public readonly width: number, public readonly height: number) {13this.array = new Array<T>(width * height);14}1516get(x: number, y: number): T {17return this.array[x + y * this.width];18}1920set(x: number, y: number, value: T): void {21this.array[x + y * this.width] = value;22}23}2425export function isSpace(charCode: number): boolean {26return charCode === CharCode.Space || charCode === CharCode.Tab;27}2829export class LineRangeFragment {30private static chrKeys = new Map<string, number>();3132private static getKey(chr: string): number {33let key = this.chrKeys.get(chr);34if (key === undefined) {35key = this.chrKeys.size;36this.chrKeys.set(chr, key);37}38return key;39}4041private readonly totalCount: number;42private readonly histogram: number[] = [];43constructor(44public readonly range: LineRange,45public readonly lines: string[],46public readonly source: DetailedLineRangeMapping,47) {48let counter = 0;49for (let i = range.startLineNumber - 1; i < range.endLineNumberExclusive - 1; i++) {50const line = lines[i];51for (let j = 0; j < line.length; j++) {52counter++;53const chr = line[j];54const key = LineRangeFragment.getKey(chr);55this.histogram[key] = (this.histogram[key] || 0) + 1;56}57counter++;58const key = LineRangeFragment.getKey('\n');59this.histogram[key] = (this.histogram[key] || 0) + 1;60}6162this.totalCount = counter;63}6465public computeSimilarity(other: LineRangeFragment): number {66let sumDifferences = 0;67const maxLength = Math.max(this.histogram.length, other.histogram.length);68for (let i = 0; i < maxLength; i++) {69sumDifferences += Math.abs((this.histogram[i] ?? 0) - (other.histogram[i] ?? 0));70}71return 1 - (sumDifferences / (this.totalCount + other.totalCount));72}73}747576