1 | import { flatten, generalize, identify } from '../../utils/array'
|
2 | import { factory } from '../../utils/factory'
|
3 |
|
4 | const name = 'setDifference'
|
5 | const dependencies = ['typed', 'size', 'subset', 'compareNatural', 'Index', 'DenseMatrix']
|
6 |
|
7 | export const createSetDifference = factory(name, dependencies, ({ typed, size, subset, compareNatural, Index, DenseMatrix }) => {
|
8 | |
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | return typed(name, {
|
30 | 'Array | Matrix, Array | Matrix': function (a1, a2) {
|
31 | let result
|
32 | if (subset(size(a1), new Index(0)) === 0) {
|
33 | result = []
|
34 | } else if (subset(size(a2), new Index(0)) === 0) {
|
35 | return flatten(a1.toArray())
|
36 | } else {
|
37 | const b1 = identify(flatten(Array.isArray(a1) ? a1 : a1.toArray()).sort(compareNatural))
|
38 | const b2 = identify(flatten(Array.isArray(a2) ? a2 : a2.toArray()).sort(compareNatural))
|
39 | result = []
|
40 | let inb2
|
41 | for (let i = 0; i < b1.length; i++) {
|
42 | inb2 = false
|
43 | for (let j = 0; j < b2.length; j++) {
|
44 | if (compareNatural(b1[i].value, b2[j].value) === 0 && b1[i].identifier === b2[j].identifier) {
|
45 | inb2 = true
|
46 | break
|
47 | }
|
48 | }
|
49 | if (!inb2) {
|
50 | result.push(b1[i])
|
51 | }
|
52 | }
|
53 | }
|
54 |
|
55 | if (Array.isArray(a1) && Array.isArray(a2)) {
|
56 | return generalize(result)
|
57 | }
|
58 |
|
59 | return new DenseMatrix(generalize(result))
|
60 | }
|
61 | })
|
62 | })
|