-
Notifications
You must be signed in to change notification settings - Fork 1
/
FlipRotate4D.cs
195 lines (180 loc) · 5.94 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
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) {
if(dimensions == 0) return FiniteSet<T>.Empty;
return GetValues<IFiniteSet<T>>(
dimensions, ref _reflections,
dim => new ReflectionsSet<T, P>(dim, (P p, int v) => new T(p, v))
);
}
public static IFiniteGroup<T> GetRotations(int dimensions) {
if(dimensions == 0) dimensions = 1;
return GetValues<IFiniteGroup<T>>(
dimensions, ref _rotations,
dim => new RotationsGroup<T, P>(dim, (P p, int v) => new T(p, v))
);
}
public static IFiniteGroup<T> GetAllValues(int dimensions) {
if(dimensions == 0) return GetRotations(1);
return GetValues<IFiniteGroup<T>>(
dimensions, ref _allValues,
dim => new FlipRotateGroup<T, P>(dim, (P p, int v) => new T(p, v))
);
}
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 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, Vertex.GetPrev<P>(p, _dimCount));
}
}
public T Add(T other) {
P p = Permutation;
return new T(p + other.Permutation,
Vertex ^ other.Vertex.GetPrev<P>(p, _dimCount)
);
}
public T Subtract(T other) {
P p = Permutation - other.Permutation;
return new T(p,
Vertex ^ other.Vertex.GetPrev<P>(p, _dimCount)
);
}
public T Times(int count) {
return FiniteGroupExtension.Times<T>(this, count);
}
public override int GetHashCode() {
return Permutation.GetHashCode() ^ Vertex;
}
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); }
}
}