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
|
using System;
using System.Collections.Generic;
using System.Text;
namespace Org.BouncyCastle.Utilities.Collections
{
public abstract class CollectionUtilities
{
public static void CollectMatches<T>(ICollection<T> matches, ISelector<T> selector,
IEnumerable<IStore<T>> stores)
{
if (matches == null)
throw new ArgumentNullException(nameof(matches));
if (stores == null)
return;
foreach (var store in stores)
{
if (store == null)
continue;
foreach (T match in store.EnumerateMatches(selector))
{
matches.Add(match);
}
}
}
public static IStore<T> CreateStore<T>(IEnumerable<T> contents)
{
return new StoreImpl<T>(contents);
}
public static T GetValueOrKey<T>(IDictionary<T, T> d, T k)
{
return d.TryGetValue(k, out var v) ? v : k;
}
public static V GetValueOrNull<K, V>(IDictionary<K, V> d, K k)
where V : class
{
return d.TryGetValue(k, out var v) ? v : null;
}
public static IEnumerable<T> Proxy<T>(IEnumerable<T> e)
{
return new EnumerableProxy<T>(e);
}
public static ICollection<T> ReadOnly<T>(ICollection<T> c)
{
return new ReadOnlyCollectionProxy<T>(c);
}
public static IDictionary<K, V> ReadOnly<K, V>(IDictionary<K, V> d)
{
return new ReadOnlyDictionaryProxy<K, V>(d);
}
public static IList<T> ReadOnly<T>(IList<T> l)
{
return new ReadOnlyListProxy<T>(l);
}
public static ISet<T> ReadOnly<T>(ISet<T> s)
{
return new ReadOnlySetProxy<T>(s);
}
public static bool Remove<K, V>(IDictionary<K, V> d, K k, out V v)
{
if (!d.TryGetValue(k, out v))
return false;
d.Remove(k);
return true;
}
public static T RequireNext<T>(IEnumerator<T> e)
{
if (!e.MoveNext())
throw new InvalidOperationException();
return e.Current;
}
public static string ToString<T>(IEnumerable<T> c)
{
IEnumerator<T> e = c.GetEnumerator();
if (!e.MoveNext())
return "[]";
StringBuilder sb = new StringBuilder("[");
sb.Append(e.Current);
while (e.MoveNext())
{
sb.Append(", ");
sb.Append(e.Current);
}
sb.Append(']');
return sb.ToString();
}
}
}
|