├── docs ├── api │ └── index.md ├── index.md ├── articles │ ├── toc.yml │ └── index.md ├── images │ └── icon.png ├── toc.yml ├── web.config └── docfx.json ├── serve-docs.cmd ├── src ├── Petabridge.Collections │ ├── Properties │ │ └── Friends.cs │ ├── Petabridge.Collections.csproj │ ├── IFixedSizeStack.cs │ ├── ICircularBuffer.cs │ ├── ConcurrentFixedSizeStack.cs │ ├── PriorityQueue.cs │ ├── FixedSizeStack.cs │ ├── ConcurrentCircularBuffer.cs │ └── CircularBuffer.cs ├── Petabridge.Collections.Tests │ ├── ConcurrentCircularBufferTests.cs │ ├── Petabridge.Collections.Tests.csproj │ ├── HeliosGenerators.cs │ ├── ThreadLocalRandom.cs │ ├── CircularBufferTests.cs │ ├── PriorityQueueSpecs.cs │ ├── ConcurrentFixedSizeStackTests.cs │ └── CircularBufferPropertyTests.cs ├── Petabridge.Collections.Tests.Performance │ ├── Petabridge.Collections.Tests.Performance.csproj │ └── AllCollectionsSpec.cs └── common.props ├── RELEASE_NOTES.md ├── appsettings.json ├── .github └── dependabot.yml ├── Petabridge.Collections.sln ├── README.md ├── .gitignore ├── LICENSE └── serve-docs.ps1 /docs/api/index.md: -------------------------------------------------------------------------------- 1 | # API Docs -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | # Introduction to My Project -------------------------------------------------------------------------------- /docs/articles/toc.yml: -------------------------------------------------------------------------------- 1 | - name: Introduction 2 | href: index.md -------------------------------------------------------------------------------- /docs/articles/index.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | 3 | Article text goes here. -------------------------------------------------------------------------------- /serve-docs.cmd: -------------------------------------------------------------------------------- 1 | PowerShell.exe -file "serve-docs.ps1" %* .\docs\docfx.json --serve -p 8090 -------------------------------------------------------------------------------- /docs/images/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/petabridge/Petabridge.Collections/HEAD/docs/images/icon.png -------------------------------------------------------------------------------- /docs/toc.yml: -------------------------------------------------------------------------------- 1 | - name: Home 2 | href: index.md 3 | - name: Documentation 4 | href: articles/ 5 | - name: API Reference 6 | href: api/ -------------------------------------------------------------------------------- /src/Petabridge.Collections/Properties/Friends.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | 3 | [assembly: InternalsVisibleTo("Petabridge.Collections.Tests")] -------------------------------------------------------------------------------- /docs/web.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /RELEASE_NOTES.md: -------------------------------------------------------------------------------- 1 | #### 1.0.0 February 05 2019 #### 2 | A set of specialized collections that should probably be in CoreFx. 3 | 4 | * `CircularBuffer` 5 | * `ConcurrentCircularBuffer` 6 | * `PriorityQueue` 7 | * `ConcurrentPriorityQueue` 8 | * `FixedSizeStack` 9 | -------------------------------------------------------------------------------- /src/Petabridge.Collections/Petabridge.Collections.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | netstandard1.1 7 | A set of specialized collections, such as circular buffers, priority queues, and more. 8 | 9 | 10 | -------------------------------------------------------------------------------- /appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "SignClient": { 3 | "AzureAd": { 4 | "AADInstance": "https://login.microsoftonline.com/", 5 | "ClientId": "1e983f21-9ea5-4f21-ab99-28080225efc9", 6 | "TenantId": "2fa36080-af12-4894-a64b-a17d8f29ec52" 7 | }, 8 | "Service": { 9 | "Url": "https://pb-sign.azurewebsites.net/", 10 | "ResourceId": "https://SignService/eef8e2e7-24b1-4a3b-a73b-a84d66f9abee" 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: nuget 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | time: "11:00" 8 | open-pull-requests-limit: 10 9 | ignore: 10 | - dependency-name: FsCheck.Xunit 11 | versions: 12 | - 2.14.4 13 | - 2.14.5 14 | - 2.14.6 15 | - 2.15.0 16 | - 2.15.1 17 | - dependency-name: Microsoft.NET.Test.Sdk 18 | versions: 19 | - 16.8.3 20 | - 16.9.1 21 | -------------------------------------------------------------------------------- /src/Petabridge.Collections.Tests/ConcurrentCircularBufferTests.cs: -------------------------------------------------------------------------------- 1 | // ----------------------------------------------------------------------- 2 | // 3 | // Copyright (C) 2015 - 2019 Petabridge, LLC 4 | // 5 | // ----------------------------------------------------------------------- 6 | 7 | namespace Petabridge.Collections.Tests 8 | { 9 | public class ConcurrentCircularBufferTests : CircularBufferTests 10 | { 11 | protected override ICircularBuffer GetBuffer(int capacity) 12 | { 13 | return new ConcurrentCircularBuffer(capacity); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /src/Petabridge.Collections.Tests/Petabridge.Collections.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | netcoreapp2.1 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/Petabridge.Collections.Tests/HeliosGenerators.cs: -------------------------------------------------------------------------------- 1 | // Copyright (c) Petabridge . All rights reserved. 2 | // Licensed under the Apache 2.0 license. See LICENSE file in the project root for full license information. 3 | // See ThirdPartyNotices.txt for references to third party code used inside Helios. 4 | 5 | using FsCheck; 6 | 7 | namespace Petabridge.Collections.Tests 8 | { 9 | public class HeliosGenerators 10 | { 11 | public static Arbitrary> CreateCircularBuffer() 12 | { 13 | var generator = Gen.Choose(1, 10).Select(i => new CircularBuffer(i)); 14 | return Arb.From(generator); 15 | } 16 | 17 | public static Arbitrary> CreateCircularBufferInt() 18 | { 19 | return CreateCircularBuffer(); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /src/Petabridge.Collections.Tests.Performance/Petabridge.Collections.Tests.Performance.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | netcoreapp2.1 7 | true 8 | 2.1.6 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/common.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | Copyright © 2017 Your Company 4 | Your Authors 5 | 1.0.0 6 | A set of specialized collections that should probably be in CoreFx. 7 | `CircularBuffer<T>` 8 | `ConcurrentCircularBuffer<T>` 9 | `PriorityQueue<T>` 10 | `ConcurrentPriorityQueue<T>` 11 | `FixedSizeStack<T>` 12 | 13 | 14 | 15 | 16 | 17 | 18 | $(NoWarn);CS1591 19 | 20 | 21 | 1.2.2 22 | 2.4.1 23 | 16.10.0 24 | 25 | -------------------------------------------------------------------------------- /src/Petabridge.Collections.Tests/ThreadLocalRandom.cs: -------------------------------------------------------------------------------- 1 | // ----------------------------------------------------------------------- 2 | // 3 | // Copyright (C) 2015 - 2019 Petabridge, LLC 4 | // 5 | // ----------------------------------------------------------------------- 6 | 7 | using System; 8 | using System.Threading; 9 | 10 | namespace Petabridge.Collections.Tests 11 | { 12 | /// 13 | /// Create random numbers with Thread-specific seeds. 14 | /// Borrowed form Jon Skeet's brilliant C# in Depth: http://csharpindepth.com/Articles/Chapter12/Random.aspx 15 | /// 16 | public static class ThreadLocalRandom 17 | { 18 | private static int _seed = Environment.TickCount; 19 | 20 | private static readonly ThreadLocal Rng = 21 | new ThreadLocal(() => new Random(Interlocked.Increment(ref _seed))); 22 | 23 | /// 24 | /// The current random number seed available to this thread 25 | /// 26 | public static Random Current => Rng.Value; 27 | } 28 | } -------------------------------------------------------------------------------- /docs/docfx.json: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": [ 3 | { 4 | "src": [ 5 | { 6 | "files": [ "**/*.csproj" ], 7 | "exclude": [ 8 | "**/obj/**", 9 | "**/bin/**", 10 | "_site/**", 11 | "**/*Tests*.csproj", 12 | "**/*Tests.*.csproj" 13 | ], 14 | "src": "../src" 15 | } 16 | ], 17 | "dest": "api" 18 | } 19 | ], 20 | "build": { 21 | "content": [ 22 | { 23 | "files": [ 24 | "api/**.yml", 25 | "api/index.md" 26 | ] 27 | }, 28 | { 29 | "files": [ 30 | "articles/**.md", 31 | "articles/**/toc.yml", 32 | "toc.yml", 33 | "*.md" 34 | ], 35 | "exclude": [ 36 | "obj/**", 37 | "_site/**" 38 | ] 39 | }, 40 | ], 41 | "resource": [ 42 | { 43 | "files": [ 44 | "images/**", 45 | "web.config", 46 | ], 47 | "exclude": [ 48 | "obj/**", 49 | "_site/**" 50 | ] 51 | } 52 | ], 53 | "sitemap": { 54 | "baseUrl": "https://yoursite.github.io/" 55 | }, 56 | "dest": "_site", 57 | "globalMetadata": { 58 | "_appTitle": "Petabridge.Collections", 59 | "_disableContribution": "true", 60 | "_appLogoPath": "/images/icon.png", 61 | }, 62 | "globalMetadataFiles": [], 63 | "fileMetadataFiles": [], 64 | "template": [ 65 | "default", 66 | "template" 67 | ], 68 | "postProcessors": ["ExtractSearchIndex"], 69 | "noLangKeyword": false 70 | } 71 | } -------------------------------------------------------------------------------- /src/Petabridge.Collections/IFixedSizeStack.cs: -------------------------------------------------------------------------------- 1 | // ----------------------------------------------------------------------- 2 | // 3 | // Copyright (C) 2015 - 2019 Petabridge, LLC 4 | // 5 | // ----------------------------------------------------------------------- 6 | 7 | using System.Collections; 8 | using System.Collections.Generic; 9 | 10 | namespace Petabridge.Collections 11 | { 12 | public interface IFixedSizeStack : IEnumerable, ICollection 13 | { 14 | int Capacity { get; } 15 | T Peek(); 16 | T Pop(); 17 | void Push(T item); 18 | T[] ToArray(); 19 | void Clear(); 20 | 21 | /// 22 | /// Copies the contents of the FixedSizeStack into a new array 23 | /// 24 | /// The destination array for the copy 25 | /// The starting index for copying in the destination array 26 | /// The number of items to copy from the current buffer (max value = current Size of buffer) 27 | void CopyTo(T[] array, int index, int count); 28 | 29 | /// 30 | /// Copies the contents of the FixedSizeStack into a new array 31 | /// 32 | /// The destination array for the copy 33 | void CopyTo(T[] array); 34 | 35 | /// 36 | /// Copies the contents of the FixedSizeStack into a new array 37 | /// 38 | /// The destination array for the copy 39 | /// The starting index for copying in the destination array 40 | void CopyTo(T[] array, int index); 41 | } 42 | } -------------------------------------------------------------------------------- /src/Petabridge.Collections.Tests/CircularBufferTests.cs: -------------------------------------------------------------------------------- 1 | // ----------------------------------------------------------------------- 2 | // 3 | // Copyright (C) 2015 - 2019 Petabridge, LLC 4 | // 5 | // ----------------------------------------------------------------------- 6 | 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Text; 10 | using Xunit; 11 | 12 | namespace Petabridge.Collections.Tests 13 | { 14 | public class CircularBufferTests 15 | { 16 | protected virtual ICircularBuffer GetBuffer(int capacity) 17 | { 18 | return new CircularBuffer(capacity); 19 | } 20 | 21 | 22 | /// 23 | /// If a circular buffer is defined with a fixed maximum capacity, it should 24 | /// simply overwrite the old elements even if they haven't been dequed 25 | /// 26 | [Fact] 27 | public void CircularBuffer_should_not_expand() 28 | { 29 | var byteArrayOne = Encoding.Unicode.GetBytes("ONE STRING"); 30 | var byteArrayTwo = Encoding.Unicode.GetBytes("TWO STRING"); 31 | var buffer = GetBuffer(byteArrayOne.Length); 32 | buffer.Enqueue(byteArrayOne); 33 | Assert.Equal(buffer.Size, buffer.Capacity); 34 | buffer.Enqueue(byteArrayTwo); 35 | Assert.Equal(buffer.Size, buffer.Capacity); 36 | var availableBytes = buffer.DequeueAll().ToArray(); 37 | Assert.False(byteArrayOne.SequenceEqual(availableBytes)); 38 | Assert.True(byteArrayTwo.SequenceEqual(availableBytes)); 39 | } 40 | 41 | 42 | [Fact] 43 | public void ShouldBeAbleAcceptIEnumerableAsParameter() 44 | { 45 | var list = new List {1, 2, 4}; 46 | var x = new CircularBuffer(list, 4); 47 | var t = x.Dequeue(); 48 | Assert.Equal(list[0],t); 49 | t = x.Dequeue(); 50 | Assert.Equal(list[1], t); 51 | t = x.Dequeue(); 52 | Assert.Equal(list[2], t); 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /Petabridge.Collections.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26430.14 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Petabridge.Collections", "src\Petabridge.Collections\Petabridge.Collections.csproj", "{E945AABA-2779-41E8-9B43-8898FFD64F22}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Petabridge.Collections.Tests", "src\Petabridge.Collections.Tests\Petabridge.Collections.Tests.csproj", "{0F9B9BC6-9F86-40E8-BA9B-D27BF3AC7970}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Petabridge.Collections.Tests.Performance", "src\Petabridge.Collections.Tests.Performance\Petabridge.Collections.Tests.Performance.csproj", "{CAE7CA7C-0D0C-4FDA-BDE9-BE16A27343EF}" 11 | EndProject 12 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{79D71264-186B-4F62-8930-35DD9ECCAF3B}" 13 | ProjectSection(SolutionItems) = preProject 14 | build.cmd = build.cmd 15 | build.fsx = build.fsx 16 | build.ps1 = build.ps1 17 | build.sh = build.sh 18 | EndProjectSection 19 | EndProject 20 | Global 21 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 22 | Debug|Any CPU = Debug|Any CPU 23 | Release|Any CPU = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 26 | {E945AABA-2779-41E8-9B43-8898FFD64F22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {E945AABA-2779-41E8-9B43-8898FFD64F22}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {E945AABA-2779-41E8-9B43-8898FFD64F22}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {E945AABA-2779-41E8-9B43-8898FFD64F22}.Release|Any CPU.Build.0 = Release|Any CPU 30 | {0F9B9BC6-9F86-40E8-BA9B-D27BF3AC7970}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 | {0F9B9BC6-9F86-40E8-BA9B-D27BF3AC7970}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 | {0F9B9BC6-9F86-40E8-BA9B-D27BF3AC7970}.Release|Any CPU.ActiveCfg = Release|Any CPU 33 | {0F9B9BC6-9F86-40E8-BA9B-D27BF3AC7970}.Release|Any CPU.Build.0 = Release|Any CPU 34 | {CAE7CA7C-0D0C-4FDA-BDE9-BE16A27343EF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 35 | {CAE7CA7C-0D0C-4FDA-BDE9-BE16A27343EF}.Debug|Any CPU.Build.0 = Debug|Any CPU 36 | {CAE7CA7C-0D0C-4FDA-BDE9-BE16A27343EF}.Release|Any CPU.ActiveCfg = Release|Any CPU 37 | {CAE7CA7C-0D0C-4FDA-BDE9-BE16A27343EF}.Release|Any CPU.Build.0 = Release|Any CPU 38 | EndGlobalSection 39 | GlobalSection(SolutionProperties) = preSolution 40 | HideSolutionNode = FALSE 41 | EndGlobalSection 42 | GlobalSection(ExtensibilityGlobals) = postSolution 43 | SolutionGuid = {B99E6BB8-642A-4A68-86DF-69567CBA700A} 44 | EndGlobalSection 45 | EndGlobal 46 | -------------------------------------------------------------------------------- /src/Petabridge.Collections.Tests/PriorityQueueSpecs.cs: -------------------------------------------------------------------------------- 1 | // ----------------------------------------------------------------------- 2 | // 3 | // Copyright (C) 2015 - 2019 Petabridge, LLC 4 | // 5 | // ----------------------------------------------------------------------- 6 | 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using FsCheck; 10 | using FsCheck.Xunit; 11 | 12 | namespace Petabridge.Collections.Tests 13 | { 14 | public class PriorityQueueSpecs 15 | { 16 | [Property] 17 | public Property Default_PriorityQueue_count_will_correctly_reflect_all_added_items(long[] items) 18 | { 19 | var priorityQueue = new PriorityQueue(); 20 | foreach (var item in items) 21 | priorityQueue.Enqueue(item); 22 | 23 | return (priorityQueue.Count == items.Length).Label( 24 | $"Expected priority queue to have size of {items.Length} " + 25 | $"when fully populated, but was {priorityQueue.Count}"); 26 | } 27 | 28 | [Property] 29 | public Property Default_PriorityQueue_will_contain_all_added_items(long[] items) 30 | { 31 | var priorityQueue = new PriorityQueue(); 32 | foreach (var item in items) 33 | priorityQueue.Enqueue(item); 34 | 35 | return items.All(x => priorityQueue.Contains(x)).Label( 36 | $"Expected priority queue to contain [{string.Join(";", items)}] " + 37 | $"but instead was [{string.Join(";", priorityQueue)}] (unsorted)"); 38 | } 39 | 40 | [Property] 41 | public Property Default_PriorityQueue_will_sort_items_into_ascending_order_on_dequeue(long[] items) 42 | { 43 | var priorityQueue = new PriorityQueue(); 44 | var expectedOrder = items.OrderBy(x => x).ToArray(); 45 | var actualOrder = new List(items.Length); 46 | 47 | foreach (var item in items) 48 | priorityQueue.Enqueue(item); 49 | 50 | 51 | var count = priorityQueue.Count; 52 | for (var i = 0; i < count; i++) 53 | actualOrder.Add(priorityQueue.Dequeue()); 54 | 55 | return expectedOrder.SequenceEqual(actualOrder).Label($"Expected input [{string.Join(";", items)}] to " + 56 | $"be sorted into [{string.Join(";", expectedOrder)}]" + 57 | $" but was [{string.Join(";", actualOrder)}]"); 58 | } 59 | } 60 | } -------------------------------------------------------------------------------- /src/Petabridge.Collections/ICircularBuffer.cs: -------------------------------------------------------------------------------- 1 | // ----------------------------------------------------------------------- 2 | // 3 | // Copyright (C) 2015 - 2019 Petabridge, LLC 4 | // 5 | // ----------------------------------------------------------------------- 6 | 7 | using System.Collections.Concurrent; 8 | using System.Collections.Generic; 9 | 10 | namespace Petabridge.Collections 11 | { 12 | public interface ICircularBuffer : IProducerConsumerCollection 13 | { 14 | /// 15 | /// The gets the max capacity of the buffer 16 | /// 17 | int Capacity { get; } 18 | 19 | /// 20 | /// The current size of the buffer. 21 | /// 22 | int Size { get; } 23 | 24 | /// 25 | /// Peeks at the next message in the buffer 26 | /// 27 | /// The object at the start position of the buffer 28 | T Peek(); 29 | 30 | /// 31 | /// Adds an object to the end of the circular buffer 32 | /// 33 | /// An object of type T 34 | void Enqueue(T obj); 35 | 36 | /// 37 | /// Adds an array of objects to the end of the circular buffer 38 | /// 39 | /// An array of objects of type T 40 | void Enqueue(T[] objs); 41 | 42 | /// 43 | /// Dequeues an object from the start of the circular buffer 44 | /// 45 | /// An object of type T 46 | T Dequeue(); 47 | 48 | /// 49 | /// Dequeues multiple items at once, if available 50 | /// 51 | /// The maximum number of items to dequeue 52 | /// An enumerable list of items 53 | IEnumerable Dequeue(int count); 54 | 55 | /// 56 | /// Dequeues the entire buffer in one dump 57 | /// 58 | /// All of the active contents of a circular buffer 59 | IEnumerable DequeueAll(); 60 | 61 | /// 62 | /// Clears the contents from the buffer 63 | /// 64 | void Clear(); 65 | 66 | /// 67 | /// Copies the contents of the Circular Buffer into a new array 68 | /// 69 | /// The destination array for the copy 70 | void CopyTo(T[] array); 71 | 72 | /// 73 | /// Copies the contents of the Circular Buffer into a new array 74 | /// 75 | /// The destination array for the copy 76 | /// The starting index for copying in the destination array 77 | /// The number of items to copy from the current buffer (max value = current Size of buffer) 78 | void CopyTo(T[] array, int index, int count); 79 | } 80 | } -------------------------------------------------------------------------------- /src/Petabridge.Collections/ConcurrentFixedSizeStack.cs: -------------------------------------------------------------------------------- 1 | // ----------------------------------------------------------------------- 2 | // 3 | // Copyright (C) 2015 - 2019 Petabridge, LLC 4 | // 5 | // ----------------------------------------------------------------------- 6 | 7 | namespace Petabridge.Collections 8 | { 9 | /// 10 | /// Stack with a fixed size number of members - old items get pushed 11 | /// off the stack. Concurrent. 12 | /// 13 | public class ConcurrentFixedSizeStack : FixedSizeStack 14 | { 15 | #region Internal members 16 | 17 | private readonly object m_lockObject = new object(); 18 | 19 | #endregion 20 | 21 | public ConcurrentFixedSizeStack() 22 | { 23 | } 24 | 25 | public ConcurrentFixedSizeStack(int capacity) : base(capacity) 26 | { 27 | } 28 | 29 | #region Stack Methods 30 | 31 | public override T Peek() 32 | { 33 | var item = default(T); 34 | 35 | lock (m_lockObject) 36 | { 37 | item = base.Peek(); 38 | } 39 | 40 | return item; 41 | } 42 | 43 | public override T Pop() 44 | { 45 | T item; 46 | 47 | lock (m_lockObject) 48 | { 49 | item = base.Pop(); 50 | } 51 | 52 | return item; 53 | } 54 | 55 | public override void Push(T item) 56 | { 57 | lock (m_lockObject) 58 | { 59 | base.Push(item); 60 | } 61 | } 62 | 63 | public override T[] ToArray() 64 | { 65 | lock (m_lockObject) 66 | { 67 | return base.ToArray(); 68 | } 69 | } 70 | 71 | public override void Clear() 72 | { 73 | lock (m_lockObject) 74 | { 75 | base.Clear(); 76 | } 77 | } 78 | 79 | #endregion 80 | 81 | #region ICollection methods 82 | 83 | /// 84 | /// Copies the contents of the ConcurrentFixedSizeStack into a new array 85 | /// 86 | /// The destination array for the copy 87 | /// The starting index for copying in the destination array 88 | /// The number of items to copy from the current buffer (max value = current Size of buffer) 89 | public override void CopyTo(T[] array, int index, int count) 90 | { 91 | //Make a copy of the internal buffer and pointers 92 | var internalCopy = new T[Capacity]; 93 | var internalHead = 0; 94 | var internalSize = 0; 95 | 96 | lock (m_lockObject) 97 | { 98 | _buffer.CopyTo(internalCopy, 0); 99 | internalHead = _head; 100 | internalSize = _size; 101 | } 102 | 103 | //Now we're thread-safe - have an internal copy to work with 104 | 105 | if (count > internalSize) //The maximum value of count is Size 106 | count = internalSize; 107 | 108 | for (var i = 0; i < count; i++, internalHead++, index++) array[index] = internalCopy[internalHead]; 109 | } 110 | 111 | public override object SyncRoot => m_lockObject; 112 | 113 | public override bool IsSynchronized => true; 114 | 115 | #endregion 116 | } 117 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Petabridge.Collections 2 | 3 | A set of specialized collections that should probably be in CoreFx. 4 | 5 | * `CircularBuffer` 6 | * `ConcurrentCircularBuffer` 7 | * `PriorityQueue` 8 | * `ConcurrentPriorityQueue` 9 | * `FixedSizeStack` 10 | 11 | To install, simply use the [`Petabridge.Collections` NuGet package](https://www.nuget.org/packages/Petabridge.Collections/): 12 | 13 | ``` 14 | PS> Install-Package Petabridge.Collections 15 | ``` 16 | 17 | ## Building this solution 18 | To run the build script associated with this solution, execute the following: 19 | 20 | **Windows** 21 | ``` 22 | c:\> build.cmd all 23 | ``` 24 | 25 | **Linux / OS X** 26 | ``` 27 | c:\> build.sh all 28 | ``` 29 | 30 | If you need any information on the supported commands, please execute the `build.[cmd|sh] help` command. 31 | 32 | This build script is powered by [FAKE](https://fake.build/); please see their API documentation should you need to make any changes to the [`build.fsx`](build.fsx) file. 33 | 34 | ### Conventions 35 | The attached build script will automatically do the following based on the conventions of the project names added to this project: 36 | 37 | * Any project name ending with `.Tests` will automatically be treated as a [XUnit2](https://xunit.github.io/) project and will be included during the test stages of this build script; 38 | * Any project name ending with `.Tests` will automatically be treated as a [NBench](https://github.com/petabridge/NBench) project and will be included during the test stages of this build script; and 39 | * Any project meeting neither of these conventions will be treated as a NuGet packaging target and its `.nupkg` file will automatically be placed in the `bin\nuget` folder upon running the `build.[cmd|sh] all` command. 40 | 41 | ### DocFx for Documentation 42 | This solution also supports [DocFx](http://dotnet.github.io/docfx/) for generating both API documentation and articles to describe the behavior, output, and usages of your project. 43 | 44 | All of the relevant articles you wish to write should be added to the `/docs/articles/` folder and any API documentation you might need will also appear there. 45 | 46 | All of the documentation will be statically generated and the output will be placed in the `/docs/_site/` folder. 47 | 48 | #### Previewing Documentation 49 | To preview the documentation for this project, execute the following command at the root of this folder: 50 | 51 | ``` 52 | C:\> serve-docs.cmd 53 | ``` 54 | 55 | This will use the built-in `docfx.console` binary that is installed as part of the NuGet restore process from executing any of the usual `build.cmd` or `build.sh` steps to preview the fully-rendered documentation. For best results, do this immediately after calling `build.cmd buildRelease`. 56 | 57 | ### Release Notes, Version Numbers, Etc 58 | This project will automatically populate its release notes in all of its modules via the entries written inside [`RELEASE_NOTES.md`](RELEASE_NOTES.md) and will automatically update the versions of all assemblies and NuGet packages via the metadata included inside [`common.props`](src/common.props). 59 | 60 | If you add any new projects to the solution created with this template, be sure to add the following line to each one of them in order to ensure that you can take advantage of `common.props` for standardization purposes: 61 | 62 | ``` 63 | 64 | ``` 65 | 66 | ### Code Signing via SignService 67 | This project uses [SignService](https://github.com/onovotny/SignService) to code-sign NuGet packages prior to publication. The `build.cmd` and `build.sh` scripts will automatically download the `SignClient` needed to execute code signing locally on the build agent, but it's still your responsibility to set up the SignService server per the instructions at the linked repository. 68 | 69 | Once you've gone through the ropes of setting up a code-signing server, you'll need to set a few configuration options in your project in order to use the `SignClient`: 70 | 71 | * Add your Active Directory settings to [`appsettings.json`](appsettings.json) and 72 | * Pass in your signature information to the `signingName`, `signingDescription`, and `signingUrl` values inside `build.fsx`. 73 | 74 | Whenever you're ready to run code-signing on the NuGet packages published by `build.fsx`, execute the following command: 75 | 76 | ``` 77 | C:\> build.cmd nuget SignClientSecret={your secret} SignClientUser={your username} 78 | ``` 79 | 80 | This will invoke the `SignClient` and actually execute code signing against your `.nupkg` files prior to NuGet publication. 81 | 82 | If one of these two values isn't provided, the code signing stage will skip itself and simply produce unsigned NuGet code packages. 83 | -------------------------------------------------------------------------------- /src/Petabridge.Collections/PriorityQueue.cs: -------------------------------------------------------------------------------- 1 | // ----------------------------------------------------------------------- 2 | // 3 | // Copyright (C) 2015 - 2019 Petabridge, LLC 4 | // 5 | // ----------------------------------------------------------------------- 6 | 7 | using System; 8 | using System.Collections; 9 | using System.Collections.Generic; 10 | 11 | namespace Petabridge.Collections 12 | { 13 | public class PriorityQueue : IEnumerable 14 | { 15 | private readonly IComparer _comparer; 16 | private int _capacity; 17 | private T[] _items; 18 | 19 | public PriorityQueue(IComparer comparer) 20 | { 21 | _comparer = comparer; 22 | _capacity = 11; 23 | _items = new T[_capacity]; 24 | } 25 | 26 | public PriorityQueue() 27 | : this(Comparer.Default) 28 | { 29 | } 30 | 31 | public int Count { get; private set; } 32 | 33 | /// 34 | /// TODO: need to make this return the priority queue contents in the correct order 35 | /// 36 | /// 37 | public IEnumerator GetEnumerator() 38 | { 39 | for (var i = 0; i < Count; i++) yield return _items[i]; 40 | } 41 | 42 | IEnumerator IEnumerable.GetEnumerator() 43 | { 44 | return GetEnumerator(); 45 | } 46 | 47 | public T Dequeue() 48 | { 49 | var result = Peek(); 50 | if (result == null) return default(T); 51 | 52 | var newCount = --Count; 53 | var lastItem = _items[newCount]; 54 | _items[newCount] = default(T); 55 | if (newCount > 0) TrickleDown(0, lastItem); 56 | 57 | return result; 58 | } 59 | 60 | public T Peek() 61 | { 62 | return Count == 0 ? default(T) : _items[0]; 63 | } 64 | 65 | public void Enqueue(T item) 66 | { 67 | var oldCount = Count; 68 | if (oldCount == _capacity) GrowHeap(); 69 | Count = oldCount + 1; 70 | BubbleUp(oldCount, item); 71 | } 72 | 73 | public void Remove(T item) 74 | { 75 | var index = Array.IndexOf(_items, item); 76 | if (index == -1) return; 77 | 78 | Count--; 79 | if (index == Count) 80 | { 81 | _items[index] = default(T); 82 | } 83 | else 84 | { 85 | var last = _items[Count]; 86 | _items[Count] = default(T); 87 | TrickleDown(index, last); 88 | if (Equals(_items[index], last)) BubbleUp(index, last); 89 | } 90 | } 91 | 92 | private void BubbleUp(int index, T item) 93 | { 94 | // index > 0 means there is a parent 95 | while (index > 0) 96 | { 97 | var parentIndex = (index - 1) >> 1; 98 | var parentItem = _items[parentIndex]; 99 | if (_comparer.Compare(item, parentItem) >= 0) break; 100 | _items[index] = parentItem; 101 | index = parentIndex; 102 | } 103 | 104 | _items[index] = item; 105 | } 106 | 107 | private void GrowHeap() 108 | { 109 | var oldCapacity = _capacity; 110 | _capacity = oldCapacity + (oldCapacity <= 64 ? oldCapacity + 2 : oldCapacity >> 1); 111 | var newHeap = new T[_capacity]; 112 | Array.Copy(_items, 0, newHeap, 0, Count); 113 | _items = newHeap; 114 | } 115 | 116 | private void TrickleDown(int index, T item) 117 | { 118 | var middleIndex = Count >> 1; 119 | while (index < middleIndex) 120 | { 121 | var childIndex = (index << 1) + 1; 122 | var childItem = _items[childIndex]; 123 | var rightChildIndex = childIndex + 1; 124 | if (rightChildIndex < Count 125 | && _comparer.Compare(childItem, _items[rightChildIndex]) > 0) 126 | { 127 | childIndex = rightChildIndex; 128 | childItem = _items[rightChildIndex]; 129 | } 130 | 131 | if (_comparer.Compare(item, childItem) <= 0) break; 132 | _items[index] = childItem; 133 | index = childIndex; 134 | } 135 | 136 | _items[index] = item; 137 | } 138 | 139 | public void Clear() 140 | { 141 | Count = 0; 142 | Array.Clear(_items, 0, 0); 143 | } 144 | } 145 | } -------------------------------------------------------------------------------- /src/Petabridge.Collections.Tests.Performance/AllCollectionsSpec.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Concurrent; 2 | using NBench; 3 | 4 | namespace Petabridge.Collections.Tests.Performance 5 | { 6 | /// 7 | /// Performance specs for checking the underlying Collections' implementation's read / write performance 8 | /// 9 | public class AllCollectionsSpec 10 | { 11 | private const string InsertCounterName = "ItemInserts"; 12 | private const int ItemCount = 1000; 13 | private const int ResizedItemCount = 10*ItemCount; 14 | private Counter _insertsCounter; 15 | 16 | private readonly CircularBuffer _circularBuffer = new CircularBuffer(ItemCount); 17 | 18 | private readonly ConcurrentCircularBuffer _concurrentCircularBuffer = 19 | new ConcurrentCircularBuffer(ItemCount); 20 | 21 | private readonly ConcurrentQueue _concurrentQueue = new ConcurrentQueue(); 22 | 23 | [PerfSetup] 24 | public void SetUp(BenchmarkContext context) 25 | { 26 | _insertsCounter = context.GetCounter(InsertCounterName); 27 | } 28 | 29 | [PerfBenchmark(NumberOfIterations = 13, RunMode = RunMode.Iterations, TestMode = TestMode.Measurement)] 30 | [CounterMeasurement(InsertCounterName)] 31 | [MemoryMeasurement(MemoryMetric.TotalBytesAllocated)] 32 | [GcMeasurement(GcMetric.TotalCollections, GcGeneration.AllGc)] 33 | public void CircularBufferWithoutResizing(BenchmarkContext context) 34 | { 35 | for (var i = 0; i < ItemCount;) 36 | { 37 | _circularBuffer.Add(i); 38 | _insertsCounter.Increment(); 39 | ++i; 40 | } 41 | } 42 | 43 | [PerfBenchmark(NumberOfIterations = 13, RunMode = RunMode.Iterations, TestMode = TestMode.Measurement)] 44 | [CounterMeasurement(InsertCounterName)] 45 | [MemoryMeasurement(MemoryMetric.TotalBytesAllocated)] 46 | [GcMeasurement(GcMetric.TotalCollections, GcGeneration.AllGc)] 47 | public void CircularBufferWithResizing(BenchmarkContext context) 48 | { 49 | for (var i = 0; i < ResizedItemCount;) 50 | { 51 | _circularBuffer.Add(i); 52 | _insertsCounter.Increment(); 53 | ++i; 54 | } 55 | } 56 | 57 | [PerfBenchmark(NumberOfIterations = 13, RunMode = RunMode.Iterations, TestMode = TestMode.Measurement)] 58 | [CounterMeasurement(InsertCounterName)] 59 | [MemoryMeasurement(MemoryMetric.TotalBytesAllocated)] 60 | [GcMeasurement(GcMetric.TotalCollections, GcGeneration.AllGc)] 61 | public void ConcurrentCircularBufferWithoutResizing(BenchmarkContext context) 62 | { 63 | for (var i = 0; i < ItemCount;) 64 | { 65 | _concurrentCircularBuffer.Add(i); 66 | _insertsCounter.Increment(); 67 | ++i; 68 | } 69 | } 70 | 71 | [PerfBenchmark(NumberOfIterations = 13, RunMode = RunMode.Iterations, TestMode = TestMode.Measurement)] 72 | [CounterMeasurement(InsertCounterName)] 73 | [MemoryMeasurement(MemoryMetric.TotalBytesAllocated)] 74 | [GcMeasurement(GcMetric.TotalCollections, GcGeneration.AllGc)] 75 | public void ConcurrentCircularBufferWithResizing(BenchmarkContext context) 76 | { 77 | for (var i = 0; i < ResizedItemCount;) 78 | { 79 | _concurrentCircularBuffer.Add(i); 80 | _insertsCounter.Increment(); 81 | ++i; 82 | } 83 | } 84 | 85 | [PerfBenchmark(NumberOfIterations = 13, RunMode = RunMode.Iterations, TestMode = TestMode.Measurement)] 86 | [CounterMeasurement(InsertCounterName)] 87 | [MemoryMeasurement(MemoryMetric.TotalBytesAllocated)] 88 | [GcMeasurement(GcMetric.TotalCollections, GcGeneration.AllGc)] 89 | public void ConcurrentQueueWithoutResizing(BenchmarkContext context) 90 | { 91 | for (var i = 0; i < ItemCount;) 92 | { 93 | _concurrentQueue.Enqueue(i); 94 | _insertsCounter.Increment(); 95 | ++i; 96 | } 97 | } 98 | 99 | [PerfBenchmark(NumberOfIterations = 13, RunMode = RunMode.Iterations, TestMode = TestMode.Measurement)] 100 | [CounterMeasurement(InsertCounterName)] 101 | [MemoryMeasurement(MemoryMetric.TotalBytesAllocated)] 102 | [GcMeasurement(GcMetric.TotalCollections, GcGeneration.AllGc)] 103 | public void ConcurrentQueueWithResizing(BenchmarkContext context) 104 | { 105 | for (var i = 0; i < ResizedItemCount;) 106 | { 107 | _concurrentQueue.Enqueue(i); 108 | _insertsCounter.Increment(); 109 | ++i; 110 | } 111 | } 112 | } 113 | } -------------------------------------------------------------------------------- /src/Petabridge.Collections/FixedSizeStack.cs: -------------------------------------------------------------------------------- 1 | // ----------------------------------------------------------------------- 2 | // 3 | // Copyright (C) 2015 - 2019 Petabridge, LLC 4 | // 5 | // ----------------------------------------------------------------------- 6 | 7 | using System; 8 | using System.Collections; 9 | using System.Collections.Generic; 10 | 11 | namespace Petabridge.Collections 12 | { 13 | /// 14 | /// Stack with a fixed size number of members - old items get pushed 15 | /// off the stack 16 | /// 17 | public class FixedSizeStack : IFixedSizeStack 18 | { 19 | /// 20 | /// Default capacity for fixed size stacks 21 | /// 22 | // ReSharper disable once InconsistentNaming 23 | public const int DEFAULT_CAPACITY = 10; 24 | 25 | public FixedSizeStack() : this(DEFAULT_CAPACITY) 26 | { 27 | } 28 | 29 | public FixedSizeStack(int capacity) 30 | { 31 | Capacity = capacity; 32 | _size = 0; 33 | _head = 0; 34 | _buffer = new T[capacity]; 35 | } 36 | 37 | #region Properties 38 | 39 | public int Capacity { get; } 40 | 41 | #endregion 42 | 43 | #region Internal Fields 44 | 45 | protected int _size; 46 | protected int _head; 47 | 48 | /// 49 | /// The buffer itself 50 | /// 51 | protected T[] _buffer; 52 | 53 | #endregion 54 | 55 | #region Stack Methods 56 | 57 | public virtual T Peek() 58 | { 59 | var item = default(T); 60 | 61 | if (Count > 0) 62 | return _buffer[_head % Capacity]; 63 | 64 | return item; 65 | } 66 | 67 | public virtual T Pop() 68 | { 69 | var item = default(T); 70 | 71 | if (Count > 0) 72 | { 73 | item = _buffer[_head % Capacity]; 74 | if (_head > 0) //Don't decrement the head if it's zero 75 | _head--; 76 | _size--; 77 | } 78 | 79 | return item; 80 | } 81 | 82 | public virtual void Push(T item) 83 | { 84 | if (Count > 0) 85 | _head++; 86 | 87 | _buffer[_head % Capacity] = item; 88 | 89 | if (Count < Capacity) 90 | _size++; 91 | } 92 | 93 | public virtual T[] ToArray() 94 | { 95 | var resultArray = new T[_size]; 96 | var availableItems = _size; 97 | var head = _head; 98 | 99 | for (var i = 0; i < availableItems; i++, head--) resultArray[i] = _buffer[head % Capacity]; 100 | 101 | return resultArray; 102 | } 103 | 104 | public virtual void Clear() 105 | { 106 | _size = 0; 107 | _head = 0; 108 | } 109 | 110 | #endregion 111 | 112 | #region IEnumerable members 113 | 114 | public virtual IEnumerator GetEnumerator() 115 | { 116 | return ((IEnumerable) ToArray()).GetEnumerator(); 117 | } 118 | 119 | IEnumerator IEnumerable.GetEnumerator() 120 | { 121 | return GetEnumerator(); 122 | } 123 | 124 | #endregion 125 | 126 | #region ICollection members 127 | 128 | /// 129 | /// Copies the contents of the FixedSizeStack into a new array 130 | /// 131 | /// The destination array for the copy 132 | /// The starting index for copying in the destination array 133 | /// The number of items to copy from the current buffer (max value = current Size of buffer) 134 | public virtual void CopyTo(T[] array, int index, int count) 135 | { 136 | //Make a copy of the internal buffer and pointers 137 | var internalCopy = new T[Capacity]; 138 | var internalHead = 0; 139 | var internalSize = 0; 140 | 141 | _buffer.CopyTo(internalCopy, 0); 142 | internalHead = _head; 143 | internalSize = _size; 144 | 145 | if (count > internalSize) //The maximum value of count is Size 146 | count = internalSize; 147 | 148 | for (var i = 0; i < count; i++, internalHead++, index++) array[index] = internalCopy[internalHead]; 149 | } 150 | 151 | /// 152 | /// Copies the contents of the FixedSizeStack into a new array 153 | /// 154 | /// The destination array for the copy 155 | public virtual void CopyTo(T[] array) 156 | { 157 | CopyTo(array, 0); 158 | } 159 | 160 | /// 161 | /// Copies the contents of the FixedSizeStack into a new array 162 | /// 163 | /// The destination array for the copy 164 | /// The starting index for copying in the destination array 165 | public virtual void CopyTo(T[] array, int index) 166 | { 167 | CopyTo(array, index, Count); 168 | } 169 | 170 | public virtual void CopyTo(Array array, int index) 171 | { 172 | CopyTo((T[]) array, index); 173 | } 174 | 175 | public virtual int Count => _size; 176 | 177 | public virtual object SyncRoot { get; private set; } 178 | 179 | public virtual bool IsSynchronized => false; 180 | 181 | #endregion 182 | } 183 | } -------------------------------------------------------------------------------- /src/Petabridge.Collections.Tests/ConcurrentFixedSizeStackTests.cs: -------------------------------------------------------------------------------- 1 | // ----------------------------------------------------------------------- 2 | // 3 | // Copyright (C) 2015 - 2019 Petabridge, LLC 4 | // 5 | // ----------------------------------------------------------------------- 6 | 7 | using Xunit; 8 | 9 | namespace Petabridge.Collections.Tests 10 | { 11 | public class ConcurrentFixedSizeStackTests 12 | { 13 | /// 14 | /// Should be able to fill a concurrent beyond capacity with no problems 15 | /// 16 | [Fact] 17 | public void Should_add_items_to_Stack_is_over_Capacity() 18 | { 19 | //arrange 20 | var items = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 21 | var stack = new ConcurrentFixedSizeStack(5); 22 | 23 | //act 24 | Assert.Empty(stack); 25 | 26 | foreach (var item in items) 27 | { 28 | stack.Push(item); 29 | Assert.Equal(item, stack.Peek()); 30 | } 31 | 32 | //assert 33 | Assert.Equal(stack.Capacity, stack.Count); // "Stack should be full to capacity" 34 | 35 | var length = items.Length - 1; 36 | for (var i = 0; i < stack.Capacity; i++, length--) 37 | { 38 | var stackItem = stack.Pop(); 39 | Assert.Equal(items[length], stackItem); 40 | } 41 | 42 | Assert.True(length == items.Length / 2 - 1); 43 | } 44 | 45 | /// 46 | /// Should be able to fill a concurrent stack to capacity with no problems 47 | /// 48 | [Fact] 49 | public void Should_add_items_to_Stack_up_to_Capacity() 50 | { 51 | //arrange 52 | var items = new[] {1, 2, 3, 4, 5}; 53 | var stack = new ConcurrentFixedSizeStack(5); 54 | 55 | //act 56 | Assert.Empty(stack); 57 | Assert.Equal(items.Length, stack.Capacity); 58 | 59 | foreach (var item in items) 60 | { 61 | stack.Push(item); 62 | Assert.Equal(item, stack.Peek()); 63 | } 64 | 65 | //assert 66 | Assert.Equal(stack.Capacity, stack.Count); 67 | 68 | var length = stack.Capacity - 1; 69 | for (var i = 0; i < stack.Capacity; i++, length--) 70 | { 71 | var stackItem = stack.Pop(); 72 | Assert.Equal(items[length], stackItem); 73 | } 74 | } 75 | 76 | /// 77 | /// When a stack has been filled to capacity, we should get an array of all items back 78 | /// 79 | [Fact] 80 | public void Should_get_array_of_all_items_when_Stack_is_at_Capacity() 81 | { 82 | //arrange 83 | var items = new[] {1, 2, 3, 4, 5}; 84 | var stack = new ConcurrentFixedSizeStack(5); 85 | 86 | //act 87 | Assert.Empty(stack); 88 | Assert.Equal(items.Length, stack.Capacity); 89 | 90 | foreach (var item in items) 91 | { 92 | stack.Push(item); 93 | Assert.Equal(item, stack.Peek()); 94 | } 95 | 96 | var array = stack.ToArray(); 97 | 98 | //assert 99 | Assert.Equal(stack.Capacity, stack.Count); // "Stack should STILL be full to capacity" 100 | Assert.Equal(array.Length, stack.Count); // "Resultant array and original Stack should be of same size" 101 | 102 | var arrCount = items.Length - 1; 103 | foreach (var i in array) 104 | { 105 | Assert.Equal(items[arrCount], i); 106 | arrCount--; 107 | } 108 | } 109 | 110 | /// 111 | /// When a stack has been filled over capacity, we should get an array of the most recent items back 112 | /// 113 | [Fact] 114 | public void Should_get_array_of_all_items_when_Stack_is_over_Capacity() 115 | { 116 | //arrange 117 | var items = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 118 | var stack = new ConcurrentFixedSizeStack(5); 119 | 120 | //act 121 | Assert.Empty(stack); 122 | 123 | foreach (var item in items) 124 | { 125 | stack.Push(item); 126 | Assert.Equal(item, stack.Peek()); 127 | } 128 | 129 | var array = stack.ToArray(); 130 | 131 | //assert 132 | Assert.Equal(stack.Capacity, stack.Count); // "Stack should STILL be full to capacity" 133 | Assert.Equal(array.Length, stack.Count); // "Resultant array and original Stack should be of same size" 134 | 135 | var arrCount = items.Length - 1; 136 | foreach (var i in array) 137 | { 138 | Assert.Equal(items[arrCount], i); 139 | arrCount--; 140 | } 141 | 142 | Assert.True(arrCount == items.Length / 2 - 1); 143 | } 144 | 145 | /// 146 | /// If we call Stack.Array() when the stack has no items, we should get a non-null array of size 0 147 | /// 148 | [Fact] 149 | public void Should_get_array_with_no_items_on_ToArray_when_Empty() 150 | { 151 | //arrange 152 | var stack = new ConcurrentFixedSizeStack(5); 153 | 154 | //act 155 | var array = stack.ToArray(); 156 | 157 | //assert 158 | Assert.NotNull(array); 159 | Assert.Empty(array); 160 | } 161 | 162 | /// 163 | /// Should get a default value, rather than an exception, if we peek or pop when the stack is empty 164 | /// 165 | [Fact] 166 | public void Should_get_default_value_on_Stack_Peek_or_Pop_when_Empty() 167 | { 168 | //arrange 169 | var stack = new ConcurrentFixedSizeStack(5); 170 | 171 | //act 172 | 173 | //assert 174 | Assert.Equal(default(int), stack.Pop()); 175 | Assert.Equal(default(int), stack.Peek()); 176 | } 177 | } 178 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Custom 2 | tools/ 3 | build/ 4 | .nuget/ 5 | .dotnet/ 6 | .idea/ 7 | .[Dd][Ss]_[Ss]tore 8 | 9 | ## NBench output 10 | [Pp]erf[Rr]esult*/ 11 | 12 | ## Ignore Visual Studio temporary files, build results, and 13 | ## files generated by popular Visual Studio add-ons. 14 | ## 15 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 16 | 17 | # User-specific files 18 | *.suo 19 | *.user 20 | *.userosscache 21 | *.sln.docstates 22 | 23 | # User-specific files (MonoDevelop/Xamarin Studio) 24 | *.userprefs 25 | 26 | # Build results 27 | [Dd]ebug/ 28 | [Dd]ebugPublic/ 29 | [Rr]elease/ 30 | [Rr]eleases/ 31 | x64/ 32 | x86/ 33 | bld/ 34 | [Bb]in/ 35 | [Oo]bj/ 36 | [Ll]og/ 37 | 38 | #FAKE 39 | .fake 40 | tools/ 41 | 42 | #DocFx output 43 | _site/ 44 | 45 | # Visual Studio 2015 cache/options directory 46 | .vs/ 47 | # Uncomment if you have tasks that create the project's static files in wwwroot 48 | #wwwroot/ 49 | 50 | # MSTest test Results 51 | [Tt]est[Rr]esult*/ 52 | [Bb]uild[Ll]og.* 53 | 54 | # NUNIT 55 | *.VisualState.xml 56 | TestResult.xml 57 | 58 | # Build Results of an ATL Project 59 | [Dd]ebugPS/ 60 | [Rr]eleasePS/ 61 | dlldata.c 62 | 63 | # .NET Core 64 | project.lock.json 65 | project.fragment.lock.json 66 | artifacts/ 67 | **/Properties/launchSettings.json 68 | 69 | *_i.c 70 | *_p.c 71 | *_i.h 72 | *.ilk 73 | *.meta 74 | *.obj 75 | *.pch 76 | *.pdb 77 | *.pgc 78 | *.pgd 79 | *.rsp 80 | *.sbr 81 | *.tlb 82 | *.tli 83 | *.tlh 84 | *.tmp 85 | *.tmp_proj 86 | *.log 87 | *.vspscc 88 | *.vssscc 89 | .builds 90 | *.pidb 91 | *.svclog 92 | *.scc 93 | 94 | # Chutzpah Test files 95 | _Chutzpah* 96 | 97 | # Visual C++ cache files 98 | ipch/ 99 | *.aps 100 | *.ncb 101 | *.opendb 102 | *.opensdf 103 | *.sdf 104 | *.cachefile 105 | *.VC.db 106 | *.VC.VC.opendb 107 | 108 | # Visual Studio profiler 109 | *.psess 110 | *.vsp 111 | *.vspx 112 | *.sap 113 | 114 | # TFS 2012 Local Workspace 115 | $tf/ 116 | 117 | # Guidance Automation Toolkit 118 | *.gpState 119 | 120 | # ReSharper is a .NET coding add-in 121 | _ReSharper*/ 122 | *.[Rr]e[Ss]harper 123 | *.DotSettings.user 124 | 125 | # JustCode is a .NET coding add-in 126 | .JustCode 127 | 128 | # TeamCity is a build add-in 129 | _TeamCity* 130 | 131 | # DotCover is a Code Coverage Tool 132 | *.dotCover 133 | 134 | # Visual Studio code coverage results 135 | *.coverage 136 | *.coveragexml 137 | 138 | # NCrunch 139 | _NCrunch_* 140 | .*crunch*.local.xml 141 | nCrunchTemp_* 142 | 143 | # MightyMoose 144 | *.mm.* 145 | AutoTest.Net/ 146 | 147 | # Web workbench (sass) 148 | .sass-cache/ 149 | 150 | # Installshield output folder 151 | [Ee]xpress/ 152 | 153 | # DocProject is a documentation generator add-in 154 | DocProject/buildhelp/ 155 | DocProject/Help/*.HxT 156 | DocProject/Help/*.HxC 157 | DocProject/Help/*.hhc 158 | DocProject/Help/*.hhk 159 | DocProject/Help/*.hhp 160 | DocProject/Help/Html2 161 | DocProject/Help/html 162 | 163 | # Click-Once directory 164 | publish/ 165 | 166 | # Publish Web Output 167 | *.[Pp]ublish.xml 168 | *.azurePubxml 169 | # TODO: Comment the next line if you want to checkin your web deploy settings 170 | # but database connection strings (with potential passwords) will be unencrypted 171 | *.pubxml 172 | *.publishproj 173 | 174 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 175 | # checkin your Azure Web App publish settings, but sensitive information contained 176 | # in these scripts will be unencrypted 177 | PublishScripts/ 178 | 179 | # NuGet Packages 180 | *.nupkg 181 | # The packages folder can be ignored because of Package Restore 182 | **/packages/* 183 | # except build/, which is used as an MSBuild target. 184 | !**/packages/build/ 185 | # Uncomment if necessary however generally it will be regenerated when needed 186 | #!**/packages/repositories.config 187 | # NuGet v3's project.json files produces more ignorable files 188 | *.nuget.props 189 | *.nuget.targets 190 | 191 | # Microsoft Azure Build Output 192 | csx/ 193 | *.build.csdef 194 | 195 | # Microsoft Azure Emulator 196 | ecf/ 197 | rcf/ 198 | 199 | # Windows Store app package directories and files 200 | AppPackages/ 201 | BundleArtifacts/ 202 | Package.StoreAssociation.xml 203 | _pkginfo.txt 204 | 205 | # Visual Studio cache files 206 | # files ending in .cache can be ignored 207 | *.[Cc]ache 208 | # but keep track of directories ending in .cache 209 | !*.[Cc]ache/ 210 | 211 | # Others 212 | ClientBin/ 213 | ~$* 214 | *~ 215 | *.dbmdl 216 | *.dbproj.schemaview 217 | *.jfm 218 | *.pfx 219 | *.publishsettings 220 | orleans.codegen.cs 221 | 222 | # Since there are multiple workflows, uncomment next line to ignore bower_components 223 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 224 | #bower_components/ 225 | 226 | # RIA/Silverlight projects 227 | Generated_Code/ 228 | 229 | # Backup & report files from converting an old project file 230 | # to a newer Visual Studio version. Backup files are not needed, 231 | # because we have git ;-) 232 | _UpgradeReport_Files/ 233 | Backup*/ 234 | UpgradeLog*.XML 235 | UpgradeLog*.htm 236 | 237 | # SQL Server files 238 | *.mdf 239 | *.ldf 240 | *.ndf 241 | 242 | # Business Intelligence projects 243 | *.rdl.data 244 | *.bim.layout 245 | *.bim_*.settings 246 | 247 | # Microsoft Fakes 248 | FakesAssemblies/ 249 | 250 | # GhostDoc plugin setting file 251 | *.GhostDoc.xml 252 | 253 | # Node.js Tools for Visual Studio 254 | .ntvs_analysis.dat 255 | node_modules/ 256 | 257 | # Typescript v1 declaration files 258 | typings/ 259 | 260 | # Visual Studio 6 build log 261 | *.plg 262 | 263 | # Visual Studio 6 workspace options file 264 | *.opt 265 | 266 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 267 | *.vbw 268 | 269 | # Visual Studio LightSwitch build output 270 | **/*.HTMLClient/GeneratedArtifacts 271 | **/*.DesktopClient/GeneratedArtifacts 272 | **/*.DesktopClient/ModelManifest.xml 273 | **/*.Server/GeneratedArtifacts 274 | **/*.Server/ModelManifest.xml 275 | _Pvt_Extensions 276 | 277 | # Paket dependency manager 278 | .paket/paket.exe 279 | paket-files/ 280 | 281 | # FAKE - F# Make 282 | .fake/ 283 | 284 | # JetBrains Rider 285 | .idea/ 286 | *.sln.iml 287 | 288 | # CodeRush 289 | .cr/ 290 | 291 | # Python Tools for Visual Studio (PTVS) 292 | __pycache__/ 293 | *.pyc 294 | 295 | # Cake - Uncomment if you are using it 296 | # tools/** 297 | # !tools/packages.config 298 | 299 | # Telerik's JustMock configuration file 300 | *.jmconfig 301 | 302 | # BizTalk build output 303 | *.btp.cs 304 | *.btm.cs 305 | *.odx.cs 306 | *.xsd.cs 307 | -------------------------------------------------------------------------------- /src/Petabridge.Collections/ConcurrentCircularBuffer.cs: -------------------------------------------------------------------------------- 1 | // ----------------------------------------------------------------------- 2 | // 3 | // Copyright (C) 2015 - 2019 Petabridge, LLC 4 | // 5 | // ----------------------------------------------------------------------- 6 | 7 | using System; 8 | using System.Collections; 9 | using System.Collections.Generic; 10 | using System.Linq; 11 | 12 | namespace Petabridge.Collections 13 | { 14 | /// 15 | /// Concurrent circular buffer implementation, synchronized using a monitor 16 | /// 17 | /// 18 | public class ConcurrentCircularBuffer : ICircularBuffer 19 | { 20 | #region Internal members 21 | 22 | /// 23 | /// The buffer itself 24 | /// 25 | private readonly T[] _buffer; 26 | 27 | #endregion 28 | 29 | private bool _full; 30 | 31 | public ConcurrentCircularBuffer(int capacity) 32 | { 33 | Head = 0; 34 | Tail = 0; 35 | _buffer = new T[capacity]; 36 | } 37 | 38 | private int SizeUnsafe => _full ? Capacity : (Tail - Head + Capacity) % Capacity; 39 | 40 | /// 41 | /// FOR TESTING PURPOSES ONLY 42 | /// 43 | internal int Head { get; private set; } 44 | 45 | /// 46 | /// FOR TESTING PURPOSES ONLY 47 | /// 48 | internal int Tail { get; private set; } 49 | 50 | public bool IsReadOnly => false; 51 | 52 | public int Capacity => _buffer.Length; 53 | 54 | 55 | public int Size 56 | { 57 | get 58 | { 59 | lock (SyncRoot) 60 | { 61 | return SizeUnsafe; 62 | } 63 | } 64 | } 65 | 66 | public T Peek() 67 | { 68 | lock (SyncRoot) 69 | { 70 | return _buffer[Head]; 71 | } 72 | } 73 | 74 | public void Enqueue(T obj) 75 | { 76 | lock (SyncRoot) 77 | { 78 | UnsafeEnqueue(obj); 79 | } 80 | } 81 | 82 | public void Enqueue(T[] objs) 83 | { 84 | //Expand 85 | lock (SyncRoot) 86 | { 87 | foreach (var item in objs) UnsafeEnqueue(item); 88 | } 89 | } 90 | 91 | public T Dequeue() 92 | { 93 | lock (SyncRoot) 94 | { 95 | return UnsafeDequeue(); 96 | } 97 | } 98 | 99 | public IEnumerable Dequeue(int count) 100 | { 101 | IList returnItems; 102 | 103 | lock (SyncRoot) 104 | { 105 | var availabileItems = Math.Min(count, SizeUnsafe); 106 | returnItems = new List(availabileItems); 107 | 108 | if (availabileItems == 0) 109 | return returnItems; 110 | 111 | for (var i = 0; i < availabileItems; i++) returnItems.Add(UnsafeDequeue()); 112 | } 113 | 114 | return returnItems; 115 | } 116 | 117 | public IEnumerable DequeueAll() 118 | { 119 | return Dequeue(Size); 120 | } 121 | 122 | public void Clear() 123 | { 124 | lock (SyncRoot) 125 | { 126 | Head = 0; 127 | Tail = 0; 128 | _full = false; 129 | } 130 | } 131 | 132 | public void CopyTo(T[] array) 133 | { 134 | CopyTo(array, 0); 135 | } 136 | 137 | public void CopyTo(T[] array, int index) 138 | { 139 | CopyTo(array, index, Size); 140 | } 141 | 142 | public bool TryAdd(T item) 143 | { 144 | Enqueue(item); 145 | return true; 146 | } 147 | 148 | public bool TryTake(out T item) 149 | { 150 | item = default(T); 151 | if (Size == 0) return false; 152 | item = Dequeue(); 153 | return true; 154 | } 155 | 156 | public T[] ToArray() 157 | { 158 | lock (SyncRoot) 159 | { 160 | var bufferCopy = new T[SizeUnsafe]; 161 | CopyToUnsafe(bufferCopy, 0, bufferCopy.Length); 162 | return bufferCopy; 163 | } 164 | } 165 | 166 | public void CopyTo(T[] array, int index, int count) 167 | { 168 | lock (SyncRoot) 169 | { 170 | CopyToUnsafe(array, index, count); 171 | } 172 | } 173 | 174 | public void CopyTo(Array array, int index) 175 | { 176 | CopyTo((T[]) array, index); 177 | } 178 | 179 | public IEnumerator GetEnumerator() 180 | { 181 | return ((IEnumerable) ToArray()).GetEnumerator(); 182 | } 183 | 184 | IEnumerator IEnumerable.GetEnumerator() 185 | { 186 | return GetEnumerator(); 187 | } 188 | 189 | public int Count => Size; 190 | 191 | public object SyncRoot { get; } = new object(); 192 | 193 | public bool IsSynchronized => true; 194 | 195 | private void UnsafeEnqueue(T obj) 196 | { 197 | _full = _full || Tail + 1 == Capacity; // leave FULL flag on 198 | _buffer[Tail] = obj; 199 | Tail = (Tail + 1) % Capacity; 200 | } 201 | 202 | private T UnsafeDequeue() 203 | { 204 | _full = false; // full is always false as soon as we dequeue 205 | var item = _buffer[Head]; 206 | Head = (Head + 1) % Capacity; 207 | return item; 208 | } 209 | 210 | public void Add(T item) 211 | { 212 | Enqueue(item); 213 | } 214 | 215 | public bool Contains(T item) 216 | { 217 | lock (SyncRoot) 218 | { 219 | return _buffer.Any(x => x.GetHashCode() == item.GetHashCode()); 220 | } 221 | } 222 | 223 | public bool Remove(T item) 224 | { 225 | return false; 226 | } 227 | 228 | private void CopyToUnsafe(T[] array, int index, int count) 229 | { 230 | if (count > SizeUnsafe) //The maximum value of count is Size 231 | count = SizeUnsafe; 232 | 233 | var bufferBegin = Head; 234 | for (var i = 0; i < count; i++, bufferBegin = (bufferBegin + 1) % Capacity, index++) 235 | array[index] = _buffer[bufferBegin]; 236 | } 237 | } 238 | } -------------------------------------------------------------------------------- /src/Petabridge.Collections/CircularBuffer.cs: -------------------------------------------------------------------------------- 1 | // ----------------------------------------------------------------------- 2 | // 3 | // Copyright (C) 2015 - 2019 Petabridge, LLC 4 | // 5 | // ----------------------------------------------------------------------- 6 | 7 | using System; 8 | using System.Collections; 9 | using System.Collections.Generic; 10 | using System.Linq; 11 | 12 | namespace Petabridge.Collections 13 | { 14 | /// 15 | /// Base class for working with circular buffers 16 | /// 17 | /// The type being stored in the circular buffer 18 | public class CircularBuffer : ICircularBuffer 19 | { 20 | private bool _full; 21 | 22 | /// 23 | /// Front of the buffer 24 | /// 25 | private int _head; 26 | 27 | /// 28 | /// Back of the buffer 29 | /// 30 | private int _tail; 31 | 32 | /// 33 | /// The buffer itself 34 | /// 35 | protected T[] Buffer; 36 | 37 | public CircularBuffer(int capacity) 38 | { 39 | if (capacity > 0) 40 | { 41 | _head = 0; 42 | _tail = 0; 43 | Buffer = new T[capacity]; 44 | } 45 | else 46 | { 47 | throw new Exception("Buffer size has to be greater than 0"); 48 | } 49 | } 50 | 51 | public CircularBuffer(IEnumerable values, int capacity) 52 | { 53 | _head = 0; 54 | _tail = 0; 55 | Buffer = new T[capacity]; 56 | foreach (var x in values) 57 | { 58 | Add(x); 59 | } 60 | } 61 | 62 | /// 63 | /// FOR TESTING PURPOSES ONLY 64 | /// 65 | internal int Head => _head; 66 | 67 | /// 68 | /// FOR TESTING PURPOSES ONLY 69 | /// 70 | internal int Tail => _tail; 71 | 72 | public bool IsReadOnly { get; private set; } 73 | 74 | public int Capacity => Buffer.Length; 75 | 76 | // We use an N+1 trick here to make sure we can distinguish full queues from empty ones 77 | public int Size => _full ? Capacity : (_tail - _head + Capacity) % Capacity; 78 | 79 | public virtual T Peek() 80 | { 81 | return Buffer[_head]; 82 | } 83 | 84 | public virtual void Enqueue(T obj) 85 | { 86 | _full = _full || _tail + 1 == Capacity; // leave FULL flag on 87 | Buffer[_tail] = obj; 88 | _tail = (_tail + 1) % Capacity; 89 | } 90 | 91 | public void Enqueue(T[] objs) 92 | { 93 | foreach (var item in objs) Enqueue(item); 94 | } 95 | 96 | public virtual T Dequeue() 97 | { 98 | _full = false; // full is always false as soon as we dequeue 99 | var item = Buffer[_head]; 100 | _head = (_head + 1) % Capacity; 101 | return item; 102 | } 103 | 104 | public virtual IEnumerable Dequeue(int count) 105 | { 106 | var availabileItems = Math.Min(count, Size); 107 | var returnItems = new List(availabileItems); 108 | for (var i = 0; i < availabileItems; i++) returnItems.Add(Dequeue()); 109 | 110 | return returnItems; 111 | } 112 | 113 | public IEnumerable DequeueAll() 114 | { 115 | return Dequeue(Size); 116 | } 117 | 118 | public virtual void Clear() 119 | { 120 | _head = 0; 121 | _tail = 0; 122 | _full = false; 123 | } 124 | 125 | /// 126 | /// Copies the contents of the Circular Buffer into a new array 127 | /// 128 | /// The destination array for the copy 129 | public void CopyTo(T[] array) 130 | { 131 | CopyTo(array, 0); 132 | } 133 | 134 | /// 135 | /// Copies the contents of the Circular Buffer into a new array 136 | /// 137 | /// The destination array for the copy 138 | /// The starting index for copying in the destination array 139 | public void CopyTo(T[] array, int index) 140 | { 141 | CopyTo(array, index, Size); 142 | } 143 | 144 | public bool TryAdd(T item) 145 | { 146 | Enqueue(item); 147 | return true; 148 | } 149 | 150 | public bool TryTake(out T item) 151 | { 152 | item = Dequeue(); 153 | return item != null; 154 | } 155 | 156 | public T[] ToArray() 157 | { 158 | var bufferCopy = new T[Size]; 159 | CopyTo(bufferCopy, 0, bufferCopy.Length); 160 | return bufferCopy; 161 | } 162 | 163 | /// 164 | /// Copies the contents of the Circular Buffer into a new array 165 | /// 166 | /// The destination array for the copy 167 | /// The starting index for copying in the destination array 168 | /// The number of items to copy from the current buffer (max value = current Size of buffer) 169 | public void CopyTo(T[] array, int index, int count) 170 | { 171 | if (count > Size) //The maximum value of count is Size 172 | count = Size; 173 | 174 | var bufferBegin = _head; 175 | for (var i = 0; i < count; i++, bufferBegin = (bufferBegin + 1) % Capacity, index++) 176 | array[index] = Buffer[bufferBegin]; 177 | } 178 | 179 | public IEnumerator GetEnumerator() 180 | { 181 | return ((IEnumerable) ToArray()).GetEnumerator(); 182 | } 183 | 184 | IEnumerator IEnumerable.GetEnumerator() 185 | { 186 | return GetEnumerator(); 187 | } 188 | 189 | public void CopyTo(Array array, int index) 190 | { 191 | CopyTo((T[]) array, index); 192 | } 193 | 194 | public int Count => Size; 195 | public virtual object SyncRoot { get; private set; } 196 | 197 | public virtual bool IsSynchronized => false; 198 | 199 | public void Add(T item) 200 | { 201 | Enqueue(item); 202 | } 203 | 204 | public bool Contains(T item) 205 | { 206 | return Buffer.Any(x => x.GetHashCode() == item.GetHashCode()); 207 | } 208 | 209 | public bool Remove(T item) 210 | { 211 | return false; 212 | } 213 | 214 | public override string ToString() 215 | { 216 | return $"CircularBuffer<{GetType()}>(Capacity = {Capacity})"; 217 | } 218 | 219 | } 220 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2015-2019 Petabridge, LLC 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /serve-docs.ps1: -------------------------------------------------------------------------------- 1 | # docfx.ps1 2 | $VisualStudioVersion = "15.0"; 3 | $DotnetSDKVersion = "2.0.0"; 4 | 5 | # Get dotnet paths 6 | $MSBuildExtensionsPath = "C:\Program Files\dotnet\sdk\" + $DotnetSDKVersion; 7 | $MSBuildSDKsPath = $MSBuildExtensionsPath + "\SDKs"; 8 | 9 | # Get Visual Studio install path 10 | $VSINSTALLDIR = $(Get-ItemProperty "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7").$VisualStudioVersion; 11 | 12 | # Add Visual Studio environment variables 13 | $env:VisualStudioVersion = $VisualStudioVersion; 14 | $env:VSINSTALLDIR = $VSINSTALLDIR; 15 | 16 | # Add dotnet environment variables 17 | $env:MSBuildExtensionsPath = $MSBuildExtensionsPath; 18 | $env:MSBuildSDKsPath = $MSBuildSDKsPath; 19 | 20 | # Build our docs 21 | & .\tools\docfx.console\tools\docfx @args 22 | # SIG # Begin signature block 23 | # MIIgTwYJKoZIhvcNAQcCoIIgQDCCIDwCAQExDzANBglghkgBZQMEAgEFADB5Bgor 24 | # BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG 25 | # KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCAu28PwIacVj8hJ 26 | # OhR6cc8rx13+1IgHGI8+2asnxss7+KCCDiIwggO3MIICn6ADAgECAhAM5+DlF9hG 27 | # /o/lYPwb8DA5MA0GCSqGSIb3DQEBBQUAMGUxCzAJBgNVBAYTAlVTMRUwEwYDVQQK 28 | # EwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5jb20xJDAiBgNV 29 | # BAMTG0RpZ2lDZXJ0IEFzc3VyZWQgSUQgUm9vdCBDQT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DAc 106 | # BgorBgEEAYI3AgELMQ4wDAYKKwYBBAGCNwIBFTAvBgkqhkiG9w0BCQQxIgQgsjXi 107 | # Twp+0Eh6IO6yNtqHu++IvsrTjuOHyEJBPpRRUBwwgZQGCisGAQQBgjcCAQwxgYUw 108 | # gYKgSIBGAFAAZQB0AGEAYgByAGkAZABnAGUAIABTAHQAYQBuAGQAYQByAGQAIABC 109 | # AHUAaQBsAGQAIABUAGUAbQBwAGwAYQB0AGUAc6E2gDRodHRwczovL2dpdGh1Yi5j 110 | # b20vcGV0YWJyaWRnZS9wZXRhYnJpZGdlLWRvdG5ldC1uZXcgMA0GCSqGSIb3DQEB 111 | # AQUABIIBAFL0OBBmM22Pxb5zeNkbUMFeQkbRlptkjl7hz4L2qHxbZOznCToKLJFv 112 | # Gh1QBhVGPkdw4+gFwSgvrOtrmLhh/prC0k1TDXx4za6bcvMmclQR7U6UAuQ9uUGD 113 | # 87KDlQb9bY6KF7LrihG85WDhHe4hfMAAVOdU7UW/3Oc8Womu7AU20rUljg0wjEd6 114 | # ad4Wudo2zKIKnnfY1K0cnDSb5YhpoYjV4PxDg1W1A8kDqU+Haql6g5lmra+ep0/N 115 | # XRDaQrGdnbl9KGjTMroCY1Ex52UkRI8C+UrefGHuKm1oIYxmw9Mez2cG4M8E/lbe 116 | # uggTwXOXJNXvJhAorFlcVA8FUmc0acGhgg7IMIIOxAYKKwYBBAGCNwMDATGCDrQw 117 | # gg6wBgkqhkiG9w0BBwKggg6hMIIOnQIBAzEPMA0GCWCGSAFlAwQCAQUAMHcGCyqG 118 | # SIb3DQEJEAEEoGgEZjBkAgEBBglghkgBhv1sBwEwMTANBglghkgBZQMEAgEFAAQg 119 | # SrcTUO2tkO5ZC0o3DYCq+QWk+ddcXNrQnFDBfVt8e74CEH4iL/2Tm6U4ody84WnH 120 | # I0kYDzIwMTgxMjMxMjIyMjE5WqCCC7swggaCMIIFaqADAgECAhAJwPxGyARCE7VZ 121 | # i68oT05BMA0GCSqGSIb3DQ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NK3ppki 152 | # /cF4z/WL2AyMBQnuROaA1W1wzJ9THifdKkje2pNlrW5lo5mnwkAOc8xYT49FKOW8 153 | # nIjmKM5gXS0lXYtzLqUNW1Hamk7/UAWJKNryeLvSWHiNRKesOgCReGmJZATTXZbf 154 | # Kr/5pUwsk//mit2CrPHSs6KGmsFViVZqRz/61jOVQzWJBXhaOmnaIrgEQ9NvaDU2 155 | # ehQ+RemYZIYPEwwmSjCCBTEwggQZoAMCAQICEAqhJdbWMht+QeQF2jaXwhUwDQYJ 156 | # KoZIhvcNAQELBQAwZTELMAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IElu 157 | # YzEZMBcGA1UECxMQd3d3LmRpZ2ljZXJ0LmNvbTEkMCIGA1UEAxMbRGlnaUNlcnQg 158 | # QXNzdXJlZCBJRCBSb290IENBMB4XDTE2MDEwNzEyMDAwMFoXDTMxMDEwNzEyMDAw 159 | # MFowcjELMAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IEluYzEZMBcGA1UE 160 | # CxMQd3d3LmRpZ2ljZXJ0LmNvbTExMC8GA1UEAxMoRGlnaUNlcnQgU0hBMiBBc3N1 161 | # cmVkIElEIFRpbWVzdGFtcGluZyBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC 162 | # AQoCggEBAL3QMu5LzY9/3am6gpnFOVQoV7YjSsQOB0UzURB90Pl9TWh+57ag9I2z 163 | # iOSXv2MhkJi/E7xX08PhfgjWahQAOPcuHjvuzKb2Mln+X2U/4Jvr40ZHBhpVfgsn 164 | # fsCi9aDg3iI/Dv9+lfvzo7oiPhisEeTwmQNtO4V8CdPuXciaC1TjqAlxa+DPIhAP 165 | # dc9xck4Krd9AOly3UeGheRTGTSQjMF287DxgaqwvB8z98OpH2YhQXv1mblZhJymJ 166 | # hFHmgudGUP2UKiyn5HU+upgPhH+fMRTWrdXyZMt7HgXQhBlyF/EXBu89zdZN7wZC 167 | # /aJTKk+FHcQdPK/P2qwQ9d2srOlW/5MCAwEAAaOCAc4wggHKMB0GA1UdDgQWBBT0 168 | # tuEgHf4prtLkYaWyoiWyyBc1bjAfBgNVHSMEGDAWgBRF66Kv9JLLgjEtUYunpyGd 169 | # 823IDzASBgNVHRMBAf8ECDAGAQH/AgEAMA4GA1UdDwEB/wQEAwIBhjATBgNVHSUE 170 | # DDAKBggrBgEFBQcDCDB5BggrBgEFBQcBAQRtMGswJAYIKwYBBQUHMAGGGGh0dHA6 171 | # Ly9vY3NwLmRpZ2ljZXJ0LmNvbTBDBggrBgEFBQcwAoY3aHR0cDovL2NhY2VydHMu 172 | # ZGlnaWNlcnQuY29tL0RpZ2lDZXJ0QXNzdXJlZElEUm9vdENBLmNydDCBgQYDVR0f 173 | # BHoweDA6oDigNoY0aHR0cDovL2NybDQuZGlnaWNlcnQuY29tL0RpZ2lDZXJ0QXNz 174 | # dXJlZElEUm9vdENBLmNybDA6oDigNoY0aHR0cDovL2NybDMuZGlnaWNlcnQuY29t 175 | # L0RpZ2lDZXJ0QXNzdXJlZElEUm9vdENBLmNybDBQBgNVHSAESTBHMDgGCmCGSAGG 176 | # /WwAAgQwKjAoBggrBgEFBQcCARYcaHR0cHM6Ly93d3cuZGlnaWNlcnQuY29tL0NQ 177 | # UzALBglghkgBhv1sBwEwDQYJKoZIhvcNAQELBQADggEBAHGVEulRh1Zpze/d2nyq 178 | # Y3qzeM8GN0CE70uEv8rPAwL9xafDDiBCLK938ysfDCFaKrcFNB1qrpn4J6Jmvwmq 179 | # YN92pDqTD/iy0dh8GWLoXoIlHsS6HHssIeLWWywUNUMEaLLbdQLgcseY1jxk5R9I 180 | # EBhfiThhTWJGJIdjjJFSLK8pieV4H9YLFKWA1xJHcLN11ZOFk362kmf7U2GJqPVr 181 | # lsD0WGkNfMgBsbkodbeZY4UijGHKeZR+WfyMD+NvtQEmtmyl7odRIeRYYJu6DC0r 182 | # baLEfrvEJStHAgh8Sa4TtuF8QkIoxhhWz0E0tmZdtnR79VYzIi8iNrJLokqV2PWm 183 | # jlIxggJNMIICSQIBATCBhjByMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNl 184 | # cnQgSW5jMRkwFwYDVQQLExB3d3cuZGlnaWNlcnQuY29tMTEwLwYDVQQDEyhEaWdp 185 | # Q2VydCBTSEEyIEFzc3VyZWQgSUQgVGltZXN0YW1waW5nIENBAhAJwPxGyARCE7VZ 186 | # i68oT05BMA0GCWCGSAFlAwQCAQUAoIGYMBoGCSqGSIb3DQEJAzENBgsqhkiG9w0B 187 | # CRABBDAcBgkqhkiG9w0BCQUxDxcNMTgxMjMxMjIyMjE5WjArBgsqhkiG9w0BCRAC 188 | # DDEcMBowGDAWBBRAAZFHXJiJHeuhBK9HCRtettTLyzAvBgkqhkiG9w0BCQQxIgQg 189 | # JwN/89Ga1SSd2VOkKWMIgZCx45giDiXu3ULujAunmwYwDQYJKoZIhvcNAQEBBQAE 190 | # ggEAl491I4Y38Jx10GI/Y1uQwm4716ORxKSJ/Jz0zY5ZEkIELil2S9niwd4UMYqp 191 | # o6hZq8kIIJYBCW/8trEXwtM3+D5vVL9/+B2MyaWqSbfKcs2bFwhDIe3Azaj8eHvL 192 | # Ofi6jLRJtTNIIIJ46a2MwX00RTJaXMaktT4LVz7lUPtRhDAXF9ppK4m4UTGs/MOY 193 | # PyqTcRQXfMPM9xavS2Vk1qevQ83SvVtqfTbadnBYJbmcv2ZuZ4qcBHeWWbs6wot+ 194 | # 0lojr9Sngprgc0pZ8ZhZrWPKVHc9y4YWE7zjeMEYELEQAyt83tvNVlHlG/W+PoVy 195 | # a/KeBH4OXw1ViUSwNPAPRG10fA== 196 | # SIG # End signature block 197 | -------------------------------------------------------------------------------- /src/Petabridge.Collections.Tests/CircularBufferPropertyTests.cs: -------------------------------------------------------------------------------- 1 | // ----------------------------------------------------------------------- 2 | // 3 | // Copyright (C) 2015 - 2019 Petabridge, LLC 4 | // 5 | // ----------------------------------------------------------------------- 6 | 7 | using System; 8 | using System.Collections.Generic; 9 | using System.Linq; 10 | using FsCheck; 11 | using FsCheck.Xunit; 12 | 13 | namespace Petabridge.Collections.Tests 14 | { 15 | public class CircularBufferSpecs 16 | { 17 | public CircularBufferSpecs() 18 | { 19 | Arb.Register(); 20 | } 21 | 22 | [Property] 23 | public Property CircularBuffer_dequeue_after_enqueue_should_return_original_item(CircularBuffer buffer, 24 | int[] itemsToAdd) 25 | { 26 | for (var i = 0; i < itemsToAdd.Length; i++) 27 | { 28 | buffer.Enqueue(itemsToAdd[i]); 29 | var dequeue = buffer.Dequeue(); 30 | if (dequeue != itemsToAdd[i]) return false.When(buffer.Capacity > 0).Label($"Failed with {buffer}"); 31 | } 32 | 33 | return true.ToProperty(); 34 | } 35 | 36 | [Property] 37 | public Property CircularBuffer_size_is_always_accurate(CircularBuffer buffer, int[] itemsToAdd) 38 | { 39 | buffer.Clear(); // found issues with old buffers being reused by FsCheck generators 40 | var currentSize = buffer.Size; 41 | if (currentSize > 0) 42 | return 43 | false.Label( 44 | $"Began with invalid buffer size {currentSize}. Should be 0. Head: {buffer.Head}, Tail: {buffer.Tail}"); 45 | for (var i = 0; i < itemsToAdd.Length; i++) 46 | { 47 | var full = currentSize == buffer.Capacity; 48 | var expectedSize = full ? currentSize : currentSize + 1; 49 | buffer.Enqueue(itemsToAdd[i]); 50 | var nextSize = buffer.Size; 51 | if (nextSize != expectedSize) 52 | return 53 | false.When(buffer.Capacity > 0) 54 | .Label( 55 | $"Failed with {buffer} on operation {i + 1}. Expected size to be {expectedSize}, was {nextSize}. Head: {buffer.Head}, Tail: {buffer.Tail}."); 56 | currentSize = nextSize; 57 | } 58 | 59 | return true.ToProperty(); 60 | } 61 | 62 | [Property(MaxTest = 1000)] 63 | public Property CircularBuffer_Model_Should_Pass() 64 | { 65 | Func generator = () => ThreadLocalRandom.Current.Next(); 66 | var tests = new CircularBufferPropertyTests(generator, i => new CircularBuffer(i)); 67 | return tests.ToProperty(); 68 | } 69 | 70 | [Property(MaxTest = 1000)] 71 | public Property ConcurrentCircularBuffer_Model_Should_Pass() 72 | { 73 | Func generator = () => ThreadLocalRandom.Current.Next(); 74 | var tests = new CircularBufferPropertyTests(generator, i => new ConcurrentCircularBuffer(i)); 75 | return tests.ToProperty(); 76 | } 77 | } 78 | 79 | public class CModel 80 | { 81 | public CModel(List items, int capacity) 82 | { 83 | Items = items; 84 | Capacity = capacity; 85 | } 86 | 87 | public List Items { get; } 88 | public int Capacity { get; } 89 | } 90 | 91 | public class CircularBufferPropertyTests : ICommandGenerator, CModel> 92 | 93 | { 94 | public CircularBufferPropertyTests(Func generator, Func> bufferFactory) 95 | { 96 | Generator = generator; 97 | BufferFactory = bufferFactory; 98 | } 99 | 100 | public Func Generator { get; } 101 | 102 | public Func> BufferFactory { get; } 103 | 104 | public Gen, CModel>> Next(CModel obj0) 105 | { 106 | return 107 | Gen.Elements(new Command, CModel>[] 108 | { 109 | new Allocate(BufferFactory), new EnqueueNoWrapAround(Generator), 110 | new EnqueueWithWrapAround(Generator), 111 | new Dequeue(), new Size(), new Clear() 112 | }); 113 | } 114 | 115 | public ICircularBuffer InitialActual => null; // no model yet - must be allocated as part of spec 116 | public CModel InitialModel => null; // no actual yet - must be allocated as part of spec 117 | 118 | private class Allocate : Command, CModel> 119 | { 120 | private readonly Func> _factory; 121 | private readonly Lazy _listSize = new Lazy(() => ThreadLocalRandom.Current.Next(1, 10)); 122 | 123 | public Allocate(Func> factory) 124 | { 125 | _factory = factory; 126 | } 127 | 128 | public override ICircularBuffer RunActual(ICircularBuffer obj0) 129 | { 130 | obj0 = _factory(_listSize.Value); 131 | return obj0; 132 | } 133 | 134 | public override CModel RunModel(CModel obj0) 135 | { 136 | obj0 = new CModel(new List(), _listSize.Value); 137 | return obj0; 138 | } 139 | 140 | public override string ToString() 141 | { 142 | return $"new CircularBuffer{typeof(T)}({_listSize.Value})"; 143 | } 144 | 145 | public override bool Pre(CModel _arg1) 146 | { 147 | return _arg1 == null; 148 | } 149 | } 150 | 151 | private class EnqueueNoWrapAround : Command, CModel> 152 | { 153 | private readonly Lazy _data; 154 | 155 | public EnqueueNoWrapAround(Func generator) 156 | { 157 | Generator = generator; 158 | _data = new Lazy(Generator); 159 | } 160 | 161 | private Func Generator { get; } 162 | 163 | public override ICircularBuffer RunActual(ICircularBuffer obj0) 164 | { 165 | obj0.Enqueue(_data.Value); 166 | return obj0; 167 | } 168 | 169 | public override CModel RunModel(CModel obj0) 170 | { 171 | obj0 = new CModel(obj0.Items.Concat(new[] {_data.Value}).ToList(), obj0.Capacity); 172 | return obj0; 173 | } 174 | 175 | public override bool Pre(CModel _arg1) 176 | { 177 | // ensure no wrap-around 178 | return _arg1 != null && _arg1.Items.Count < _arg1.Items.Capacity; 179 | } 180 | 181 | public override Property Post(ICircularBuffer _arg2, CModel _arg3) 182 | { 183 | var cbTail = _arg2.ToArray().Last(); 184 | var modelTail = _arg3.Items.Last(); 185 | 186 | return 187 | cbTail.Equals(modelTail) 188 | .ToProperty() 189 | .Label($"After enqueue expected Actual.Last()[{cbTail}] == Model.Last()[{modelTail}]"); 190 | } 191 | 192 | public override string ToString() 193 | { 194 | return $"CircularBuffer<{typeof(T)}>.EnqueueNoWrapAround({_data.Value})"; 195 | } 196 | } 197 | 198 | private class EnqueueWithWrapAround : Command, CModel> 199 | { 200 | private readonly Lazy _data; 201 | 202 | public EnqueueWithWrapAround(Func generator) 203 | { 204 | Generator = generator; 205 | _data = new Lazy(Generator); 206 | } 207 | 208 | private Func Generator { get; } 209 | 210 | public override ICircularBuffer RunActual(ICircularBuffer obj0) 211 | { 212 | obj0.Enqueue(_data.Value); 213 | return obj0; 214 | } 215 | 216 | public override CModel RunModel(CModel obj0) 217 | { 218 | obj0 = new CModel(obj0.Items.Skip(1).Concat(new[] {_data.Value}).ToList(), obj0.Capacity); 219 | return obj0; 220 | } 221 | 222 | public override bool Pre(CModel _arg1) 223 | { 224 | return _arg1 != null // must have called allocate first 225 | && _arg1.Items.Any() // must have added at least 1 item to model 226 | && _arg1.Items.Count == _arg1.Items.Capacity; 227 | // model size must equal capacity (forces wrap-around) 228 | } 229 | 230 | public override Property Post(ICircularBuffer _arg2, CModel _arg3) 231 | { 232 | var cbTail = _arg2.ToArray().Last(); 233 | var modelTail = _arg3.Items.Last(); 234 | 235 | return 236 | cbTail.Equals(modelTail) 237 | .ToProperty() 238 | .Label($"After enqueue expected Actual.Last()[{cbTail}] == Model.Last()[{modelTail}]"); 239 | } 240 | 241 | public override string ToString() 242 | { 243 | return $"CircularBuffer<{typeof(T)}>.EnqueueWithWrapAround({_data.Value})"; 244 | } 245 | } 246 | 247 | private class Dequeue : Command, CModel> 248 | { 249 | private T dequeValue; 250 | 251 | public override ICircularBuffer RunActual(ICircularBuffer obj0) 252 | { 253 | dequeValue = obj0.Dequeue(); 254 | return obj0; 255 | } 256 | 257 | public override CModel RunModel(CModel obj0) 258 | { 259 | obj0 = new CModel(obj0.Items.Skip(1).ToList(), obj0.Capacity); 260 | return obj0; 261 | } 262 | 263 | public override bool Pre(CModel _arg1) 264 | { 265 | return _arg1 != null && _arg1.Items.Any(); 266 | } 267 | 268 | public override Property Post(ICircularBuffer _arg2, CModel _arg3) 269 | { 270 | return _arg2.Skip(1).SequenceEqual(_arg3.Items.Skip(1)).ToProperty(); 271 | } 272 | 273 | public override string ToString() 274 | { 275 | return $"CircularBuffer<{typeof(T)}>.Dequeue() => {dequeValue}"; 276 | } 277 | } 278 | 279 | private class Size : Command, CModel> 280 | { 281 | public override ICircularBuffer RunActual(ICircularBuffer obj0) 282 | { 283 | return obj0; 284 | } 285 | 286 | public override CModel RunModel(CModel obj0) 287 | { 288 | return obj0; 289 | } 290 | 291 | public override bool Pre(CModel _arg1) 292 | { 293 | return _arg1 != null; 294 | } 295 | 296 | public override Property Post(ICircularBuffer _arg2, CModel _arg3) 297 | { 298 | return 299 | (_arg2.Count == _arg3.Items.Count).ToProperty() 300 | .Label($"Expected {_arg3.Items.Count}, got {_arg2.Count}"); 301 | } 302 | 303 | public override string ToString() 304 | { 305 | return $"CircularBuffer<{typeof(T)}>.Size()"; 306 | } 307 | } 308 | 309 | private class Clear : Command, CModel> 310 | { 311 | public override ICircularBuffer RunActual(ICircularBuffer obj0) 312 | { 313 | obj0.Clear(); 314 | return obj0; 315 | } 316 | 317 | public override CModel RunModel(CModel obj0) 318 | { 319 | obj0.Items.Clear(); 320 | return obj0; 321 | } 322 | 323 | public override bool Pre(CModel _arg1) 324 | { 325 | return _arg1 != null; 326 | } 327 | 328 | public override Property Post(ICircularBuffer _arg2, CModel _arg3) 329 | { 330 | return 331 | (_arg2.Count == _arg3.Items.Count).ToProperty() 332 | .Label($"Expected {_arg3.Items.Count}, got {_arg2.Count}"); 333 | } 334 | 335 | public override string ToString() 336 | { 337 | return $"CircularBuffer<{typeof(T)}>.Clear()"; 338 | } 339 | } 340 | } 341 | } --------------------------------------------------------------------------------