private double[] computeStatistic(Statistic statistic, int[] sortedGroupArray, double[] sortedValueArray, int numSingleValuedGroups) { StatFunction stat = aggregationFunction(statistic); return Aggregation.aggregateSorted( stat, sortedGroupArray, sortedValueArray, sortedValueArray.length, numSingleValuedGroups); }
@Test public void emptyArray() { int[] groupId = new int[0]; double[] values = new double[0]; int numGroups = 21; double[] result = Aggregation.sortAndAggregate(CountFunction.INSTANCE, groupId, values, 0, numGroups); assertThat(result.length, equalTo(numGroups)); }
private ColumnView join() { // The nested column may contain multiple rows // for each row on the left ColumnView subColumn = nestedColumn.get(); int numSubRows = subColumn.numRows(); // In order to produce a column with one summarized entry per // output row, we need to assign "groupIds" going from // parentId -> master row index via the primary key int masterRowId[] = new int[numSubRows]; double subColumnValues[] = new double[numSubRows]; SubFormJoin join = Iterables.getOnlyElement(links); PrimaryKeyMap parentLookup = join.getMasterPrimaryKey().get(); ColumnView parentColumn = join.getParentColumn().get(); for (int i = 0; i < numSubRows; ++i) { // Get the parent id of this row String parentId = parentColumn.getString(i); masterRowId[i] = parentLookup.getRowIndex(parentId); // Store the value subColumnValues[i] = subColumn.getDouble(i); } int numMasterRows = parentLookup.numRows(); // Sort the data values by master row index double[] result = Aggregation.sortAndAggregate(statistic, masterRowId, subColumnValues, numSubRows, numMasterRows); return new DoubleArrayColumnView(result); }
/** * Sorts the {@code group} and {@code values} array in tandem and calculates the statistic per group. * * <p>Note: the {@code group} and {@code values} array are sorted in place!</p> * * @param statistic the statistic to apply * @param groupId an array of at least length {@code numValues} containing group ids * @param values an array of at least length {@code numValues} containing the values to be aggregated * @param numValues number of values to aggregate. * @param numGroups number of groups present. The maximum value in the groupId values should be {@code numGroups-1} * @return an array of length {@code numGroups} containing the aggregated values. */ public static double[] sortAndAggregate(StatFunction statistic, int[] groupId, double[] values, int numValues, int numGroups) { if(numValues > 1) { HeapsortTandem.heapsortDescending(groupId, values, numValues); } return aggregateSorted(statistic, groupId, values, numValues, numGroups); }