Skip to content

Commit 4496e20

Browse files
author
jiangtao.yang
committed
feat: add compare
1 parent 4a04e83 commit 4496e20

3 files changed

Lines changed: 65 additions & 6 deletions

File tree

apps/dsv/src/Chart.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ export const Chart: FC = () => {
2929
if (action.type === 'set') {
3030
arrayBar.set(action.args[0], action.args[1]);
3131
}
32+
if (action.type === 'get') {
33+
arrayBar.get(action.args[0]);
34+
}
35+
if (action.type === 'compare') {
36+
arrayBar.compare(action.args[0], action.args[1], action.args[2]);
37+
}
3238
if (action.type === 'swap') {
3339
arrayBar.swap(action.args[0], action.args[1]);
3440
}

apps/dsv/src/CodeEditor/CodeEditor.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@ import { useDsv } from '../model';
55
export const CodeEditor = () => {
66
const setSchema = useDsv((state) => state.setSchema);
77

8-
const [code, setCode] = useState(`Array.prototype.swap = function(i, j){
9-
const temp = this[i]
10-
this[i] = this[j]
11-
this[j] = temp
8+
const ArrayProtoType = `
9+
Array.prototype.swap = function(i, j){
10+
const temp = this[i]
11+
this[i] = this[j]
12+
this[j] = temp
1213
}
1314
15+
Array.prototype.compare = function(i, j, fn) {
16+
return fn(this[i], this[j]);
17+
};
18+
`;
19+
const [code, setCode] = useState(`${ArrayProtoType}
1420
const bubbleSort = (arr) => {
1521
for (let i = 0; i < arr.length; i++) {
1622
for (let j = 0; j < arr.length - i - 1; j++) {
17-
if (arr[j] > arr[j + 1]) {
23+
if (arr.compare(j, j + 1, (a, b) => a > b)) {
1824
arr.swap(j, j+1)
1925
}
2026
}

packages/data-structure/src/data-structures/array-bar/array-bar.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,55 @@ export class ArrayBar<T> {
229229
return this;
230230
}
231231

232+
compare(i: number, j: number, fn: (a: T, b: T) => boolean) {
233+
const id = this._id;
234+
const dataId = this._dataId;
235+
const interval = this._interval;
236+
const length = this._actions.length;
237+
const valueI = this._data[i];
238+
const valueJ = this._data[j];
239+
const compareRes = fn(valueI.value, valueJ.value);
240+
const actionI = {
241+
action: "highlight",
242+
startTime: interval * length,
243+
payload: {
244+
id: dataId,
245+
value: valueI,
246+
animation: {
247+
duration: interval,
248+
},
249+
style: {
250+
fill: "red",
251+
},
252+
},
253+
} as IActionSpec;
254+
const actionJ = {
255+
action: "highlight",
256+
startTime: interval * length,
257+
payload: {
258+
id: dataId,
259+
value: valueJ,
260+
animation: {
261+
duration: interval,
262+
},
263+
style: {
264+
fill: "red",
265+
},
266+
},
267+
} as IActionSpec;
268+
if (compareRes) {
269+
actionI.payload.style.fill = "green";
270+
} else {
271+
actionJ.payload.style.fill = "green";
272+
}
273+
this._actions.push({
274+
characterId: id,
275+
characterActions: [actionI, actionJ],
276+
});
277+
return this;
278+
}
232279
get(index: number) {
233-
const value = this._data[index].value;
280+
const value = this._data[index];
234281
const action = {
235282
action: "highlight",
236283
startTime: this._interval * this._actions.length,

0 commit comments

Comments
 (0)