private static void FromAndPerformance() { ShortSet s0 = new ShortSet(ushort.MaxValue); ShortSet s1 = new ShortSet(ushort.MaxValue); s1.Not(); ShortSet s2 = Arriba.Test.ShortSetTests.BuildRandom(ushort.MaxValue, 10000, new Random()); s2.Count(); int iterations = 3000000; Stopwatch w = Stopwatch.StartNew(); for (int i = 0; i < iterations; ++i) { s0.FromAnd(s1, s2); } w.Stop(); double milliseconds = w.ElapsedMilliseconds; double operationsPerMillisecond = iterations / milliseconds; Trace.Write(String.Format("{0:n0}\r\n", s0.Count())); Trace.Write(String.Format("{0:n0} operations in {1:n0} milliseconds; {2:n0} per millisecond.\r\n", iterations, milliseconds, operationsPerMillisecond)); }
private static void AggregateAllDimensionsFlat(object aggregationContext, DataBlock block, ushort itemCount, ShortSet baseWhereSet, List <List <Tuple <IExpression, ShortSet> > > allDimensionValueSets, IUntypedColumn[] columns, IAggregator aggregator) { int nextBlockRow = 0; int dimensionCount = allDimensionValueSets.Count; int currentDimension = 0; int[] nextIndexPerDimension = new int[dimensionCount]; ShortSet[] setsPerDimension = new ShortSet[dimensionCount + 1]; setsPerDimension[0] = baseWhereSet; for (int i = 1; i < setsPerDimension.Length; ++i) { setsPerDimension[i] = new ShortSet(itemCount); } object[] aggregationResults = new object[block.RowCount]; while (currentDimension >= 0) { if (currentDimension == dimensionCount) { // Leaf: Compute Aggregate, then ask for next combination aggregationResults[nextBlockRow] = aggregator.Aggregate(aggregationContext, setsPerDimension[currentDimension], columns); nextBlockRow++; currentDimension--; } else if (nextIndexPerDimension[currentDimension] < allDimensionValueSets[currentDimension].Count) { // Non-leaf, more values: Get next value and combine Tuple <IExpression, ShortSet> dimensionValue = allDimensionValueSets[currentDimension][nextIndexPerDimension[currentDimension]]; ShortSet current = setsPerDimension[currentDimension + 1]; current.FromAnd(setsPerDimension[currentDimension], dimensionValue.Item2); nextIndexPerDimension[currentDimension]++; currentDimension++; // Skip rest of range if set already empty [slower; IsEmpty too expensive] if (current.IsEmpty()) { int rowsToSkip = 1; for (int i = currentDimension; i < dimensionCount; ++i) { rowsToSkip *= allDimensionValueSets[i].Count; } nextBlockRow += rowsToSkip; currentDimension--; } } else { // Non-leaf, no more values: Pop up to previous dimension for next value there nextIndexPerDimension[currentDimension] = 0; currentDimension--; } } block.SetColumn(block.ColumnCount - 1, aggregationResults); }
public void ShortSet_Basic() { // Constructor ShortSet s1 = new ShortSet(100); // Empty Assert.AreEqual("", String.Join(", ", s1.Values)); // Set value and enumerate s1.Add(0); Assert.AreEqual("0", String.Join(", ", s1.Values)); // Set additional values s1.Add(15); s1.Add(64); Assert.AreEqual("0, 15, 64", String.Join(", ", s1.Values)); // Clear values s1.Remove(64); Assert.AreEqual("0, 15", String.Join(", ", s1.Values)); // Or ShortSet s2 = new ShortSet(120); s2.Or(new ushort[] { 0, 1, 2 }); s1.Or(s2); Assert.AreEqual("0, 1, 2, 15", String.Join(", ", s1.Values)); Assert.AreEqual("0, 1, 2", String.Join(", ", s2.Values)); Verify.Exception <ArgumentNullException>(() => s1.Or((ShortSet)null)); Verify.Exception <ArgumentNullException>(() => s1.Or((IEnumerable <ushort>)null)); // OrNot [only 15, 16 not set, so only they should be added] ShortSet s3 = new ShortSet(100); s3.Not(); s3.Remove(15); s3.Remove(16); s1.OrNot(s3); Assert.AreEqual("0, 1, 2, 15, 16", String.Join(", ", s1.Values)); Verify.Exception <ArgumentNullException>(() => s1.OrNot((ShortSet)null)); // And s1.And(s2); s1.And(new ushort[] { 1, 2 }); Assert.AreEqual("1, 2", String.Join(", ", s1.Values)); s1.And(new ushort[] { 1 }); Assert.AreEqual("1", String.Join(", ", s1.Values)); Verify.Exception <ArgumentNullException>(() => s1.And((ShortSet)null)); Verify.Exception <ArgumentNullException>(() => s1.And((IEnumerable <ushort>)null)); // AndNot s1.Add(96); s1.Add(64); s1.AndNot(s2); s1.AndNot(new ushort[] { 96 }); Assert.AreEqual("64", String.Join(", ", s1.Values)); Verify.Exception <ArgumentNullException>(() => s1.AndNot((ShortSet)null)); Verify.Exception <ArgumentNullException>(() => s1.AndNot((IEnumerable <ushort>)null)); // Clear s1.Clear(); Assert.AreEqual("", String.Join(", ", s1.Values)); // From s1.From(s2); Assert.AreEqual("0, 1, 2", String.Join(", ", s1.Values)); Verify.Exception <ArgumentNullException>(() => s1.From((ShortSet)null)); // FromAnd ShortSet s4 = new ShortSet(100); s4.Or(new ushort[] { 1, 2, 3 }); s1.Clear(); s1.Not(); s1.FromAnd(s2, s4); Assert.AreEqual("1, 2", String.Join(", ", s1.Values)); Verify.Exception <ArgumentNullException>(() => s1.FromAnd((ShortSet)null, s2)); Verify.Exception <ArgumentNullException>(() => s1.FromAnd(s2, (ShortSet)null)); // ToString Assert.AreEqual("[1, 2]", s1.ToString()); }