Skip to content

Commit

Permalink
refactor(formula): tree id string to number
Browse files Browse the repository at this point in the history
  • Loading branch information
DR-Univer authored and Dushusir committed Oct 26, 2024
1 parent ddfb8fa commit f243adf
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 89 deletions.
30 changes: 16 additions & 14 deletions packages/core/src/shared/r-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ import type { IUnitRange } from '../sheets/typedef';
import KDBush from 'kdbush';
import RBush from 'rbush';

type StringOrNumber = string | number;

export interface IRTreeItem extends IUnitRange {
id: string;
id: StringOrNumber;
}

interface IRBushItem extends BBox {
id: string;
id: StringOrNumber;
}

interface IRdTreeItem {
x: number;
y: number;
ids: Set<string>;
ids: Set<StringOrNumber>;
}

export interface IRTreeData {
Expand All @@ -43,7 +45,7 @@ export class RTree {
private _tree: Map<string, Map<string, RBush<IRBushItem>>> = new Map();

// unitId -> subUnitId -> row -> column -> ids
private _oneCellCache = new Map<string, Map<string, Map<number, Map<number, Set<string>>>>>();
private _oneCellCache = new Map<string, Map<string, Map<number, Map<number, Set<StringOrNumber>>>>>();

private _kdTree: Map<string, Map<string, { tree: KDBush; items: IRdTreeItem[] } | undefined>> = new Map();

Expand All @@ -68,7 +70,7 @@ export class RTree {
return this._tree.get(unitId)!.get(subUnitId)!;
}

private _getOneCellCache(unitId: string, subUnitId: string, row: number, column: number): Set<string> {
private _getOneCellCache(unitId: string, subUnitId: string, row: number, column: number): Set<StringOrNumber> {
if (!this._oneCellCache.has(unitId)) {
this._oneCellCache.set(unitId, new Map());
}
Expand All @@ -85,7 +87,7 @@ export class RTree {
return this._oneCellCache.get(unitId)!.get(subUnitId)!.get(row)!.get(column)!;
}

private _removeOneCellCache(unitId: string, subUnitId: string, row: number, column: number, id: string) {
private _removeOneCellCache(unitId: string, subUnitId: string, row: number, column: number, id: StringOrNumber) {
const unitCache = this._oneCellCache.get(unitId);
if (!unitCache) return;

Expand All @@ -101,11 +103,11 @@ export class RTree {
cellCache.delete(id);
}

private _insertOneCellCache(unitId: string, subUnitId: string, row: number, column: number, id: string) {
private _insertOneCellCache(unitId: string, subUnitId: string, row: number, column: number, id: StringOrNumber) {
this._getOneCellCache(unitId, subUnitId, row, column).add(id);
}

private _getRdTreeItems(map: Map<number, Map<number, Set<string>>>) {
private _getRdTreeItems(map: Map<number, Map<number, Set<StringOrNumber>>>) {
const items: IRdTreeItem[] = [];

for (const [y, innerMap] of map) {
Expand All @@ -121,7 +123,7 @@ export class RTree {
return items;
}

private _searchByOneCellCache(search: IUnitRange): string[] {
private _searchByOneCellCache(search: IUnitRange): StringOrNumber[] {
const { unitId, sheetId: subUnitId, range } = search;
const { startRow, startColumn, endRow, endColumn } = range;
const searchObject = this._kdTree.get(unitId)?.get(subUnitId);
Expand All @@ -133,7 +135,7 @@ export class RTree {

const indexes = tree.range(startColumn, startRow, endColumn, endRow);

const result: string[] = [];
const result: StringOrNumber[] = [];

for (const index of indexes) {
const item = items[index];
Expand Down Expand Up @@ -221,10 +223,10 @@ export class RTree {
}
}

search(search: IUnitRange): string[] {
search(search: IUnitRange): StringOrNumber[] {
const { unitId, sheetId: subUnitId, range } = search;

const results: string[] = [];
const results: StringOrNumber[] = [];

if (this._enableOneCellCache && this._enableOneCellCache) {
const oneCellResults = this._searchByOneCellCache(search);
Expand Down Expand Up @@ -252,8 +254,8 @@ export class RTree {
return results;
}

bulkSearch(searchList: IUnitRange[]): Set<string> {
const result = new Set<string>();
bulkSearch(searchList: IUnitRange[]): Set<StringOrNumber> {
const result = new Set<StringOrNumber>();
for (const search of searchList) {
const items = this.search(search);
for (const item of items) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { NodeType } from './node-type';
interface IAstNodeNodeJson {
token: string;
children?: IAstNodeNodeJson[];
nodeType: string;
nodeType: number;
}

export type LambdaPrivacyVarType = Map<string, Nullable<BaseAstNode>>;
Expand Down
26 changes: 13 additions & 13 deletions packages/engine-formula/src/engine/ast-node/node-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
*/

export enum NodeType {
REFERENCE = 'ReferenceNode',
VALUE = 'ValueNode',
OPERATOR = 'OperatorNode',
FUNCTION = 'FunctionNode',
LAMBDA = 'LambdaNode',
LAMBDA_PARAMETER = 'LambdaNodeParameter',
ERROR = 'ErrorNode',
BASE = 'Base',
ROOT = 'Root',
UNION = 'UnionNode',
PREFIX = 'PrefixNode',
SUFFIX = 'SuffixNode',
NULL = 'NullNode',
REFERENCE = 1,
VALUE = 2,
OPERATOR = 3,
FUNCTION = 4,
LAMBDA = 5,
LAMBDA_PARAMETER = 6,
ERROR = 7,
BASE = 8,
ROOT = 9,
UNION = 10,
PREFIX = 11,
SUFFIX = 12,
NULL = 13,
}

export const NODE_ORDER_MAP = new Map([
Expand Down
17 changes: 6 additions & 11 deletions packages/engine-formula/src/engine/dependency/dependency-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import type { IFormulaDirtyData } from '../../services/current-data.service';
import type { IAllRuntimeData } from '../../services/runtime.service';

import type { IExecuteAstNodeData } from '../utils/ast-node-tool';
import { generateRandomId } from '@univerjs/core';

export enum FDtreeStateType {
DEFAULT,
Expand All @@ -44,13 +43,13 @@ export enum FDtreeStateType {
* is used to determine the order of formula calculations.
*/
export class FormulaDependencyTree {
treeId: string = '';
treeId: number = -1;

nodeData: Nullable<IExecuteAstNodeData>;

children: Set<string> = new Set();
children: Set<number> = new Set();

parents: Set<string> = new Set();
parents: Set<number> = new Set();

formula: string = '';

Expand All @@ -74,12 +73,8 @@ export class FormulaDependencyTree {

isCache: boolean = false;

constructor(treeId?: string) {
if (treeId != null) {
this.treeId = treeId;
} else {
this.treeId = generateRandomId(8);
}
constructor(treeId: number) {
this.treeId = treeId;
}

toJson() {
Expand Down Expand Up @@ -227,7 +222,7 @@ export class FormulaDependencyTree {
this.rangeList.push(...ranges);
}

hasChildren(treeId: string) {
hasChildren(treeId: number) {
return this.children.has(treeId);
}

Expand Down
20 changes: 10 additions & 10 deletions packages/engine-formula/src/engine/dependency/formula-dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ interface IFeatureFormulaParam {
featureId: string;
}

function generateRandomDependencyTreeId(dependencyManagerService: IDependencyManagerService): string {
function generateRandomDependencyTreeId(dependencyManagerService: IDependencyManagerService): number {
const idNum = dependencyManagerService.getLastTreeId() || 0;
return idNum.toString();
return idNum;
}

export class FormulaDependencyGenerator extends Disposable {
Expand Down Expand Up @@ -132,9 +132,9 @@ export class FormulaDependencyGenerator extends Disposable {
}

private _isCyclicUtil(
treeId: string,
visited: Set<string>,
recursionStack: Set<string>
treeId: number,
visited: Set<number>,
recursionStack: Set<number>
) {
const node = this._dependencyManagerService.getTreeById(treeId);
if (node == null) {
Expand All @@ -160,8 +160,8 @@ export class FormulaDependencyGenerator extends Disposable {
}

private _checkIsCycleDependency(treeList: FormulaDependencyTree[]) {
const visited = new Set<string>();
const recursionStack = new Set<string>();
const visited = new Set<number>();
const recursionStack = new Set<number>();

// Call the recursive helper function to detect cycle in different
// DFS trees
Expand Down Expand Up @@ -727,7 +727,7 @@ export class FormulaDependencyGenerator extends Disposable {
featureTree = this._getFeatureFormulaTree(featureId, params);
newTreeList.push(featureTree);
}
featureTree.parents = new Set<string>();
featureTree.parents = new Set<number>();
intersectTrees.forEach((tree) => {
if (tree.hasChildren(featureTree!.treeId)) {
return;
Expand All @@ -748,7 +748,7 @@ export class FormulaDependencyGenerator extends Disposable {
const featureMap = this._featureCalculationManagerService.getReferenceExecutorMap();

newTreeList.forEach((tree) => {
const newChildren = new Set<string>();
const newChildren = new Set<number>();
for (const childTreeId of tree.children) {
const child = this._dependencyManagerService.getTreeById(childTreeId);
if (!child) {
Expand All @@ -762,7 +762,7 @@ export class FormulaDependencyGenerator extends Disposable {
}
tree.children = newChildren;

const newParents = new Set<string>();
const newParents = new Set<number>();
for (const parentTreeId of tree.parents) {
const parent = this._dependencyManagerService.getTreeById(parentTreeId);
if (!parent) {
Expand Down
Loading

0 comments on commit f243adf

Please sign in to comment.