UNPKG

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