@@ -2769,6 +2769,7 @@ def _base_iterator(constraints):
27692769 # else
27702770 return None
27712771
2772+
27722773def _iterator_weight (weight ):
27732774 """
27742775 An iterator for the ordered multiset partitions into sets with weight given by
@@ -2807,33 +2808,28 @@ def _iterator_weight(weight):
28072808 (frozenset({1}), frozenset({3}), frozenset({1})),
28082809 (frozenset({1, 3}), frozenset({1})),
28092810 (frozenset({3}), frozenset({1}), frozenset({1}))]
2811+ sage: list(_iterator_weight([]))
2812+ [()]
28102813 """
2814+ # "weight" should be a dict mapping keys to weights
28112815 if isinstance (weight , (list , tuple )):
2812- weight = {k + 1 : val for k ,val in enumerate (weight ) if val > 0 }
2813- if isinstance (weight , dict ):
2814- multiset = tuple ([k for k in sorted (weight ) for _ in range (weight [k ])])
2816+ weight = {k + 1 : val for k , val in enumerate (weight ) if val }
2817+
2818+ # We first map the arbitrary keys to integers to combat unreliable
2819+ # sorting behavior.
2820+ keys = tuple (set (weight ))
2821+ multiset = []
2822+ for i , key in enumerate (keys ):
2823+ multiset += [i ] * weight [key ]
2824+
2825+ # We build ordered multiset partitions into sets of `X` by
2826+ # permutation + deconcatenation
2827+ for alpha in Permutations_mset (multiset ):
2828+ co = _break_at_descents (alpha , weak = True )
2829+ for A in OrderedMultisetPartitionIntoSets (co ).finer (strong = True ):
2830+ B = tuple ([frozenset ([keys [i ] for i in block ]) for block in A ])
2831+ yield B
28152832
2816- if not multiset :
2817- yield ()
2818- else :
2819- # We build ordered multiset partitions into sets of `X` by permutation + deconcatenation
2820- # We first standardize the multiset to combat unreliable sorting behavior.
2821- em = enumerate (set (multiset ))
2822- key_to_indx = {}
2823- indx_to_key = {}
2824- for (i ,key ) in em :
2825- key_to_indx [key ] = i
2826- indx_to_key [i ] = key
2827- std_multiset = []
2828- for a in multiset :
2829- std_multiset .append (key_to_indx [a ])
2830- std_multiset = sorted (std_multiset )
2831-
2832- for alpha in Permutations_mset (std_multiset ):
2833- co = _break_at_descents (alpha , weak = True )
2834- for A in OrderedMultisetPartitionIntoSets (co ).finer (strong = True ):
2835- B = tuple ([frozenset ([indx_to_key [i ] for i in block ]) for block in A ])
2836- yield B
28372833
28382834def _iterator_size (size , length = None , alphabet = None ):
28392835 r"""
0 commit comments