forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_set.py
More file actions
471 lines (376 loc) · 13.2 KB
/
generate_set.py
File metadata and controls
471 lines (376 loc) · 13.2 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# Licensed to the .NET Foundation under one or more agreements.
# The .NET Foundation licenses this file to you under the Apache 2.0 License.
# See the LICENSE file in the project root for more information.
from generate import generate
def get_type(mutable):
if mutable:
return 'SetCollection'
else:
return 'FrozenSetCollection'
def get_arg_ts(mutable):
return [get_type(mutable), get_type(not mutable), 'object']
def get_clrname(name):
return ''.join(map(str.capitalize, name.split('_')))
def get_items(arg_t):
if arg_t == 'object':
return 'SetStorage.GetItems(set)'
else:
return 'set._items'
def copy(cw, mutable):
if mutable:
cw.writeline('return copy();')
else:
cw.writeline('return Make(_items);')
def copy_op(cw, mutable, name):
t = get_type(mutable)
cw.enter_block('public %s %s()' % (t, name))
copy(cw, mutable)
cw.exit_block()
cw.writeline()
def simple_op(cw, t, arg_t, name):
clrname = get_clrname(name)
cw.enter_block('public %s %s(%s set)' % (t, name, arg_t))
simple_op_worker(cw, t, arg_t, name)
cw.exit_block()
cw.writeline()
def simple_op_worker(cw, t, arg_t, name):
clrname = get_clrname(name)
if arg_t == 'object':
cw.writeline('SetStorage items;')
cw.enter_block('if (SetStorage.GetItems(set, out items))')
cw.writeline('items = SetStorage.%s(_items, items);' % clrname)
cw.else_block()
cw.writeline('items.%sUpdate(_items);' % clrname)
cw.exit_block()
cw.writeline('return Make(items);')
else:
cw.writeline(
'return Make(SetStorage.%s(_items, set._items));' % clrname
)
def enter_multiarg_op(cw, t, name):
cw.enter_block('public %s %s([NotNull]params object[]/*!*/ sets)' % (t, name))
cw.writeline('Debug.Assert(sets != null);')
cw.writeline()
def union_multiarg(cw, mutable):
t = get_type(mutable)
enter_multiarg_op(cw, t, 'union')
cw.writeline('SetStorage res = _items.Clone();')
cw.enter_block('foreach (object set in sets)')
cw.writeline('res.UnionUpdate(SetStorage.GetItems(set));')
cw.exit_block()
cw.writeline()
cw.writeline('return Make(res);')
cw.exit_block()
cw.writeline()
def intersection_multiarg(cw, mutable):
t = get_type(mutable)
enter_multiarg_op(cw, t, 'intersection')
cw.enter_block('if (sets.Length == 0)')
copy(cw, mutable)
cw.exit_block()
cw.writeline()
cw.writeline('SetStorage res = _items;')
cw.enter_block('foreach (object set in sets)')
cw.writeline('SetStorage items, x = res, y;')
cw.enter_block('if (SetStorage.GetItems(set, out items))')
cw.writeline('y = items;')
cw.writeline('SetStorage.SortBySize(ref x, ref y);')
cw.writeline()
cw.enter_block('if (%s(x, items) || %s(x, _items))' %
(('object.ReferenceEquals',) * 2))
cw.writeline('x = x.Clone();')
cw.exit_block()
cw.else_block()
cw.writeline('y = items;')
cw.writeline('SetStorage.SortBySize(ref x, ref y);')
cw.writeline()
cw.enter_block('if (object.ReferenceEquals(x, _items))')
cw.writeline('x = x.Clone();')
cw.exit_block()
cw.exit_block()
cw.writeline('x.IntersectionUpdate(y);')
cw.writeline('res = x;')
cw.exit_block()
cw.writeline()
cw.writeline('Debug.Assert(!object.ReferenceEquals(res, _items));')
cw.writeline('return Make(res);')
cw.exit_block()
cw.writeline()
def difference(cw, t, arg_t):
items = get_items(arg_t)
cw.enter_block('public %s difference(%s set)' % (t, arg_t))
if (t == arg_t):
cw.enter_block('if (object.ReferenceEquals(set, this))')
cw.writeline('return Empty;')
cw.exit_block()
cw.writeline()
cw.writeline('return Make(')
cw.indent()
cw.writeline('SetStorage.Difference(_items, %s)' % items)
cw.dedent()
cw.writeline(');');
cw.exit_block()
cw.writeline()
def difference_multiarg(cw, mutable):
t = get_type(mutable)
enter_multiarg_op(cw, t, 'difference')
cw.enter_block('if (sets.Length == 0)')
copy(cw, mutable)
cw.exit_block()
cw.writeline()
cw.writeline('SetStorage res = _items;')
cw.enter_block('foreach (object set in sets)')
cw.enter_block('if (object.ReferenceEquals(set, this))')
cw.writeline('return Empty;')
cw.exit_block()
cw.writeline()
cw.writeline('SetStorage items = SetStorage.GetItems(set);')
cw.enter_block('if (object.ReferenceEquals(res, _items))')
cw.writeline('res = SetStorage.Difference(_items, items);')
cw.else_block()
cw.writeline('res.DifferenceUpdate(items);')
cw.exit_block()
cw.exit_block()
cw.writeline()
cw.writeline('Debug.Assert(!object.ReferenceEquals(res, _items));')
cw.writeline('return Make(res);')
cw.exit_block()
cw.writeline()
def symmetric_difference(cw, t, arg_t):
cw.enter_block('public %s symmetric_difference(%s set)' % (t, arg_t))
if (t == arg_t):
cw.enter_block('if (object.ReferenceEquals(set, this))')
cw.writeline('return Empty;')
cw.exit_block()
cw.writeline()
simple_op_worker(cw, t, arg_t, 'symmetric_difference')
cw.exit_block()
cw.writeline()
def gen_setops(mutable):
def _gen_setops(cw):
t = get_type(mutable)
arg_ts = get_arg_ts(mutable)
for arg_t in arg_ts:
items = get_items(arg_t)
cw.enter_block('public bool isdisjoint(%s set)' % arg_t)
cw.writeline('return _items.IsDisjoint(%s);' % items)
cw.exit_block()
cw.writeline()
for arg_t in arg_ts:
items = get_items(arg_t)
cw.enter_block('public bool issubset(%s set)' % arg_t)
cw.writeline('return _items.IsSubset(%s);' % items)
cw.exit_block()
cw.writeline()
for arg_t in arg_ts:
items = get_items(arg_t)
cw.enter_block('public bool issuperset(%s set)' % arg_t)
cw.writeline('return %s.IsSubset(_items);' % items)
cw.exit_block()
cw.writeline()
copy_op(cw, mutable, 'union')
for arg_t in arg_ts:
simple_op(cw, t, arg_t, 'union')
union_multiarg(cw, mutable)
copy_op(cw, mutable, 'intersection')
for arg_t in arg_ts:
simple_op(cw, t, arg_t, 'intersection')
intersection_multiarg(cw, mutable)
copy_op(cw, mutable, 'difference')
for arg_t in arg_ts:
difference(cw, t, arg_t)
difference_multiarg(cw, mutable)
for arg_t in arg_ts:
symmetric_difference(cw, t, arg_t)
return _gen_setops
op_symbols = [ '|', '&', '^', '-' ]
op_names = [ 'union', 'intersection', 'symmetric_difference', 'difference' ]
op_upnames = [ 'update' ] + [x + '_update' for x in op_names[1:]]
op_clrnames = [ 'BitwiseOr', 'BitwiseAnd', 'ExclusiveOr', 'Subtract' ]
def gen_op(cw, t_left, t_right, symbol, name):
cw.enter_block(
'public static %s operator %s(%s x, %s y)' %
(t_left, symbol, t_left, t_right)
)
cw.writeline('return x.%s(y);' % name)
cw.exit_block()
cw.writeline()
def gen_ops(mutable):
def _gen_ops(cw):
t = get_type(mutable)
u = get_type(not mutable)
ops = list(zip(op_symbols, op_names))
for symbol, name in ops:
gen_op(cw, t, t, symbol, name)
for symbol, name in ops:
gen_op(cw, t, u, symbol, name)
return _gen_ops
def gen_mutating_op(cw, t, arg_t, symbol, upname, clrname):
cw.writeline('[SpecialName]')
cw.enter_block('public %s InPlace%s(%s set)' % (t, clrname, arg_t))
if arg_t == 'object':
cw.enter_block(
'if (set is %s || set is %s)' %
tuple(map(get_type, [False, True]))
)
cw.writeline('%s(set);' % upname)
cw.writeline('return this;')
if arg_t == 'object':
cw.exit_block()
cw.writeline()
cw.writeline('throw PythonOps.TypeError(')
cw.indent()
cw.writeline(
'''"unsupported operand type(s) for %s=: '{0}' and '{1}'",''' %
symbol
)
cw.writeline('%s(this), %s(set)' % (('PythonTypeOps.GetName',) * 2))
cw.dedent()
cw.writeline(');')
cw.exit_block()
cw.writeline()
def gen_mutating_ops(cw):
t = get_type(True)
arg_ts = get_arg_ts(True)
for op in zip(op_symbols, op_upnames, op_clrnames):
for arg_t in arg_ts:
gen_mutating_op(cw, t, arg_t, *op)
compares = [ '>', '<', '>=', '<=' ]
def is_subset(compare):
return compare == '<' or compare == '<='
def is_strict(compare):
return not compare.endswith('=')
def gen_comparison(cw, t, compare):
cw.enter_block(
'public static bool operator %s(%s self, object other)' %
(compare, t)
)
cw.writeline('SetStorage items;')
cw.enter_block('if (SetStorage.GetItemsIfSet(other, out items))')
if is_subset(compare):
left = 'self._items'
right = 'items'
else:
left = 'items'
right = 'self._items'
if is_strict(compare):
func = 'IsStrictSubset'
else:
func = 'IsSubset'
cw.writeline('return %s.%s(%s);' % (left, func, right))
cw.exit_block()
cw.writeline()
cw.writeline('throw PythonOps.TypeError("can only compare to a set");')
cw.exit_block()
cw.writeline()
def suppress(cw, *msgs):
if len(msgs) == 0:
return
comma = ''
res = '['
for msg in msgs:
res += comma + 'System.Diagnostics.CodeAnalysis.SuppressMessage('
res += msg + ')'
comma = ' ,'
res += ']'
cw.writeline(res)
def gen_comparisons(cw, t):
cw.writeline('#region IRichComparable')
cw.writeline()
for compare in compares:
gen_comparison(cw, t, compare)
ca1822 = '"Microsoft.Performance", "CA1822:MarkMembersAsStatic"'
ca1801 = '"Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "o"'
throw_msg = 'throw PythonOps.TypeError("cannot compare sets using cmp()");'
suppress(cw, ca1822, ca1801)
cw.writeline('[SpecialName]')
cw.enter_block('public int Compare(object o)')
cw.writeline(throw_msg)
cw.exit_block()
cw.writeline()
suppress(cw, ca1822, ca1801)
cw.enter_block('public int __cmp__(object o)')
cw.writeline(throw_msg)
cw.exit_block()
cw.writeline()
cw.writeline('#endregion')
cw.writeline()
def gen_ienumerable(cw, mutable):
cw.writeline('#region IEnumerable Members')
cw.writeline()
cw.enter_block('IEnumerator IEnumerable.GetEnumerator()')
cw.writeline('return new SetIterator(_items, %s);' % str(mutable).lower())
cw.exit_block()
cw.writeline()
cw.writeline('#endregion')
cw.writeline()
cw.writeline('#region IEnumerable<object> Members')
cw.writeline()
cw.enter_block('IEnumerator<object> IEnumerable<object>.GetEnumerator()')
cw.writeline('return new SetIterator(_items, %s);' % str(mutable).lower())
cw.exit_block()
cw.writeline()
cw.writeline('#endregion')
cw.writeline()
def gen_icodeformattable(cw):
cw.writeline('#region ICodeFormattable Members')
cw.writeline()
cw.enter_block('public virtual string/*!*/ __repr__(CodeContext/*!*/ context)')
cw.writeline('return SetStorage.SetToString(context, this, _items);')
cw.exit_block()
cw.writeline()
cw.writeline('#endregion')
cw.writeline()
def gen_icollection(cw):
cw.writeline('#region ICollection Members')
cw.writeline()
cw.enter_block('void ICollection.CopyTo(Array array, int index)')
cw.writeline('int i = 0;')
cw.enter_block('foreach (object o in this)')
cw.writeline('array.SetValue(o, index + i++);')
cw.exit_block()
cw.exit_block()
cw.writeline()
cw.enter_block('public int Count')
cw.writeline('[PythonHidden]')
cw.writeline('get { return _items.Count; }')
cw.exit_block()
cw.writeline()
cw.enter_block('bool ICollection.IsSynchronized')
cw.writeline('get { return false; }')
cw.exit_block()
cw.writeline()
cw.enter_block('object ICollection.SyncRoot')
cw.writeline('get { return this; }')
cw.exit_block()
cw.writeline()
cw.writeline('#endregion')
cw.writeline()
def gen_interfaces(mutable):
def _gen_interfaces(cw):
t = get_type(mutable)
gen_comparisons(cw, t)
gen_ienumerable(cw, mutable)
gen_icodeformattable(cw)
gen_icollection(cw)
return _gen_interfaces
def main():
generators = [
('NonOperator Operations', gen_setops),
('Operators', gen_ops),
('Interface Implementations', gen_interfaces),
]
mutable_generators = [
('Mutating Operators', gen_mutating_ops),
]
_generators = []
for title, func in generators:
for bit in [True, False]:
_generators.append((
title + ' (' + get_type(bit) + ')',
func(bit)
))
_generators.extend(mutable_generators)
return generate(*_generators)
if __name__ == '__main__':
main()