public void AddRange_AddSelfAsEnumerable_ThrowsExceptionWhenNotEmpty() { PooledList <T> list = GenericListFactory(0); // Succeeds when list is empty. list.AddRange(list); list.AddRange(list.Where(_ => true)); // Succeeds when list has elements and is added as collection. list.Add(default);
public static PooledList Allocate(List <ObjectType> objects) { PooledList pooledList = pool.Allocate(); pooledList.AddRange(objects); return(pooledList); }
public void TerribleBugWhenAppendingString() { var expected = "longer than the capacity"; var builder = new PooledList <char>(1); builder.AddRange(expected.AsSpan()); Assert.Equal(expected, builder.AsEnumerable()); }
private ExpressionMinigameSettings FindContextualLevel() { Level currentLevel = Session.instance.currentLevel; ExpressionMinigameCategories category; if (currentLevel.isSingleDigitProblem) { category = ExpressionMinigameCategories.Single; } else { if (currentLevel.value < LOW_DOUBLE_THRESHOLD) { category = ExpressionMinigameCategories.DoubleLow; } else { category = ExpressionMinigameCategories.Double; if (currentLevel.requiresRegrouping) { category |= ExpressionMinigameCategories.Regroup; } } } if (currentLevel.isSubtractionProblem) { category |= ExpressionMinigameCategories.Subtraction; } else { category |= ExpressionMinigameCategories.Addition; } using (PooledList <ExpressionMinigameSettings> listToUse = PooledList <ExpressionMinigameSettings> .Create()) { listToUse.AddRange(m_Levels[category]); if ((category | ExpressionMinigameCategories.Regroup) > 0) { listToUse.AddRange(m_Levels[category & ~ExpressionMinigameCategories.Regroup]); } return(RNG.Instance.Choose(listToUse)); } }
protected void RegisterForDispose <T>(params T[] objects) { if (objects.Length == 0) { return; } _disposables.AddRange(objects.OfType <IDisposable>()); }
public void PooledAddRangeIEnumerable() { for (int i = 0; i < 5000; i++) { var emptyList = new PooledList <int>(); emptyList.AddRange(IntEnumerable()); emptyList.Dispose(); } }
public void PooledAddRangeICollection() { for (int i = 0; i < 5000; i++) { var emptyList = new PooledList <int>(); emptyList.AddRange(list); emptyList.Dispose(); } }
public void AddRange_NullEnumerable_ThrowsArgumentNullException(int count) { PooledList <T> list = GenericListFactory(count); var listBeforeAdd = list.ToPooledList(); Assert.Throws <ArgumentNullException>(() => list.AddRange((IEnumerable <T>)null)); Assert.Equal(listBeforeAdd, list); list.Dispose(); listBeforeAdd.Dispose(); }
/// <summary> /// Gets all metadata for all metrics in the specified enumerable of <see cref="MetricSource"/>. /// </summary> internal static PooledList <Metadata> GetMetadata(this IEnumerable <MetricSource> sources) { var metadata = new PooledList <Metadata>(); foreach (var source in sources) { metadata.AddRange(source.GetMetadata()); } return(metadata); }
public void LastIndexOf_int_OrderIsCorrectWithManyDuplicates(int count) { PooledList <T> list = GenericListFactory(count); PooledList <T> withoutDuplicates = list.ToPooledList(); list.AddRange(list); list.AddRange(list); list.AddRange(list); Assert.All(Enumerable.Range(0, count), i => { Assert.All(Enumerable.Range(0, 4), j => { int expectedIndex = (j * count) + i; Assert.Equal(expectedIndex, list.LastIndexOf(withoutDuplicates[i], (count * (j + 1)) - 1)); Assert.Equal(expectedIndex, list.LastIndexOf(withoutDuplicates[i], (count * (j + 1)) - 1, count)); }); }); list.Dispose(); withoutDuplicates.Dispose(); }
private static void CollectFrame() { ProfilerFrame frame = new ProfilerFrame(); frame.frameNumber = currentFrame; frame.frameLength = (stopwatch.ElapsedTicks / (double)Stopwatch.Frequency) * 1000000.0; frame.threadMethods = PooledList <(string, PooledList <ProfilerMethod>)> .Create(); foreach (var dataList in collectedMethods.Values) { if (dataList.Count == 0) { continue; } if (profilingThreads.TryGetValue(dataList[0].threadId, out string threadName)) { var tuple = frame.threadMethods.SingleOrDefault(x => x.Item1 == threadName); PooledList <ProfilerMethod> outList = null; if (tuple.Item1 == null) { outList = PooledList <ProfilerMethod> .Create(); frame.threadMethods.Add((threadName, outList)); } else { outList = tuple.Item2; } outList.AddRange(dataList); } } foreach (var dataList in collectedMarkers.Values) { if (frame.markers == null) { frame.markers = PooledList <ProfilerMarker> .Create(); } frame.markers.AddRange(dataList); } if (collectedFrames.IsFull) { collectedFrames.Front().Dispose(); } collectedFrames.PushBack(frame); }
// Resumes all paused input. public void ResumeInputs() { using (PooledList <string> keys = PooledList <string> .Create()) { keys.AddRange(_temporaryPausedInputs); _temporaryPausedInputs.Clear(); foreach (string key in keys) { if (_activatedInputs.Contains(key)) { EnableInput(key, _objects[key], true, false); } } } Logger.Log("RESUMING INPUTS"); }
public void AddRange(EnumerableType enumerableType, int listLength, int enumerableLength, int numberOfMatchingElements, int numberOfDuplicateElements) { PooledList <T> list = GenericListFactory(listLength); var listBeforeAdd = list.ToPooledList(); IEnumerable <T> enumerable = CreateEnumerable(enumerableType, list, enumerableLength, numberOfMatchingElements, numberOfDuplicateElements); list.AddRange(enumerable); // Check that the first section of the List is unchanged Assert.All(Enumerable.Range(0, listLength), index => { Assert.Equal(listBeforeAdd[index], list[index]); }); // Check that the added elements are correct Assert.All(Enumerable.Range(0, enumerableLength), index => { Assert.Equal(enumerable.ElementAt(index), list[index + listLength]); }); list.Dispose(); }
public void IndexOf_OrderIsCorrect(IndexOfMethod indexOfMethod, int count, bool frontToBackOrder) { PooledList <T> list = GenericListFactory(count); PooledList <T> withoutDuplicates = list.ToPooledList(); list.AddRange(list); IndexOfDelegate IndexOf = IndexOfDelegateFromType(indexOfMethod); Assert.All(Enumerable.Range(0, count), i => { if (frontToBackOrder) { Assert.Equal(i, IndexOf(list, withoutDuplicates[i])); } else { Assert.Equal(count + i, IndexOf(list, withoutDuplicates[i])); } }); list.Dispose(); withoutDuplicates.Dispose(); }
public void GlobalSetup() { int samples = GetSampleLength(); sampleSet = new string[samples]; for (int i = 0; i < samples; i++) { sampleSet[i] = i.ToString(); } addLoops = LARGE_SAMPLE_LENGTH / samples; // create lists big enough to hold 17 copies of the sample set startingCapacity = 17 * samples * addLoops; list = new List<string>(startingCapacity); pooled = new PooledList<string>(startingCapacity); for (int j = 0; j < addLoops; j++) { list.AddRange(sampleSet); list.AddRange(sampleSet); list.AddRange(sampleSet); list.AddRange(sampleSet); list.AddRange(sampleSet); list.AddRange(sampleSet); list.AddRange(sampleSet); list.AddRange(sampleSet); list.AddRange(sampleSet); list.AddRange(sampleSet); list.AddRange(sampleSet); list.AddRange(sampleSet); list.AddRange(sampleSet); list.AddRange(sampleSet); list.AddRange(sampleSet); list.AddRange(sampleSet); list.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); } }
public void Add(T value) { backing.AddRange(MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref value, 1))); }
public static void WriteToFile(string filename, ProfilingFormat format, FrameSelection frameSelection = FrameSelection.Median) { if (!ProfilingEnabled) { throw new InvalidOperationException("Cannot write profile to file when profiling is not enabled."); } ProfilerFrame frame = default; if (collectedFrames.IsEmpty) { return; } bool needsDispose = false; switch (frameSelection) { case FrameSelection.Median: var ordered = collectedFrames.OrderBy(x => x.frameLength).ToArray(); frame = ordered[(int)Math.Floor(ordered.Length / 2.0)]; break; case FrameSelection.Longest: frame = collectedFrames.MaxBy(x => x.frameLength); break; case FrameSelection.Shortest: frame = collectedFrames.MinBy(x => x.frameLength); break; case FrameSelection.Latest: frame = collectedFrames.Back(); break; case FrameSelection.Percentile95: ordered = collectedFrames.OrderBy(x => x.frameLength).ToArray(); int index = ordered.Length - (int)(ordered.Length * 0.05f); if (index >= ordered.Length) { index = ordered.Length - 1; } frame = ordered[index]; break; case FrameSelection.All: frame = new ProfilerFrame(); frame.frameNumber = collectedFrames.Min(x => x.frameNumber); frame.frameLength = collectedFrames.Sum(x => x.frameLength); frame.markers = PooledList <ProfilerMarker> .Create(); double elapsedTime = 0; foreach (var collectedFrame in collectedFrames) { if (collectedFrame.markers != null) { frame.markers.AddRange(collectedFrame.markers.Select(x => new ProfilerMarker(x.markName, x.time + elapsedTime))); } elapsedTime += collectedFrame.frameLength; } frame.threadMethods = PooledList <(string, PooledList <ProfilerMethod>)> .Create(); elapsedTime = 0; foreach (var collectedFrame in collectedFrames) { foreach (var collectedFrameThreadMethod in collectedFrame.threadMethods) { var tuple = frame.threadMethods.SingleOrDefault(x => x.Item1 == collectedFrameThreadMethod.Item1); PooledList <ProfilerMethod> outList = null; if (tuple.Item1 == null) { outList = PooledList <ProfilerMethod> .Create(); frame.threadMethods.Add((collectedFrameThreadMethod.Item1, outList)); } else { outList = tuple.Item2; } outList.AddRange(collectedFrameThreadMethod.Item2.Select(x => new ProfilerMethod( x.methodName, x.depth, x.startTime + elapsedTime, x.endTime + elapsedTime, x.threadId))); } elapsedTime += collectedFrame.frameLength; } needsDispose = true; break; default: throw new ArgumentOutOfRangeException(nameof(frameSelection), frameSelection, null); } switch (format) { case ProfilingFormat.SpeedScope: SpeedScopeWriter.WriteToFile(frame, filename); break; case ProfilingFormat.ChromeTracing: ChromeTracingWriter.WriteToFile(frame, filename); break; default: throw new ArgumentOutOfRangeException(nameof(format), format, null); } if (needsDispose) { frame.Dispose(); } }
public void PooledAddRange_Int_CapacityIncrease_Enumerable() { var pooled = new PooledList <string>(); var enumerable = (IEnumerable <string>)sampleSet; for (int j = 0; j < addLoops; j++) { pooled.AddRange(enumerable); pooled.AddRange(enumerable); pooled.AddRange(enumerable); pooled.AddRange(enumerable); pooled.AddRange(enumerable); pooled.AddRange(enumerable); pooled.AddRange(enumerable); pooled.AddRange(enumerable); pooled.AddRange(enumerable); pooled.AddRange(enumerable); pooled.AddRange(enumerable); pooled.AddRange(enumerable); pooled.AddRange(enumerable); pooled.AddRange(enumerable); pooled.AddRange(enumerable); pooled.AddRange(enumerable); pooled.AddRange(enumerable); } pooled.Dispose(); }
public void PooledAddRange_String_CapacityIncrease_Array() { var pooled = new PooledList <string>(); for (int j = 0; j < addLoops; j++) { pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); pooled.AddRange(sampleSet); } pooled.Dispose(); }