Skip to content

Instantly share code, notes, and snippets.

@foolmoron
Last active March 1, 2022 06:08
Show Gist options
  • Save foolmoron/cfe0fbc31102001a3b53df6e92b520fc to your computer and use it in GitHub Desktop.
Save foolmoron/cfe0fbc31102001a3b53df6e92b520fc to your computer and use it in GitHub Desktop.
C# List-backed dictionary, for garbage-free and consistent performance
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