{"id":499,"date":"2021-07-23T08:37:03","date_gmt":"2021-07-23T08:37:03","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=499"},"modified":"2021-07-24T05:32:09","modified_gmt":"2021-07-24T05:32:09","slug":"cccollection_cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2021\/07\/23\/cccollection_cs\/","title":{"rendered":"CcCollection.cs"},"content":{"rendered":"\n<p>Concurrent Collection Class without Blocking<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System;\nusing System.Collections;\nusing System.Collections.Generic;\nusing System.Diagnostics;\nusing System.Threading;\n[DebuggerDisplay(\"Count = {\" + nameof(Count) + \"}\")]\npublic class CcCollection&lt;T> : IEnumerable&lt;T>\n{\n    private readonly IntA&lt;T>[] _array;\n    private readonly int       _size;\n    private volatile int[]     _activeThreads;\n    private volatile int       _bP;\n    public volatile  int       NumberOfActiveThreads;\n    public CcCollection() : this(1024)\n    {\n    }\n    public CcCollection(int size)\n    {\n        ThreadPool.GetMaxThreads(out var nW, out var nI);\n        _array                = new IntA&lt;T>[nW];\n        _size                 = size;\n        NumberOfActiveThreads = 0;\n        _bP                   = 0;\n        _activeThreads        = new int[Environment.ProcessorCount];\n        _activeThreads.Fill(-1);\n    }\n    public int Count\n    {\n        get\n        {\n            var totalCount = 0;\n            for (var i = 0; i &lt; _activeThreads.Length; ++i)\n                if (_activeThreads[i] != -1)\n                    if (_array[_activeThreads[i]].Allocated)\n                        totalCount += _array[_activeThreads[i]].Count;\n            return totalCount;\n        }\n    }\n    public IEnumerator&lt;T> GetEnumerator()\n    {\n        return GetEnum();\n    }\n    IEnumerator IEnumerable.GetEnumerator()\n    {\n        return GetEnum();\n    }\n    public void Add(T item)\n    {\n        var id = Thread.CurrentThread.ManagedThreadId;\n        if (!_array[id].Allocated)\n        {\n            _array[id] = new IntA&lt;T>(_size);\n            Interlocked.Increment(ref NumberOfActiveThreads);\n            if (_bP >= _activeThreads.Length)\n            {\n                var nAtA = new int[_activeThreads.Length &lt;&lt; 1];\n                nAtA.Fill(-1);\n                for (var i = 0; i &lt; _activeThreads.Length; ++i)\n                    if (_activeThreads[i] != -1)\n                        nAtA[i] = _activeThreads[i];\n                _activeThreads = nAtA;\n            }\n            _activeThreads[_bP] = id;\n            Interlocked.Increment(ref _bP);\n        }\n        _array[id].Add(item);\n    }\n    public IEnumerator&lt;T> GetEnum()\n    {\n        var arr = ToArray();\n        foreach (var i in arr)\n            yield return i;\n    }\n    public T[] ToArray()\n    {\n        var totalCount = 0;\n        for (var i = 0; i &lt; _activeThreads.Length; ++i)\n            if (_activeThreads[i] != -1)\n                if (_array[_activeThreads[i]].Allocated)\n                    totalCount += _array[_activeThreads[i]].Count;\n        var ta  = new T[totalCount];\n        var ptr = 0;\n        for (var i = 0; i &lt; _activeThreads.Length; ++i)\n            if (_activeThreads[i] != -1)\n                if (_array[_activeThreads[i]].Allocated)\n                {\n                    var it = _array[_activeThreads[i]].ToArray();\n                    for (var j = 0; j &lt; it.Length; ++j)\n                        ta[ptr++] = it[j];\n                }\n        return ta;\n    }\n    internal struct IntA&lt;T> : IEnumerable&lt;T>\n    {\n        private T[]  _array;\n        public  bool Allocated;\n        public IntA(int cap)\n        {\n            Count     = 0;\n            _array    = new T[cap];\n            Allocated = true;\n        }\n        public int Count\n        {\n            get;\n            private set;\n        }\n        public int Length => _array.Length;\n        public T this[int index]\n        {\n            get\n            {\n                if (index > _array.Length)\n                    throw new Exception(\"Error: Index out of range.\");\n                return _array[index];\n            }\n            set\n            {\n                if (index > _array.Length)\n                    throw new Exception(\"Error: Index out of range.\");\n                _array[index] = value;\n            }\n        }\n        IEnumerator&lt;T> IEnumerable&lt;T>.GetEnumerator()\n        {\n            return GetEnumerator();\n        }\n        IEnumerator IEnumerable.GetEnumerator()\n        {\n            return GetEnumerator();\n        }\n        public void Add(T item)\n        {\n            if (Count >= _array.Length)\n                Array.Resize(ref _array, _array.Length &lt;&lt; 1);\n            _array[Count] = item;\n            Count++;\n        }\n        public T[] ToArray()\n        {\n            var newtArray = new T[Count];\n            Array.Copy(_array, 0, newtArray, 0, Count);\n            return newtArray;\n        }\n        public void Trim()\n        {\n            var newtArray = new T[Count];\n            Array.Copy(_array, 0, newtArray, 0, Count);\n            _array = newtArray;\n        }\n        public void Clear()\n        {\n            Array.Clear(_array, 0, Count);\n            Count = 0;\n        }\n        public void Zero()\n        {\n            _array = Array.Empty&lt;T>();\n            Count  = 0;\n        }\n        public IEnumerator&lt;T> GetEnumerator()\n        {\n            return GetEnum();\n        }\n        public IEnumerator&lt;T> GetEnum()\n        {\n            for (var i = 0; i &lt; Count; i++)\n                yield return _array[i];\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Concurrent Collection Class without Blocking<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[84,94,207],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/499"}],"collection":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/comments?post=499"}],"version-history":[{"count":2,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/499\/revisions"}],"predecessor-version":[{"id":509,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/499\/revisions\/509"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}