private void PostSortAndFilter(XArray groups, XArray counts, int totalRowCount, bool wasAllRows) { int[] finalIndices = new int[groups.Count]; int[] finalCounts = new int[groups.Count]; int groupCount = 0; // Filter to counts over the minimum percentage threshold int[] countsArray = (int[])counts.Array; if (countsArray != null) { int threshold = (int)(totalRowCount * MinimumPercentageToReport); for (int i = 0; i < groups.Count; ++i) { int count = countsArray[counts.Index(i)]; if (count >= threshold) { finalIndices[groupCount] = i; finalCounts[groupCount] = count; groupCount++; } } } // Sort the values by count descending Array.Sort <int, int>(finalCounts, finalIndices, 0, groupCount, new ReverseComparer()); // Limit to the top N if needed if (groupCount > MaximumCountToReturn) { groupCount = MaximumCountToReturn; } // Set the distinct count (now that it's known) _distinctCount = groupCount; // Set the output values int[] groupsRemap = null; XArray finalCountsX = XArray.All(finalCounts, groupCount); _columns[0].SetValues(groups.Select(ArraySelector.Map(finalIndices, groupCount), ref groupsRemap)); _columns[1].SetValues(finalCountsX); if (wasAllRows) { _columns[2].SetValues(PercentageAggregator.ToPercentageStrings(finalCountsX, totalRowCount, PercentageAggregator.TwoSigFigs)); } else { _columns[2].SetValues(PercentageAggregator.ToPercentageStrings(finalCountsX, totalRowCount, PercentageAggregator.WholePercentage)); } }
public void Verb_Peek() { String8Block block = new String8Block(); int[] values = BuildPeekSample(); IXTable expected = TableTestHarness.DatabaseContext.FromArrays(3) .WithColumn("Value", new int[] { 0, 1, 2 }) .WithColumn("Count", new int[] { 500, 250, 150 }) .WithColumn("Percentage", TableTestHarness.ToString8(new string[] { PercentageAggregator.TwoSigFigs(500, 1000), PercentageAggregator.TwoSigFigs(250, 1000), PercentageAggregator.TwoSigFigs(150, 1000) })); IXTable actual = TableTestHarness.DatabaseContext.FromArrays(values.Length) .WithColumn("Value", values) .Query("peek [Value]", TableTestHarness.DatabaseContext); TableTestHarness.AssertAreEqual(expected, actual, 2); }