-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilCollection.cs
More file actions
341 lines (297 loc) · 7.64 KB
/
UtilCollection.cs
File metadata and controls
341 lines (297 loc) · 7.64 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
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;
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));
}
}
public UtilCollection(List<UtilCollection> list__)
{
list = list__;
isOrdered = true;
}
public UtilCollection(HashSet<UtilCollection> set__)
{
set = 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");
list[index] = value;
}
}
public void Add(UtilCollection item)
{
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;
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;
}
public override int GetHashCode()
{
return ToString().GetHashCode();
}
public List<UtilCollection> ToList()
{
if (isOrdered)
{
return 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;
}
}