1 | import { flatten } from '../../utils/array'
|
2 | import { factory } from '../../utils/factory'
|
3 |
|
4 | const name = 'setDistinct'
|
5 | const dependencies = ['typed', 'size', 'subset', 'compareNatural', 'Index', 'DenseMatrix']
|
6 |
|
7 | export const createSetDistinct = 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 | return typed(name, {
|
28 | 'Array | Matrix': function (a) {
|
29 | let result
|
30 | if (subset(size(a), new Index(0)) === 0) {
|
31 | result = []
|
32 | } else {
|
33 | const b = flatten(Array.isArray(a) ? a : a.toArray()).sort(compareNatural)
|
34 | result = []
|
35 | result.push(b[0])
|
36 | for (let i = 1; i < b.length; i++) {
|
37 | if (compareNatural(b[i], b[i - 1]) !== 0) {
|
38 | result.push(b[i])
|
39 | }
|
40 | }
|
41 | }
|
42 |
|
43 | if (Array.isArray(a)) {
|
44 | return result
|
45 | }
|
46 |
|
47 | return new DenseMatrix(result)
|
48 | }
|
49 | })
|
50 | })
|