1 | 'use strict'
|
2 |
|
3 | const flatten = require('../../utils/array').flatten
|
4 | const identify = require('../../utils/array').identify
|
5 | const generalize = require('../../utils/array').generalize
|
6 |
|
7 | function factory (type, config, load, typed) {
|
8 | const MatrixIndex = load(require('../../type/matrix/MatrixIndex'))
|
9 | const DenseMatrix = load(require('../../type/matrix/DenseMatrix'))
|
10 | const size = load(require('../matrix/size'))
|
11 | const subset = load(require('../matrix/subset'))
|
12 | const 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 | const setDifference = typed('setDifference', {
|
36 | 'Array | Matrix, Array | Matrix': function (a1, a2) {
|
37 | let result
|
38 | if (subset(size(a1), new MatrixIndex(0)) === 0) {
|
39 | result = []
|
40 | } else if (subset(size(a2), new MatrixIndex(0)) === 0) {
|
41 | return flatten(a1.toArray())
|
42 | } else {
|
43 | const b1 = identify(flatten(Array.isArray(a1) ? a1 : a1.toArray()).sort(compareNatural))
|
44 | const b2 = identify(flatten(Array.isArray(a2) ? a2 : a2.toArray()).sort(compareNatural))
|
45 | result = []
|
46 | let inb2
|
47 | for (let i = 0; i < b1.length; i++) {
|
48 | inb2 = false
|
49 | for (let j = 0; j < b2.length; j++) {
|
50 | if (compareNatural(b1[i].value, b2[j].value) === 0 && b1[i].identifier === b2[j].identifier) {
|
51 | inb2 = true
|
52 | break
|
53 | }
|
54 | }
|
55 | if (!inb2) {
|
56 | result.push(b1[i])
|
57 | }
|
58 | }
|
59 | }
|
60 |
|
61 | if (Array.isArray(a1) && Array.isArray(a2)) {
|
62 | return generalize(result)
|
63 | }
|
64 |
|
65 | return new DenseMatrix(generalize(result))
|
66 | }
|
67 | })
|
68 |
|
69 | return setDifference
|
70 | }
|
71 |
|
72 | exports.name = 'setDifference'
|
73 | exports.factory = factory
|