Commit 3fc454e0 by qlintonger xeno

feat: 使用LCS对比+1234

parent 538dc862
...@@ -3,6 +3,7 @@ import { NewTreeModification, OldTreeModification, TreeRenderResult, TreeRenderR ...@@ -3,6 +3,7 @@ import { NewTreeModification, OldTreeModification, TreeRenderResult, TreeRenderR
import { md5 } from 'js-md5' import { md5 } from 'js-md5'
import { getUUID } from '../utils/uuid.ts' import { getUUID } from '../utils/uuid.ts'
import { dualCompare } from '@/lib/XMLProcessor/src/core/dualCompare.ts' import { dualCompare } from '@/lib/XMLProcessor/src/core/dualCompare.ts'
import {cloneDeep} from 'lodash'
// 定义 Processing 类,用于处理 XML 数据 // 定义 Processing 类,用于处理 XML 数据
export class Processing { export class Processing {
...@@ -39,6 +40,43 @@ export class Processing { ...@@ -39,6 +40,43 @@ export class Processing {
return resp return resp
} }
private buildTreeFromFlatted(
flatted: TreeRenderResultFlatted[],
): TreeRenderResult[] {
const result: TreeRenderResult[] = []
if (!flatted.length) {
return result
}
const flattedTreeSorted = flatted.toSorted((a, b) => {
return a.chained.length - b.chained.length;
});
const buildTree = (singleItem: TreeRenderResultFlatted, flattedDataSet: TreeRenderResultFlatted[]) => {
const singleItemRendered: TreeRenderResult = Object.assign({}, singleItem, {children: []});
const currentChainedLength = singleItemRendered.chained.length;
const maxChainedLength = Math.max(...flattedDataSet.map(a=>a.chained.length));
const currentChainedPrefix = singleItemRendered.chained.join('.');
for (let i = currentChainedLength + 1; i<= maxChainedLength; i++) {
const allSubItems = flattedDataSet.filter(a=>a.chained.length === i && a.chained.join('.').startsWith(currentChainedPrefix));
if (allSubItems.length) {
for (let j = flattedDataSet.length - 1; j>=0; j--) {
if (allSubItems.includes(flattedDataSet[j])) {
flattedDataSet.splice(j, 1)
}
}
for (const item of allSubItems) {
const subTree = buildTree(item, flattedDataSet);
singleItemRendered.children?.push(subTree)
}
}
}
return singleItemRendered
}
for (const item of flattedTreeSorted) {
result.push(buildTree(item, flattedTreeSorted))
}
return result
}
dualCompareFromString( dualCompareFromString(
xmlStringOld: string, xmlStringOld: string,
xmlStringNew: string, xmlStringNew: string,
...@@ -66,13 +104,18 @@ export class Processing { ...@@ -66,13 +104,18 @@ export class Processing {
const results = dualCompare(resultAFlatted, resultBFlatted).filter((a) => a.type !== 'same') const results = dualCompare(resultAFlatted, resultBFlatted).filter((a) => a.type !== 'same')
console.log('results here', results) console.log('after-compare', results)
// 分组构建树
const allDeletedTree = results.filter((item) => item.type === 'deleted').map(a=>a.item);
const allAddedTree = results.filter((item) => item.type === 'added').map(a=>a.item);
const dataForOld: OldTreeModification = { const dataForOld: OldTreeModification = {
Deleted: results.filter((item) => item.type === 'deleted').map((a) => a.item) Deleted: this.buildTreeFromFlatted(allDeletedTree)
} }
const dataForNew: NewTreeModification = { const dataForNew: NewTreeModification = {
Added: results.filter((item) => item.type === 'added').map((a) => a.item) Added: this.buildTreeFromFlatted(allAddedTree)
} }
return { return {
......
...@@ -9,7 +9,7 @@ export function dualCompare(oldItems: TreeRenderResultFlatted[], newItems: TreeR ...@@ -9,7 +9,7 @@ export function dualCompare(oldItems: TreeRenderResultFlatted[], newItems: TreeR
for (let i = 1; i <= oldItemsCount; i++) { for (let i = 1; i <= oldItemsCount; i++) {
for (let j = 1; j <= newItemsCount; j++) { for (let j = 1; j <= newItemsCount; j++) {
if (oldItems[i - 1].hash === newItems[j - 1].hash && oldItems[i - 1].label === newItems[j - 1].label) { if (oldItems[i - 1].hash === newItems[j - 1].hash) {
dp[i][j] = dp[i - 1][j - 1] + 1 dp[i][j] = dp[i - 1][j - 1] + 1
} else { } else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]) dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1])
...@@ -21,7 +21,7 @@ export function dualCompare(oldItems: TreeRenderResultFlatted[], newItems: TreeR ...@@ -21,7 +21,7 @@ export function dualCompare(oldItems: TreeRenderResultFlatted[], newItems: TreeR
j = newItemsCount j = newItemsCount
const result: Array<{ type: 'same' | 'deleted' | 'added' | 'changedOld' | 'changedNew'; item: TreeRenderResultFlatted }> = [] const result: Array<{ type: 'same' | 'deleted' | 'added' | 'changedOld' | 'changedNew'; item: TreeRenderResultFlatted }> = []
while (i > 0 && j > 0) { while (i > 0 && j > 0) {
if (oldItems[i - 1].hash === newItems[j - 1].hash && oldItems[i - 1].label === newItems[j - 1].label) { if (oldItems[i - 1].hash === newItems[j - 1].hash) {
// result.unshift({ type: 'same', item: cloneDeep(oldItems[i - 1]) }) // result.unshift({ type: 'same', item: cloneDeep(oldItems[i - 1]) })
i-- i--
j-- j--
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment