Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sheet): add range merge util #1615

Merged
merged 3 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 15 additions & 28 deletions packages/sheets/src/basics/__tests__/rangeMerge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import type { IRange } from '@univerjs/core';
import { describe, expect, it } from 'vitest';

import { rangeMerge } from '../rangeMerge';
import { rangeMerge, RangeMergeUtil } from '../rangeMerge';

const cellToRange = (row: number, col: number) =>
({ startColumn: col, endColumn: col, endRow: row, startRow: row }) as IRange;
Expand Down Expand Up @@ -171,31 +171,18 @@ describe('test rangeMerge', () => {
]);
});

// it('performance 10 rows and 250 columns', () => {
// // column sparse matrices have better performance
// const now = performance.now();
// rangeMerge([{ startRow: 0, endRow: 10, startColumn: 0, endColumn: 250 }]);
// const end = performance.now();
// const result = end - now;
// console.log(result, ':ms');
// expect(result).toBeLessThanOrEqual(5000); // 4002
// });

// it('performance 100 rows and 100 columns', () => {
// const now = performance.now();
// rangeMerge([{ startRow: 0, endRow: 100, startColumn: 0, endColumn: 100 }]);
// const end = performance.now();
// const result = end - now;
// console.log(result, ':ms');
// expect(result).toBeLessThanOrEqual(5000); // 4640
// });

// it('performance 1000 rows and 10 columns', () => {
// const now = performance.now();
// rangeMerge([{ startRow: 0, endRow: 1000, startColumn: 0, endColumn: 10 }]);
// const end = performance.now();
// const result = end - now;
// console.log(result, ':ms');
// expect(result).toBeLessThanOrEqual(4000); // 3483
// });
it('test rangeMergeUtil', () => {
const testRange: IRange[] = [cellToRange(2, 2), cellToRange(3, 2), cellToRange(4, 2)];
expect(new RangeMergeUtil().add(...testRange).merge()).toEqual([
{ startColumn: 2, endColumn: 2, startRow: 2, endRow: 4 },
]);
expect(new RangeMergeUtil().add(...testRange).subtract(cellToRange(4, 2)).merge()).toEqual([
{ startColumn: 2, endColumn: 2, startRow: 2, endRow: 3 },
]);
expect(new RangeMergeUtil().add(...testRange).subtract(cellToRange(3, 2)).merge()).toEqual([
{ startColumn: 2, endColumn: 2, startRow: 2, endRow: 2 },
{ startColumn: 2, endColumn: 2, startRow: 4, endRow: 4 },
]);
expect(new RangeMergeUtil().add(...testRange).subtract(...testRange).merge()).toEqual([]);
});
});
43 changes: 40 additions & 3 deletions packages/sheets/src/basics/rangeMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import type { IRange } from '@univerjs/core';
import { ObjectMatrix, Range } from '@univerjs/core';

const createTopMatrix = (ranges: IRange[]) => {
export const createTopMatrixFromRanges = (ranges: IRange[]) => {
const matrix = new ObjectMatrix<number>();
ranges.forEach((range) => {
Range.foreach(range, (row, col) => {
Expand All @@ -32,6 +32,16 @@ const createTopMatrix = (ranges: IRange[]) => {
});
return matrix;
};
export const createTopMatrixFromMatrix = (matrix: ObjectMatrix<1>) => {
const _matrix = matrix as ObjectMatrix<number>;
_matrix.forValue((row, col) => {
const theLastRowValue = matrix.getValue(row - 1, col);
if (theLastRowValue) {
_matrix.setValue(row, col, theLastRowValue + 1);
}
});
return _matrix;
};
const findMaximalRectangle = (topMatrix: ObjectMatrix<number>) => {
const res: {
area: number;
Expand Down Expand Up @@ -92,7 +102,7 @@ const filterLeftMatrix = (topMatrix: ObjectMatrix<number>, range: IRange) => {
return topMatrix;
};

const findAllRectangle = (topMatrix: ObjectMatrix<number>) => {
export const findAllRectangle = (topMatrix: ObjectMatrix<number>) => {
const resultList = [];
let result = findMaximalRectangle(topMatrix);
while (result.area > 0) {
Expand All @@ -113,6 +123,33 @@ const findAllRectangle = (topMatrix: ObjectMatrix<number>) => {
* @returns {IRange[]}
*/
export const rangeMerge = (ranges: IRange[]) => {
const topMatrix = createTopMatrix(ranges);
const topMatrix = createTopMatrixFromRanges(ranges);
return findAllRectangle(topMatrix);
};

export class RangeMergeUtil {
private _matrix = new ObjectMatrix<1>();
add(...ranges: IRange[]) {
ranges.forEach((range) => {
Range.foreach(range, (row, col) => {
this._matrix.setValue(row, col, 1);
});
});
return this;
}

subtract(...ranges: IRange[]) {
ranges.forEach((range) => {
Range.foreach(range, (row, col) => {
this._matrix.realDeleteValue(row, col);
});
});
return this;
}

merge() {
const topMatrix = createTopMatrixFromMatrix(this._matrix);
const ranges = findAllRectangle(topMatrix);
return ranges;
}
}
2 changes: 1 addition & 1 deletion packages/sheets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export {

// #region commands

export { rangeMerge } from './basics/rangeMerge';
export { rangeMerge, createTopMatrixFromRanges, createTopMatrixFromMatrix, findAllRectangle, RangeMergeUtil } from './basics/rangeMerge';
export { ClearSelectionAllCommand } from './commands/commands/clear-selection-all.command';
export { ClearSelectionContentCommand } from './commands/commands/clear-selection-content.command';
export { ClearSelectionFormatCommand } from './commands/commands/clear-selection-format.command';
Expand Down
Loading