-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEvent.cs
More file actions
218 lines (187 loc) · 5.8 KB
/
Event.cs
File metadata and controls
218 lines (187 loc) · 5.8 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
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
using System.Runtime.Serialization;
using System;
using System.Diagnostics;
namespace itcsharp
{
[Serializable]
public sealed class Event : ICloneable, IEquatable<Event>
{
public readonly Event Left;
public readonly Event Right;
public readonly int N;
public Event()
{
Left = null;
Right = null;
N = 0;
}
public Event(int value)
{
this.N = value;
}
public Event(int value, Event left, Event right)
{
this.N = value;
this.Left = left;
this.Right = right;
CheckConsistancy();
}
// with Normalization
internal Event(int value, Event left, Event right, NormalizeInitFlag _)
:this(value,left,right)
{
Normalize(ref N, ref Left, ref Right);
}
// for Clone
private Event(Event rhs)
{
this.N = rhs.N;
if (rhs.Left != null)
{
Left = new Event(rhs.Left);
Right = new Event(rhs.Right);
}
}
public object Clone()
{
return CloneT();
}
public Event CloneT()
{
return new Event(this);
}
public bool Equals(Event rhs)
{
if (rhs == null)
return false;
if (IsSimplex() && rhs.IsSimplex())
return N == rhs.N;
if (!IsSimplex() && !rhs.IsSimplex())
return N == rhs.N && Left.Equals(rhs.Left) && Right.Equals(rhs.Right);
return false;
}
public override string ToString()
{
if (IsSimplex())
return N.ToString();
return N + "+(" + Left + ", " + Right + ")";
}
public bool IsSimplex()
{
return Left == null;
}
//////////////////////////////////////////////////////////////////////////
// Event Operations
//////////////////////////////////////////////////////////////////////////
internal static int Min(Event e)
{
if (e == null)
return 0;
if (e.IsSimplex())
return e.N;
else
return e.N + Math.Min(Min(e.Left), Min(e.Right));
}
internal static int Max(Event e)
{
if (e == null)
return 0;
if (e.IsSimplex())
return e.N;
else
return e.N + Math.Max(Max(e.Left), Max(e.Right));
}
private Event Lift(int m)
{
if (IsSimplex())
return new Event(N + m);
else
return new Event(N + m, Left, Right);
}
private Event Sink(int m)
{
if (IsSimplex())
return new Event(N - m);
else
return new Event(N - m, Left, Right);
}
public Event Normalize()
{
var value = N;
var left = Left;
var right = Right;
if (Normalize(ref value, ref left, ref right))
return new Event(value,left, right);
return this;
}
private static bool Normalize(ref int value, ref Event left, ref Event right)
{
if (left == null)
return false;
// simply merge sub-events if they are equal
if (left.Left == null && right.Left == null && left.N == right.N)
{
value += left.N;
left = right = null;
}
else
{
var m = Math.Min(Min(left), Min(right));
value += m;
left = left.Sink(m);
right = right.Sink(m);
}
return true;
}
public static bool Leq(Event e1, Event e2)
{
// leq(n1,(n2,l2,r2)) => n1 <= n2
if (e1.IsSimplex())
return e1.N <= e2.N;
// leq((n1,l1,r1),n2) => n1 <= n2 && leq(l1^n1,n2) && leq(r1^n1,n2)
else if (e2.IsSimplex())
return e1.N <= e2.N &&
Leq(e1.Left.Lift(e1.N), e2) &&
Leq(e1.Right.Lift( e1.N), e2);
// leq((n1,l1,r1),(n2,l2,r2)) => n1 <= n2 && leq(l1^n1,l2^n2) && leq(r1^n1,r2^n2)
else
return e1.N <= e2.N &&
Leq(e1.Left.Lift(e1.N), e2.Left.Lift(e2.N)) &&
Leq(e1.Right.Lift(e1.N), e2.Right.Lift(e2.N));
}
public static Event Join(Event e1, Event e2)
{
if (e1.IsSimplex() && e2.IsSimplex())
return new Event(Math.Max(e1.N, e2.N));
else if (e1.IsSimplex())
return Join(new Event(e1.N, new Event(0), new Event(0)), e2);
else if (e2.IsSimplex())
return Join(e1, new Event(e2.N, new Event(0), new Event(0)));
else
{
if (e1.N > e2.N)
return Join(e2, e1);
else
{
Event left = Join(e1.Left, e2.Left.Lift( e2.N - e1.N));
Event right = Join(e1.Right, e2.Right.Lift( e2.N - e1.N));
NormalizeInitFlag f;
return new Event(e1.N, left, right, f);
}
}
}
public int MaxDepth()
{
if (IsSimplex())
return 0;
return Math.Max(Left.MaxDepth(), Right.MaxDepth())+1;
}
// Utils
private void CheckConsistancy()
{
if ((Left == null) ^ (Right == null))
throw new ChildNodeInconsistantException();
}
}
}