1 | 'use strict'
|
2 |
|
3 | const clone = require('../../utils/object').clone
|
4 | const isInteger = require('../../utils/number').isInteger
|
5 |
|
6 | function factory (type) {
|
7 | |
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function Index (ranges) {
|
28 | if (!(this instanceof Index)) {
|
29 | throw new SyntaxError('Constructor must be called with the new operator')
|
30 | }
|
31 |
|
32 | this._dimensions = []
|
33 | this._isScalar = true
|
34 |
|
35 | for (let i = 0, ii = arguments.length; i < ii; i++) {
|
36 | const arg = arguments[i]
|
37 |
|
38 | if (type.isRange(arg)) {
|
39 | this._dimensions.push(arg)
|
40 | this._isScalar = false
|
41 | } else if (Array.isArray(arg) || type.isMatrix(arg)) {
|
42 |
|
43 | const m = _createImmutableMatrix(arg.valueOf())
|
44 | this._dimensions.push(m)
|
45 |
|
46 | const size = m.size()
|
47 |
|
48 | if (size.length !== 1 || size[0] !== 1) {
|
49 | this._isScalar = false
|
50 | }
|
51 | } else if (typeof arg === 'number') {
|
52 | this._dimensions.push(_createImmutableMatrix([arg]))
|
53 | } else if (typeof arg === 'string') {
|
54 |
|
55 | this._dimensions.push(arg)
|
56 | } else {
|
57 | throw new TypeError('Dimension must be an Array, Matrix, number, string, or Range')
|
58 | }
|
59 |
|
60 | }
|
61 | }
|
62 |
|
63 | |
64 |
|
65 |
|
66 | Index.prototype.type = 'Index'
|
67 | Index.prototype.isIndex = true
|
68 |
|
69 | function _createImmutableMatrix (arg) {
|
70 |
|
71 | for (let i = 0, l = arg.length; i < l; i++) {
|
72 | if (typeof arg[i] !== 'number' || !isInteger(arg[i])) {
|
73 | throw new TypeError('Index parameters must be positive integer numbers')
|
74 | }
|
75 | }
|
76 |
|
77 | return new type.ImmutableDenseMatrix(arg)
|
78 | }
|
79 |
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 | Index.prototype.clone = function () {
|
86 | const index = new Index()
|
87 | index._dimensions = clone(this._dimensions)
|
88 | index._isScalar = this._isScalar
|
89 | return index
|
90 | }
|
91 |
|
92 | |
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | Index.create = function (ranges) {
|
100 | const index = new Index()
|
101 | Index.apply(index, ranges)
|
102 | return index
|
103 | }
|
104 |
|
105 | |
106 |
|
107 |
|
108 |
|
109 |
|
110 | Index.prototype.size = function () {
|
111 | const size = []
|
112 |
|
113 | for (let i = 0, ii = this._dimensions.length; i < ii; i++) {
|
114 | const d = this._dimensions[i]
|
115 | size[i] = (typeof d === 'string') ? 1 : d.size()[0]
|
116 | }
|
117 |
|
118 | return size
|
119 | }
|
120 |
|
121 | |
122 |
|
123 |
|
124 |
|
125 |
|
126 | Index.prototype.max = function () {
|
127 | const values = []
|
128 |
|
129 | for (let i = 0, ii = this._dimensions.length; i < ii; i++) {
|
130 | const range = this._dimensions[i]
|
131 | values[i] = (typeof range === 'string') ? range : range.max()
|
132 | }
|
133 |
|
134 | return values
|
135 | }
|
136 |
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 | Index.prototype.min = function () {
|
143 | const values = []
|
144 |
|
145 | for (let i = 0, ii = this._dimensions.length; i < ii; i++) {
|
146 | const range = this._dimensions[i]
|
147 | values[i] = (typeof range === 'string') ? range : range.min()
|
148 | }
|
149 |
|
150 | return values
|
151 | }
|
152 |
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | Index.prototype.forEach = function (callback) {
|
161 | for (let i = 0, ii = this._dimensions.length; i < ii; i++) {
|
162 | callback(this._dimensions[i], i, this)
|
163 | }
|
164 | }
|
165 |
|
166 | |
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | Index.prototype.dimension = function (dim) {
|
173 | return this._dimensions[dim] || null
|
174 | }
|
175 |
|
176 | |
177 |
|
178 |
|
179 |
|
180 | Index.prototype.isObjectProperty = function () {
|
181 | return this._dimensions.length === 1 && typeof this._dimensions[0] === 'string'
|
182 | }
|
183 |
|
184 | |
185 |
|
186 |
|
187 |
|
188 |
|
189 | Index.prototype.getObjectProperty = function () {
|
190 | return this.isObjectProperty() ? this._dimensions[0] : null
|
191 | }
|
192 |
|
193 | |
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 | Index.prototype.isScalar = function () {
|
202 | return this._isScalar
|
203 | }
|
204 |
|
205 | |
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | Index.prototype.toArray = function () {
|
212 | const array = []
|
213 | for (let i = 0, ii = this._dimensions.length; i < ii; i++) {
|
214 | const dimension = this._dimensions[i]
|
215 | array.push((typeof dimension === 'string') ? dimension : dimension.toArray())
|
216 | }
|
217 | return array
|
218 | }
|
219 |
|
220 | |
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | Index.prototype.valueOf = Index.prototype.toArray
|
227 |
|
228 | |
229 |
|
230 |
|
231 |
|
232 |
|
233 | Index.prototype.toString = function () {
|
234 | const strings = []
|
235 |
|
236 | for (let i = 0, ii = this._dimensions.length; i < ii; i++) {
|
237 | const dimension = this._dimensions[i]
|
238 | if (typeof dimension === 'string') {
|
239 | strings.push(JSON.stringify(dimension))
|
240 | } else {
|
241 | strings.push(dimension.toString())
|
242 | }
|
243 | }
|
244 |
|
245 | return '[' + strings.join(', ') + ']'
|
246 | }
|
247 |
|
248 | |
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | Index.prototype.toJSON = function () {
|
255 | return {
|
256 | mathjs: 'Index',
|
257 | dimensions: this._dimensions
|
258 | }
|
259 | }
|
260 |
|
261 | |
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 | Index.fromJSON = function (json) {
|
269 | return Index.create(json.dimensions)
|
270 | }
|
271 |
|
272 | return Index
|
273 | }
|
274 |
|
275 | exports.name = 'Index'
|
276 | exports.path = 'type'
|
277 | exports.factory = factory
|