Advertisement
mwchase

Too much itertools

Oct 1st, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1.     def __new__(cls, iterable=()):
  2.         if isinstance(iterable, cls):
  3.             return iterable
  4.         iterable = frozenset(
  5.             item for item in iterable if Conjunction() != item)
  6.         for clause in iterable:
  7.             assert isinstance(clause, Disjunction)
  8.         bins = sorted((i, tuple(g)) for (i, g) in itertools.groupby(
  9.             iterable, lambda clause: len(clause.terms)))
  10.         output = frozenset()
  11.         while bins:
  12.             bin = bins.pop()
  13.             intersections = ()
  14.             non_compressed = set(bin[1])
  15.             for fst, snd in itertools.combinations(bin[1], 2):
  16.                 diff = list(fst.terms.symmetric_difference(snd.terms))
  17.                 if (len(diff) == 2 and
  18.                         Conjunction.cast(diff[0]) == not_(diff[1])):
  19.                     intersections += Disjunction(
  20.                         fst.terms.intersection(snd.terms)),
  21.                     non_compressed.discard(fst)
  22.                     non_compressed.discard(snd)
  23.             output = output.union(non_compressed)
  24.             if intersections:
  25.                 if bins and bins[-1][0] == bin[0] - 1:
  26.                     bins[-1] = bins[-1][0], bins[-1][1] + intersections
  27.                 else:
  28.                     bins.append((bin[0] - 1, intersections))
  29.         return super().__new__(cls, output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement