Skip to content

Commit f756dda

Browse files
added collection support
1 parent b9a8dcf commit f756dda

File tree

4 files changed

+251
-71
lines changed

4 files changed

+251
-71
lines changed

src/Reflector/Is.cs

+1-70
Original file line numberDiff line numberDiff line change
@@ -13,74 +13,5 @@ public static bool Equal(this DateTimeOffset actual, DateTimeOffset expected, Ti
1313
(actual - expected).Duration() < tolerance;
1414
public static bool Equal(this TimeOnly actual, TimeOnly expected, TimeSpan tolerance) => (actual - expected).Duration() < tolerance;
1515
public static bool Equal(this TimeSpan actual, TimeSpan expected, TimeSpan tolerance) => (actual - expected).Duration() < tolerance;
16-
public static bool SequenceEqual<T>([DisallowNull] this IEnumerable<T> actual, [NotNullWhen(true)] IEnumerable<T> expected)
17-
{
18-
if (actual == null || expected == null)
19-
return actual == expected;
20-
21-
return actual.SequenceEqual(expected);
22-
}
23-
public static bool SequenceEqual<T>([DisallowNull] this IEnumerable<T> actual, [NotNullWhen(true)] IEnumerable<T> expected, IEqualityComparer<T>? comparer)
24-
{
25-
if (actual == null || expected == null)
26-
return actual == expected;
27-
28-
return actual.SequenceEqual(expected, comparer);
29-
}
30-
public static bool SequenceEqualIgnoreOrder<T>([DisallowNull] this IEnumerable<T> actual, [NotNullWhen(true)] IEnumerable<T> expected)
31-
{
32-
if (actual == null || expected == null)
33-
return actual == expected;
34-
35-
var actualList = actual.ToList();
36-
var expectedList = expected.ToList();
37-
38-
if (actualList.Count != expectedList.Count)
39-
return false;
40-
41-
actualList.Sort();
42-
expectedList.Sort();
43-
44-
return actualList.SequenceEqual(expectedList);
45-
}
46-
public static bool SequenceEqualIgnoreOrder<T>([DisallowNull] this IEnumerable<T> actual, [NotNullWhen(true)] IEnumerable<T> expected, IEqualityComparer<T>? comparer)
47-
{
48-
if (actual == null || expected == null)
49-
return actual == expected;
50-
51-
var actualList = actual.ToList();
52-
var expectedList = expected.ToList();
53-
54-
if (actualList.Count != expectedList.Count)
55-
return false;
56-
57-
actualList.Sort();
58-
expectedList.Sort();
59-
60-
return actualList.SequenceEqual(expectedList, comparer);
61-
}
62-
public static bool AreSpansEqual([DisallowNull] this ReadOnlySpan<char> span1, [NotNullWhen(true)] ReadOnlySpan<char> span2)
63-
{
64-
return span1.SequenceEqual(span2);
65-
}
66-
public static bool HasUniqueItems<T>([DisallowNull] this IEnumerable<T> sequence)
67-
{
68-
var set = new HashSet<T>();
69-
foreach (var item in sequence)
70-
{
71-
if (!set.Add(item))
72-
return false;
73-
}
74-
return true;
75-
}
76-
public static bool HasUniqueItems<T>([DisallowNull] this IEnumerable<T> sequence, IEqualityComparer<T> comparer)
77-
{
78-
var set = new HashSet<T>(comparer);
79-
foreach (var item in sequence)
80-
{
81-
if (!set.Add(item))
82-
return false;
83-
}
84-
return true;
85-
}
16+
8617
}

src/Reflector/IsSequence.cs

+212
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
namespace VReflector;
4+
5+
public static class IsSequence
6+
{
7+
public static bool SequenceEqual<T>([DisallowNull] this IEnumerable<T> actual, [NotNullWhen(true)] IEnumerable<T> expected)
8+
{
9+
if (actual == null || expected == null)
10+
return actual == expected;
11+
12+
return actual.SequenceEqual(expected);
13+
}
14+
public static bool SequenceEqual<T>([DisallowNull] this IEnumerable<T> actual, [NotNullWhen(true)] IEnumerable<T> expected, IEqualityComparer<T>? comparer)
15+
{
16+
if (actual == null || expected == null)
17+
return actual == expected;
18+
19+
return actual.SequenceEqual(expected, comparer);
20+
}
21+
public static bool SequenceEqualIgnoreOrder<T>([DisallowNull] this IEnumerable<T> actual, [NotNullWhen(true)] IEnumerable<T> expected)
22+
{
23+
if (actual == null || expected == null)
24+
return actual == expected;
25+
26+
var actualList = actual.ToList();
27+
var expectedList = expected.ToList();
28+
29+
if (actualList.Count != expectedList.Count)
30+
return false;
31+
32+
actualList.Sort();
33+
expectedList.Sort();
34+
35+
return actualList.SequenceEqual(expectedList);
36+
}
37+
public static bool SequenceEqualIgnoreOrder<T>([DisallowNull] this IEnumerable<T> actual, [NotNullWhen(true)] IEnumerable<T> expected, IEqualityComparer<T>? comparer)
38+
{
39+
if (actual == null || expected == null)
40+
return actual == expected;
41+
42+
var actualList = actual.ToList();
43+
var expectedList = expected.ToList();
44+
45+
if (actualList.Count != expectedList.Count)
46+
return false;
47+
48+
actualList.Sort();
49+
expectedList.Sort();
50+
51+
return actualList.SequenceEqual(expectedList, comparer);
52+
}
53+
public static bool AreSpansEqual([DisallowNull] this ReadOnlySpan<char> span1, [NotNullWhen(true)] ReadOnlySpan<char> span2)
54+
{
55+
return span1.SequenceEqual(span2);
56+
}
57+
public static bool HasUniqueItems<T>([DisallowNull] this IEnumerable<T> sequence)
58+
{
59+
var set = new HashSet<T>();
60+
foreach (var item in sequence)
61+
{
62+
if (!set.Add(item))
63+
return false;
64+
}
65+
return true;
66+
}
67+
public static bool HasUniqueItems<T>([DisallowNull] this IEnumerable<T> sequence, IEqualityComparer<T> comparer)
68+
{
69+
var set = new HashSet<T>(comparer);
70+
foreach (var item in sequence)
71+
{
72+
if (!set.Add(item))
73+
return false;
74+
}
75+
return true;
76+
}
77+
public static (long DuplicateCount, long Count) GetDuplicateItems<T>([DisallowNull] this IEnumerable<T> sequence, IEqualityComparer<T> comparer = null)
78+
{
79+
long nonUniqueCount = 0;
80+
long allCount = 0;
81+
if (sequence != null)
82+
{
83+
HashSet<T> set = comparer != null ? new HashSet<T>(comparer) : new HashSet<T>();
84+
85+
foreach (var item in sequence)
86+
{
87+
allCount++;
88+
if (!set.Add(item))
89+
{
90+
nonUniqueCount++;
91+
}
92+
}
93+
}
94+
return (nonUniqueCount, allCount);
95+
}
96+
public static bool IsSortedAscending<T>([DisallowNull] this IEnumerable<T> collection) where T : IComparable<T>
97+
{
98+
if (collection == null || !collection.Any())
99+
{
100+
return true;
101+
}
102+
103+
T previous = collection.First();
104+
105+
foreach (T item in collection.Skip(1))
106+
{
107+
if (previous.CompareTo(item) >= 0)
108+
{
109+
return false;
110+
}
111+
previous = item;
112+
}
113+
114+
return true;
115+
}
116+
public static bool IsSortedDescending<T>([DisallowNull] this IEnumerable<T> collection) where T : IComparable<T>
117+
{
118+
if (collection == null || !collection.Any())
119+
{
120+
return true;
121+
}
122+
123+
T previous = collection.First();
124+
125+
foreach (T item in collection.Skip(1))
126+
{
127+
if (previous.CompareTo(item) <= 0)
128+
{
129+
return false;
130+
}
131+
previous = item;
132+
}
133+
134+
return true;
135+
}
136+
public static bool IsSortedAscendingAndUnique<T>([DisallowNull] this IEnumerable<T> collection) where T : IComparable<T>
137+
{
138+
if (collection == null || !collection.Any())
139+
{
140+
return true;
141+
}
142+
143+
T previous = collection.First();
144+
145+
foreach (T item in collection.Skip(1))
146+
{
147+
if (previous.CompareTo(item) > 0)
148+
{
149+
return false;
150+
}
151+
previous = item;
152+
}
153+
154+
return true;
155+
}
156+
public static bool IsSortedDescendingAndUnique<T>([DisallowNull] this IEnumerable<T> collection) where T : IComparable<T>
157+
{
158+
if (collection == null || !collection.Any())
159+
{
160+
return true;
161+
}
162+
163+
T previous = collection.First();
164+
165+
foreach (T item in collection.Skip(1))
166+
{
167+
if (previous.CompareTo(item) < 0)
168+
{
169+
return false;
170+
}
171+
previous = item;
172+
}
173+
174+
return true;
175+
}
176+
public static bool IsSubset<T>([DisallowNull] this IEnumerable<T> subset, IEnumerable<T> superset)
177+
{
178+
if (subset == null || superset == null)
179+
{
180+
return false;
181+
}
182+
var supersetHashSet = new HashSet<T>(superset);
183+
return subset.All(item => supersetHashSet.Contains(item));
184+
}
185+
public static IDictionary<TKey, TValue> MergeWith<TKey, TValue>([DisallowNull] this IDictionary<TKey, TValue> first, [DisallowNull] IDictionary<TKey, TValue> second)
186+
{
187+
if (first == null || second == null)
188+
{
189+
throw new ArgumentNullException(first == null ? nameof(first) : nameof(second));
190+
}
191+
192+
var result = new Dictionary<TKey, TValue>(first);
193+
194+
foreach (var kvp in second)
195+
{
196+
result[kvp.Key] = kvp.Value;
197+
}
198+
199+
return result;
200+
}
201+
public static bool ContainsAllKeys<TKey, TValue>([DisallowNull] this IDictionary<TKey, TValue> dictionary, [DisallowNull] IEnumerable<TKey> keys)
202+
{
203+
if (dictionary == null || keys == null)
204+
{
205+
throw new ArgumentNullException(dictionary == null ? nameof(dictionary) : nameof(keys));
206+
}
207+
208+
return keys.All(key => dictionary.ContainsKey(key));
209+
}
210+
211+
212+
}

src/Reflector/IsStackFrame.cs

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Diagnostics;
2+
using System.Diagnostics.CodeAnalysis;
3+
4+
namespace VReflector;
5+
6+
public static class IsStackFrame
7+
{
8+
public static string? GetSourceCodeStatementFrom([DisallowNull] this StackFrame frame)
9+
{
10+
if (frame == null) return null;
11+
12+
var fileName = frame.GetFileName();
13+
if (string.IsNullOrEmpty(fileName))
14+
{
15+
return null;
16+
}
17+
18+
var lineNumber = frame!.GetFileLineNumber();
19+
if (lineNumber == 0)
20+
{
21+
return null;
22+
}
23+
24+
try
25+
{
26+
var lines = File.ReadAllLines(fileName);
27+
return lines[lineNumber - 1].Trim();
28+
}
29+
catch
30+
{
31+
32+
return null;
33+
}
34+
35+
}
36+
37+
}

src/Reflector/VReflector.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7-
<VersionPrefix>2.1.0.0</VersionPrefix>
7+
<VersionPrefix>2.1.1.0</VersionPrefix>
88
<SignAssembly>True</SignAssembly>
99
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1010
<Title>VReflector</Title>

0 commit comments

Comments
 (0)