Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
provide iterators which return lists of lists
Browse files Browse the repository at this point in the history
  • Loading branch information
mantepse committed Jul 11, 2018
1 parent ba08ff3 commit 95ce20f
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/sage/combinat/set_partition.py
Expand Up @@ -1970,6 +1970,24 @@ def __iter__(self):
sage: SetPartitions(3).list()
[{{1, 2, 3}}, {{1, 2}, {3}}, {{1, 3}, {2}}, {{1}, {2, 3}}, {{1}, {2}, {3}}]
"""
for sp in self._fast_iterator():
yield self.element_class(self, sp, check=False)

def _fast_iterator(self):
"""
A fast iterator for the set partitions of the base set, which
returns lists of lists instead of set partitions types.
EXAMPLES::
sage: list(SetPartitions([1,-1,x])._fast_iterator())
[[[1, -1, x]],
[[1, -1], [x]],
[[1, x], [-1]],
[[1], [-1, x]],
[[1], [-1], [x]]]
"""
base_set = list(self.base_set())
def from_word(w):
sp = []
Expand All @@ -1978,7 +1996,7 @@ def from_word(w):
sp.append([i])
else:
sp[b].append(i)
return self.element_class(self, sp, check=False)
return sp

# Knuth, TAOCP 4A 7.2.1.5, Algorithm H
N = len(base_set)
Expand Down Expand Up @@ -2247,6 +2265,21 @@ def __iter__(self):
{{1, 2}, {3, 4}},
{{1, 2, 3}, {4}}]
"""
for sp in self._fast_iterator():
yield self.element_class(self, sp, check=False)

def _fast_iterator(self):
"""
A fast iterator for the set partitions of the base set into the
specified number of blocks, which returns lists of lists
instead of set partitions types.
EXAMPLES::
sage: list(SetPartitions([1,-1,x], 2)._fast_iterator())
[[[1, x], [-1]], [[1], [-1, x]], [[1, -1], [x]]]
"""
base_set = list(self.base_set())
def from_word(w):
sp = []
Expand All @@ -2255,7 +2288,7 @@ def from_word(w):
sp.append([i])
else:
sp[b].append(i)
return self.element_class(self, sp, check=False)
return sp

# Ruskey, Combinatorial Generation, Algorithm 4.23
n = len(base_set)
Expand Down

0 comments on commit 95ce20f

Please sign in to comment.