1 | 'use strict';
|
2 |
|
3 | var flatten = require('../../utils/array').flatten;
|
4 | var identify = require('../../utils/array').identify;
|
5 | var generalize = require('../../utils/array').generalize;
|
6 |
|
7 | function factory(type, config, load, typed) {
|
8 | var MatrixIndex = load(require('../../type/matrix/MatrixIndex'));
|
9 | var DenseMatrix = load(require('../../type/matrix/DenseMatrix'));
|
10 | var size = load(require('../matrix/size'));
|
11 | var subset = load(require('../matrix/subset'));
|
12 | var compareNatural = load(require('../relational/compareNatural'));
|
13 |
|
14 | |
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | var setDifference = typed('setDifference', {
|
36 | 'Array | Matrix, Array | Matrix': function ArrayMatrixArrayMatrix(a1, a2) {
|
37 | var result = void 0;
|
38 | if (subset(size(a1), new MatrixIndex(0)) === 0) {
|
39 |
|
40 | result = [];
|
41 | } else if (subset(size(a2), new MatrixIndex(0)) === 0) {
|
42 |
|
43 | return flatten(a1.toArray());
|
44 | } else {
|
45 | var b1 = identify(flatten(Array.isArray(a1) ? a1 : a1.toArray()).sort(compareNatural));
|
46 | var b2 = identify(flatten(Array.isArray(a2) ? a2 : a2.toArray()).sort(compareNatural));
|
47 | result = [];
|
48 | var inb2 = void 0;
|
49 | for (var i = 0; i < b1.length; i++) {
|
50 | inb2 = false;
|
51 | for (var j = 0; j < b2.length; j++) {
|
52 | if (compareNatural(b1[i].value, b2[j].value) === 0 && b1[i].identifier === b2[j].identifier) {
|
53 |
|
54 | inb2 = true;
|
55 | break;
|
56 | }
|
57 | }
|
58 | if (!inb2) {
|
59 | result.push(b1[i]);
|
60 | }
|
61 | }
|
62 | }
|
63 |
|
64 | if (Array.isArray(a1) && Array.isArray(a2)) {
|
65 | return generalize(result);
|
66 | }
|
67 |
|
68 | return new DenseMatrix(generalize(result));
|
69 | }
|
70 | });
|
71 |
|
72 | return setDifference;
|
73 | }
|
74 |
|
75 | exports.name = 'setDifference';
|
76 | exports.factory = factory; |
\ | No newline at end of file |