-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashMapHelper.cs
48 lines (43 loc) · 1.5 KB
/
HashMapHelper.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
//---------------------------------------------------------------------------------------------------------
// Copyright © 2007 - 2021 Tangible Software Solutions, Inc.
// This class can be used by anyone provided that the copyright notice remains intact.
//
// This class is used to replace calls to some Java HashMap or Hashtable methods.
//---------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
internal static class HashMapHelper
{
public static HashSet<KeyValuePair<TKey, TValue>> SetOfKeyValuePairs<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
HashSet<KeyValuePair<TKey, TValue>> entries = new HashSet<KeyValuePair<TKey, TValue>>();
foreach (KeyValuePair<TKey, TValue> keyValuePair in dictionary)
{
entries.Add(keyValuePair);
}
return entries;
}
public static TValue GetValueOrNull<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
{
TValue ret;
dictionary.TryGetValue(key, out ret);
return ret;
}
public static TValue GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
{
TValue ret;
if (dictionary.TryGetValue(key, out ret))
return ret;
else
return defaultValue;
}
public static void PutAll<TKey, TValue>(this IDictionary<TKey, TValue> d1, IDictionary<TKey, TValue> d2)
{
if (d2 is null)
throw new NullReferenceException();
foreach (TKey key in d2.Keys)
{
d1[key] = d2[key];
}
}
}