-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubjects.cs
More file actions
106 lines (83 loc) · 3.23 KB
/
Copy pathSubjects.cs
File metadata and controls
106 lines (83 loc) · 3.23 KB
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
using System.Runtime.CompilerServices;
using StringSortProof.Counting;
namespace StringSortProof;
/// <summary>
/// Все измеряемые записи. Каждая сортирует копию одного и того же набора:
/// без копии второй вызов сортировал бы уже отсортированное.
/// Сколько занимает копирование само по себе, показывает Copy.
/// </summary>
public static class Subjects
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static string[] Copy(string[] source) => (string[])source.Clone();
// Массив. Различается только то, чем задан порядок.
[MethodImpl(MethodImplOptions.NoInlining)]
public static string[] ArrayDefault(string[] source)
{
string[] work = (string[])source.Clone();
Array.Sort(work);
return work;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static string[] ArrayWith(string[] source, IComparer<string> comparer)
{
string[] work = (string[])source.Clone();
Array.Sort(work, comparer);
return work;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static string[] ArrayComparison(string[] source, Comparison<string> comparison)
{
string[] work = (string[])source.Clone();
Array.Sort(work, comparison);
return work;
}
// Другие способы отсортировать тот же набор.
[MethodImpl(MethodImplOptions.NoInlining)]
public static string[] ListDefault(string[] source)
{
List<string> work = [.. source];
work.Sort();
return [.. work];
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static string[] ListWith(string[] source, IComparer<string> comparer)
{
List<string> work = [.. source];
work.Sort(comparer);
return [.. work];
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static string[] SpanDefault(string[] source)
{
string[] work = (string[])source.Clone();
work.AsSpan().Sort();
return work;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static string[] SpanWith(string[] source, IComparer<string> comparer)
{
string[] work = (string[])source.Clone();
work.AsSpan().Sort(comparer);
return work;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static string[] OrderByDefault(string[] source) => [.. source.OrderBy(static x => x)];
[MethodImpl(MethodImplOptions.NoInlining)]
public static string[] OrderByWith(string[] source, IComparer<string> comparer) => [.. source.OrderBy(static x => x, comparer)];
/// <summary>
/// Проверка, что набор отсортирован по правилу того компаратора,
/// которым его сортировали.
/// </summary>
public static bool IsSorted(string[] items, IComparer<string> comparer)
{
for (int i = 1; i < items.Length; i++)
{
if (comparer.Compare(items[i - 1], items[i]) > 0)
{
return false;
}
}
return true;
}
}