public
Created

DefaultingDictionary<>

  • Download Gist
DefaultingDictionary.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
using System;
using System.Collections;
using System.Collections.Generic;
 
namespace SODemo
{
public class DefaultingDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
public static implicit operator DefaultingDictionary<TKey,TValue>(Dictionary<TKey,TValue> wrappable)
{
return new DefaultingDictionary<TKey, TValue>(wrappable, default(TValue));
}
 
private readonly IDictionary<TKey, TValue> _dictionary;
private readonly Func<TKey, TValue> _generateDefault;
 
public DefaultingDictionary(IDictionary<TKey, TValue> dictionary, Func<TKey, TValue> generateGenerateDefault)
{
_dictionary = dictionary;
_generateDefault = generateGenerateDefault;
}
 
public DefaultingDictionary(IDictionary<TKey, TValue> dictionary, Func<TValue> generateGenerateDefault)
{
_dictionary = dictionary;
_generateDefault = curry => generateGenerateDefault();
}
 
public DefaultingDictionary(IDictionary<TKey, TValue> dictionary, TValue defaultValue)
{
_dictionary = dictionary;
_generateDefault = curry => defaultValue;
}
 
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _dictionary.GetEnumerator();
}
 
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
 
public void Add(KeyValuePair<TKey, TValue> item)
{
_dictionary.Add(item);
}
 
public void Clear()
{
_dictionary.Clear();
}
 
public bool Contains(KeyValuePair<TKey, TValue> item)
{
return _dictionary.Contains(item);
}
 
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
_dictionary.CopyTo(array, arrayIndex);
}
 
public bool Remove(KeyValuePair<TKey, TValue> item)
{
return _dictionary.Remove(item);
}
 
public int Count
{
get { return _dictionary.Count; }
}
 
public bool IsReadOnly
{
get { return _dictionary.IsReadOnly; }
}
 
public bool ContainsKey(TKey key)
{
return _dictionary.ContainsKey(key);
}
 
public void Add(TKey key, TValue value)
{
_dictionary.Add(key, value);
}
 
public bool Remove(TKey key)
{
return _dictionary.Remove(key);
}
 
public bool TryGetValue(TKey key, out TValue value)
{
if (!_dictionary.TryGetValue(key, out value))
value = _generateDefault(key);
 
return true;
}
 
public TValue this[TKey key]
{
get
{
if (!_dictionary.ContainsKey(key))
_dictionary.Add(key, _generateDefault(key));
return _dictionary[key];
}
 
set
{
if (!_dictionary.ContainsKey(key))
_dictionary.Add(key, value);
else
_dictionary[key] = value;
}
}
 
public ICollection<TKey> Keys
{
get { return _dictionary.Keys; }
}
 
public ICollection<TValue> Values
{
get
{
return _dictionary.Values;
}
}
 
public bool ContainsValue(TValue value)
{
// preferred because it could use a custom Comparer:
var instance = _dictionary as Dictionary<TKey, TValue>;
return instance != null
? instance.ContainsValue(value)
: Values.Contains(value);
}
}
 
public static class DefaultingDictionaryExtensions
{
public static IDictionary<TKey, TValue> AsDefaulting<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{ return new DefaultingDictionary<TKey, TValue>(dictionary, default(TValue)); }
 
public static IDictionary<TKey, TValue> AsDefaultingTo<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TValue defaultValue )
{ return new DefaultingDictionary<TKey, TValue>(dictionary, defaultValue); }
 
public static IDictionary<TKey, TValue> AsDefaulting<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Func<TKey, TValue> func)
{ return new DefaultingDictionary<TKey, TValue>(dictionary, func); }
 
public static IDictionary<TKey, TValue> AsDefaulting<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Func<TValue> func)
{ return new DefaultingDictionary<TKey, TValue>(dictionary, func); }
}
}
DefaultingDictionaryTests.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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
 
namespace SODemo.Tests
{
[TestClass]
public class DefaultingDictionaryTests
{
[TestMethod]
public void DefaultingDictionary()
{
var vanilla = new Dictionary<int, bool>();
 
Assert.IsFalse(vanilla.ContainsKey(42));
 
Throws(typeof (KeyNotFoundException), () => Assert.IsFalse(vanilla[42]));
Assert.IsFalse(vanilla.ContainsKey(42));
 
vanilla[42] = false;
Assert.IsFalse(vanilla[42]);
Assert.IsTrue(vanilla.ContainsKey(42));
 
vanilla[42] = true;
Assert.IsTrue(vanilla[42]);
Assert.IsTrue(vanilla.ContainsKey(42));
 
vanilla[42] = false;
Assert.IsFalse(vanilla[42]);
Assert.IsTrue(vanilla.ContainsKey(42));
 
vanilla.Remove(42);
 
Assert.IsFalse(vanilla.ContainsKey(42));
 
///////////////////////////////////////////////////
var defaulting = vanilla.AsDefaulting();
 
Assert.IsFalse(defaulting.ContainsKey(42));
 
Assert.IsFalse(defaulting[42]);
Assert.IsTrue(defaulting.ContainsKey(42));
 
defaulting[42] = false;
Assert.IsFalse(defaulting[42]);
Assert.IsTrue(defaulting.ContainsKey(42));
 
defaulting[42] = true;
Assert.IsTrue(defaulting[42]);
Assert.IsTrue(defaulting.ContainsKey(42));
 
defaulting[42] = false;
Assert.IsFalse(defaulting[42]);
Assert.IsTrue(defaulting.ContainsKey(42));
 
defaulting.Remove(42);
Assert.IsFalse(defaulting.ContainsKey(42));
Assert.IsFalse(vanilla.ContainsKey(42));
 
Assert.IsFalse(defaulting[42]);
Assert.IsTrue(defaulting.ContainsKey(42));
Assert.IsTrue(vanilla.ContainsKey(42));
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.