Last active
March 1, 2022 06:08
-
-
Save foolmoron/cfe0fbc31102001a3b53df6e92b520fc to your computer and use it in GitHub Desktop.
C# List-backed dictionary, for garbage-free and consistent performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ListDict<TKey, TValue> { | |
public readonly List<TKey> Keys; | |
public readonly List<TValue> Values; | |
public int Count { get { return Keys.Count; } } | |
public ListDict(int capacity = 0) { | |
Keys = new List<TKey>(capacity); | |
Values = new List<TValue>(capacity); | |
} | |
public void Add(TKey key, TValue value) { | |
var index = Keys.IndexOf(key); | |
if (index < 0) { | |
Keys.Add(key); | |
Values.Add(default(TValue)); | |
index = Keys.Count - 1; | |
} | |
Values[index] = value; | |
} | |
public bool Remove(TKey key) { | |
return RemoveAt(Keys.IndexOf(key)); | |
} | |
public bool RemoveAt(int index) { | |
if (index < 0 || index >= Count) { | |
return false; | |
} | |
Keys.RemoveAt(index); | |
Values.RemoveAt(index); | |
return true; | |
} | |
public void Clear() { | |
Keys.Clear(); | |
Values.Clear(); | |
} | |
public bool ContainsKey(TKey key) { | |
return Keys.IndexOf(key) >= 0; | |
} | |
public TValue this[TKey key] { | |
get { return Values[Keys.IndexOf(key)]; } | |
set { Add(key, value); } | |
} | |
public override string ToString() { | |
return string.Format("Count = {0}", Count); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment