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