from typing import Any, TypeVar, Iterator, Collection, List import itertools from .sets_representation import MyFiniteSet from .sets_properties import SolFiniteSetProperties import act4e_interfaces as I X = TypeVar("X") C = TypeVar("C") # Type of elements of set E = MyFiniteSet[C] # Type of sets class MyFiniteSetOfFiniteSubsets(I.FiniteSetOfFiniteSubsets[C, E]): # FIXME: _set could be removed _set: I.FiniteSet[C] _subsets: List[I.FiniteSet[C]] def __init__(self, theset: I.FiniteSet[C], subsets: Collection[I.FiniteSet[C]]): self._set = theset self._subsets = subsets self.fsp = SolFiniteSetProperties() assert(self.fsp.is_subset(s, theset) for s in subsets) def size(self) -> int: return len(self._subsets) def contains(self, x: E): # print(f"Checking whether {x.elements()} is contained in " + # str(list(map(lambda l: l.elements(), self._subsets)))) # Heuristic filter to make it a bit faster same_size = lambda s: s.size() == x.size() for s in filter(same_size, self._subsets): if self.fsp.equal(x, s): # print("it is in there!") return True return False def elements(self): return self._subsets def save(self, h: I.IOHelper, x: E) -> List[E]: # FIXME: I don't think this is correct, same problem as in save return cast(I.ConcreteRepr, x) def load(self, h: I.IOHelper, o: I.ConcreteRepr): # FIXME: what the f am I supposed to do here return cast(E, o) def contents(self, e: E) -> Iterator[C]: return e.elements() def construct(self, elements: Collection[C]) -> E: return MyFiniteSet(elements) class SolFiniteMakePowerSet(I.FiniteMakePowerSet): def powerset(self, s: I.FiniteSet[X]) -> I.FiniteSetOfFiniteSubsets[X, Any]: subsets = [MyFiniteSet([]), s] for i in range(1, s.size()): # generate all possible subsets with i elements for comb in itertools.combinations(s.elements(), i): subsets.append(MyFiniteSet(list(comb))) return MyFiniteSetOfFiniteSubsets(s, subsets)