forked from rebcabin/DotNetExtensionsImproved
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DictionaryExtensions.cs
397 lines (368 loc) · 18.4 KB
/
DictionaryExtensions.cs
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// The MIT License
// Portions Copyright (c) 2011 Jordan E. Terrell, licensed to
// Microsoft Corporation under the MIT license (copied below).
//
// Portions Copyright (c) 2011 Microsoft Corporation
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using Experimental.DotNetExtensions.iSynaptic;
namespace Experimental.DotNetExtensions
{
/// <summary>
/// Extensions to the IDictionary interface, supporting chainable dictionary combinators.
/// </summary>
public static class IDictionary
{
/// <summary>
/// Creation through explicitly supplied first values.
/// </summary>
/// <typeparam name="K">The type of keys in the dictionary.</typeparam>
/// <typeparam name="V">The type of values in the dictionary.</typeparam>
/// <param name="key">A value of type K.</param>
/// <param name="value">A value of type V.</param>
/// <returns>The IDictionary interface of a new dictionary.</returns>
public static IDictionary<K, V> AddUnconditionally<K, V>(K key, V value)
{
return new Dictionary<K, V>()
.AddUnconditionally(key, value);
}
/// <summary>
/// Creation through explicitly supplied first values.
/// </summary>
/// <typeparam name="K">The type of keys in the dictionary.</typeparam>
/// <typeparam name="V">The type of values in the dictionary.</typeparam>
/// <param name="key">A value of type K.</param>
/// <param name="value">A value of type V.</param>
/// <returns>The IDictionary interface of a new dictionary.</returns>
public static IDictionary<K, V> AddConditionally<K, V>(K key, V value)
{
return new Dictionary<K, V>()
.AddConditionally(key, value);
}
/// <summary>
/// Creation based on types of ignored values. Recommended usage IDictionary.Create(default(K), default(V)).
/// </summary>
/// <typeparam name="K">The type of keys in the dictionary.</typeparam>
/// <typeparam name="V">The type of values in the dictionary.</typeparam>
/// <param name="key">A value of type K to assist type inference; ignored.</param>
/// <param name="value">A value of type V to assist type inference; ignored.</param>
/// <returns>The IDictionary interface of a new dictionary.</returns>
public static IDictionary<K, V> Create<K, V>(K key, V value)
{
return new Dictionary<K, V>();
}
/// <summary>
/// Creation based on explicitly supplied type arguments.
/// </summary>
/// <typeparam name="K">The type of keys in the dictionary.</typeparam>
/// <typeparam name="V">The type of values in the dictionary.</typeparam>
/// <returns>The IDictionary interface of a new dictionary.</returns>
public static IDictionary<K, V> Create<K, V>()
{
return new Dictionary<K, V>();
}
/// <summary>
/// Returns a Maybe instance encapsulating a potentially absent value.
/// </summary>
/// <typeparam name="K">The type of keys in the dictionary.</typeparam>
/// <typeparam name="V">The type of values in the dictionary.</typeparam>
/// <param name="theDictionary">The input dictionary.</param>
/// <param name="key">The key to try.</param>
/// <returns>The potentially absent value encapsulated in a Maybe.</returns>
public static Maybe<V> TryGetValue<K, V>(
this IDictionary<K, V> theDictionary,
K key)
{
//Contract.Requires(null != theDictionary, "theDictionary");
V retreived = default(V);
return theDictionary.TryGetValue(key, out retreived)
? new Maybe<V>(retreived)
: Maybe<V>.NoValue;
}
/// <summary>
/// Add whether the key is present or not.
/// </summary>
/// <typeparam name="K">The type of keys in the dictionary.</typeparam>
/// <typeparam name="V">The type of values in the dictionary.</typeparam>
/// <param name="theDictionary">The input dictionary.</param>
/// <param name="keyValuePair">The input key-value pair.</param>
/// <returns>The modified dictionary.</returns>
public static IDictionary<K, V> AddUnconditionally<K, V>(
this IDictionary<K, V> theDictionary,
KeyValuePair<K, V> keyValuePair)
{
Contract.Requires(theDictionary != null);
#if (false)
theDictionary[keyValuePair.Key] = keyValuePair.Value;
return theDictionary;
#else
// Take the kvp . . .
return keyValuePair
// Bring it into the state monad with the IDictionary as state
// and the default propagator that does nothing to the state . . .
.ToState<IDictionary<K, V>, KeyValuePair<K, V>>()
// Shove this through a transformer that produces a bool and state . . .
.Bind<IDictionary<K, V>, KeyValuePair<K, V>, bool>(
// From the provided kvp . . .
kvp => new State<IDictionary<K, V>, bool>(
// with a propagator that UNCONDITIONALLY puts the kvp in
// the dictionary, and reports whether the key was already
// present . . .
#if (false)
propagator: dict => ValueStatePair.Create(dict
// TODO: Investigate intermittent stress failure of the Maybe monad!
// via the Maybe monad . . .
.TryGetValue(kvp.Key)
// if the value was not in the dictionary, add it . . .
.OnNoValue(() => dict.Add(key: kvp.Key, value: kvp.Value))
// if the value was in the dictionary, replace it . . .
.OnValue(_ => dict[kvp.Key] = kvp.Value)
// be thread-safe . . .
.Synchronize(dict)
// provide the bool . . .
.HasValue,
// and the original dictionary.
dict)))
#else
propagator: dict =>
{
var value = default(V);
var hasValue = dict.TryGetValue(kvp.Key, out value);
if (hasValue)
dict[kvp.Key] = kvp.Value;
else
dict.Add(key: kvp.Key, value: kvp.Value);
return ValueStatePair.Create(hasValue, dict);
}))
#endif
// Apply the newly bound state to the input dictionary . . .
.Propagator(theDictionary)
// Extract the dictionary from the tuple and return it:
.State
;
// Sadly, the bool info about whether the key was in the dictionary
// is lost, the price we pay to thread the dictionary out.
#endif
}
/// <summary>
/// Add whether the key is present or not.
/// </summary>
/// <typeparam name="K">The type of keys in the dictionary.</typeparam>
/// <typeparam name="V">The type of values in the dictionary.</typeparam>
/// <param name="theDictionary">The input dictionary.</param>
/// <param name="key">The key to install.</param>
/// <param name="value">The value to install.</param>
/// <returns>The modified dictionary.</returns>
public static IDictionary<K, V> AddUnconditionally<K, V>(
this IDictionary<K, V> theDictionary,
K key,
V value)
{
return theDictionary
.AddUnconditionally(
new KeyValuePair<K, V>(key: key, value: value));
}
/// <summary>
/// Add only if key is NOT already present.
/// </summary>
/// <typeparam name="K">The type of keys in the dictionary.</typeparam>
/// <typeparam name="V">The type of values in the dictionary.</typeparam>
/// <param name="theDictionary">The input dictionary.</param>
/// <param name="keyValuePair">The input key-value pair.</param>
/// <returns>The modified dictionary.</returns>
public static IDictionary<K, V> AddConditionally<K, V>(
this IDictionary<K, V> theDictionary,
KeyValuePair<K, V> kvpInput)
{
Contract.Requires(theDictionary != null);
#if (false)
var value = default(V);
var hasValue = theDictionary.TryGetValue(kvpInput.Key, out value);
if (!hasValue)
theDictionary[kvpInput.Key] = kvpInput.Value;
return theDictionary;
#else
// Take the kvp . . .
return kvpInput
// Bring it into the state monad with the IDictionary as state
// and the default propagator that does nothing to the state . . .
.ToState<IDictionary<K, V>, KeyValuePair<K, V>>()
// Shove this through a transformer that produces a bool and state . . .
.Bind<IDictionary<K, V>, KeyValuePair<K, V>, bool>(
// From the provided kvp . . .
kvp => new State<IDictionary<K, V>, bool>(
// with a propagator that CONDITIONALLY puts the kvp in
// the dictionary, and reports whether the key was already
// present . . .
#if (false)
propagator: dict => ValueStatePair.Create(dict
// TODO: investigate failure of the Maybe monad!
// via the Maybe monad . . .
.TryGetValue(kvp.Key)
// if the value was not in the dictionary, add it . . .
.OnNoValue(() => dict.Add(key: kvp.Key, value: kvp.Value))
// be thread safe . . .
.Synchronize(dict)
// provide the bool . . .
.HasValue,
// and the original dictionary.
dict)))
#else
propagator: dict =>
{
var value = default(V);
var hasValue = dict.TryGetValue(kvp.Key, out value);
if (!hasValue)
dict.Add(key: kvp.Key, value: kvp.Value);
return ValueStatePair.Create(hasValue, dict);
}))
#endif
// Apply the newly bound state to the input dictionary . . .
.Propagator(theDictionary)
// Extract the dictionary from the tuple and return it:
.State
;
// Sadly, the info about whether the key was in the dictionary
// is lost, the price we pay to thread the dictionary through.
#endif
}
public static IDictionary<K, V> AddConditionally<K, V>(
this IDictionary<K, V> theDictionary,
K key,
V value)
{
return theDictionary
.AddConditionally(
new KeyValuePair<K, V>(key: key, value: value));
}
/// <summary>
/// Look up the value, and return the default of type 'Value' if the key is not present.
/// </summary>
/// <typeparam name="K">The type of keys.</typeparam>
/// <typeparam name="V">The type of values.</typeparam>
/// <param name="theDictionary">The dictionary in which to look up values.</param>
/// <param name="key">They key to look up.</param>
/// <returns>The value of type T corresponding to the key if the key is present in the dictionary, otherwise, the defined default value for the type T of all values.</returns>
public static V GetValueOrDefault<K, V>(
this IDictionary<K, V> theDictionary,
K key)
where V : class
{
V result;
var foundP = theDictionary.TryGetValue(key, out result);
Contract.Assert(foundP == true || result == null);
return result ?? default(V);
}
public static V GetValueOrDefaultValueType<K, V>(
this IDictionary<K, V> theDictionary,
K key)
where V : struct
{
V result;
var foundP = theDictionary.TryGetValue(key, out result);
Contract.Assert(foundP == true || result.Equals(default(V)));
return result; // lapses automatically to default(T) if TryGetValue returns false
}
/// <summary>
/// Look up the value, and return an empty IEnumerable of the specified type if the key is not present.
/// </summary>
/// <typeparam name="K">The type of keys.</typeparam>
/// <typeparam name="V">The type of values.</typeparam>
/// <param name="theDictionary">The dictionary in which to look up values.</param>
/// <param name="key">They key to look up.</param>
/// <returns>The value of type T corresponding to the key if the key is present in the dictionary, otherwise, an empty enumerable of V's.</returns>
public static IEnumerable<V> GetValueOrEmpty<K, V>(
this IDictionary<K, IEnumerable<V>> theDictionary,
K key)
{
IEnumerable<V> result;
var foundP = theDictionary.TryGetValue(key, out result);
Contract.Assert(foundP == true || result == null); // null is the default for IEnumerable
var result2 = result ?? Enumerable.Empty<V>();
return result2;
}
/// <summary>
/// Look up the value, and return the specified value if the key is not present.
/// </summary>
/// <typeparam name="K">The type of keys.</typeparam>
/// <typeparam name="V">The type of values.</typeparam>
/// <param name="theDictionary">The dictionary in which to look up values.</param>
/// <param name="key">They key to look up.</param>
/// <param name="specified">The specified value to return if the key is not present.</param>
/// <returns>The value of type T corresponding to the key if the key is present in the dictionary, otherwise, the specified value of type V.</returns>
public static V GetValueOrSpecified<K, V>(
this IDictionary<K, V> theDictionary,
K key,
V specified)
where V : class
{
V result;
var foundP = theDictionary.TryGetValue(key, out result);
Contract.Assert(foundP == true || result == null);
var result2 = result ?? specified;
return result2;
}
/// <summary>
/// Look up the value, and return the specified value if the key is not present.
/// </summary>
/// <typeparam name="K">The type of keys.</typeparam>
/// <typeparam name="V">The type of values.</typeparam>
/// <param name="theDictionary">The dictionary in which to look up values.</param>
/// <param name="key">They key to look up.</param>
/// <param name="specified">The specified value to return if the key is not present.</param>
/// <returns>The value of type T corresponding to the key if the key is present in the dictionary, otherwise, the specified value of type V.</returns>
public static V GetValueOrSpecifiedValueType<K, V>(
this IDictionary<K, V> theDictionary,
K key,
V specified)
where V : struct
{
V result;
var foundP = theDictionary.TryGetValue(key, out result);
Contract.Assert(foundP == true || result.Equals(default(V)));
return result;
}
/// <summary>
/// Increases the tally count for the given key in the dictionary..
/// </summary>
/// <typeparam name="K">The type of keys.</typeparam>
/// <param name="theDictionary">The dictionary in which to tally keys.</param>
/// <param name="key">The key to tally.</param>
public static IDictionary<K, long> Tally<K>(
this IDictionary<K, long> theDictionary,
K key)
{
return theDictionary.AddUnconditionally(
key,
theDictionary.GetValueOrDefaultValueType(key) + 1);
}
/// <summary>
/// (Overload that creates the dictionary) Increases the tally count for the given key in the dictionary..
/// </summary>
/// <typeparam name="K">The type of keys.</typeparam>
/// <param name="key">The key to tally.</param>
public static IDictionary<K, long> Tally<K>(
K key)
{
return IDictionary.Create(key, 1L);
}
}
}