public void Array_using_comparer_should_sort_values_correct() { var count = 50; var array = new UserValueWrapper[count]; var rnd = new Random(); foreach (var i in Enumerable.Range(0, count)) { array[i] = new UserValueWrapper(rnd.Next(1, 100), "userValue"); } Array.Sort(array, UserValueWrapper.Comparer); array.ToList().Select(x => x.Value).Should().BeInAscendingOrder(); }
public void Update(long value, string userValue = null) { long c = this.count.Increment(); if (c <= this.values.Length) { values[(int)c - 1] = new UserValueWrapper(value, userValue); } else { long r = NextLong(c); if (r < values.Length) { values[(int)r] = new UserValueWrapper(value, userValue); } } }
/// <summary> /// Updates the sample set adding the specified value using /// <see href="http://www.cs.umd.edu/~samir/498/vitter.pdf">Vitter's Algorithm R</see>. /// </summary> /// Algorithm R pseudo code /// <example> /// <code> /// <![CDATA[ /// -- S has items to sample, R will contain the result /// ReservoirSample(S[1..n], R[1..k]) /// -- fill the reservoir array /// for i = 1 to k /// R[i] := S[i] /// -- replace elements with gradually decreasing probability /// for i = k+1 to n /// j := random(1, i) -- important: inclusive range /// if j <= k /// R[j] := S[i] /// ]]> /// </code> /// </example> /// <param name="value">The value to add to the sample set.</param> /// <param name="userValue">The user value to track, which records the last, min and max user values within the sample.</param> public void Update(long value, string userValue) { var c = _count.Increment(); if (c <= _values.Length) { _values[(int)c - 1] = new UserValueWrapper(value, userValue); } else { var r = ThreadLocalRandom.NextLong(c); if (r < _values.Length) { _values[(int)r] = new UserValueWrapper(value, userValue); } } }
public Snapshot GetSnapshot(bool resetReservoir = false) { var size = this.Size; if (size == 0) { return new UniformSnapshot(0, Enumerable.Empty<long>()); } UserValueWrapper[] values = new UserValueWrapper[size]; Array.Copy(this.values, values, size); if (resetReservoir) { count.SetValue(0L); } Array.Sort(values, UserValueWrapper.Comparer); var minValue = values[0].UserValue; var maxValue = values[size - 1].UserValue; return new UniformSnapshot(this.count.Value, values.Select(v => v.Value), valuesAreSorted: true, minUserValue: minValue, maxUserValue: maxValue); }
public void Reset() { this.last = new UserValueWrapper(); this.reservoir.Reset(); }
public void Update(long value, string userValue = null) { this.last = new UserValueWrapper(value, userValue); this.reservoir.Update(value, userValue); }
/// <inheritdoc /> public void Update(long value) { _last = new UserValueWrapper(value); _reservoir.Value.Update(value); }
/// <inheritdoc /> public void Reset() { _last = UserValueWrapper.Empty; _reservoir.Value.Reset(); }
public void Reset() { this.last = UserValueWrapper.Empty; this.reservoir.Reset(); }
public void Reset() { this.last = new UserValueWrapper(); this.counter.SetValue(0L); this.reservoir.Reset(); }
/// <inheritdoc /> public void Update(long value, string userValue) { _last = new UserValueWrapper(value, userValue); _reservoir.Update(value, userValue); }