-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPermutationExtension.cs
More file actions
150 lines (145 loc) · 4.21 KB
/
PermutationExtension.cs
File metadata and controls
150 lines (145 loc) · 4.21 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Collections.Generic;
using DotNetTransformer.Extensions;
using DotNetTransformer.Math.Set;
namespace DotNetTransformer.Math.Permutation {
public static class PermutationExtension
{
public static IFiniteSet<T> GetCyclesAll<T>(this T _this)
where T : IPermutation<T>, new()
{
return _this.GetCycles(p => true);
}
public static IFiniteSet<T> GetCyclesNonTrivial<T>(this T _this)
where T : IPermutation<T>, new()
{
return _this.GetCycles(p => p.CycleLength > 1);
}
public static int GetCyclesCountAll<T>(this T _this)
where T : IPermutation<T>, new()
{
return _this.GetCyclesCount(i => true);
}
public static int GetCyclesCountNonTrivial<T>(this T _this)
where T : IPermutation<T>, new()
{
return _this.GetCyclesCount(i => i > 1);
}
public static byte GetNext<T>(this byte v, T p)
where T : IPermutation<T>, new()
{
return (byte)GetNext<T>(v, p, 8);
}
public static byte GetPrev<T>(this byte v, T p)
where T : IPermutation<T>, new()
{
return (byte)GetPrev<T>(v, p, 8);
}
public static short GetNext<T>(this short v, T p)
where T : IPermutation<T>, new()
{
return (short)GetNext<T>(v, p, 16);
}
public static short GetPrev<T>(this short v, T p)
where T : IPermutation<T>, new()
{
return (short)GetPrev<T>(v, p, 16);
}
public static int GetNext<T>(this int v, T p)
where T : IPermutation<T>, new()
{
return GetNext<T>(v, p, 32);
}
public static int GetPrev<T>(this int v, T p)
where T : IPermutation<T>, new()
{
return GetPrev<T>(v, p, 32);
}
public static int GetNext<T>(this int v, T p, byte size)
where T : IPermutation<T>, new()
{
int r = 0;
for(byte i = 0; i < size; ++i)
r |= (v >> p[i] & 1) << i;
return r;
}
public static int GetPrev<T>(this int v, T p, byte size)
where T : IPermutation<T>, new()
{
int r = 0;
for(byte i = 0; i < size; ++i)
r |= (v >> i & 1) << p[i];
return r;
}
public static void ApplyNextPermutation<T>(this T[] a, IEnumerable<int> indexes, Order<T> match) {
if(a == null || indexes == null) return;
if(match == null) throw new ArgumentNullException();
IEnumerator<int> ie = indexes.GetEnumerator();
bool moved = ie.MoveNext();
if(!moved) return;
int n = ie.Current, p = n;
Stack<int> stack = new Stack<int>();
do
stack.Push(n);
while((moved = ie.MoveNext()) && match(a[p = n], a[n = ie.Current]));
if(moved) {
T t = a[n];
ie = indexes.GetEnumerator();
while(ie.MoveNext() && match(a[p = ie.Current], t)) ;
a[n] = a[p];
a[p] = t;
}
ie = indexes.GetEnumerator();
while(ie.MoveNext() && (n = ie.Current) < (p = stack.Pop())) {
T t = a[n];
a[n] = a[p];
a[p] = t;
}
stack.Clear();
}
public static void ApplyNextPermutation<T>(this T[] a, IEnumerable<int> indexes)
where T : IComparable<T>
{
ApplyNextPermutation<T>(a, indexes, (T l, T r) => l != null && l.CompareTo(r) >= 0);
}
public static void ApplyPrevPermutation<T>(this T[] a, IEnumerable<int> indexes)
where T : IComparable<T>
{
ApplyNextPermutation<T>(a, indexes, (T l, T r) => l != null && l.CompareTo(r) <= 0);
}
public static void ApplyNextPermutation<T>(this T[] a, int maxLength, Order<T> match) {
if(a == null) return;
if(match == null) throw new ArgumentNullException();
int length = a.GetLength(0);
if(length > maxLength) length = maxLength;
int n = 0, p = 0;
while(++n < length && match(a[n - 1], a[n])) ;
if(n < length) {
T t = a[n];
while(match(a[p], t)) ++p;
a[n] = a[p];
a[p] = t;
}
for(p = 0; p < --n; ++p) {
T t = a[n];
a[n] = a[p];
a[p] = t;
}
}
public static void ApplyNextPermutation<T>(this T[] a, int maxLength)
where T : IComparable<T>
{
ApplyNextPermutation<T>(a, maxLength, (T l, T r) => l != null && l.CompareTo(r) >= 0);
}
public static void ApplyPrevPermutation<T>(this T[] a, int maxLength)
where T : IComparable<T>
{
ApplyNextPermutation<T>(a, maxLength, (T l, T r) => l != null && l.CompareTo(r) <= 0);
}
public static IEnumerable<T> GetRange<T>(this T start, T stop, int maxLength)
where T : IPermutation<T>, new()
{
return start.GetRange<T>(stop, p => p.GetNextPermutation(maxLength));
}
}
}