/DLR_Main/Languages/IronPython/Scripts/generate_set.py
Python | 482 lines | 437 code | 31 blank | 14 comment | 6 complexity | 13e4d9a0e2066c4961a20148361a9525 MD5 | raw file
- #####################################################################################
- #
- # Copyright (c) Microsoft Corporation. All rights reserved.
- #
- # This source code is subject to terms and conditions of the Apache License, Version 2.0. A
- # copy of the license can be found in the License.html file at the root of this distribution. If
- # you cannot locate the Apache License, Version 2.0, please send an email to
- # ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
- # by the terms of the Apache License, Version 2.0.
- #
- # You must not remove this notice, or any other, from this software.
- #
- #
- #####################################################################################
-
- 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' ] + map(lambda x: x + '_update', op_names[1:])
- op_clrnames = [ 'BitwiseOr', 'BitwiseAnd', 'ExclusiveOr', 'Subtract' ]
-
- def gen_op(cw, t_left, t_right, symbol, name):
-