StructV2/demo/dataStruct/ChainHashTable.js

182 lines
4.5 KiB
JavaScript
Raw Normal View History

2021-04-16 07:38:52 +00:00
/**
2021-04-20 11:12:41 +00:00
* 连地址哈希表
2021-04-16 07:38:52 +00:00
*/
2021-04-20 11:12:41 +00:00
class ChainHashTable extends Engine {
2021-04-16 07:38:52 +00:00
defineOptions() {
return {
element: {
2021-04-20 11:12:41 +00:00
head: {
type: 'two-cell-node',
2021-04-16 07:38:52 +00:00
label: '[id]',
2021-04-20 11:12:41 +00:00
size: [70, 40],
2021-04-16 07:38:52 +00:00
style: {
stroke: '#333',
fill: '#b83b5e'
}
},
node: {
type: 'link-list-node',
label: '[id]',
size: [60, 30],
style: {
stroke: '#333',
fill: '#b83b5e'
}
}
},
link: {
2021-04-20 11:12:41 +00:00
start: {
type: 'line',
sourceAnchor: 1,
targetAnchor: 0,
style: {
stroke: '#333',
endArrow: {
path: G6.Arrow.triangle(8, 6, 0),
fill: '#333'
},
startArrow: {
path: G6.Arrow.circle(2, -1),
fill: '#333'
}
}
},
next: {
2021-04-16 07:38:52 +00:00
type: 'line',
sourceAnchor: 1,
targetAnchor: 0,
style: {
stroke: '#333',
endArrow: {
path: G6.Arrow.triangle(8, 6, 0),
fill: '#333'
},
startArrow: {
path: G6.Arrow.circle(2, -1),
fill: '#333'
}
}
}
},
pointer: {
external: {
2021-04-20 11:12:41 +00:00
offset: 8,
2021-04-16 07:38:52 +00:00
style: {
fill: '#f08a5d'
}
}
},
layout: {
xInterval: 50,
yInterval: 50
2021-04-20 11:12:41 +00:00
},
interaction: {
dragNode: ['node']
2021-04-16 07:38:52 +00:00
}
};
}
/**
* 对子树进行递归布局
* @param node
* @param parent
*/
layoutItem(node, prev, layoutOptions) {
if(!node) {
return null;
}
let width = node.get('size')[0];
if(prev) {
node.set('y', prev.get('y'));
node.set('x', prev.get('x') + layoutOptions.xInterval + width);
}
if(node.next) {
this.layoutItem(node.next, node, layoutOptions);
}
}
layout(elements, layoutOptions) {
2021-05-17 06:02:19 +00:00
let headNode = elements.filter(item => item.type === 'head');
2021-04-20 11:12:41 +00:00
for(let i = 0; i < headNode.length; i++) {
let node = headNode[i],
height = node.get('size')[1];
2021-04-16 07:38:52 +00:00
2021-04-20 11:12:41 +00:00
node.set('y', node.get('y') + i * height);
2021-04-16 07:38:52 +00:00
2021-04-20 11:12:41 +00:00
if(node.start) {
let y = node.get('y') + height - node.start.get('size')[1],
x = layoutOptions.xInterval * 2.5;
node.start.set({ x, y });
this.layoutItem(node.start, null, layoutOptions);
}
2021-04-16 07:38:52 +00:00
}
}
2021-04-20 11:12:41 +00:00
2021-04-16 07:38:52 +00:00
}
2021-05-17 06:02:19 +00:00
const CHT = function(container, options) {
2021-04-16 07:38:52 +00:00
return{
2021-05-17 06:02:19 +00:00
engine: new ChainHashTable(container, options),
2021-04-20 11:12:41 +00:00
data: [
{
head: [{
id: 0,
start: 'node#0'
}, {
id: 2,
start: 'node#2'
}],
node: [{
id: 0,
next: 1
}, {
id: 1
},{
id: 2,
next: 3
}, {
id: 3
}]
},
{
2021-05-17 06:02:19 +00:00
head: [{
id: 0,
start: 'node#0'
}, {
id: 2,
start: 'node#2'
}, {
id: 3,
start: 'node#4'
}],
node: [{
id: 0,
next: 1
}, {
id: 1
},{
id: 2
},{
id: 4
}]
2021-04-20 11:12:41 +00:00
}
]
2021-04-16 07:38:52 +00:00
}
};