UNPKG

3.77 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.createSpaClass = void 0;
7
8var _factory = require("../../utils/factory");
9
10var name = 'Spa';
11var dependencies = ['addScalar', 'equalScalar', 'FibonacciHeap'];
12var createSpaClass =
13/* #__PURE__ */
14(0, _factory.factory)(name, dependencies, function (_ref) {
15 var addScalar = _ref.addScalar,
16 equalScalar = _ref.equalScalar,
17 FibonacciHeap = _ref.FibonacciHeap;
18
19 /**
20 * An ordered Sparse Accumulator is a representation for a sparse vector that includes a dense array
21 * of the vector elements and an ordered list of non-zero elements.
22 */
23 function Spa() {
24 if (!(this instanceof Spa)) {
25 throw new SyntaxError('Constructor must be called with the new operator');
26 } // allocate vector, TODO use typed arrays
27
28
29 this._values = [];
30 this._heap = new FibonacciHeap();
31 }
32 /**
33 * Attach type information
34 */
35
36
37 Spa.prototype.type = 'Spa';
38 Spa.prototype.isSpa = true;
39 /**
40 * Set the value for index i.
41 *
42 * @param {number} i The index
43 * @param {number | BigNumber | Complex} The value at index i
44 */
45
46 Spa.prototype.set = function (i, v) {
47 // check we have a value @ i
48 if (!this._values[i]) {
49 // insert in heap
50 var node = this._heap.insert(i, v); // set the value @ i
51
52
53 this._values[i] = node;
54 } else {
55 // update the value @ i
56 this._values[i].value = v;
57 }
58 };
59
60 Spa.prototype.get = function (i) {
61 var node = this._values[i];
62
63 if (node) {
64 return node.value;
65 }
66
67 return 0;
68 };
69
70 Spa.prototype.accumulate = function (i, v) {
71 // node @ i
72 var node = this._values[i];
73
74 if (!node) {
75 // insert in heap
76 node = this._heap.insert(i, v); // initialize value
77
78 this._values[i] = node;
79 } else {
80 // accumulate value
81 node.value = addScalar(node.value, v);
82 }
83 };
84
85 Spa.prototype.forEach = function (from, to, callback) {
86 // references
87 var heap = this._heap;
88 var values = this._values; // nodes
89
90 var nodes = []; // node with minimum key, save it
91
92 var node = heap.extractMinimum();
93
94 if (node) {
95 nodes.push(node);
96 } // extract nodes from heap (ordered)
97
98
99 while (node && node.key <= to) {
100 // check it is in range
101 if (node.key >= from) {
102 // check value is not zero
103 if (!equalScalar(node.value, 0)) {
104 // invoke callback
105 callback(node.key, node.value, this);
106 }
107 } // extract next node, save it
108
109
110 node = heap.extractMinimum();
111
112 if (node) {
113 nodes.push(node);
114 }
115 } // reinsert all nodes in heap
116
117
118 for (var i = 0; i < nodes.length; i++) {
119 // current node
120 var n = nodes[i]; // insert node in heap
121
122 node = heap.insert(n.key, n.value); // update values
123
124 values[node.key] = node;
125 }
126 };
127
128 Spa.prototype.swap = function (i, j) {
129 // node @ i and j
130 var nodei = this._values[i];
131 var nodej = this._values[j]; // check we need to insert indeces
132
133 if (!nodei && nodej) {
134 // insert in heap
135 nodei = this._heap.insert(i, nodej.value); // remove from heap
136
137 this._heap.remove(nodej); // set values
138
139
140 this._values[i] = nodei;
141 this._values[j] = undefined;
142 } else if (nodei && !nodej) {
143 // insert in heap
144 nodej = this._heap.insert(j, nodei.value); // remove from heap
145
146 this._heap.remove(nodei); // set values
147
148
149 this._values[j] = nodej;
150 this._values[i] = undefined;
151 } else if (nodei && nodej) {
152 // swap values
153 var v = nodei.value;
154 nodei.value = nodej.value;
155 nodej.value = v;
156 }
157 };
158
159 return Spa;
160}, {
161 isClass: true
162});
163exports.createSpaClass = createSpaClass;
\No newline at end of file