{"id":402,"date":"2021-04-12T16:14:14","date_gmt":"2021-04-12T16:14:14","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=402"},"modified":"2021-04-12T16:14:14","modified_gmt":"2021-04-12T16:14:14","slug":"objectdictionary-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2021\/04\/12\/objectdictionary-cs\/","title":{"rendered":"ObjectDictionary.cs"},"content":{"rendered":"\n<p>Object Dictionary Single Associative Array, Searchable Keys and Values <\/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;\n[DebuggerDisplay(\"Count = {Count}\")]\n[Serializable]\npublic class ObjectDictionary : MonitorActionFuncWrapper, IEnumerable&lt;KeyValuePair&lt;object, object>>\n{\n    public ObjectBase _keys;\n    public ObjectBase _values;\n    public ObjectDictionary() : this(101)\n    {\n    }\n    public ObjectDictionary(int size)\n    {\n        _keys   = new ObjectBase(size);\n        _values = new ObjectBase(size);\n    }\n    public ObjectDictionary(IEnumerable&lt;KeyValuePair&lt;object, object>> collection)\n    {\n        foreach (var kp in collection)\n            Add(kp.Key, kp.Value);\n    }\n    public int Count => _keys.Count;\n    public object this[object key]\n    {\n        get\n        {\n            var pos = _keys.GetObjectIndex(key, false);\n            return pos.idx == -1 ? default : _values[pos.idx];\n        }\n        set => Add(key, value);\n    }\n    public object[]                       Values        => _values.ToArray();\n    public KeyValuePair&lt;object, object>[] KeyValuePairs => ToArray();\n    public object[]                       Keys          => _keys.ToArray();\n    public IEnumerator&lt;KeyValuePair&lt;object, object>> GetEnumerator()\n    {\n        return Lock(this, () =>\n        {\n            return GetEnum();\n        });\n    }\n    IEnumerator IEnumerable.GetEnumerator()\n    {\n        return Lock(this, () =>\n        {\n            return GetEnum();\n        });\n    }\n    private IEnumerator&lt;KeyValuePair&lt;object, object>> GetEnum()\n    {\n        for (var i = 0; i &lt; Count; i++)\n            yield return new KeyValuePair&lt;object, object>(_keys[i], _values[i]);\n    }\n    public bool Add(object key, object value)\n    {\n        return Lock(this, () =>\n        {\n            if (_keys.Contains(key) || _values.Contains(value))\n                return false;\n            \n            var ka = _keys.Add(key);\n            var va = _values.Add(value);\n\n            if (ka || va)\n            {\n                int a = 1;\n            }\n\n            return ka &amp;&amp; va;\n        });\n    }\n    public void RemoveKey(object key)\n    {\n        var kidx = _keys.GetObjectIndex(key, false).idx;\n        if (kidx != -1)\n        {\n            var keys   = _keys;\n            var values = _values;\n            Clear();\n            for (var i = 0; i &lt; keys.Count; ++i)\n                if (i != kidx)\n                    Add(keys[i], values[i]);\n        }\n    }\n    public void RemoveValue(object value)\n    {\n        var vidx = _values.GetObjectIndex(value, false).idx;\n        if (vidx != -1)\n        {\n            var keys   = _keys;\n            var values = _values;\n            Clear();\n            for (var i = 0; i &lt; keys.Count; ++i)\n                if (i != vidx)\n                    Add(keys[i], values[i]);\n        }\n    }\n    public void RebuildLists()\n    {\n        var keys   = _keys;\n        var values = _values;\n        Clear();\n        for (var i = 0; i &lt; keys.Count; ++i)\n            Add(keys[i], values[i]);\n    }\n    public bool ContainsKey(object key)\n    {\n        return Lock(this, () =>\n        {\n            return _keys.Contains(key);\n        });\n    }\n    public bool ContainsValue(object value)\n    {\n        return Lock(this, () =>\n        {\n            return _values.Contains(value);\n        });\n    }\n    public int FindKeyIndex(object key)\n    {\n        return Lock(this, () =>\n        {\n            return _keys.GetObjectIndex(key, false).idx;\n        });\n    }\n    public int FindValueIndex(object value)\n    {\n        return Lock(this, () =>\n        {\n            return _values.GetObjectIndex(value, false).idx;\n        });\n    }\n    public KeyValuePair&lt;object, object>[] ToArray()\n    {\n        return Lock(this, () =>\n        {\n            var array = new KeyValuePair&lt;object, object>[Count];\n            for (var i = 0; i &lt; Count; i++)\n                array[i] = new KeyValuePair&lt;object, object>(_keys[i], _values[i]);\n            return array;\n        });\n    }\n    public void Clear()\n    {\n        Lock(this, () =>\n        {\n            _keys   = new ObjectBase(_keys.Count);\n            _values = new ObjectBase(_values.Count);\n        });\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Object Dictionary Single Associative Array, Searchable Keys and Values<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[174,59,177,26,175,176],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/402"}],"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=402"}],"version-history":[{"count":1,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/402\/revisions"}],"predecessor-version":[{"id":403,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/402\/revisions\/403"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}