Repository navigation
Expand file tree
/
Copy pathUtilCollection.cs
More file actions
416 lines (365 loc) · 10.3 KB
/
Copy pathUtilCollection.cs
File metadata and controls
416 lines (365 loc) · 10.3 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
using System.Collections;
using System.Runtime.Serialization;
namespace SPADE;
#pragma warning disable CS1591
public class UtilCollection
{
private HashSet<UtilCollection> set = new HashSet<UtilCollection>();
private List<UtilCollection> list = new List<UtilCollection>();
private string? value;
private bool isOrdered;
private bool isValue;
// Set the first time this collection is hashed. A HashSet keeps an element under the hash it
// had when it went in, so changing the element (or anything inside it) afterwards would leave
// it under a stale hash, and lookups would quietly miss it. Once frozen, every change throws.
// Hashing a list or set hashes its elements, so they freeze too, all the way down.
// volatile: GetHashCode writes hashCode before frozen, so a thread that sees frozen also sees
// the hash. A frozen collection is safe to read from several threads.
private volatile bool frozen;
private int hashCode;
public UtilCollection()
{
}
public UtilCollection(string instance)
{
if (instance[0] == '{')
{
isOrdered = false;
isValue = false;
instance = instance.Substring(1, instance.Length - 2);
while (instance != "")
{
string item = findMatchingBrace(instance);
set.Add(new UtilCollection(item));
instance = instance.Substring(item.Length).TrimStart(',');
}
}
else if (instance[0] == '(')
{
isOrdered = true;
isValue = false;
instance = instance.Substring(1, instance.Length - 2);
while (instance != "")
{
string item = findMatchingBrace(instance);
list.Add(new UtilCollection(item));
instance = instance.Substring(item.Length).TrimStart(',');
}
}
else
{
isValue = true;
value = instance;
}
}
public UtilCollection(HashSet<string> set_)
{
isOrdered = false;
set = new();
foreach (string i in set_)
{
set.Add(new UtilCollection(i));
}
}
public UtilCollection(HashSet<HashSet<string>> set_)
{
isOrdered = false;
set = new();
foreach (HashSet<string> i in set_)
{
set.Add(new UtilCollection(i));
}
}
// Both copy, so later changes to the caller's list or set can't change this collection.
public UtilCollection(List<UtilCollection> list__)
{
list = new List<UtilCollection>(list__);
isOrdered = true;
}
public UtilCollection(HashSet<UtilCollection> set__)
{
set = new HashSet<UtilCollection>(set__);
isOrdered = false;
}
private string findMatchingBrace(string instance)
{
Dictionary<char, char> braces = new Dictionary<char, char>
{
{ '(', ')' },
{ '{', '}' },
};
if (!braces.ContainsKey(instance[0]))
{
return instance.Split(',')[0];
}
Stack<char> stack = new Stack<char>();
for (int i = 0; i < instance.Length; i++)
{
if (braces.ContainsKey(instance[i]))
{
stack.Push(instance[i]);
}
else if (braces.Values.Contains(instance[i]))
{
char openBracket = stack.Pop();
if (braces[openBracket] != instance[i])
{
throw new Exception("Braces not matched");
}
if (stack.Count() == 0)
{
return instance.Substring(0, i + 1);
}
}
}
throw new Exception("Braces not matched");
}
public UtilCollection this[int index]
{
get
{
if (!isOrdered) throw new InvalidOperationException("Cannot index into a set");
return list[index];
}
set
{
if (!isOrdered) throw new InvalidOperationException("Cannot index into a set");
ThrowIfFrozen();
list[index] = value;
}
}
public void Add(UtilCollection item)
{
ThrowIfFrozen();
if (isOrdered)
{
list.Add(item);
}
else
{
set.Add(item);
}
}
public IEnumerator GetEnumerator()
{
if (isValue) throw new InvalidOperationException("Cannot enumerate over a value");
if (isOrdered)
return list.GetEnumerator();
else
return set.GetEnumerator();
}
public void assertPair(int len)
{
if (!isOrdered || Count() != len)
throw new InvalidOperationException("Not a list of correct length");
}
public void assertPair()
{
assertPair(2);
}
public void assertCount(int len)
{
if (len != Count())
{
throw new InvalidOperationException("collection has incorrect length");
}
}
public void assertOrdered()
{
if (!isOrdered) throw new InvalidOperationException("Not ordered");
}
public void assertUnordered()
{
if (isOrdered) throw new InvalidOperationException("Ordered");
}
public bool IsUnordered()
{
return !isOrdered;
}
public bool IsOrdered()
{
return isOrdered;
}
public bool IsValue()
{
return isValue;
}
public bool Contains(UtilCollection collection)
{
if (isOrdered)
{
return list.Contains(collection);
}
else
{
return set.Contains(collection);
}
}
public int Count()
{
if (isOrdered)
{
return list.Count;
}
else
{
return set.Count;
}
}
public int parseInt()
{
return int.Parse(ToString());
}
public UtilCollection Intersect(UtilCollection other)
{
if (isOrdered != other.isOrdered) throw new InvalidOperationException("Can't take the intersection of a set and a list");
if (!isOrdered)
{
return new UtilCollection(set.Intersect(other.set).ToHashSet());
}
else
{
throw new NotImplementedException();
}
}
public UtilCollection Union(UtilCollection other)
{
if (isOrdered != other.isOrdered) throw new InvalidOperationException("Can't take the union of a set and a list");
if (!isOrdered)
{
return new UtilCollection(set.Union(other.set).ToHashSet());
}
else
{
throw new NotImplementedException();
}
}
public UtilCollection Cross(UtilCollection other)
{
UtilCollection ret = new UtilCollection();
foreach (UtilCollection a in this)
{
foreach (UtilCollection b in other)
{
UtilCollection add = new UtilCollection(new List<UtilCollection> { a, b });
ret.Add(add);
}
}
return ret;
}
public override bool Equals(object? obj)
{
if (obj == null || GetType() != obj.GetType()) return false;
UtilCollection other = (UtilCollection)obj;
// A value, a list and a set are never equal to each other, even when empty.
if (isValue != other.isValue || isOrdered != other.isOrdered) return false;
if (isValue)
{
return value == other.value;
}
if (isOrdered)
{
return list.SequenceEqual(other.list);
}
else
{
return set.SetEquals(other.set);
}
}
public bool NotEquals(object? obj)
{
if (Equals(obj)) return false;
return true;
}
// Freezes this collection and everything inside it, so it can be shared between threads, or
// kept, knowing nothing will change it. Hashing does the freezing, all the way down.
public UtilCollection Freeze()
{
GetHashCode();
return this;
}
public bool IsFrozen()
{
return frozen;
}
private void ThrowIfFrozen()
{
if (frozen)
{
throw new InvalidOperationException(
"This collection has been hashed (it is in a set or used as a key) and can't be changed; "
+ "build it fully before adding it to a set.");
}
}
// The first call freezes the collection, so the hash can't change and is worked out only once.
public override int GetHashCode()
{
if (!frozen)
{
hashCode = ComputeHashCode();
frozen = true;
}
return hashCode;
}
private int ComputeHashCode()
{
if (isValue)
{
return value!.GetHashCode();
}
if (isOrdered)
{
HashCode hash = new HashCode();
hash.Add(isOrdered);
foreach (UtilCollection item in list)
{
hash.Add(item);
}
return hash.ToHashCode();
}
// Sets are equal regardless of order, so combine element hashes with
// an order-independent sum rather than hashing ToString(), which
// follows insertion order.
int sum = 0;
foreach (UtilCollection item in set)
{
sum = unchecked(sum + item.GetHashCode());
}
return HashCode.Combine(isOrdered, set.Count, sum);
}
public List<UtilCollection> ToList()
{
if (isOrdered)
{
// A copy, so changes to it can't reach a collection that may already be frozen.
return new List<UtilCollection>(list);
}
else
{
return set.ToList();
}
}
override public string ToString()
{
if (isValue)
{
return value!;
}
string str = "";
if (isOrdered)
str += "(";
else
str += "{";
foreach (var item in this)
{
//Console.WriteLine(item.ToString());
str += item.ToString() + ",";
}
str = str.TrimEnd(',');
if (isOrdered)
str += ")";
else
str += "}";
return str;
}
}