PageRenderTime 59ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/Languages/IronPython/Scripts/generate_dict_views.py

https://github.com/thomo13/ironruby
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. from generate import generate
  16. view_types = ['DictionaryKeyView', 'DictionaryItemView']
  17. set_types = ['SetCollection', 'FrozenSetCollection']
  18. ops = [
  19. ('|', 'Union'),
  20. ('&', 'Intersection'),
  21. ('^', 'SymmetricDifference'),
  22. ('-', 'Difference'),
  23. ]
  24. comps = [
  25. ('==', 'xs.Count == ys.Count && xs.IsSubset(ys)'),
  26. ('!=', 'xs.Count != ys.Count || !xs.IsSubset(ys)'),
  27. ('>', 'ys.IsStrictSubset(xs)'),
  28. ('<', 'xs.IsStrictSubset(ys)'),
  29. ('>=', 'ys.IsSubset(xs)'),
  30. ('<=', 'xs.IsSubset(ys)'),
  31. ]
  32. def equality(comp):
  33. return 'true' if comp != '!=' and '=' in comp else 'false'
  34. def inequality(comp):
  35. return 'true' if '=' not in comp or comp == '!=' else 'false'
  36. def gen_ops(ty):
  37. def _gen_ops(cw):
  38. for op, op_name in ops:
  39. for format_args in [
  40. (op, ty + ' x', 'IEnumerable y'),
  41. (op, 'IEnumerable y', ty + ' x'),
  42. ]:
  43. cw.enter_block('public static SetCollection operator %s(%s, %s)' % format_args)
  44. cw.writeline('return new SetCollection(SetStorage.%s(' % op_name)
  45. cw.indent()
  46. cw.writeline('SetStorage.GetItemsWorker(x.GetEnumerator()),')
  47. cw.writeline('SetStorage.GetItems(y)')
  48. cw.dedent()
  49. cw.writeline('));')
  50. cw.exit_block()
  51. cw.writeline()
  52. return _gen_ops
  53. def gen_comps(ty):
  54. view_types_sorted = [ty] + [x for x in view_types if x != ty]
  55. def _gen_comps(cw):
  56. cw.enter_block('public override bool Equals(object obj)')
  57. cw.enter_block('if (obj == null)')
  58. cw.writeline('return false;')
  59. cw.exit_block()
  60. enter_block = cw.enter_block
  61. for check in view_types_sorted + set_types:
  62. enter_block('if (obj is %s)' % check)
  63. enter_block = cw.else_block
  64. cw.writeline('return this == (%s)obj;' % check)
  65. cw.exit_block()
  66. cw.writeline('return false;')
  67. cw.exit_block()
  68. cw.writeline()
  69. for right in view_types_sorted + set_types:
  70. for comp, expr in comps:
  71. cw.enter_block('public static bool operator %s(%s x, %s y)' % (comp, ty, right))
  72. if right == ty:
  73. cw.enter_block('if (object.ReferenceEquals(x._dict, y._dict))')
  74. cw.writeline('return %s;' % equality(comp))
  75. cw.exit_block()
  76. elif right in view_types:
  77. cw.enter_block('if (object.ReferenceEquals(x._dict, y._dict))')
  78. cw.writeline('return %s;' % inequality(comp))
  79. cw.exit_block()
  80. xs = 'SetStorage.GetItemsWorker(x.GetEnumerator())'
  81. if right in view_types:
  82. ys = 'SetStorage.GetItemsWorker(y.GetEnumerator())'
  83. else:
  84. ys = 'y._items'
  85. cw.writeline('SetStorage xs = %s;' % xs)
  86. cw.writeline('SetStorage ys = %s;' % ys)
  87. cw.writeline('return %s;' % expr)
  88. cw.exit_block()
  89. cw.writeline()
  90. return _gen_comps
  91. def main():
  92. generators = [
  93. ('Set Operations (Keys)', gen_ops('DictionaryKeyView')),
  94. ('Set Comparison Operations (Keys)', gen_comps('DictionaryKeyView')),
  95. ('Set Operations (Items)', gen_ops('DictionaryItemView')),
  96. ('Set Comparison Operations (Items)', gen_comps('DictionaryItemView')),
  97. ]
  98. return generate(*generators)
  99. if __name__ == '__main__':
  100. main()