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