Skip to content

Commit 0a3520b

Browse files
fixed collection
1 parent f756dda commit 0a3520b

File tree

2 files changed

+102
-10
lines changed

2 files changed

+102
-10
lines changed

src/Reflector/IsSequence.cs

+101-9
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public static (long DuplicateCount, long Count) GetDuplicateItems<T>([DisallowNu
9393
}
9494
return (nonUniqueCount, allCount);
9595
}
96-
public static bool IsSortedAscending<T>([DisallowNull] this IEnumerable<T> collection) where T : IComparable<T>
96+
public static bool IsSortedAscending<T>([DisallowNull] this IEnumerable<T> collection, IComparer<T> comparer)
9797
{
9898
if (collection == null || !collection.Any())
9999
{
@@ -104,7 +104,7 @@ public static bool IsSortedAscending<T>([DisallowNull] this IEnumerable<T> colle
104104

105105
foreach (T item in collection.Skip(1))
106106
{
107-
if (previous.CompareTo(item) >= 0)
107+
if (comparer.Compare(previous, item) >= 0)
108108
{
109109
return false;
110110
}
@@ -113,7 +113,30 @@ public static bool IsSortedAscending<T>([DisallowNull] this IEnumerable<T> colle
113113

114114
return true;
115115
}
116-
public static bool IsSortedDescending<T>([DisallowNull] this IEnumerable<T> collection) where T : IComparable<T>
116+
public static bool IsSortedAscending<T>([DisallowNull] this IEnumerable<T> collection)
117+
{
118+
var comparer = Comparer<T>.Default;
119+
var enumerator = collection.GetEnumerator();
120+
121+
if (!enumerator.MoveNext())
122+
{
123+
return true;
124+
}
125+
126+
T previous = enumerator.Current;
127+
128+
while (enumerator.MoveNext())
129+
{
130+
if (comparer.Compare(previous, enumerator.Current) >= 0)
131+
{
132+
return false;
133+
}
134+
previous = enumerator.Current;
135+
}
136+
137+
return true;
138+
}
139+
public static bool IsSortedDescending<T>([DisallowNull] this IEnumerable<T> collection, IComparer<T> comparer)
117140
{
118141
if (collection == null || !collection.Any())
119142
{
@@ -124,7 +147,7 @@ public static bool IsSortedDescending<T>([DisallowNull] this IEnumerable<T> coll
124147

125148
foreach (T item in collection.Skip(1))
126149
{
127-
if (previous.CompareTo(item) <= 0)
150+
if (comparer.Compare(previous, item) <= 0)
128151
{
129152
return false;
130153
}
@@ -133,7 +156,30 @@ public static bool IsSortedDescending<T>([DisallowNull] this IEnumerable<T> coll
133156

134157
return true;
135158
}
136-
public static bool IsSortedAscendingAndUnique<T>([DisallowNull] this IEnumerable<T> collection) where T : IComparable<T>
159+
public static bool IsSortedDescending<T>([DisallowNull] this IEnumerable<T> collection)
160+
{
161+
var comparer = Comparer<T>.Default;
162+
var enumerator = collection.GetEnumerator();
163+
164+
if (!enumerator.MoveNext())
165+
{
166+
return true;
167+
}
168+
169+
T previous = enumerator.Current;
170+
171+
while (enumerator.MoveNext())
172+
{
173+
if (comparer.Compare(previous, enumerator.Current) <= 0)
174+
{
175+
return false;
176+
}
177+
previous = enumerator.Current;
178+
}
179+
180+
return true;
181+
}
182+
public static bool IsSortedAscendingAndUnique<T>([DisallowNull] this IEnumerable<T> collection, IComparer<T> comparer)
137183
{
138184
if (collection == null || !collection.Any())
139185
{
@@ -144,7 +190,7 @@ public static bool IsSortedAscendingAndUnique<T>([DisallowNull] this IEnumerable
144190

145191
foreach (T item in collection.Skip(1))
146192
{
147-
if (previous.CompareTo(item) > 0)
193+
if (comparer.Compare(previous, item) > 0)
148194
{
149195
return false;
150196
}
@@ -153,7 +199,30 @@ public static bool IsSortedAscendingAndUnique<T>([DisallowNull] this IEnumerable
153199

154200
return true;
155201
}
156-
public static bool IsSortedDescendingAndUnique<T>([DisallowNull] this IEnumerable<T> collection) where T : IComparable<T>
202+
public static bool IsSortedAscendingAndUnique<T>([DisallowNull] this IEnumerable<T> collection)
203+
{
204+
var comparer = Comparer<T>.Default;
205+
var enumerator = collection.GetEnumerator();
206+
207+
if (!enumerator.MoveNext())
208+
{
209+
return true;
210+
}
211+
212+
T previous = enumerator.Current;
213+
214+
while (enumerator.MoveNext())
215+
{
216+
if (comparer.Compare(previous, enumerator.Current) > 0)
217+
{
218+
return false;
219+
}
220+
previous = enumerator.Current;
221+
}
222+
223+
return true;
224+
}
225+
public static bool IsSortedDescendingAndUnique<T>([DisallowNull] this IEnumerable<T> collection, IComparer<T> comparer)
157226
{
158227
if (collection == null || !collection.Any())
159228
{
@@ -164,7 +233,7 @@ public static bool IsSortedDescendingAndUnique<T>([DisallowNull] this IEnumerabl
164233

165234
foreach (T item in collection.Skip(1))
166235
{
167-
if (previous.CompareTo(item) < 0)
236+
if (comparer.Compare(previous, item) < 0)
168237
{
169238
return false;
170239
}
@@ -173,6 +242,29 @@ public static bool IsSortedDescendingAndUnique<T>([DisallowNull] this IEnumerabl
173242

174243
return true;
175244
}
245+
public static bool IsSortedDescendingAndUnique<T>([DisallowNull] this IEnumerable<T> collection)
246+
{
247+
var comparer = Comparer<T>.Default;
248+
var enumerator = collection.GetEnumerator();
249+
250+
if (!enumerator.MoveNext())
251+
{
252+
return true;
253+
}
254+
255+
T previous = enumerator.Current;
256+
257+
while (enumerator.MoveNext())
258+
{
259+
if (comparer.Compare(previous, enumerator.Current) < 0)
260+
{
261+
return false;
262+
}
263+
previous = enumerator.Current;
264+
}
265+
266+
return true;
267+
}
176268
public static bool IsSubset<T>([DisallowNull] this IEnumerable<T> subset, IEnumerable<T> superset)
177269
{
178270
if (subset == null || superset == null)
@@ -193,7 +285,7 @@ public static IDictionary<TKey, TValue> MergeWith<TKey, TValue>([DisallowNull] t
193285

194286
foreach (var kvp in second)
195287
{
196-
result[kvp.Key] = kvp.Value;
288+
result[kvp.Key] = kvp.Value;
197289
}
198290

199291
return result;

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.1.0</VersionPrefix>
7+
<VersionPrefix>2.1.1.1</VersionPrefix>
88
<SignAssembly>True</SignAssembly>
99
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1010
<Title>VReflector</Title>

0 commit comments

Comments
 (0)