/** * Remove the occurrence of a given value in a copied slice of array defined by the array part * from [begin, begin+length). * * @param values the input array * @param begin start index of the array to include * @param length number of elements to include from begin * @param removedValue the value to be removed from the sliced array * @return the copy of the sliced array after removing the removedValue */ private static double[] removeAndSlice( final double[] values, final int begin, final int length, final double removedValue) { MathArrays.verifyValues(values, begin, length); final double[] temp; // BitSet(length) to indicate where the removedValue is located final BitSet bits = new BitSet(length); for (int i = begin; i < begin + length; i++) { if (Precision.equalsIncludingNaN(removedValue, values[i])) { bits.set(i - begin); } } // Check if empty then create a new copy if (bits.isEmpty()) { temp = copyOf(values, begin, length); // Nothing removed, just copy } else if (bits.cardinality() == length) { temp = new double[0]; // All removed, just empty } else { // Some removable, so new temp = new double[length - bits.cardinality()]; int start = begin; // start index from source array (i.e values) int dest = 0; // dest index in destination array(i.e temp) int nextOne = -1; // nextOne is the index of bit set of next one int bitSetPtr = 0; // bitSetPtr is start index pointer of bitset while ((nextOne = bits.nextSetBit(bitSetPtr)) != -1) { final int lengthToCopy = nextOne - bitSetPtr; System.arraycopy(values, start, temp, dest, lengthToCopy); dest += lengthToCopy; start = begin + (bitSetPtr = bits.nextClearBit(nextOne)); } // Copy any residue past start index till begin+length if (start < begin + length) { System.arraycopy(values, start, temp, dest, begin + length - start); } } return temp; }
public void checkremoval( DescriptiveStatistics dstat, int wsize, double mean1, double mean2, double mean3) { dstat.setWindowSize(wsize); dstat.clear(); for (int i = 1; i <= 6; ++i) { dstat.addValue(i); } Assert.assertTrue(Precision.equalsIncludingNaN(mean1, dstat.getMean())); dstat.replaceMostRecentValue(0); Assert.assertTrue(Precision.equalsIncludingNaN(mean2, dstat.getMean())); dstat.removeMostRecentValue(); Assert.assertTrue(Precision.equalsIncludingNaN(mean3, dstat.getMean())); }
/** * Replace every occurrence of a given value with a replacement value in a copied slice of array * defined by array part from [begin, begin+length). * * @param values the input array * @param begin start index of the array to include * @param length number of elements to include from begin * @param original the value to be replaced with * @param replacement the value to be used for replacement * @return the copy of sliced array with replaced values */ private static double[] replaceAndSlice( final double[] values, final int begin, final int length, final double original, final double replacement) { final double[] temp = copyOf(values, begin, length); for (int i = 0; i < length; i++) { temp[i] = Precision.equalsIncludingNaN(original, temp[i]) ? replacement : temp[i]; } return temp; }