private static string AddMatches(RangeToScan range, ushort[] sortedIDs, ushort[] previousValues) { ShortSet resultSet = new ShortSet((ushort)range.Count); // If previous values are specified, add them if (previousValues != null) { for (int i = 0; i < previousValues.Length; ++i) { resultSet.Add(previousValues[i]); } } // Add matches for the range range.AddMatches(sortedIDs, resultSet); // Return the result as a string return(String.Join(", ", resultSet.Values)); }
public void ShortSet_Performance_GetAndSet() { // Goal: set operations are <10 instructions, so at 2M instructions per millisecond, >200k per millisecond (Release build) // Get and Set are used when evaluating ORDER BY for small sets and for determining aggregates each item should be included within. Random r = new Random(); ShortSet s1 = BuildRandom(ushort.MaxValue, 10000, r); ShortSet s2 = BuildRandom(ushort.MaxValue, 1000, r); ushort[] getAndSetValues = s2.Values.ToArray(); // 1k values; 2k operations; 20M total int iterations = 10000; Stopwatch w = Stopwatch.StartNew(); for (int i = 0; i < iterations; ++i) { int length = getAndSetValues.Length; for (int j = 0; j < length; ++j) { ushort value = getAndSetValues[j]; //bool initial = s1[value]; //s1[value] = !initial; bool initial = s1.Contains(value); if (!initial) { s1.Add(value); } else { s1.Remove(value); } } } int operations = (2 * getAndSetValues.Length * iterations); double milliseconds = w.ElapsedMilliseconds; double operationsPerMillisecond = operations / milliseconds; Trace.Write(String.Format("{0:n0} operations in {1:n0} milliseconds; {2:n0} per millisecond.", operations, milliseconds, operationsPerMillisecond)); Assert.IsTrue(operationsPerMillisecond > 75000, "Not within 200% of goal."); }
public void SetSize(ushort size) { ushort oldSize = this.Count; if (size != _trueItems.Capacity) { // Allocate a new size set and copy all set values ShortSet newItems = new ShortSet(size); newItems.Or(_trueItems); _trueItems = newItems; // If the default is true, set new values true initially if (this.DefaultValue == true) { for (ushort i = oldSize; i < size; ++i) { _trueItems.Add(i); } } } }
public static ShortSet BuildRandom(ushort capacity, ushort itemsToSet, Random r) { ShortSet s = new ShortSet(capacity); for (int i = 0; i <= capacity; ++i) { double chanceToIncludeThis = (double)itemsToSet / (capacity - i); if (r.NextDouble() < chanceToIncludeThis) { s.Add((ushort)i); itemsToSet--; } if (itemsToSet == 0) { break; } } return(s); }
public override void VerifyConsistency(VerificationLevel level, ExecutionDetails details) { base.VerifyConsistency(level, details); // Verify SortedIDCount agrees with ItemCount if (this.SortedIDCount != this.Count) { if (details != null) { details.AddError(ExecutionDetails.ColumnDoesNotHaveEnoughValues, this.Name, this.SortedIDCount, this.Count); } } // Verify that all IDs are in SortedIDs, all values are ordered, and no unexpected values are found ushort lastID = 0; IComparable lastValue = null; ShortSet idsInList = new ShortSet(this.Count); for (int i = 0; i < this.Count; ++i) { ushort id = this.SortedIDs[i]; if (id >= this.Count) { if (details != null) { details.AddError(ExecutionDetails.SortedIdOutOfRange, this.Name, id, this.Count); } } else if (idsInList.Contains(id)) { if (details != null) { details.AddError(ExecutionDetails.SortedIdAppearsMoreThanOnce, this.Name, id); } } else { idsInList.Add(id); IComparable value = (IComparable)this[id]; if (lastValue != null) { int compareResult = lastValue.CompareTo(value); if (compareResult > 0) { if (details != null) { details.AddError(ExecutionDetails.SortedValuesNotInOrder, this.Name, lastID, lastValue, id, value); } } } lastValue = value; lastID = id; } } idsInList.Not(); if (idsInList.Count() > 0) { if (details != null) { details.AddError(ExecutionDetails.SortedColumnMissingIDs, this.Name, String.Join(", ", idsInList.Values)); } } }
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()); }