1.1 --- a/micropython/common.py Fri Sep 17 00:48:32 2010 +0200
1.2 +++ b/micropython/common.py Tue Sep 21 00:11:34 2010 +0200
1.3 @@ -31,6 +31,12 @@
1.4 "Merge the given 'dicts' mapping keys to sets of objects."
1.5
1.6 new_dict = {}
1.7 + update_set_dict(new_dict, dicts)
1.8 + return new_dict
1.9 +
1.10 +def update_set_dict(new_dict, dicts):
1.11 +
1.12 + "Update 'new_dict' with the contents of the set dictionary 'dicts'."
1.13
1.14 for old_dict in dicts:
1.15 for key, values in old_dict.items():
1.16 @@ -39,7 +45,46 @@
1.17 else:
1.18 new_dict[key].update(values)
1.19
1.20 - return new_dict
1.21 +def deepen_set_dict(d):
1.22 +
1.23 + """
1.24 + Return a new dictionary mapping keys to sets of tuples of values, using data
1.25 + from 'd' which is a dictionary mapping keys to sets of values.
1.26 + """
1.27 +
1.28 + d2 = {}
1.29 + for key, values in d.items():
1.30 + d2[key] = set([tuple(values)])
1.31 + return d2
1.32 +
1.33 +def combine_set_dicts(d1, d2):
1.34 +
1.35 + """
1.36 + Combine dictionaries 'd1' and 'd2' in a resulting dictionary, with the set
1.37 + values of the contributing dictionaries being merged such that a "product"
1.38 + of the values for a given key are stored in the combined dictionary.
1.39 + """
1.40 +
1.41 + combined = {}
1.42 + d2_keys = d2.keys()
1.43 +
1.44 + for key in d2_keys:
1.45 +
1.46 + d1_values = d1.get(key)
1.47 +
1.48 + if d1_values is None:
1.49 + combined[key] = d2[key]
1.50 + else:
1.51 + combined[key] = set()
1.52 + for d2_value in d2[key]:
1.53 + for d1_value in d1_values:
1.54 + combined[key].add(tuple(set(d1_value).union(d2_value)))
1.55 +
1.56 + for key in d1.keys():
1.57 + if key not in d2_keys:
1.58 + combined[key] = d1[key]
1.59 +
1.60 + return combined
1.61
1.62 # Visitors and activities related to node annotations.
1.63