-
-
Notifications
You must be signed in to change notification settings - Fork 414
Expand file tree
/
Copy pathindex.js
More file actions
61 lines (44 loc) · 1.77 KB
/
index.js
File metadata and controls
61 lines (44 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import macro from 'vtk.js/Sources/macros';
// ----------------------------------------------------------------------------
// vtkPriorityQueue methods
// ----------------------------------------------------------------------------
function vtkPriorityQueue(publicAPI, model) {
// Set our classname
model.classHierarchy.push('vtkPriorityQueue');
publicAPI.push = (priority, element) => {
// naive algo
const i = model.elements.findIndex((e) => e.priority > priority);
model.elements.splice(i, 0, { priority, element });
};
publicAPI.deleteById = (id) => {
model.elements = model.elements.filter(({ element }) => element !== id);
};
publicAPI.pop = () => {
if (model.elements.length > 0) {
const element = model.elements.reduce((prev, current) =>
prev.priority > current.priority ? prev : current
);
publicAPI.deleteById(element.element);
return element.element;
}
return null;
};
publicAPI.length = () => model.elements.length;
}
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
const DEFAULT_VALUES = {
elements: [],
};
// ----------------------------------------------------------------------------
export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);
// Build VTK API
macro.obj(publicAPI, model);
vtkPriorityQueue(publicAPI, model);
}
// ----------------------------------------------------------------------------
export const newInstance = macro.newInstance(extend, 'vtkPriorityQueue');
// ----------------------------------------------------------------------------
export default { newInstance, extend };