public void AddBeyondCapacity() { ConcurrentBoundedSortedCollection<int, string> cbsl = new ConcurrentBoundedSortedCollection<int, string>(3); cbsl.TryAdd(1, "1"); cbsl.TryAdd(5, "5"); cbsl.TryAdd(3, "3"); cbsl.TryAdd(2, "2"); // Should remove "1" cbsl.TryAdd(4, "4"); // Should remove "2" Assert.Equal(cbsl.Capacity, cbsl.Count()); Assert.Equal("3", cbsl.ElementAt(0).Value); Assert.Equal("4", cbsl.ElementAt(1).Value); Assert.Equal("5", cbsl.ElementAt(2).Value); }
public void AddToCapacity() { ConcurrentBoundedSortedCollection<long, int> cbsl = new ConcurrentBoundedSortedCollection<long, int>(4); cbsl.TryAdd(1, 1); cbsl.TryAdd(3, 3); cbsl.TryAdd(-1, -1); cbsl.TryAdd(0, 0); Assert.Equal(4, cbsl.Count()); Assert.Equal(-1, cbsl.ElementAt(0).Value); Assert.Equal(0, cbsl.ElementAt(1).Value); Assert.Equal(1, cbsl.ElementAt(2).Value); Assert.Equal(3, cbsl.ElementAt(3).Value); }
public void BoundedExhaustiveTest(int[] arr, int capacity) { // expected: top 'capacity' elements in the sorted version of 'arr' var expectedResult = arr.OrderBy(i => i).Reverse().Take(capacity).Reverse().ToArray(); var sl = new ConcurrentBoundedSortedCollection<int, int>(capacity); Parallel.ForEach(arr, e => sl.TryAdd(e, e)); var slArray = sl.Select(kvp => kvp.Key).ToArray(); XAssert.ArrayEqual(expectedResult, slArray); }
public void AddWithSortValueBelowDefault() { ConcurrentBoundedSortedCollection<int, int> cbsl = new ConcurrentBoundedSortedCollection<int, int>(10); cbsl.TryAdd(-30, 0); Assert.Equal(1, cbsl.Count()); int value = cbsl.First().Value; Assert.Equal(0, value); }
/// <summary> /// Increments <see cref="Count"/>, adds <paramref name="weight"/> to <see cref="AggregateWeight"/>, /// and (optionally) associates the weight with <paramref name="path"/>. /// </summary> public int Increment(long weight, string path = null) { var result = Interlocked.Increment(ref m_count); Interlocked.Add(ref m_aggregateWeight, weight); if (!string.IsNullOrEmpty(path)) { SortedList.TryAdd(weight, path); } return(result); }
public void AddParallelManyElements(int capacity) { Random r = new Random(); ConcurrentBoundedSortedCollection<int, int> cbsl = new ConcurrentBoundedSortedCollection<int, int>(capacity); // Build array of random ints int arraySize = 400; int[] valArray = new int[arraySize]; for (int i = 0; i < arraySize; i++) { valArray[i] = r.Next(); } Parallel.ForEach(valArray, i => cbsl.TryAdd(i, i)); XAssert.ArrayEqual(valArray.OrderByDescending(i => i).Take(capacity).Reverse().ToArray(), cbsl.Select(kvp => kvp.Key).ToArray()); }
public void BoundedExhaustiveTest(int n) { var arr = Enumerable.Range(0, n).ToArray(); var perms = GenAllPermutations(arr).ToArray(); Assert.All( Enumerable.Range(1, n + 1), // test for all possible capacities from 1 to n+1 capacity => { var top = arr.OrderBy(i => i).Reverse().Take(capacity).Reverse().ToArray(); Assert.All( perms, // test for all possible permutations perm => { var sl = new ConcurrentBoundedSortedCollection <int, int>(capacity); Parallel.ForEach(perm, e => sl.TryAdd(e, e)); var slArray = sl.Select(kvp => kvp.Key).ToArray(); XAssert.ArrayEqual(top, slArray); }); }); }
/// <summary> /// Add a Pip into the builds top N pips. /// Will be ignored if it's execution time was lower than the minumum execution time of existing N pips. /// </summary> public bool AddPip(PerProcessPipPerformanceInformation perPipInfo) { return(m_sortedTopPipPerformanceInfo.TryAdd(perPipInfo.PipExecutionMs, perPipInfo)); }