UNPKG

monaco-editor

Version:
62 lines (61 loc) 2.34 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ export class Array2D { constructor(width, height) { this.width = width; this.height = height; this.array = []; this.array = new Array(width * height); } get(x, y) { return this.array[x + y * this.width]; } set(x, y, value) { this.array[x + y * this.width] = value; } } export function isSpace(charCode) { return charCode === 32 /* CharCode.Space */ || charCode === 9 /* CharCode.Tab */; } export class LineRangeFragment { static getKey(chr) { let key = this.chrKeys.get(chr); if (key === undefined) { key = this.chrKeys.size; this.chrKeys.set(chr, key); } return key; } constructor(range, lines, source) { this.range = range; this.lines = lines; this.source = source; this.histogram = []; let counter = 0; for (let i = range.startLineNumber - 1; i < range.endLineNumberExclusive - 1; i++) { const line = lines[i]; for (let j = 0; j < line.length; j++) { counter++; const chr = line[j]; const key = LineRangeFragment.getKey(chr); this.histogram[key] = (this.histogram[key] || 0) + 1; } counter++; const key = LineRangeFragment.getKey('\n'); this.histogram[key] = (this.histogram[key] || 0) + 1; } this.totalCount = counter; } computeSimilarity(other) { var _a, _b; let sumDifferences = 0; const maxLength = Math.max(this.histogram.length, other.histogram.length); for (let i = 0; i < maxLength; i++) { sumDifferences += Math.abs(((_a = this.histogram[i]) !== null && _a !== void 0 ? _a : 0) - ((_b = other.histogram[i]) !== null && _b !== void 0 ? _b : 0)); } return 1 - (sumDifferences / (this.totalCount + other.totalCount)); } } LineRangeFragment.chrKeys = new Map();