Skip to content
Open
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
29 changes: 29 additions & 0 deletions apps/web/docs/en/api/data-structure/array.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,32 @@ Delete any element in the array.
## 6. length

Return the length of array.

## 7. swap(param1, param2)

Swap two elements in an array.

**Required parameters**

`param1|param2`:Parameter 1 & 2, only supports array subscript | current element object.

**eg**: `arr.swap({ month: 'Tuesday', sales: 13 },{ month: 'Wednesday', sales: 25 })`.

## 8. shift

Pop the first element of an array, no parameters.

## 9. unshift(params)

Add data to the head of the queue.

**Required parameters 1**

`value`: value
**eg**: `arr.unshift(1)`

**Required parameters 2**

`object`:new add object

**eg**: `arr.unshift({ month: 'Monday1', sales: 55 })`, where month is category and sales is value.
31 changes: 31 additions & 0 deletions apps/web/docs/zh/api/data-structure/array.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,34 @@
## 6. length

返回数组长度,无参数。

## 7. swap(param1, param2)

交换数组中的两个元素

**必选参数**

`param1| param2`:参数 1,仅支持数组下标|当前元素对象。

**实例**: `arr.swap({ month: 'Tuesday', sales: 13 },{ month: 'Wednesday', sales: 25 })`.

## 8. shift

弹出数组的第一个元素,无参数。

## 9. unshift(params)

队头增加数据。

**必选参数 1**

`value`:数值
**实例**: `arr.unshift(1)`

**必选参数 2**

`category`:类别,如 x 轴标签或类别标签

`value`: 值,如 y 轴标签或值标签

**实例**: `arr.unshift({ month: 'Monday1', sales: 55 })`,其中 month 为 category,sales 为 value.
15 changes: 7 additions & 8 deletions apps/web/src/gallery/contanst.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* @Author: hh 1441211576@qq.com
* @Date: 2024-08-09 11:03:31
* @LastEditors: hh 1441211576@qq.com
* @LastEditTime: 2024-08-09 15:23:56
* @LastEditors: hjy 1441211576@qq.com
* @LastEditTime: 2025-01-12 20:18:42
* @FilePath: \algorithm-visualization\apps\website\src\gallery\contanst.ts
* @Description:
*
Expand All @@ -15,16 +15,14 @@ const bruteForce = [
for (let i = 0; i < n - 1; i++) {
for (let j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
// 交换元素
let temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
// swap
arr.swap(j, j + 1)
}
}
}
return arr
}

return defaultFunctionTemplate`
]
const dp = [
Expand Down Expand Up @@ -69,7 +67,8 @@ const divideConquer = [
}

return defaultFunctionTemplate`,
`const defaultFunctionTemplate = function () {
`// quickSort
const defaultFunctionTemplate = function () {
const nums = new AlVisArray([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])
qSort(nums, 0, nums.length - 1)
return nums;
Expand Down
42 changes: 36 additions & 6 deletions packages/data-structure/src/alvis/array/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: hjy 1441211576@qq.com
* @Date: 2024-07-01 14:22:28
* @LastEditors: hjy 1441211576@qq.com
* @LastEditTime: 2024-08-10 10:17:19
* @LastEditTime: 2025-01-12 20:40:24
* @FilePath: \algorithm-visualization\packages\data-structure\src\alvis\array\array.ts
* @Description: This is the monoarray
*/
Expand All @@ -14,6 +14,8 @@ import { getFields } from '../../utils/fileds.ts'
import { schemaBuilder } from '@alvis/schema/src/builder/schemaBuilder.ts'
import { dataStructureInitPropsMap } from '../init/initMap.ts'
import { handleInitData } from './initData.ts'
import { findEqualObject } from '../util.ts'

export class AlVisArray {
[key: string]: any
constructor(dataStructureType: string, config: IInitConfigurationProps) {
Expand Down Expand Up @@ -61,23 +63,25 @@ export class AlVisArray {

getProxy() {
const that = this
// debugger

return new Proxy(this, {
get(target, property: string) {
// 如果属性是索引,代理数组元素的访问
// if the property is index, get the element

if (!isNaN(Number(property))) {
return target.get(property)
}
// 如果属性是类的方法或属性,返回其值
// if the property is method or attribute, return its value
const value = target?.[property]

if (typeof value === 'function') {
// 返回方法的绑定版本,使其可以在代理上调用
// value bind this, to call this func in the proxy
return value.bind(that)
}
return value
},
set(target, property: string, value) {
set(target, property: string, value, receiver: any) {
debugger
if (!isNaN(Number(property))) {
// 代理数组元素的设置
target.set(+property, value)
Expand All @@ -96,10 +100,36 @@ export class AlVisArray {
this.schema.actions.push({ op: 'push', value: pushData })
}

unshift(unshiftParams: object | number) {
const unshiftData = checkValue(unshiftParams, this.data.length)
this.data.push(unshiftData)
this.schema.actions.push({ op: 'push', value: unshiftData })
}

pop() {
this.data.pop()
this.schema.actions.push({ op: 'pop' })
}
shift() {
this.data.shift()
this.schema.actions.push({ op: 'shift' })
}

swap(param1: number, param2: number)
swap(param1: object, param2: object)
swap(param1, param2) {
if (typeof param1 === 'number' && typeof param1 === typeof param2) {
;[this.data[param1], this.data[param2]] = [this.data[param2], this.data[param1]]
} else if (typeof param1 === 'object' && typeof param1 === typeof param2) {
const index1 = findEqualObject(this.data, param1)
const index2 = findEqualObject(this.data, param2)
;[this.data[index1], this.data[index2]] = [this.data[index2], this.data[index1]]
} else {
alert('swap function incorrectly called')
return
}
this.schema.actions.push({ op: 'swap', value: _.cloneDeep(this.data) })
}

set(params: object | number, value: string) {
const category = getFields(this.schema, 'category')
Expand Down
153 changes: 83 additions & 70 deletions packages/data-structure/src/alvis/array/function.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,83 @@
import { arrayOPMap } from '.'
import { OperationParams } from '../../utils/strategy'

/*
* @Author: hh 1441211576@qq.com
* @Date: 2024-07-11 20:35:09
* @LastEditors: hh 1441211576@qq.com
* @LastEditTime: 2024-08-05 19:12:58
* @FilePath: \algorithm-visualization\packages\data-structure\src\alvis\array\function.ts
* @Description:
*
*/
import _ from 'lodash'

const arrayOperations: arrayOPMap = {
push: (props: OperationParams): Object[] => {
const { data, modifyValue = {} } = props
data.push(modifyValue)
return data
},
pop: (props: OperationParams) => {
const { data } = props
data.pop()
return data
},
set: (props: OperationParams): object[] => {
const { data, modifyValue = {}, category = 'key' } = props
let index: number = -1
data.map((d: object, i) => {
if (
(d as { [key: string]: object })[category] ===
(modifyValue as { [key: string]: object })?.[category]
) {
index = i
return
}
})

if (index < 0) {
const { key, value } = modifyValue as { [key: string]: number }
data[key] = {
id: `${value}-${key}`,
key: key,
value: value
}
} else {
data[index] = modifyValue
}

return data
},
insert: (props: OperationParams): Object[] => {
const { data, modifyValue = {}, place = 0 } = props
data.splice(place, 0, modifyValue)
return data
},
delete: (props: OperationParams): Object[] => {
const { data, modifyValue = {} } = props
const type = Object.keys(modifyValue)[0]
let index = 0

data.map((obj, i) => {
if ((obj as { [key: string]: object })[type] === Object.values(modifyValue)[0]) index = i
})
data.splice(index, 1)
return data
}
}

export { arrayOperations }
import { arrayOPMap } from '.'
import { OperationParams } from '../../utils/strategy'

/*
* @Author: hh 1441211576@qq.com
* @Date: 2024-07-11 20:35:09
* @LastEditors: hjy 1441211576@qq.com
* @LastEditTime: 2025-01-12 20:39:14
* @FilePath: \algorithm-visualization\packages\data-structure\src\alvis\array\function.ts
* @Description:
*
*/
import _ from 'lodash'

const arrayOperations: arrayOPMap = {
push: (props: OperationParams): Object[] => {
const { data, modifyValue = {} } = props
data.push(modifyValue)
return data
},
pop: (props: OperationParams) => {
const { data } = props
data.pop()
return data
},
set: (props: OperationParams): object[] => {
const { data, modifyValue = {}, category = 'key' } = props
let index: number = -1
data.map((d: object, i) => {
if (
(d as { [key: string]: object })[category] ===
(modifyValue as { [key: string]: object })?.[category]
) {
index = i
return
}
})

if (index < 0) {
const { key, value } = modifyValue as { [key: string]: number }
data[key] = {
id: `${value}-${key}`,
key: key,
value: value
}
} else {
data[index] = modifyValue
}

return data
},
insert: (props: OperationParams): Object[] => {
const { data, modifyValue = {}, place = 0 } = props
data.splice(place, 0, modifyValue)
return data
},
delete: (props: OperationParams): Object[] => {
const { data, modifyValue = {} } = props
const type = Object.keys(modifyValue)[0]
let index = 0

data.map((obj, i) => {
if ((obj as { [key: string]: object })[type] === Object.values(modifyValue)[0]) index = i
})
data.splice(index, 1)
return data
},
swap: (props: OperationParams): Object[] => {
return props?.modifyValue as Object[]
},
shift: (props: OperationParams): Object[] => {
const { data } = props
data.shift()
return data
},
unshift: (props: OperationParams): Object[] => {
const { data, modifyValue = {} } = props
data.unshift(modifyValue)
return data
}
}

export { arrayOperations }
35 changes: 18 additions & 17 deletions packages/data-structure/src/alvis/array/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
/*
* @Author: hh 1441211576@qq.com
* @Date: 2024-07-11 20:35:28
* @LastEditors: hh 1441211576@qq.com
* @LastEditTime: 2024-08-05 19:13:11
* @FilePath: \algorithm-visualization\packages\data-structure\src\alvis\array\index.ts
* @Description:
*
*/

export type arrayOP = 'push' | 'pop' | 'set' | 'insert' | 'delete' //| 'length' | 'shift' | 'unshift' | 'swap'

export type arrayOPMap = Record<arrayOP, Function>

import { AlVisArray } from './array'
import { arrayOperations } from './function'
export { AlVisArray, arrayOperations }
/*
* @Author: hh 1441211576@qq.com
* @Date: 2024-07-11 20:35:28
* @LastEditors: hjy 1441211576@qq.com
* @LastEditTime: 2025-01-12 20:16:35
* @FilePath: \algorithm-visualization\packages\data-structure\src\alvis\array\index.ts
* @Description:
*
*/

// This describes the opereation will infulence the chart.
export type arrayOP = 'push' | 'pop' | 'set' | 'insert' | 'delete' | 'swap' | 'shift' | 'unshift'

export type arrayOPMap = Record<arrayOP, Function>

import { AlVisArray } from './array'
import { arrayOperations } from './function'
export { AlVisArray, arrayOperations }
Loading