/Languages/IronPython/Scripts/generate_dict_views.py
Python | 113 lines | 86 code | 13 blank | 14 comment | 18 complexity | ad0320bd5ca5f937d5d978d361045fbf MD5 | raw file
1#####################################################################################
2#
3# Copyright (c) Microsoft Corporation. All rights reserved.
4#
5# This source code is subject to terms and conditions of the Apache License, Version 2.0. A
6# copy of the license can be found in the License.html file at the root of this distribution. If
7# you cannot locate the Apache License, Version 2.0, please send an email to
8# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
9# by the terms of the Apache License, Version 2.0.
10#
11# You must not remove this notice, or any other, from this software.
12#
13#
14#####################################################################################
15
16from generate import generate
17
18view_types = ['DictionaryKeyView', 'DictionaryItemView']
19set_types = ['SetCollection', 'FrozenSetCollection']
20
21ops = [
22 ('|', 'Union'),
23 ('&', 'Intersection'),
24 ('^', 'SymmetricDifference'),
25 ('-', 'Difference'),
26]
27comps = [
28 ('==', 'xs.Count == ys.Count && xs.IsSubset(ys)'),
29 ('!=', 'xs.Count != ys.Count || !xs.IsSubset(ys)'),
30 ('>', 'ys.IsStrictSubset(xs)'),
31 ('<', 'xs.IsStrictSubset(ys)'),
32 ('>=', 'ys.IsSubset(xs)'),
33 ('<=', 'xs.IsSubset(ys)'),
34]
35
36def equality(comp):
37 return 'true' if comp != '!=' and '=' in comp else 'false'
38
39def inequality(comp):
40 return 'true' if '=' not in comp or comp == '!=' else 'false'
41
42def gen_ops(ty):
43 def _gen_ops(cw):
44 for op, op_name in ops:
45 for format_args in [
46 (op, ty + ' x', 'IEnumerable y'),
47 (op, 'IEnumerable y', ty + ' x'),
48 ]:
49 cw.enter_block('public static SetCollection operator %s(%s, %s)' % format_args)
50 cw.writeline('return new SetCollection(SetStorage.%s(' % op_name)
51 cw.indent()
52 cw.writeline('SetStorage.GetItemsWorker(x.GetEnumerator()),')
53 cw.writeline('SetStorage.GetItems(y)')
54 cw.dedent()
55 cw.writeline('));')
56 cw.exit_block()
57 cw.writeline()
58
59 return _gen_ops
60
61def gen_comps(ty):
62 view_types_sorted = [ty] + [x for x in view_types if x != ty]
63 def _gen_comps(cw):
64 cw.enter_block('public override bool Equals(object obj)')
65 cw.enter_block('if (obj == null)')
66 cw.writeline('return false;')
67 cw.exit_block()
68 enter_block = cw.enter_block
69 for check in view_types_sorted + set_types:
70 enter_block('if (obj is %s)' % check)
71 enter_block = cw.else_block
72 cw.writeline('return this == (%s)obj;' % check)
73 cw.exit_block()
74 cw.writeline('return false;')
75 cw.exit_block()
76 cw.writeline()
77
78 for right in view_types_sorted + set_types:
79 for comp, expr in comps:
80 cw.enter_block('public static bool operator %s(%s x, %s y)' % (comp, ty, right))
81 if right == ty:
82 cw.enter_block('if (object.ReferenceEquals(x._dict, y._dict))')
83 cw.writeline('return %s;' % equality(comp))
84 cw.exit_block()
85 elif right in view_types:
86 cw.enter_block('if (object.ReferenceEquals(x._dict, y._dict))')
87 cw.writeline('return %s;' % inequality(comp))
88 cw.exit_block()
89 xs = 'SetStorage.GetItemsWorker(x.GetEnumerator())'
90 if right in view_types:
91 ys = 'SetStorage.GetItemsWorker(y.GetEnumerator())'
92 else:
93 ys = 'y._items'
94 cw.writeline('SetStorage xs = %s;' % xs)
95 cw.writeline('SetStorage ys = %s;' % ys)
96 cw.writeline('return %s;' % expr)
97 cw.exit_block()
98 cw.writeline()
99
100 return _gen_comps
101
102def main():
103 generators = [
104 ('Set Operations (Keys)', gen_ops('DictionaryKeyView')),
105 ('Set Comparison Operations (Keys)', gen_comps('DictionaryKeyView')),
106 ('Set Operations (Items)', gen_ops('DictionaryItemView')),
107 ('Set Comparison Operations (Items)', gen_comps('DictionaryItemView')),
108 ]
109
110 return generate(*generators)
111
112if __name__ == '__main__':
113 main()