View Javadoc
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   * Stores dense data for a FloatVector and carries out basic operations.
11   * <p>
12   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="https://djunits.org/docs/license.html">DJUNITS License</a>.
14   * </p>
15   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
17   */
18  public class FloatVectorDataDense extends FloatVectorData
19  {
20      /** */
21      private static final long serialVersionUID = 1L;
22  
23      /**
24       * Create a vector with dense data.
25       * @param vectorSI float[]; the data to store
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      /** {@inheritDoc} */
35      @Override
36      public final int cardinality()
37      {
38          // this does not copy the data. See http://stackoverflow.com/questions/23106093/how-to-get-a-stream-from-a-float
39          return (int) IntStream.range(0, this.vectorSI.length).parallel().mapToDouble(i -> this.vectorSI[i])
40                  .filter(d -> d != 0.0).count();
41      }
42  
43      /** {@inheritDoc} */
44      @Override
45      public final FloatVectorDataDense assign(final FloatFunction floatFunction)
46      {
47          IntStream.range(0, size()).parallel().forEach(i -> this.vectorSI[i] = floatFunction.apply(this.vectorSI[i]));
48          return this;
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      public final FloatVectorDataDense assign(final FloatFunction2 floatFunction2, final FloatVectorData right)
54      {
55          if (right.isDense())
56          {
57              IntStream.range(0, size()).parallel()
58                      .forEach(i -> this.vectorSI[i] = floatFunction2.apply(this.vectorSI[i], right.vectorSI[i]));
59          }
60          else
61          { // right is sparse
62              IntStream.range(0, size()).parallel()
63                      .forEach(i -> this.vectorSI[i] = floatFunction2.apply(this.vectorSI[i], right.getSI(i)));
64          }
65          return this;
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public final FloatVectorDataDense toDense()
71      {
72          return this;
73      }
74  
75      /** {@inheritDoc} */
76      @Override
77      public final FloatVectorDataSparse toSparse()
78      {
79          return FloatVectorDataSparse.instantiate(this.vectorSI);
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public final int size()
85      {
86          return this.vectorSI.length;
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      public final float getSI(final int index)
92      {
93          return this.vectorSI[index];
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public final void setSI(final int index, final float valueSI)
99      {
100         this.vectorSI[index] = valueSI;
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public final float[] getDenseVectorSI()
106     {
107         return this.vectorSI.clone();
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     public final FloatVectorDataDense copy()
113     {
114         float[] vCopy = new float[this.vectorSI.length];
115         System.arraycopy(this.vectorSI, 0, vCopy, 0, this.vectorSI.length);
116         return new FloatVectorDataDense(vCopy);
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     public final FloatVectorDataDense plus(final FloatVectorData right)
122     {
123         checkSizes(right);
124         float[] out = new float[size()];
125         IntStream.range(0, size()).parallel().forEach(i -> out[i] = getSI(i) + right.getSI(i));
126         return new FloatVectorDataDense(out);
127     }
128 
129     /** {@inheritDoc} */
130     @Override
131     public final FloatVectorDataDense minus(final FloatVectorData right)
132     {
133         checkSizes(right);
134         float[] out = new float[size()];
135         IntStream.range(0, size()).parallel().forEach(i -> out[i] = getSI(i) - right.getSI(i));
136         return new FloatVectorDataDense(out);
137     }
138 
139     /** {@inheritDoc} */
140     @Override
141     public final FloatVectorData times(final FloatVectorData right)
142     {
143         if (right.isSparse())
144         {
145             // result shall be sparse
146             return right.times(this);
147         }
148         // Both are dense
149         checkSizes(right);
150         return this.copy().multiplyBy(right);
151     }
152 
153     /** {@inheritDoc} */
154     @Override
155     public final FloatVectorData divide(final FloatVectorData right)
156     {
157         checkSizes(right);
158         return this.copy().divideBy(right);
159     }
160 
161 }