1 package org.djunits.value.vdouble.vector.data;
2
3 import java.util.Arrays;
4 import java.util.stream.IntStream;
5
6 import org.djunits.value.storage.StorageType;
7 import org.djunits.value.vdouble.function.DoubleFunction;
8 import org.djunits.value.vdouble.function.DoubleFunction2;
9
10
11
12
13
14
15
16
17
18
19 public class DoubleVectorDataDense extends DoubleVectorData
20 {
21
22 private static final long serialVersionUID = 1L;
23
24
25
26
27
28 public DoubleVectorDataDense(final double[] vectorSI)
29 {
30 super(StorageType.DENSE);
31 this.vectorSI = new double[vectorSI.length];
32 System.arraycopy(vectorSI, 0, this.vectorSI, 0, vectorSI.length);
33 }
34
35 @Override
36 public final int cardinality()
37 {
38 return (int) Arrays.stream(this.vectorSI).parallel().filter(d -> d != 0.0).count();
39 }
40
41 @Override
42 public final DoubleVectorDataDense assign(final DoubleFunction doubleFunction)
43 {
44 IntStream.range(0, size()).parallel().forEach(i -> this.vectorSI[i] = doubleFunction.apply(this.vectorSI[i]));
45 return this;
46 }
47
48 @Override
49 public final DoubleVectorDataDense assign(final DoubleFunction2 doubleFunction2, final DoubleVectorData right)
50 {
51 if (right.isDense())
52 {
53 IntStream.range(0, size()).parallel()
54 .forEach(i -> this.vectorSI[i] = doubleFunction2.apply(this.vectorSI[i], right.vectorSI[i]));
55 }
56 else
57 {
58 IntStream.range(0, size()).parallel()
59 .forEach(i -> this.vectorSI[i] = doubleFunction2.apply(this.vectorSI[i], right.getSI(i)));
60 }
61 return this;
62 }
63
64 @Override
65 public final DoubleVectorDataDense toDense()
66 {
67 return this;
68 }
69
70 @Override
71 public final DoubleVectorDataSparse toSparse()
72 {
73 return DoubleVectorDataSparse.instantiate(this.vectorSI);
74 }
75
76 @Override
77 public final int size()
78 {
79 return this.vectorSI.length;
80 }
81
82 @Override
83 public final double getSI(final int index)
84 {
85 return this.vectorSI[index];
86 }
87
88 @Override
89 public final void setSI(final int index, final double valueSI)
90 {
91 this.vectorSI[index] = valueSI;
92 }
93
94 @Override
95 public final double[] getDenseVectorSI()
96 {
97 return this.vectorSI.clone();
98 }
99
100 @Override
101 public final DoubleVectorDataDense copy()
102 {
103 double[] vCopy = new double[this.vectorSI.length];
104 System.arraycopy(this.vectorSI, 0, vCopy, 0, this.vectorSI.length);
105 return new DoubleVectorDataDense(vCopy);
106 }
107
108 @Override
109 public final DoubleVectorDataDense plus(final DoubleVectorData right)
110 {
111 checkSizes(right);
112 return new DoubleVectorDataDense(
113 IntStream.range(0, size()).parallel().mapToDouble(i -> getSI(i) + right.getSI(i)).toArray());
114 }
115
116 @Override
117 public final DoubleVectorDataDense minus(final DoubleVectorData right)
118 {
119 checkSizes(right);
120 return new DoubleVectorDataDense(
121 IntStream.range(0, size()).parallel().mapToDouble(i -> getSI(i) - right.getSI(i)).toArray());
122 }
123
124 @Override
125 public final DoubleVectorData times(final DoubleVectorData right)
126 {
127 if (right.isSparse())
128 {
129
130 return right.times(this);
131 }
132
133 checkSizes(right);
134 return this.copy().multiplyBy(right);
135 }
136
137 @Override
138 public final DoubleVectorData divide(final DoubleVectorData right)
139 {
140 checkSizes(right);
141 return this.copy().divideBy(right);
142 }
143
144 }