public
Created

Ordered dictionary class for C# and .NET (an omission from the standard library). A dictionary that remembers the order that keys were first inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end. See http://stackoverflow.com/questions/2629027/no-generic-implementation-of-ordereddictionary

  • Download Gist
OrderedDictionary.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
 
/// <summary>
/// A dictionary that remembers the order that keys were first inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.
/// </summary>
/// <typeparam name="TKey">The type of keys</typeparam>
/// <typeparam name="TValue">The type of values</typeparam>
public interface IOrderedDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
/// <summary>
/// The value of the element at the given index.
/// </summary>
TValue this[int index] { get; set; }
 
/// <summary>
/// Find the position of an element by key. Returns -1 if the dictionary does not contain an element with the given key.
/// </summary>
int IndexOf(TKey key);
 
/// <summary>
/// Insert an element at the given index.
/// </summary>
void Insert(int index, TKey key, TValue value);
 
/// <summary>
/// Remove the element at the given index.
/// </summary>
void RemoveAt(int index);
}
 
/// <summary>
/// A dictionary that remembers the order that keys were first inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.
/// </summary>
/// <typeparam name="TKey">The type of keys. Musn't be <see cref="int"/></typeparam>
/// <typeparam name="TValue">The type of values.</typeparam>
public sealed class OrderedDictionary<TKey, TValue> : IOrderedDictionary<TKey, TValue>
{
/// <summary>
/// An unordered dictionary of key pairs.
/// </summary>
private readonly Dictionary<TKey, TValue> fDictionary;
 
/// <summary>
/// The keys of the dictionary in the exposed order.
/// </summary>
private readonly List<TKey> fKeys;
 
/// <summary>
/// A dictionary that remembers the order that keys were first inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.
/// </summary>
public OrderedDictionary()
{
if (typeof(TKey) == typeof(int))
throw new NotSupportedException("Integer-like type is not appropriate for keys in an ordered dictionary - accessing values by key or index would be confusing.");
 
fDictionary = new Dictionary<TKey, TValue>();
fKeys = new List<TKey>();
}
 
/// <summary>
/// The number of elements in the dictionary.
/// </summary>
public int Count
{
get
{
return fDictionary.Count;
}
}
 
/// <summary>
/// This dictionary is not read only.
/// </summary>
public bool IsReadOnly
{
get
{
return false;
}
}
 
/// <summary>
/// The keys of the dictionary, in order.
/// </summary>
public ICollection<TKey> Keys
{
get
{
return fKeys.AsReadOnly();
}
}
 
/// <summary>
/// The values in the dictionary, in order.
/// </summary>
public ICollection<TValue> Values
{
get
{
return fKeys.Select(key => fDictionary[key]).ToArray();
}
}
 
/// <summary>
/// The value at the given index.
/// </summary>
public TValue this[int index]
{
get
{
var key = fKeys[index];
return fDictionary[key];
}
set
{
var key = fKeys[index];
fDictionary[key] = value;
}
}
 
/// <summary>
/// The value under the given key. New entries are added at the end of the order. Updating an existing entry does not change its position.
/// </summary>
public TValue this[TKey key]
{
get
{
return fDictionary[key];
}
set
{
if (!fDictionary.ContainsKey(key))
{
// New entries are added at the end of the order.
fKeys.Add(key);
}
 
fDictionary[key] = value;
}
}
 
/// <summary>
/// Find the position of an element by key. Returns -1 if the dictionary does not contain an element with the given key.
/// </summary>
public int IndexOf(TKey key)
{
return fKeys.IndexOf(key);
}
 
/// <summary>
/// Remove the element at the given index.
/// </summary>
public void RemoveAt(int index)
{
var key = fKeys[index];
fDictionary.Remove(key);
fKeys.RemoveAt(index);
}
 
/// <summary>
/// Test whether there is an element with the given key.
/// </summary>
public bool ContainsKey(TKey key)
{
return fDictionary.ContainsKey(key);
}
 
/// <summary>
/// Try to get a value from the dictionary, by key. Returns false if there is no element with the given key.
/// </summary>
public bool TryGetValue(TKey key, out TValue value)
{
return fDictionary.TryGetValue(key, out value);
}
 
/// <summary>
/// Insert an element at the given index.
/// </summary>
public void Insert(int index, TKey key, TValue value)
{
// Dictionary operation first, so exception thrown if key already exists.
fDictionary.Add(key, value);
fKeys.Insert(index, key);
}
 
/// <summary>
/// Add an element to the dictionary.
/// </summary>
public void Add(TKey key, TValue value)
{
// Dictionary operation first, so exception thrown if key already exists.
fDictionary.Add(key, value);
fKeys.Add(key);
}
 
/// <summary>
/// Add an element to the dictionary.
/// </summary>
public void Add(KeyValuePair<TKey, TValue> pair)
{
Add(pair.Key, pair.Value);
}
 
/// <summary>
/// Test whether the dictionary contains an element equal to that given.
/// </summary>
public bool Contains(KeyValuePair<TKey, TValue> pair)
{
return fDictionary.Contains(pair);
}
 
/// <summary>
/// Remove a key-value pair from the dictionary. Return true if pair was successfully removed. Otherwise, if the pair was not found, return false.
/// </summary>
public bool Remove(KeyValuePair<TKey, TValue> pair)
{
if (Contains(pair))
{
Remove(pair.Key);
return true;
}
 
return false;
}
 
/// <summary>
/// Remove the element with the given key key. If there is no element with the key, no exception is thrown.
/// </summary>
public bool Remove(TKey key)
{
bool wasInDictionary = fDictionary.Remove(key);
bool wasInKeys = fKeys.Remove(key);
Contract.Assume(wasInDictionary == wasInKeys);
return wasInDictionary;
}
 
/// <summary>
/// Delete all elements from the dictionary.
/// </summary>
public void Clear()
{
fDictionary.Clear();
fKeys.Clear();
}
 
/// <summary>
/// Copy the elements of the dictionary to an array, starting at at the given index.
/// </summary>
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
{
if (array == null)
throw new ArgumentNullException("array");
 
if (index < 0)
throw new ArgumentOutOfRangeException("index", "Must be greater than or equal to zero.");
 
if (index + fDictionary.Count > array.Length)
throw new ArgumentException("array", "Array is too small");
 
foreach (var pair in this)
{
array[index] = pair;
index++;
}
}
 
private IEnumerable<KeyValuePair<TKey, TValue>> Enumerate()
{
foreach (var key in fKeys)
{
var value = fDictionary[key];
yield return new KeyValuePair<TKey, TValue>(key, value);
}
}
 
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
return Enumerate().GetEnumerator();
}
 
IEnumerator IEnumerable.GetEnumerator()
{
return Enumerate().GetEnumerator();
}
 
/// <summary>
/// Conditions that should be true at the end of every public method.
/// </summary>
[ContractInvariantMethod]
private void ObjectInvariant()
{
Contract.Invariant(fDictionary.Count == fKeys.Count, "Unordered dictionary and ordered key list should be the same length.");
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.