PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/couchjs/scons/scons-local-2.0.1/SCons/compat/_scons_sets.py

http://github.com/cloudant/bigcouch
Python | 563 lines | 434 code | 29 blank | 100 comment | 21 complexity | 37b772e3274c1d75f0ff9706d5933b4a MD5 | raw file
Possible License(s): Apache-2.0
  1. """Classes to represent arbitrary sets (including sets of sets).
  2. This module implements sets using dictionaries whose values are
  3. ignored. The usual operations (union, intersection, deletion, etc.)
  4. are provided as both methods and operators.
  5. Important: sets are not sequences! While they support 'x in s',
  6. 'len(s)', and 'for x in s', none of those operations are unique for
  7. sequences; for example, mappings support all three as well. The
  8. characteristic operation for sequences is subscripting with small
  9. integers: s[i], for i in range(len(s)). Sets don't support
  10. subscripting at all. Also, sequences allow multiple occurrences and
  11. their elements have a definite order; sets on the other hand don't
  12. record multiple occurrences and don't remember the order of element
  13. insertion (which is why they don't support s[i]).
  14. The following classes are provided:
  15. BaseSet -- All the operations common to both mutable and immutable
  16. sets. This is an abstract class, not meant to be directly
  17. instantiated.
  18. Set -- Mutable sets, subclass of BaseSet; not hashable.
  19. ImmutableSet -- Immutable sets, subclass of BaseSet; hashable.
  20. An iterable argument is mandatory to create an ImmutableSet.
  21. _TemporarilyImmutableSet -- A wrapper around a Set, hashable,
  22. giving the same hash value as the immutable set equivalent
  23. would have. Do not use this class directly.
  24. Only hashable objects can be added to a Set. In particular, you cannot
  25. really add a Set as an element to another Set; if you try, what is
  26. actually added is an ImmutableSet built from it (it compares equal to
  27. the one you tried adding).
  28. When you ask if `x in y' where x is a Set and y is a Set or
  29. ImmutableSet, x is wrapped into a _TemporarilyImmutableSet z, and
  30. what's tested is actually `z in y'.
  31. """
  32. # Code history:
  33. #
  34. # - Greg V. Wilson wrote the first version, using a different approach
  35. # to the mutable/immutable problem, and inheriting from dict.
  36. #
  37. # - Alex Martelli modified Greg's version to implement the current
  38. # Set/ImmutableSet approach, and make the data an attribute.
  39. #
  40. # - Guido van Rossum rewrote much of the code, made some API changes,
  41. # and cleaned up the docstrings.
  42. #
  43. # - Raymond Hettinger added a number of speedups and other
  44. # improvements.
  45. # protect this import from the fixers...
  46. exec('from itertools import ifilterfalse as filterfalse')
  47. __all__ = ['BaseSet', 'Set', 'ImmutableSet']
  48. class BaseSet(object):
  49. """Common base class for mutable and immutable sets."""
  50. __slots__ = ['_data']
  51. # Constructor
  52. def __init__(self):
  53. """This is an abstract class."""
  54. # Don't call this from a concrete subclass!
  55. if self.__class__ is BaseSet:
  56. raise TypeError("BaseSet is an abstract class. "
  57. "Use Set or ImmutableSet.")
  58. # Standard protocols: __len__, __repr__, __str__, __iter__
  59. def __len__(self):
  60. """Return the number of elements of a set."""
  61. return len(self._data)
  62. def __repr__(self):
  63. """Return string representation of a set.
  64. This looks like 'Set([<list of elements>])'.
  65. """
  66. return self._repr()
  67. # __str__ is the same as __repr__
  68. __str__ = __repr__
  69. def _repr(self, sort_them=False):
  70. elements = list(self._data.keys())
  71. if sort_them:
  72. elements.sort()
  73. return '%s(%r)' % (self.__class__.__name__, elements)
  74. def __iter__(self):
  75. """Return an iterator over the elements or a set.
  76. This is the keys iterator for the underlying dict.
  77. """
  78. # Wrapping name in () prevents fixer from "fixing" this
  79. return (self._data.iterkeys)()
  80. # Three-way comparison is not supported. However, because __eq__ is
  81. # tried before __cmp__, if Set x == Set y, x.__eq__(y) returns True and
  82. # then cmp(x, y) returns 0 (Python doesn't actually call __cmp__ in this
  83. # case).
  84. def __cmp__(self, other):
  85. raise TypeError("can't compare sets using cmp()")
  86. # Equality comparisons using the underlying dicts. Mixed-type comparisons
  87. # are allowed here, where Set == z for non-Set z always returns False,
  88. # and Set != z always True. This allows expressions like "x in y" to
  89. # give the expected result when y is a sequence of mixed types, not
  90. # raising a pointless TypeError just because y contains a Set, or x is
  91. # a Set and y contain's a non-set ("in" invokes only __eq__).
  92. # Subtle: it would be nicer if __eq__ and __ne__ could return
  93. # NotImplemented instead of True or False. Then the other comparand
  94. # would get a chance to determine the result, and if the other comparand
  95. # also returned NotImplemented then it would fall back to object address
  96. # comparison (which would always return False for __eq__ and always
  97. # True for __ne__). However, that doesn't work, because this type
  98. # *also* implements __cmp__: if, e.g., __eq__ returns NotImplemented,
  99. # Python tries __cmp__ next, and the __cmp__ here then raises TypeError.
  100. def __eq__(self, other):
  101. if isinstance(other, BaseSet):
  102. return self._data == other._data
  103. else:
  104. return False
  105. def __ne__(self, other):
  106. if isinstance(other, BaseSet):
  107. return self._data != other._data
  108. else:
  109. return True
  110. # Copying operations
  111. def copy(self):
  112. """Return a shallow copy of a set."""
  113. result = self.__class__()
  114. result._data.update(self._data)
  115. return result
  116. __copy__ = copy # For the copy module
  117. def __deepcopy__(self, memo):
  118. """Return a deep copy of a set; used by copy module."""
  119. # This pre-creates the result and inserts it in the memo
  120. # early, in case the deep copy recurses into another reference
  121. # to this same set. A set can't be an element of itself, but
  122. # it can certainly contain an object that has a reference to
  123. # itself.
  124. from copy import deepcopy
  125. result = self.__class__()
  126. memo[id(self)] = result
  127. data = result._data
  128. value = True
  129. for elt in self:
  130. data[deepcopy(elt, memo)] = value
  131. return result
  132. # Standard set operations: union, intersection, both differences.
  133. # Each has an operator version (e.g. __or__, invoked with |) and a
  134. # method version (e.g. union).
  135. # Subtle: Each pair requires distinct code so that the outcome is
  136. # correct when the type of other isn't suitable. For example, if
  137. # we did "union = __or__" instead, then Set().union(3) would return
  138. # NotImplemented instead of raising TypeError (albeit that *why* it
  139. # raises TypeError as-is is also a bit subtle).
  140. def __or__(self, other):
  141. """Return the union of two sets as a new set.
  142. (I.e. all elements that are in either set.)
  143. """
  144. if not isinstance(other, BaseSet):
  145. return NotImplemented
  146. return self.union(other)
  147. def union(self, other):
  148. """Return the union of two sets as a new set.
  149. (I.e. all elements that are in either set.)
  150. """
  151. result = self.__class__(self)
  152. result._update(other)
  153. return result
  154. def __and__(self, other):
  155. """Return the intersection of two sets as a new set.
  156. (I.e. all elements that are in both sets.)
  157. """
  158. if not isinstance(other, BaseSet):
  159. return NotImplemented
  160. return self.intersection(other)
  161. def intersection(self, other):
  162. """Return the intersection of two sets as a new set.
  163. (I.e. all elements that are in both sets.)
  164. """
  165. if not isinstance(other, BaseSet):
  166. other = Set(other)
  167. if len(self) <= len(other):
  168. little, big = self, other
  169. else:
  170. little, big = other, self
  171. common = iter(filter(big._data.has_key, little))
  172. return self.__class__(common)
  173. def __xor__(self, other):
  174. """Return the symmetric difference of two sets as a new set.
  175. (I.e. all elements that are in exactly one of the sets.)
  176. """
  177. if not isinstance(other, BaseSet):
  178. return NotImplemented
  179. return self.symmetric_difference(other)
  180. def symmetric_difference(self, other):
  181. """Return the symmetric difference of two sets as a new set.
  182. (I.e. all elements that are in exactly one of the sets.)
  183. """
  184. result = self.__class__()
  185. data = result._data
  186. value = True
  187. selfdata = self._data
  188. try:
  189. otherdata = other._data
  190. except AttributeError:
  191. otherdata = Set(other)._data
  192. for elt in filterfalse(otherdata.has_key, selfdata):
  193. data[elt] = value
  194. for elt in filterfalse(selfdata.has_key, otherdata):
  195. data[elt] = value
  196. return result
  197. def __sub__(self, other):
  198. """Return the difference of two sets as a new Set.
  199. (I.e. all elements that are in this set and not in the other.)
  200. """
  201. if not isinstance(other, BaseSet):
  202. return NotImplemented
  203. return self.difference(other)
  204. def difference(self, other):
  205. """Return the difference of two sets as a new Set.
  206. (I.e. all elements that are in this set and not in the other.)
  207. """
  208. result = self.__class__()
  209. data = result._data
  210. try:
  211. otherdata = other._data
  212. except AttributeError:
  213. otherdata = Set(other)._data
  214. value = True
  215. for elt in filterfalse(otherdata.has_key, self):
  216. data[elt] = value
  217. return result
  218. # Membership test
  219. def __contains__(self, element):
  220. """Report whether an element is a member of a set.
  221. (Called in response to the expression `element in self'.)
  222. """
  223. try:
  224. return element in self._data
  225. except TypeError:
  226. transform = getattr(element, "__as_temporarily_immutable__", None)
  227. if transform is None:
  228. raise # re-raise the TypeError exception we caught
  229. return transform() in self._data
  230. # Subset and superset test
  231. def issubset(self, other):
  232. """Report whether another set contains this set."""
  233. self._binary_sanity_check(other)
  234. if len(self) > len(other): # Fast check for obvious cases
  235. return False
  236. for elt in filterfalse(other._data.has_key, self):
  237. return False
  238. return True
  239. def issuperset(self, other):
  240. """Report whether this set contains another set."""
  241. self._binary_sanity_check(other)
  242. if len(self) < len(other): # Fast check for obvious cases
  243. return False
  244. for elt in filterfalse(self._data.has_key, other):
  245. return False
  246. return True
  247. # Inequality comparisons using the is-subset relation.
  248. __le__ = issubset
  249. __ge__ = issuperset
  250. def __lt__(self, other):
  251. self._binary_sanity_check(other)
  252. return len(self) < len(other) and self.issubset(other)
  253. def __gt__(self, other):
  254. self._binary_sanity_check(other)
  255. return len(self) > len(other) and self.issuperset(other)
  256. # Assorted helpers
  257. def _binary_sanity_check(self, other):
  258. # Check that the other argument to a binary operation is also
  259. # a set, raising a TypeError otherwise.
  260. if not isinstance(other, BaseSet):
  261. raise TypeError("Binary operation only permitted between sets")
  262. def _compute_hash(self):
  263. # Calculate hash code for a set by xor'ing the hash codes of
  264. # the elements. This ensures that the hash code does not depend
  265. # on the order in which elements are added to the set. This is
  266. # not called __hash__ because a BaseSet should not be hashable;
  267. # only an ImmutableSet is hashable.
  268. result = 0
  269. for elt in self:
  270. result ^= hash(elt)
  271. return result
  272. def _update(self, iterable):
  273. # The main loop for update() and the subclass __init__() methods.
  274. data = self._data
  275. # Use the fast update() method when a dictionary is available.
  276. if isinstance(iterable, BaseSet):
  277. data.update(iterable._data)
  278. return
  279. value = True
  280. if type(iterable) in (list, tuple, xrange):
  281. # Optimized: we know that __iter__() and next() can't
  282. # raise TypeError, so we can move 'try:' out of the loop.
  283. it = iter(iterable)
  284. while True:
  285. try:
  286. for element in it:
  287. data[element] = value
  288. return
  289. except TypeError:
  290. transform = getattr(element, "__as_immutable__", None)
  291. if transform is None:
  292. raise # re-raise the TypeError exception we caught
  293. data[transform()] = value
  294. else:
  295. # Safe: only catch TypeError where intended
  296. for element in iterable:
  297. try:
  298. data[element] = value
  299. except TypeError:
  300. transform = getattr(element, "__as_immutable__", None)
  301. if transform is None:
  302. raise # re-raise the TypeError exception we caught
  303. data[transform()] = value
  304. class ImmutableSet(BaseSet):
  305. """Immutable set class."""
  306. __slots__ = ['_hashcode']
  307. # BaseSet + hashing
  308. def __init__(self, iterable=None):
  309. """Construct an immutable set from an optional iterable."""
  310. self._hashcode = None
  311. self._data = {}
  312. if iterable is not None:
  313. self._update(iterable)
  314. def __hash__(self):
  315. if self._hashcode is None:
  316. self._hashcode = self._compute_hash()
  317. return self._hashcode
  318. def __getstate__(self):
  319. return self._data, self._hashcode
  320. def __setstate__(self, state):
  321. self._data, self._hashcode = state
  322. class Set(BaseSet):
  323. """ Mutable set class."""
  324. __slots__ = []
  325. # BaseSet + operations requiring mutability; no hashing
  326. def __init__(self, iterable=None):
  327. """Construct a set from an optional iterable."""
  328. self._data = {}
  329. if iterable is not None:
  330. self._update(iterable)
  331. def __getstate__(self):
  332. # getstate's results are ignored if it is not
  333. return self._data,
  334. def __setstate__(self, data):
  335. self._data, = data
  336. def __hash__(self):
  337. """A Set cannot be hashed."""
  338. # We inherit object.__hash__, so we must deny this explicitly
  339. raise TypeError("Can't hash a Set, only an ImmutableSet.")
  340. # In-place union, intersection, differences.
  341. # Subtle: The xyz_update() functions deliberately return None,
  342. # as do all mutating operations on built-in container types.
  343. # The __xyz__ spellings have to return self, though.
  344. def __ior__(self, other):
  345. """Update a set with the union of itself and another."""
  346. self._binary_sanity_check(other)
  347. self._data.update(other._data)
  348. return self
  349. def union_update(self, other):
  350. """Update a set with the union of itself and another."""
  351. self._update(other)
  352. def __iand__(self, other):
  353. """Update a set with the intersection of itself and another."""
  354. self._binary_sanity_check(other)
  355. self._data = (self & other)._data
  356. return self
  357. def intersection_update(self, other):
  358. """Update a set with the intersection of itself and another."""
  359. if isinstance(other, BaseSet):
  360. self &= other
  361. else:
  362. self._data = (self.intersection(other))._data
  363. def __ixor__(self, other):
  364. """Update a set with the symmetric difference of itself and another."""
  365. self._binary_sanity_check(other)
  366. self.symmetric_difference_update(other)
  367. return self
  368. def symmetric_difference_update(self, other):
  369. """Update a set with the symmetric difference of itself and another."""
  370. data = self._data
  371. value = True
  372. if not isinstance(other, BaseSet):
  373. other = Set(other)
  374. if self is other:
  375. self.clear()
  376. for elt in other:
  377. if elt in data:
  378. del data[elt]
  379. else:
  380. data[elt] = value
  381. def __isub__(self, other):
  382. """Remove all elements of another set from this set."""
  383. self._binary_sanity_check(other)
  384. self.difference_update(other)
  385. return self
  386. def difference_update(self, other):
  387. """Remove all elements of another set from this set."""
  388. data = self._data
  389. if not isinstance(other, BaseSet):
  390. other = Set(other)
  391. if self is other:
  392. self.clear()
  393. for elt in filter(data.has_key, other):
  394. del data[elt]
  395. # Python dict-like mass mutations: update, clear
  396. def update(self, iterable):
  397. """Add all values from an iterable (such as a list or file)."""
  398. self._update(iterable)
  399. def clear(self):
  400. """Remove all elements from this set."""
  401. self._data.clear()
  402. # Single-element mutations: add, remove, discard
  403. def add(self, element):
  404. """Add an element to a set.
  405. This has no effect if the element is already present.
  406. """
  407. try:
  408. self._data[element] = True
  409. except TypeError:
  410. transform = getattr(element, "__as_immutable__", None)
  411. if transform is None:
  412. raise # re-raise the TypeError exception we caught
  413. self._data[transform()] = True
  414. def remove(self, element):
  415. """Remove an element from a set; it must be a member.
  416. If the element is not a member, raise a KeyError.
  417. """
  418. try:
  419. del self._data[element]
  420. except TypeError:
  421. transform = getattr(element, "__as_temporarily_immutable__", None)
  422. if transform is None:
  423. raise # re-raise the TypeError exception we caught
  424. del self._data[transform()]
  425. def discard(self, element):
  426. """Remove an element from a set if it is a member.
  427. If the element is not a member, do nothing.
  428. """
  429. try:
  430. self.remove(element)
  431. except KeyError:
  432. pass
  433. def pop(self):
  434. """Remove and return an arbitrary set element."""
  435. return self._data.popitem()[0]
  436. def __as_immutable__(self):
  437. # Return a copy of self as an immutable set
  438. return ImmutableSet(self)
  439. def __as_temporarily_immutable__(self):
  440. # Return self wrapped in a temporarily immutable set
  441. return _TemporarilyImmutableSet(self)
  442. class _TemporarilyImmutableSet(BaseSet):
  443. # Wrap a mutable set as if it was temporarily immutable.
  444. # This only supplies hashing and equality comparisons.
  445. def __init__(self, set):
  446. self._set = set
  447. self._data = set._data # Needed by ImmutableSet.__eq__()
  448. def __hash__(self):
  449. return self._set._compute_hash()
  450. # Local Variables:
  451. # tab-width:4
  452. # indent-tabs-mode:nil
  453. # End:
  454. # vim: set expandtab tabstop=4 shiftwidth=4: