{"id":124,"date":"2020-07-30T08:37:47","date_gmt":"2020-07-30T08:37:47","guid":{"rendered":"https:\/\/michaeljohnsteiner.com\/?p=124"},"modified":"2020-12-17T21:48:28","modified_gmt":"2020-12-17T21:48:28","slug":"tinydictionary-cs","status":"publish","type":"post","link":"https:\/\/michaeljohnsteiner.com\/index.php\/2020\/07\/30\/tinydictionary-cs\/","title":{"rendered":"TinyDictionary.cs"},"content":{"rendered":"\n<p>A Small Dictionary Class<\/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 TinyDictionary&lt;TKey, TValue> : IEnumerable&lt;KeyValuePair&lt;TKey, TValue>>\n{\n    public MSet15&lt;TKey> Keys;\n    public int          Resizes;\n    public TValue[]     Values;\n    public TinyDictionary() : this(101, EqualityComparer&lt;TKey>.Default)\n    {\n    }\n    public TinyDictionary(int size) : this(size, EqualityComparer&lt;TKey>.Default)\n    {\n    }\n    public TinyDictionary(int size, IEqualityComparer&lt;TKey> comparer)\n    {\n        if (comparer == null)\n            comparer = EqualityComparer&lt;TKey>.Default;\n        Keys          = new MSet15&lt;TKey>(size);\n        Values        = new TValue[size];\n        Keys.Comparer = comparer;\n    }\n    public TinyDictionary(IEnumerable&lt;KeyValuePair&lt;TKey, TValue>> collection, IEqualityComparer&lt;TKey> comparer = null)\n    {\n        if (comparer == null)\n            comparer = EqualityComparer&lt;TKey>.Default;\n        Keys.Comparer = comparer;\n        foreach (var kp in collection)\n            Add(kp.Key, kp.Value);\n    }\n    public int Count => Keys.Count;\n    public TValue this[TKey key]\n    {\n        get\n        {\n            var pos = Keys.FindEntry(key);\n            return pos == -1 ? default : Values[pos];\n        }\n        set => Add(key, value);\n    }\n    public IEnumerator&lt;KeyValuePair&lt;TKey, TValue>> GetEnumerator()\n    {\n        for (var i = 0; i &lt; Count; i++)\n            if (Keys.Slots[i].HashCode > 0)\n                yield return new KeyValuePair&lt;TKey, TValue>(Keys.Slots[i].Value, Values[i]);\n    }\n    IEnumerator IEnumerable.GetEnumerator()\n    {\n        return GetEnumerator();\n    }\n    public bool Add(TKey key, TValue value)\n    {\n        if (!Keys.Add(key))\n        {\n            if (Values.Length != Keys.Slots.Length)\n            {\n                var nValues = new TValue[Keys.Slots.Length];\n                Array.Copy(Values, nValues, Values.Length);\n                Values = nValues;\n                Resizes++;\n            }\n            Values[Keys.Position] = value;\n            return false;\n        }\n        Values[Keys.Position] = value;\n        return true;\n    }\n    public void Remove(TKey key)\n    {\n        var pos = Keys.FindEntry(key);\n        if (pos != -1)\n        {\n            Values[pos] = default;\n            Keys.Remove(key);\n        }\n    }\n    public bool ContainsKey(TKey key)\n    {\n        return Keys.FindEntry(key) != -1;\n    }\n    public int FindKeyIndex(TKey key)\n    {\n        return Keys.FindEntry(key);\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A Small Dictionary Class<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[5,59,57,56],"_links":{"self":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/124"}],"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=124"}],"version-history":[{"count":3,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"predecessor-version":[{"id":295,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/posts\/124\/revisions\/295"}],"wp:attachment":[{"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaeljohnsteiner.com\/index.php\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}