forked from leofun01/Transformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlipRotate4D.cs
275 lines (255 loc) · 7.79 KB
/
FlipRotate4D.cs
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using System;
using System.Collections.Generic;
using System.Diagnostics;
using CultureInfo = System.Globalization.CultureInfo;
using DotNetTransformer.Extensions;
using DotNetTransformer.Math.Group;
using DotNetTransformer.Math.Permutation;
using DotNetTransformer.Math.Set;
namespace DotNetTransformer.Math.Transform {
using T = FlipRotate4D;
using P = PermutationByte;
[Serializable]
[DebuggerDisplay("{ToString()}, CycleLength = {CycleLength}")]
public struct FlipRotate4D : IFlipRotate<T, P>
{
private readonly short _value;
private FlipRotate4D(short value) { _value = value; }
public FlipRotate4D(P permutation, int vertex) {
_value = (short)((vertex << _s | permutation._value) & 0x0FFF);
}
public static T None { get { return new T(); } }
public static T GetFlip(int dimension) {
if((dimension & -_dimCount) != 0)
throw new ArgumentOutOfRangeException("dimension");
return new T((short)(1 << dimension << _s));
}
public static T GetRotate(int dimFrom, int dimTo) {
if((dimFrom & -_dimCount) != 0)
throw new ArgumentOutOfRangeException("dimFrom");
if((dimTo & -_dimCount) != 0)
throw new ArgumentOutOfRangeException("dimTo");
if(dimFrom == dimTo)
throw new ArgumentException(
);
int x = dimFrom ^ dimTo;
P p = new P((byte)((x << (dimFrom << 1)) ^ (x << (dimTo << 1))));
return new T(p, 1 << dimTo);
}
private static IDictionary<byte, IFiniteSet<T>> _reflections;
private static IDictionary<byte, IFiniteGroup<T>> _rotations;
private static IDictionary<byte, IFiniteGroup<T>> _allValues;
public static IFiniteSet<T> GetReflections(int dimensions) {
return GetValues<IFiniteSet<T>>(
dimensions, ref _reflections,
dim => new ReflectionsSet(dim)
);
}
public static IFiniteGroup<T> GetRotations(int dimensions) {
return GetValues<IFiniteGroup<T>>(
dimensions, ref _rotations,
dim => new RotationsGroup(dim)
);
}
public static IFiniteGroup<T> GetAllValues(int dimensions) {
return GetValues<IFiniteGroup<T>>(
dimensions, ref _allValues,
dim => new FlipRotateGroup(dim)
);
}
private static S GetValues<S>(int dimensions,
ref IDictionary<byte, S> collection,
Converter<byte, S> ctor
)
where S : IFiniteSet<T>
{
if(dimensions < 0 || dimensions > _dimCount)
throw new ArgumentOutOfRangeException(
);
byte dim = (byte)dimensions;
if(ReferenceEquals(collection, null))
collection = new SortedList<byte, S>(_dimCount + 1);
if(collection.ContainsKey(dim))
return collection[dim];
else {
S r = ctor(dim);
collection.Add(dim, r);
return r;
}
}
private abstract class FlipRotateSet : FiniteSet<T>
{
protected readonly byte _dim;
protected FlipRotateSet(byte dimensions) {
_dim = dimensions;
}
protected bool IsRotational(int swaps, int vertex) {
for(int i = 1; i < _dim; i <<= 1)
vertex ^= vertex >> i;
return ((swaps ^ vertex) & 1) == 0;
}
public override long Count {
get {
long c = 1L;
for(byte i = 1; i < _dim; c *= ++i) ;
return c << _dim;
}
}
public override bool Contains(T item) {
return item.Vertex >> _dim == 0 &&
item.Permutation.ReducibleTo(_dim);
}
public override IEnumerator<T> GetEnumerator() {
int c = 1 << _dim;
P i = new P();
foreach(P p in i.GetRange<P>(i, _dim))
for(int v = 0; v < c; ++v)
yield return new T(p, v);
}
}
private class FlipRotateGroup : FlipRotateSet, IFiniteGroup<T>
{
public FlipRotateGroup(byte dimensions) : base(dimensions) { }
public T IdentityElement { get { return None; } }
}
private sealed class ReflectionsSet : FlipRotateSet
{
public ReflectionsSet(byte dimensions) : base(dimensions) { }
public override long Count {
get {
return base.Count >> 1;
}
}
public override bool Contains(T item) {
return base.Contains(item) && item.IsReflection;
}
public override IEnumerator<T> GetEnumerator() {
int c = 1 << _dim;
P i = new P();
foreach(P p in i.GetRange<P>(i, _dim)) {
int s = p.SwapsCount;
for(int v = 0; v < c; ++v)
if(!IsRotational(s, v))
yield return new T(p, v);
}
}
}
private sealed class RotationsGroup : FlipRotateGroup
{
public RotationsGroup(byte dimensions) : base(dimensions) { }
public override long Count {
get {
long c = base.Count;
return c - (c >> 1);
}
}
public override bool Contains(T item) {
return base.Contains(item) && item.IsRotation;
}
public override IEnumerator<T> GetEnumerator() {
int c = 1 << _dim;
P i = new P();
foreach(P p in i.GetRange<P>(i, _dim)) {
int s = p.SwapsCount;
for(int v = 0; v < c; ++v)
if(IsRotational(s, v))
yield return new T(p, v);
}
}
}
private const byte _dimCount = 4;
private const short _s = 8, _perm = (-1 << _s) ^ -1;
public bool IsReflection { get { return !IsRotation; } }
public bool IsRotation {
get {
int v = Vertex;
for(int i = 1; i < _dimCount; i <<= 1)
v ^= v >> i;
return ((Permutation.SwapsCount ^ v) & 1) == 0;
}
}
public P Permutation { get { return new P((byte)(_value & _perm)); } }
public int Vertex { get { return _value >> _s; } }
public int CycleLength {
get {
int c = Permutation.CycleLength;
return GroupExtension.Times<T>(this, c).Equals(None) ? c : (c << 1);
}
}
public T InverseElement {
get {
P p = -Permutation;
return new T(p, p.GetNextVertex<P>(Vertex));
}
}
public T Add(T other) {
P p = Permutation;
return new T(p + other.Permutation,
p.GetNextVertex<P>(other.Vertex) ^ Vertex
);
}
public T Subtract(T other) {
P p = Permutation - other.Permutation;
return new T(p,
p.GetNextVertex<P>(other.Vertex) ^ Vertex
);
}
public T Times(int count) {
return FiniteGroupExtension.Times<T>(this, count);
}
public override int GetHashCode() { return _value; }
public override bool Equals(object o) {
return o is T && Equals((T)o);
}
public bool Equals(T o) { return _value == o._value; }
public override string ToString() {
return string.Format(
CultureInfo.InvariantCulture,
"P:{0} V:{1:X1}", Permutation, Vertex
);
}
public PermutationInt64 ToPermutationInt64() {
P p = Permutation;
long v = Vertex;
const long b = 0x1111111111111111L;
for(byte i = 0, l = 4; i < _dimCount; ++i, l <<= 1)
v ^= ((1L << l) - 1L & (b << p[i]) ^ v) << l;
return PermutationInt64.FromInt64Internal(v);
}
/// <exception cref="ArgumentException">
/// <exception cref="ArgumentNullException">
/// Invalid <paramref name="s"/>.
/// </exception>
/// </exception>
public static T FromString(string s) {
if(ReferenceEquals(s, null)) throw new ArgumentNullException();
string[] ss = s.Trim().Split(
(" ").ToCharArray(),
StringSplitOptions.RemoveEmptyEntries
);
int len = ss.GetLength(0);
if(len != 2) throw new ArgumentException();
Dictionary<string, string> dict = new Dictionary<string, string>();
for(int j = 0; j < len; ++j) {
int i = ss[j].IndexOf(':');
dict.Add(ss[j].Substring(0, i), ss[j].Substring(i + 1));
}
return new T(
P.FromString(dict["P"]),
int.Parse(dict["V"]
, System.Globalization.NumberStyles.HexNumber
, CultureInfo.InvariantCulture
)
);
}
public static bool operator ==(T l, T r) { return l.Equals(r); }
public static bool operator !=(T l, T r) { return !l.Equals(r); }
public static T operator +(T o) { return o; }
public static T operator -(T o) { return o.InverseElement; }
public static T operator +(T l, T r) { return l.Add(r); }
public static T operator -(T l, T r) { return l.Subtract(r); }
public static T operator *(T l, int r) { return l.Times(r); }
public static T operator *(int l, T r) { return r.Times(l); }
public static implicit operator T(P o) { return new T(o._value); }
}
}