You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
7.1 KiB

4 years ago
  1. # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
  2. # Copyright (C) 2003-2017 Nominum, Inc.
  3. #
  4. # Permission to use, copy, modify, and distribute this software and its
  5. # documentation for any purpose with or without fee is hereby granted,
  6. # provided that the above copyright notice and this permission notice
  7. # appear in all copies.
  8. #
  9. # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
  10. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
  12. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  15. # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. class Set(object):
  17. """A simple set class.
  18. This class was originally used to deal with sets being missing in
  19. ancient versions of python, but dnspython will continue to use it
  20. as these sets are based on lists and are thus indexable, and this
  21. ability is widely used in dnspython applications.
  22. """
  23. __slots__ = ['items']
  24. def __init__(self, items=None):
  25. """Initialize the set.
  26. *items*, an iterable or ``None``, the initial set of items.
  27. """
  28. self.items = []
  29. if items is not None:
  30. for item in items:
  31. self.add(item)
  32. def __repr__(self):
  33. return "dns.simpleset.Set(%s)" % repr(self.items)
  34. def add(self, item):
  35. """Add an item to the set.
  36. """
  37. if item not in self.items:
  38. self.items.append(item)
  39. def remove(self, item):
  40. """Remove an item from the set.
  41. """
  42. self.items.remove(item)
  43. def discard(self, item):
  44. """Remove an item from the set if present.
  45. """
  46. try:
  47. self.items.remove(item)
  48. except ValueError:
  49. pass
  50. def _clone(self):
  51. """Make a (shallow) copy of the set.
  52. There is a 'clone protocol' that subclasses of this class
  53. should use. To make a copy, first call your super's _clone()
  54. method, and use the object returned as the new instance. Then
  55. make shallow copies of the attributes defined in the subclass.
  56. This protocol allows us to write the set algorithms that
  57. return new instances (e.g. union) once, and keep using them in
  58. subclasses.
  59. """
  60. cls = self.__class__
  61. obj = cls.__new__(cls)
  62. obj.items = list(self.items)
  63. return obj
  64. def __copy__(self):
  65. """Make a (shallow) copy of the set.
  66. """
  67. return self._clone()
  68. def copy(self):
  69. """Make a (shallow) copy of the set.
  70. """
  71. return self._clone()
  72. def union_update(self, other):
  73. """Update the set, adding any elements from other which are not
  74. already in the set.
  75. """
  76. if not isinstance(other, Set):
  77. raise ValueError('other must be a Set instance')
  78. if self is other:
  79. return
  80. for item in other.items:
  81. self.add(item)
  82. def intersection_update(self, other):
  83. """Update the set, removing any elements from other which are not
  84. in both sets.
  85. """
  86. if not isinstance(other, Set):
  87. raise ValueError('other must be a Set instance')
  88. if self is other:
  89. return
  90. # we make a copy of the list so that we can remove items from
  91. # the list without breaking the iterator.
  92. for item in list(self.items):
  93. if item not in other.items:
  94. self.items.remove(item)
  95. def difference_update(self, other):
  96. """Update the set, removing any elements from other which are in
  97. the set.
  98. """
  99. if not isinstance(other, Set):
  100. raise ValueError('other must be a Set instance')
  101. if self is other:
  102. self.items = []
  103. else:
  104. for item in other.items:
  105. self.discard(item)
  106. def union(self, other):
  107. """Return a new set which is the union of ``self`` and ``other``.
  108. Returns the same Set type as this set.
  109. """
  110. obj = self._clone()
  111. obj.union_update(other)
  112. return obj
  113. def intersection(self, other):
  114. """Return a new set which is the intersection of ``self`` and
  115. ``other``.
  116. Returns the same Set type as this set.
  117. """
  118. obj = self._clone()
  119. obj.intersection_update(other)
  120. return obj
  121. def difference(self, other):
  122. """Return a new set which ``self`` - ``other``, i.e. the items
  123. in ``self`` which are not also in ``other``.
  124. Returns the same Set type as this set.
  125. """
  126. obj = self._clone()
  127. obj.difference_update(other)
  128. return obj
  129. def __or__(self, other):
  130. return self.union(other)
  131. def __and__(self, other):
  132. return self.intersection(other)
  133. def __add__(self, other):
  134. return self.union(other)
  135. def __sub__(self, other):
  136. return self.difference(other)
  137. def __ior__(self, other):
  138. self.union_update(other)
  139. return self
  140. def __iand__(self, other):
  141. self.intersection_update(other)
  142. return self
  143. def __iadd__(self, other):
  144. self.union_update(other)
  145. return self
  146. def __isub__(self, other):
  147. self.difference_update(other)
  148. return self
  149. def update(self, other):
  150. """Update the set, adding any elements from other which are not
  151. already in the set.
  152. *other*, the collection of items with which to update the set, which
  153. may be any iterable type.
  154. """
  155. for item in other:
  156. self.add(item)
  157. def clear(self):
  158. """Make the set empty."""
  159. self.items = []
  160. def __eq__(self, other):
  161. # Yes, this is inefficient but the sets we're dealing with are
  162. # usually quite small, so it shouldn't hurt too much.
  163. for item in self.items:
  164. if item not in other.items:
  165. return False
  166. for item in other.items:
  167. if item not in self.items:
  168. return False
  169. return True
  170. def __ne__(self, other):
  171. return not self.__eq__(other)
  172. def __len__(self):
  173. return len(self.items)
  174. def __iter__(self):
  175. return iter(self.items)
  176. def __getitem__(self, i):
  177. return self.items[i]
  178. def __delitem__(self, i):
  179. del self.items[i]
  180. def issubset(self, other):
  181. """Is this set a subset of *other*?
  182. Returns a ``bool``.
  183. """
  184. if not isinstance(other, Set):
  185. raise ValueError('other must be a Set instance')
  186. for item in self.items:
  187. if item not in other.items:
  188. return False
  189. return True
  190. def issuperset(self, other):
  191. """Is this set a superset of *other*?
  192. Returns a ``bool``.
  193. """
  194. if not isinstance(other, Set):
  195. raise ValueError('other must be a Set instance')
  196. for item in other.items:
  197. if item not in self.items:
  198. return False
  199. return True