setutils.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. # -*- coding: utf-8 -*-
  2. # Copyright (c) 2013, Mahmoud Hashemi
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. #
  11. # * Redistributions in binary form must reproduce the above
  12. # copyright notice, this list of conditions and the following
  13. # disclaimer in the documentation and/or other materials provided
  14. # with the distribution.
  15. #
  16. # * The names of the contributors may not be used to endorse or
  17. # promote products derived from this software without specific
  18. # prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. """\
  32. The :class:`set` type brings the practical expressiveness of
  33. set theory to Python. It has a very rich API overall, but lacks a
  34. couple of fundamental features. For one, sets are not ordered. On top
  35. of this, sets are not indexable, i.e, ``my_set[8]`` will raise an
  36. :exc:`TypeError`. The :class:`IndexedSet` type remedies both of these
  37. issues without compromising on the excellent complexity
  38. characteristics of Python's built-in set implementation.
  39. """
  40. from __future__ import print_function
  41. from bisect import bisect_left
  42. from itertools import chain, islice
  43. import operator
  44. try:
  45. from collections.abc import MutableSet
  46. except ImportError:
  47. from collections import MutableSet
  48. try:
  49. from .typeutils import make_sentinel
  50. _MISSING = make_sentinel(var_name='_MISSING')
  51. except ImportError:
  52. _MISSING = object()
  53. __all__ = ['IndexedSet', 'complement']
  54. _COMPACTION_FACTOR = 8
  55. # TODO: inherit from set()
  56. # TODO: .discard_many(), .remove_many()
  57. # TODO: raise exception on non-set params?
  58. # TODO: technically reverse operators should probably reverse the
  59. # order of the 'other' inputs and put self last (to try and maintain
  60. # insertion order)
  61. class IndexedSet(MutableSet):
  62. """``IndexedSet`` is a :class:`collections.MutableSet` that maintains
  63. insertion order and uniqueness of inserted elements. It's a hybrid
  64. type, mostly like an OrderedSet, but also :class:`list`-like, in
  65. that it supports indexing and slicing.
  66. Args:
  67. other (iterable): An optional iterable used to initialize the set.
  68. >>> x = IndexedSet(list(range(4)) + list(range(8)))
  69. >>> x
  70. IndexedSet([0, 1, 2, 3, 4, 5, 6, 7])
  71. >>> x - set(range(2))
  72. IndexedSet([2, 3, 4, 5, 6, 7])
  73. >>> x[-1]
  74. 7
  75. >>> fcr = IndexedSet('freecreditreport.com')
  76. >>> ''.join(fcr[:fcr.index('.')])
  77. 'frecditpo'
  78. Standard set operators and interoperation with :class:`set` are
  79. all supported:
  80. >>> fcr & set('cash4gold.com')
  81. IndexedSet(['c', 'd', 'o', '.', 'm'])
  82. As you can see, the ``IndexedSet`` is almost like a ``UniqueList``,
  83. retaining only one copy of a given value, in the order it was
  84. first added. For the curious, the reason why IndexedSet does not
  85. support setting items based on index (i.e, ``__setitem__()``),
  86. consider the following dilemma::
  87. my_indexed_set = [A, B, C, D]
  88. my_indexed_set[2] = A
  89. At this point, a set requires only one *A*, but a :class:`list` would
  90. overwrite *C*. Overwriting *C* would change the length of the list,
  91. meaning that ``my_indexed_set[2]`` would not be *A*, as expected with a
  92. list, but rather *D*. So, no ``__setitem__()``.
  93. Otherwise, the API strives to be as complete a union of the
  94. :class:`list` and :class:`set` APIs as possible.
  95. """
  96. def __init__(self, other=None):
  97. self.item_index_map = dict()
  98. self.item_list = []
  99. self.dead_indices = []
  100. self._compactions = 0
  101. self._c_max_size = 0
  102. if other:
  103. self.update(other)
  104. # internal functions
  105. @property
  106. def _dead_index_count(self):
  107. return len(self.item_list) - len(self.item_index_map)
  108. def _compact(self):
  109. if not self.dead_indices:
  110. return
  111. self._compactions += 1
  112. dead_index_count = self._dead_index_count
  113. items, index_map = self.item_list, self.item_index_map
  114. self._c_max_size = max(self._c_max_size, len(items))
  115. for i, item in enumerate(self):
  116. items[i] = item
  117. index_map[item] = i
  118. del items[-dead_index_count:]
  119. del self.dead_indices[:]
  120. def _cull(self):
  121. ded = self.dead_indices
  122. if not ded:
  123. return
  124. items, ii_map = self.item_list, self.item_index_map
  125. if not ii_map:
  126. del items[:]
  127. del ded[:]
  128. elif len(ded) > 384:
  129. self._compact()
  130. elif self._dead_index_count > (len(items) / _COMPACTION_FACTOR):
  131. self._compact()
  132. elif items[-1] is _MISSING: # get rid of dead right hand side
  133. num_dead = 1
  134. while items[-(num_dead + 1)] is _MISSING:
  135. num_dead += 1
  136. if ded and ded[-1][1] == len(items):
  137. del ded[-1]
  138. del items[-num_dead:]
  139. def _get_real_index(self, index):
  140. if index < 0:
  141. index += len(self)
  142. if not self.dead_indices:
  143. return index
  144. real_index = index
  145. for d_start, d_stop in self.dead_indices:
  146. if real_index < d_start:
  147. break
  148. real_index += d_stop - d_start
  149. return real_index
  150. def _get_apparent_index(self, index):
  151. if index < 0:
  152. index += len(self)
  153. if not self.dead_indices:
  154. return index
  155. apparent_index = index
  156. for d_start, d_stop in self.dead_indices:
  157. if index < d_start:
  158. break
  159. apparent_index -= d_stop - d_start
  160. return apparent_index
  161. def _add_dead(self, start, stop=None):
  162. # TODO: does not handle when the new interval subsumes
  163. # multiple existing intervals
  164. dints = self.dead_indices
  165. if stop is None:
  166. stop = start + 1
  167. cand_int = [start, stop]
  168. if not dints:
  169. dints.append(cand_int)
  170. return
  171. int_idx = bisect_left(dints, cand_int)
  172. dint = dints[int_idx - 1]
  173. d_start, d_stop = dint
  174. if start <= d_start <= stop:
  175. dint[0] = start
  176. elif start <= d_stop <= stop:
  177. dint[1] = stop
  178. else:
  179. dints.insert(int_idx, cand_int)
  180. return
  181. # common operations (shared by set and list)
  182. def __len__(self):
  183. return len(self.item_index_map)
  184. def __contains__(self, item):
  185. return item in self.item_index_map
  186. def __iter__(self):
  187. return (item for item in self.item_list if item is not _MISSING)
  188. def __reversed__(self):
  189. item_list = self.item_list
  190. return (item for item in reversed(item_list) if item is not _MISSING)
  191. def __repr__(self):
  192. return '%s(%r)' % (self.__class__.__name__, list(self))
  193. def __eq__(self, other):
  194. if isinstance(other, IndexedSet):
  195. return len(self) == len(other) and list(self) == list(other)
  196. return set(self) == set(other)
  197. @classmethod
  198. def from_iterable(cls, it):
  199. "from_iterable(it) -> create a set from an iterable"
  200. return cls(it)
  201. # set operations
  202. def add(self, item):
  203. "add(item) -> add item to the set"
  204. if item not in self.item_index_map:
  205. self.item_index_map[item] = len(self.item_list)
  206. self.item_list.append(item)
  207. def remove(self, item):
  208. "remove(item) -> remove item from the set, raises if not present"
  209. try:
  210. didx = self.item_index_map.pop(item)
  211. except KeyError:
  212. raise KeyError(item)
  213. self.item_list[didx] = _MISSING
  214. self._add_dead(didx)
  215. self._cull()
  216. def discard(self, item):
  217. "discard(item) -> discard item from the set (does not raise)"
  218. try:
  219. self.remove(item)
  220. except KeyError:
  221. pass
  222. def clear(self):
  223. "clear() -> empty the set"
  224. del self.item_list[:]
  225. del self.dead_indices[:]
  226. self.item_index_map.clear()
  227. def isdisjoint(self, other):
  228. "isdisjoint(other) -> return True if no overlap with other"
  229. iim = self.item_index_map
  230. for k in other:
  231. if k in iim:
  232. return False
  233. return True
  234. def issubset(self, other):
  235. "issubset(other) -> return True if other contains this set"
  236. if len(other) < len(self):
  237. return False
  238. for k in self.item_index_map:
  239. if k not in other:
  240. return False
  241. return True
  242. def issuperset(self, other):
  243. "issuperset(other) -> return True if set contains other"
  244. if len(other) > len(self):
  245. return False
  246. iim = self.item_index_map
  247. for k in other:
  248. if k not in iim:
  249. return False
  250. return True
  251. def union(self, *others):
  252. "union(*others) -> return a new set containing this set and others"
  253. return self.from_iterable(chain(self, *others))
  254. def iter_intersection(self, *others):
  255. "iter_intersection(*others) -> iterate over elements also in others"
  256. for k in self:
  257. for other in others:
  258. if k not in other:
  259. break
  260. else:
  261. yield k
  262. return
  263. def intersection(self, *others):
  264. "intersection(*others) -> get a set with overlap of this and others"
  265. if len(others) == 1:
  266. other = others[0]
  267. return self.from_iterable(k for k in self if k in other)
  268. return self.from_iterable(self.iter_intersection(*others))
  269. def iter_difference(self, *others):
  270. "iter_difference(*others) -> iterate over elements not in others"
  271. for k in self:
  272. for other in others:
  273. if k in other:
  274. break
  275. else:
  276. yield k
  277. return
  278. def difference(self, *others):
  279. "difference(*others) -> get a new set with elements not in others"
  280. if len(others) == 1:
  281. other = others[0]
  282. return self.from_iterable(k for k in self if k not in other)
  283. return self.from_iterable(self.iter_difference(*others))
  284. def symmetric_difference(self, *others):
  285. "symmetric_difference(*others) -> XOR set of this and others"
  286. ret = self.union(*others)
  287. return ret.difference(self.intersection(*others))
  288. __or__ = __ror__ = union
  289. __and__ = __rand__ = intersection
  290. __sub__ = difference
  291. __xor__ = __rxor__ = symmetric_difference
  292. def __rsub__(self, other):
  293. vals = [x for x in other if x not in self]
  294. return type(other)(vals)
  295. # in-place set operations
  296. def update(self, *others):
  297. "update(*others) -> add values from one or more iterables"
  298. if not others:
  299. return # raise?
  300. elif len(others) == 1:
  301. other = others[0]
  302. else:
  303. other = chain(others)
  304. for o in other:
  305. self.add(o)
  306. def intersection_update(self, *others):
  307. "intersection_update(*others) -> discard self.difference(*others)"
  308. for val in self.difference(*others):
  309. self.discard(val)
  310. def difference_update(self, *others):
  311. "difference_update(*others) -> discard self.intersection(*others)"
  312. if self in others:
  313. self.clear()
  314. for val in self.intersection(*others):
  315. self.discard(val)
  316. def symmetric_difference_update(self, other): # note singular 'other'
  317. "symmetric_difference_update(other) -> in-place XOR with other"
  318. if self is other:
  319. self.clear()
  320. for val in other:
  321. if val in self:
  322. self.discard(val)
  323. else:
  324. self.add(val)
  325. def __ior__(self, *others):
  326. self.update(*others)
  327. return self
  328. def __iand__(self, *others):
  329. self.intersection_update(*others)
  330. return self
  331. def __isub__(self, *others):
  332. self.difference_update(*others)
  333. return self
  334. def __ixor__(self, *others):
  335. self.symmetric_difference_update(*others)
  336. return self
  337. def iter_slice(self, start, stop, step=None):
  338. "iterate over a slice of the set"
  339. iterable = self
  340. if start is not None:
  341. start = self._get_real_index(start)
  342. if stop is not None:
  343. stop = self._get_real_index(stop)
  344. if step is not None and step < 0:
  345. step = -step
  346. iterable = reversed(self)
  347. return islice(iterable, start, stop, step)
  348. # list operations
  349. def __getitem__(self, index):
  350. try:
  351. start, stop, step = index.start, index.stop, index.step
  352. except AttributeError:
  353. index = operator.index(index)
  354. else:
  355. iter_slice = self.iter_slice(start, stop, step)
  356. return self.from_iterable(iter_slice)
  357. if index < 0:
  358. index += len(self)
  359. real_index = self._get_real_index(index)
  360. try:
  361. ret = self.item_list[real_index]
  362. except IndexError:
  363. raise IndexError('IndexedSet index out of range')
  364. return ret
  365. def pop(self, index=None):
  366. "pop(index) -> remove the item at a given index (-1 by default)"
  367. item_index_map = self.item_index_map
  368. len_self = len(item_index_map)
  369. if index is None or index == -1 or index == len_self - 1:
  370. ret = self.item_list.pop()
  371. del item_index_map[ret]
  372. else:
  373. real_index = self._get_real_index(index)
  374. ret = self.item_list[real_index]
  375. self.item_list[real_index] = _MISSING
  376. del item_index_map[ret]
  377. self._add_dead(real_index)
  378. self._cull()
  379. return ret
  380. def count(self, val):
  381. "count(val) -> count number of instances of value (0 or 1)"
  382. if val in self.item_index_map:
  383. return 1
  384. return 0
  385. def reverse(self):
  386. "reverse() -> reverse the contents of the set in-place"
  387. reversed_list = list(reversed(self))
  388. self.item_list[:] = reversed_list
  389. for i, item in enumerate(self.item_list):
  390. self.item_index_map[item] = i
  391. del self.dead_indices[:]
  392. def sort(self, **kwargs):
  393. "sort() -> sort the contents of the set in-place"
  394. sorted_list = sorted(self, **kwargs)
  395. if sorted_list == self.item_list:
  396. return
  397. self.item_list[:] = sorted_list
  398. for i, item in enumerate(self.item_list):
  399. self.item_index_map[item] = i
  400. del self.dead_indices[:]
  401. def index(self, val):
  402. "index(val) -> get the index of a value, raises if not present"
  403. try:
  404. return self._get_apparent_index(self.item_index_map[val])
  405. except KeyError:
  406. cn = self.__class__.__name__
  407. raise ValueError('%r is not in %s' % (val, cn))
  408. def complement(wrapped):
  409. """Given a :class:`set`, convert it to a **complement set**.
  410. Whereas a :class:`set` keeps track of what it contains, a
  411. `complement set
  412. <https://en.wikipedia.org/wiki/Complement_(set_theory)>`_ keeps
  413. track of what it does *not* contain. For example, look what
  414. happens when we intersect a normal set with a complement set::
  415. >>> list(set(range(5)) & complement(set([2, 3])))
  416. [0, 1, 4]
  417. We get the everything in the left that wasn't in the right,
  418. because intersecting with a complement is the same as subtracting
  419. a normal set.
  420. Args:
  421. wrapped (set): A set or any other iterable which should be
  422. turned into a complement set.
  423. All set methods and operators are supported by complement sets,
  424. between other :func:`complement`-wrapped sets and/or regular
  425. :class:`set` objects.
  426. Because a complement set only tracks what elements are *not* in
  427. the set, functionality based on set contents is unavailable:
  428. :func:`len`, :func:`iter` (and for loops), and ``.pop()``. But a
  429. complement set can always be turned back into a regular set by
  430. complementing it again:
  431. >>> s = set(range(5))
  432. >>> complement(complement(s)) == s
  433. True
  434. .. note::
  435. An empty complement set corresponds to the concept of a
  436. `universal set <https://en.wikipedia.org/wiki/Universal_set>`_
  437. from mathematics.
  438. Complement sets by example
  439. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  440. Many uses of sets can be expressed more simply by using a
  441. complement. Rather than trying to work out in your head the proper
  442. way to invert an expression, you can just throw a complement on
  443. the set. Consider this example of a name filter::
  444. >>> class NamesFilter(object):
  445. ... def __init__(self, allowed):
  446. ... self._allowed = allowed
  447. ...
  448. ... def filter(self, names):
  449. ... return [name for name in names if name in self._allowed]
  450. >>> NamesFilter(set(['alice', 'bob'])).filter(['alice', 'bob', 'carol'])
  451. ['alice', 'bob']
  452. What if we want to just express "let all the names through"?
  453. We could try to enumerate all of the expected names::
  454. ``NamesFilter({'alice', 'bob', 'carol'})``
  455. But this is very brittle -- what if at some point over this
  456. object is changed to filter ``['alice', 'bob', 'carol', 'dan']``?
  457. Even worse, what about the poor programmer who next works
  458. on this piece of code? They cannot tell whether the purpose
  459. of the large allowed set was "allow everything", or if 'dan'
  460. was excluded for some subtle reason.
  461. A complement set lets the programmer intention be expressed
  462. succinctly and directly::
  463. NamesFilter(complement(set()))
  464. Not only is this code short and robust, it is easy to understand
  465. the intention.
  466. """
  467. if type(wrapped) is _ComplementSet:
  468. return wrapped.complemented()
  469. if type(wrapped) is frozenset:
  470. return _ComplementSet(excluded=wrapped)
  471. return _ComplementSet(excluded=set(wrapped))
  472. def _norm_args_typeerror(other):
  473. '''normalize args and raise type-error if there is a problem'''
  474. if type(other) in (set, frozenset):
  475. inc, exc = other, None
  476. elif type(other) is _ComplementSet:
  477. inc, exc = other._included, other._excluded
  478. else:
  479. raise TypeError('argument must be another set or complement(set)')
  480. return inc, exc
  481. def _norm_args_notimplemented(other):
  482. '''normalize args and return NotImplemented (for overloaded operators)'''
  483. if type(other) in (set, frozenset):
  484. inc, exc = other, None
  485. elif type(other) is _ComplementSet:
  486. inc, exc = other._included, other._excluded
  487. else:
  488. return NotImplemented, None
  489. return inc, exc
  490. class _ComplementSet(object):
  491. """
  492. helper class for complement() that implements the set methods
  493. """
  494. __slots__ = ('_included', '_excluded')
  495. def __init__(self, included=None, excluded=None):
  496. if included is None:
  497. assert type(excluded) in (set, frozenset)
  498. elif excluded is None:
  499. assert type(included) in (set, frozenset)
  500. else:
  501. raise ValueError('one of included or excluded must be a set')
  502. self._included, self._excluded = included, excluded
  503. def __repr__(self):
  504. if self._included is None:
  505. return 'complement({0})'.format(repr(self._excluded))
  506. return 'complement(complement({0}))'.format(repr(self._included))
  507. def complemented(self):
  508. '''return a complement of the current set'''
  509. if type(self._included) is frozenset or type(self._excluded) is frozenset:
  510. return _ComplementSet(included=self._excluded, excluded=self._included)
  511. return _ComplementSet(
  512. included=None if self._excluded is None else set(self._excluded),
  513. excluded=None if self._included is None else set(self._included))
  514. __invert__ = complemented
  515. def complement(self):
  516. '''convert the current set to its complement in-place'''
  517. self._included, self._excluded = self._excluded, self._included
  518. def __contains__(self, item):
  519. if self._included is None:
  520. return not item in self._excluded
  521. return item in self._included
  522. def add(self, item):
  523. if self._included is None:
  524. if item in self._excluded:
  525. self._excluded.remove(item)
  526. else:
  527. self._included.add(item)
  528. def remove(self, item):
  529. if self._included is None:
  530. self._excluded.add(item)
  531. else:
  532. self._included.remove(item)
  533. def pop(self):
  534. if self._included is None:
  535. raise NotImplementedError # self.missing.add(random.choice(gc.objects()))
  536. return self._included.pop()
  537. def intersection(self, other):
  538. try:
  539. return self & other
  540. except NotImplementedError:
  541. raise TypeError('argument must be another set or complement(set)')
  542. def __and__(self, other):
  543. inc, exc = _norm_args_notimplemented(other)
  544. if inc is NotImplemented:
  545. return NotImplemented
  546. if self._included is None:
  547. if exc is None: # - +
  548. return _ComplementSet(included=inc - self._excluded)
  549. else: # - -
  550. return _ComplementSet(excluded=self._excluded.union(other._excluded))
  551. else:
  552. if inc is None: # + -
  553. return _ComplementSet(included=exc - self._included)
  554. else: # + +
  555. return _ComplementSet(included=self._included.intersection(inc))
  556. __rand__ = __and__
  557. def __iand__(self, other):
  558. inc, exc = _norm_args_notimplemented(other)
  559. if inc is NotImplemented:
  560. return NotImplemented
  561. if self._included is None:
  562. if exc is None: # - +
  563. self._excluded = inc - self._excluded # TODO: do this in place?
  564. else: # - -
  565. self._excluded |= exc
  566. else:
  567. if inc is None: # + -
  568. self._included -= exc
  569. self._included, self._excluded = None, self._included
  570. else: # + +
  571. self._included &= inc
  572. return self
  573. def union(self, other):
  574. try:
  575. return self | other
  576. except NotImplementedError:
  577. raise TypeError('argument must be another set or complement(set)')
  578. def __or__(self, other):
  579. inc, exc = _norm_args_notimplemented(other)
  580. if inc is NotImplemented:
  581. return NotImplemented
  582. if self._included is None:
  583. if exc is None: # - +
  584. return _ComplementSet(excluded=self._excluded - inc)
  585. else: # - -
  586. return _ComplementSet(excluded=self._excluded.intersection(exc))
  587. else:
  588. if inc is None: # + -
  589. return _ComplementSet(excluded=exc - self._included)
  590. else: # + +
  591. return _ComplementSet(included=self._included.union(inc))
  592. __ror__ = __or__
  593. def __ior__(self, other):
  594. inc, exc = _norm_args_notimplemented(other)
  595. if inc is NotImplemented:
  596. return NotImplemented
  597. if self._included is None:
  598. if exc is None: # - +
  599. self._excluded -= inc
  600. else: # - -
  601. self._excluded &= exc
  602. else:
  603. if inc is None: # + -
  604. self._included, self._excluded = None, exc - self._included # TODO: do this in place?
  605. else: # + +
  606. self._included |= inc
  607. return self
  608. def update(self, items):
  609. if type(items) in (set, frozenset):
  610. inc, exc = items, None
  611. elif type(items) is _ComplementSet:
  612. inc, exc = items._included, items._excluded
  613. else:
  614. inc, exc = frozenset(items), None
  615. if self._included is None:
  616. if exc is None: # - +
  617. self._excluded &= inc
  618. else: # - -
  619. self._excluded.discard(exc)
  620. else:
  621. if inc is None: # + -
  622. self._included &= exc
  623. self._included, self._excluded = None, self._excluded
  624. else: # + +
  625. self._included.update(inc)
  626. def discard(self, items):
  627. if type(items) in (set, frozenset):
  628. inc, exc = items, None
  629. elif type(items) is _ComplementSet:
  630. inc, exc = items._included, items._excluded
  631. else:
  632. inc, exc = frozenset(items), None
  633. if self._included is None:
  634. if exc is None: # - +
  635. self._excluded.update(inc)
  636. else: # - -
  637. self._included, self._excluded = exc - self._excluded, None
  638. else:
  639. if inc is None: # + -
  640. self._included &= exc
  641. else: # + +
  642. self._included.discard(inc)
  643. def symmetric_difference(self, other):
  644. try:
  645. return self ^ other
  646. except NotImplementedError:
  647. raise TypeError('argument must be another set or complement(set)')
  648. def __xor__(self, other):
  649. inc, exc = _norm_args_notimplemented(other)
  650. if inc is NotImplemented:
  651. return NotImplemented
  652. if inc is NotImplemented:
  653. return NotImplemented
  654. if self._included is None:
  655. if exc is None: # - +
  656. return _ComplementSet(excluded=self._excluded - inc)
  657. else: # - -
  658. return _ComplementSet(included=self._excluded.symmetric_difference(exc))
  659. else:
  660. if inc is None: # + -
  661. return _ComplementSet(excluded=exc - self._included)
  662. else: # + +
  663. return _ComplementSet(included=self._included.symmetric_difference(inc))
  664. __rxor__ = __xor__
  665. def symmetric_difference_update(self, other):
  666. inc, exc = _norm_args_typeerror(other)
  667. if self._included is None:
  668. if exc is None: # - +
  669. self._excluded |= inc
  670. else: # - -
  671. self._excluded.symmetric_difference_update(exc)
  672. self._included, self._excluded = self._excluded, None
  673. else:
  674. if inc is None: # + -
  675. self._included |= exc
  676. self._included, self._excluded = None, self._included
  677. else: # + +
  678. self._included.symmetric_difference_update(inc)
  679. def isdisjoint(self, other):
  680. inc, exc = _norm_args_typeerror(other)
  681. if inc is NotImplemented:
  682. return NotImplemented
  683. if self._included is None:
  684. if exc is None: # - +
  685. return inc.issubset(self._excluded)
  686. else: # - -
  687. return False
  688. else:
  689. if inc is None: # + -
  690. return self._included.issubset(exc)
  691. else: # + +
  692. return self._included.isdisjoint(inc)
  693. def issubset(self, other):
  694. '''everything missing from other is also missing from self'''
  695. try:
  696. return self <= other
  697. except NotImplementedError:
  698. raise TypeError('argument must be another set or complement(set)')
  699. def __le__(self, other):
  700. inc, exc = _norm_args_notimplemented(other)
  701. if inc is NotImplemented:
  702. return NotImplemented
  703. if inc is NotImplemented:
  704. return NotImplemented
  705. if self._included is None:
  706. if exc is None: # - +
  707. return False
  708. else: # - -
  709. return self._excluded.issupserset(exc)
  710. else:
  711. if inc is None: # + -
  712. return self._included.isdisjoint(exc)
  713. else: # + +
  714. return self._included.issubset(inc)
  715. def __lt__(self, other):
  716. inc, exc = _norm_args_notimplemented(other)
  717. if inc is NotImplemented:
  718. return NotImplemented
  719. if inc is NotImplemented:
  720. return NotImplemented
  721. if self._included is None:
  722. if exc is None: # - +
  723. return False
  724. else: # - -
  725. return self._excluded > exc
  726. else:
  727. if inc is None: # + -
  728. return self._included.isdisjoint(exc)
  729. else: # + +
  730. return self._included < inc
  731. def issuperset(self, other):
  732. '''everything missing from self is also missing from super'''
  733. try:
  734. return self >= other
  735. except NotImplementedError:
  736. raise TypeError('argument must be another set or complement(set)')
  737. def __ge__(self, other):
  738. inc, exc = _norm_args_notimplemented(other)
  739. if inc is NotImplemented:
  740. return NotImplemented
  741. if self._included is None:
  742. if exc is None: # - +
  743. return not self._excluded.intersection(inc)
  744. else: # - -
  745. return self._excluded.issubset(exc)
  746. else:
  747. if inc is None: # + -
  748. return False
  749. else: # + +
  750. return self._included.issupserset(inc)
  751. def __gt__(self, other):
  752. inc, exc = _norm_args_notimplemented(other)
  753. if inc is NotImplemented:
  754. return NotImplemented
  755. if self._included is None:
  756. if exc is None: # - +
  757. return not self._excluded.intersection(inc)
  758. else: # - -
  759. return self._excluded < exc
  760. else:
  761. if inc is None: # + -
  762. return False
  763. else: # + +
  764. return self._included > inc
  765. def difference(self, other):
  766. try:
  767. return self - other
  768. except NotImplementedError:
  769. raise TypeError('argument must be another set or complement(set)')
  770. def __sub__(self, other):
  771. inc, exc = _norm_args_notimplemented(other)
  772. if inc is NotImplemented:
  773. return NotImplemented
  774. if self._included is None:
  775. if exc is None: # - +
  776. return _ComplementSet(excluded=self._excluded | inc)
  777. else: # - -
  778. return _ComplementSet(included=exc - self._excluded)
  779. else:
  780. if inc is None: # + -
  781. return _ComplementSet(included=self._included & exc)
  782. else: # + +
  783. return _ComplementSet(included=self._included.difference(inc))
  784. def __rsub__(self, other):
  785. inc, exc = _norm_args_notimplemented(other)
  786. if inc is NotImplemented:
  787. return NotImplemented
  788. # rsub, so the expression being evaluated is "other - self"
  789. if self._included is None:
  790. if exc is None: # - +
  791. return _ComplementSet(included=inc & self._excluded)
  792. else: # - -
  793. return _ComplementSet(included=self._excluded - exc)
  794. else:
  795. if inc is None: # + -
  796. return _ComplementSet(excluded=exc | self._included)
  797. else: # + +
  798. return _ComplementSet(included=inc.difference(self._included))
  799. def difference_update(self, other):
  800. try:
  801. self -= other
  802. except NotImplementedError:
  803. raise TypeError('argument must be another set or complement(set)')
  804. def __isub__(self, other):
  805. inc, exc = _norm_args_notimplemented(other)
  806. if inc is NotImplemented:
  807. return NotImplemented
  808. if self._included is None:
  809. if exc is None: # - +
  810. self._excluded |= inc
  811. else: # - -
  812. self._included, self._excluded = exc - self._excluded, None
  813. else:
  814. if inc is None: # + -
  815. self._included &= exc
  816. else: # + +
  817. self._included.difference_update(inc)
  818. return self
  819. def __eq__(self, other):
  820. return (
  821. type(self) is type(other)
  822. and self._included == other._included
  823. and self._excluded == other._excluded) or (
  824. type(other) in (set, frozenset) and self._included == other)
  825. def __hash__(self):
  826. return hash(self._included) ^ hash(self._excluded)
  827. def __len__(self):
  828. if self._included is not None:
  829. return len(self._included)
  830. raise NotImplementedError('complemented sets have undefined length')
  831. def __iter__(self):
  832. if self._included is not None:
  833. return iter(self._included)
  834. raise NotImplementedError('complemented sets have undefined contents')
  835. def __bool__(self):
  836. if self._included is not None:
  837. return bool(self._included)
  838. return True
  839. __nonzero__ = __bool__ # py2 compat