1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/internal_tests/objectset.py Sun Aug 28 18:30:40 2011 +0200
1.3 @@ -0,0 +1,50 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +from micropython.common import ObjectSet
1.7 +
1.8 +o1 = ObjectSet()
1.9 +o1.add("a")
1.10 +o1.add("b")
1.11 +o1.add("c")
1.12 +print "o1 =", o1
1.13 +
1.14 +o2 = ObjectSet()
1.15 +o2.add("b")
1.16 +o2["c"] = ["x"]
1.17 +o2.add("d")
1.18 +o2["d"] += ["p", "q"]
1.19 +print "o2 =", o2
1.20 +
1.21 +assert o1 != o2
1.22 +print "o1 == o2", o1 == o2
1.23 +print "o1 != o2", o1 != o2
1.24 +
1.25 +o12 = o1.merge(o2)
1.26 +print "o1 =", o1
1.27 +print "o2 =", o2
1.28 +print "o12 =", o12
1.29 +
1.30 +o12_keys = o12.keys()
1.31 +o12_keys.sort()
1.32 +assert o12_keys == ["a", "b", "c", "d"]
1.33 +print "o12_keys =", o12_keys
1.34 +
1.35 +o1_copy = ObjectSet(o1)
1.36 +assert o1 == o1_copy
1.37 +print "o1 =", o1
1.38 +print "o1_copy =", o1_copy
1.39 +print "o1 == o1_copy", o1 == o1_copy
1.40 +
1.41 +o1_copy.update(o2)
1.42 +assert o1 != o1_copy
1.43 +assert o12 == o1_copy
1.44 +print "o1_copy =", o1_copy
1.45 +print "o1 != o1_copy", o1 != o1_copy
1.46 +print "o12 == o1_copy", o12 == o1_copy
1.47 +
1.48 +o3 = ObjectSet()
1.49 +o3.add("c")
1.50 +o3["c"].append("y")
1.51 +print "o3 =", o3
1.52 +
1.53 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/micropython/common.py Sun Aug 28 02:03:04 2011 +0200
2.2 +++ b/micropython/common.py Sun Aug 28 18:30:40 2011 +0200
2.3 @@ -20,6 +20,7 @@
2.4 """
2.5
2.6 import compiler.visitor
2.7 +import operator
2.8
2.9 try:
2.10 set
2.11 @@ -119,31 +120,7 @@
2.12 possible, and incorporating values present in only one of the sets.
2.13 """
2.14
2.15 - merged = ObjectSet()
2.16 - for key in other.keys():
2.17 - if not self.has_key(key):
2.18 - merged[key] = other[key]
2.19 - else:
2.20 - merged[key] = self[key] + other[key]
2.21 -
2.22 - for key in self.keys():
2.23 - if not merged.has_key(key):
2.24 - merged[key] = self[key]
2.25 -
2.26 - return merged
2.27 -
2.28 -def combine_object_set_lists(l1, l2):
2.29 -
2.30 - """
2.31 - Combine lists of object sets 'l1' and 'l2' to make a product of their
2.32 - members.
2.33 - """
2.34 -
2.35 - combined = set([])
2.36 - for i1 in l1:
2.37 - for i2 in l2:
2.38 - combined.add(i1.merge(i2))
2.39 - return combined
2.40 + return combine(self, other, ObjectSet(), operator.add)
2.41
2.42 def deepen_mapping_dict(d):
2.43
2.44 @@ -188,17 +165,22 @@
2.45 -> {'a' : [{'f', 'g'}, {'f', 'h'}, {'e', 'f', 'g'}, {'e', 'f', 'g', 'h'}], ...}
2.46 """
2.47
2.48 - combined = {}
2.49 + return combine(d1, d2, {}, combine_object_set_lists)
2.50 +
2.51 +def combine(d1, d2, combined, combine_op):
2.52 +
2.53 + """
2.54 + Combine dictionaries 'd1' and 'd2' in the 'combined' object provided, using
2.55 + the 'combine_op' to merge values from the dictionaries.
2.56 + """
2.57 +
2.58 d2_keys = d2.keys()
2.59
2.60 for key in d2_keys:
2.61 -
2.62 - d1_values = d1.get(key)
2.63 -
2.64 - if d1_values is None:
2.65 + if not d1.has_key(key):
2.66 combined[key] = d2[key]
2.67 else:
2.68 - combined[key] = combine_object_set_lists(d1_values, d2[key])
2.69 + combined[key] = combine_op(d1[key], d2[key])
2.70
2.71 for key in d1.keys():
2.72 if key not in d2_keys:
2.73 @@ -206,6 +188,19 @@
2.74
2.75 return combined
2.76
2.77 +def combine_object_set_lists(l1, l2):
2.78 +
2.79 + """
2.80 + Combine lists of object sets 'l1' and 'l2' to make a product of their
2.81 + members.
2.82 + """
2.83 +
2.84 + combined = set([])
2.85 + for i1 in l1:
2.86 + for i2 in l2:
2.87 + combined.add(i1.merge(i2))
2.88 + return combined
2.89 +
2.90 # Visitors and activities related to node annotations.
2.91
2.92 class ASTVisitor(compiler.visitor.ASTVisitor):
3.1 --- a/micropython/data.py Sun Aug 28 02:03:04 2011 +0200
3.2 +++ b/micropython/data.py Sun Aug 28 18:30:40 2011 +0200
3.3 @@ -448,7 +448,10 @@
3.4 all_contributors.update(contributors)
3.5
3.6 # Then get the resulting usage.
3.7 - # Combine the current usage with the contributed usage.
3.8 + # First, make the current usage compatible with the contributed
3.9 + # usage: this makes the attribute usage for each name merely one
3.10 + # member in a list of many possibilities.
3.11 + # Then, combine the current usage with the contributed usage.
3.12 # Thus, usage of {(f, g)} combined with {(a, b), (c, d)} would give
3.13 # {(f, g, a, b), (f, g, c, d)}.
3.14