Back to index

python-biopython  1.60
HashSet.py
Go to the documentation of this file.
00001 # Copyright 2001 by Tarjei Mikkelsen. All rights reserved.
00002 # This code is part of the Biopython distribution and governed by its
00003 # license.  Please see the LICENSE file that should have been included
00004 # as part of this package.
00005 
00006 import warnings
00007 warnings.warn("The module Bio.Pathway.Rep.HashSet is now deprecated, "
00008               "and will be removed in a future release of Biopython. "
00009               "Use Python's built in set object instead.",
00010               DeprecationWarning)
00011 
00012 class HashSet(object):
00013     """A set abstraction supporting the basic set operations.
00014 
00015     This implementation requires that all elements are hashable,
00016     which implies that elements must not mutate while contained.
00017     """
00018     def __init__(self, elements = []):
00019         """Initializes a new HashSet."""
00020         self._elements = {}
00021         for e in elements:
00022             self._elements[e] = 1
00023             
00024     def __contains__(self, element):
00025         """Returns true iff this set contains element."""
00026         return element in self._elements
00027     
00028     def __eq__(self, set):
00029         """Returns true iff x == y for all elements in self, set."""
00030         if not isinstance(set, HashSet):
00031             return 0
00032         for x in self.list():
00033             if not (x in set): return 0
00034         for x in set.list():
00035             if not (x in self): return 0
00036         return 1
00037 
00038     def __len__(self):
00039         """Returns the number of elements in this set."""
00040         return len(self._elements)
00041 
00042     def __ne__(self, set):
00043         """Returns true iff this set is not equal to set.""" 
00044         return not self.__eq__(set)
00045 
00046     def __repr__(self):
00047         """Returns a debugging string representation of this set."""
00048         return "HashSet(" + repr(self.list()) + ")"
00049 
00050     def __str__(self):
00051         """Returns a string representation of this set."""
00052         return "{" + ",".join(map(str, self.list())) + "}" 
00053 
00054     # Element access:
00055 
00056     def add(self, element):
00057         """Adds element to this set."""
00058         self._elements[element] = 1
00059 
00060     def contains(self, element):
00061         """Returns true iff this set contains element."""
00062         return self.__contains__(element)
00063 
00064     def remove(self, element):
00065         """Removes element from this set."""
00066         try:
00067             del self._elements[element]
00068         except KeyError:
00069             pass
00070 
00071     def list(self):
00072         """Returns the elements of this set in a list."""
00073         return self._elements.keys()
00074 
00075     # Information:
00076 
00077     def empty(self):
00078         """Returns true iff this set is empty."""
00079         return len(self._elements) == 0
00080 
00081     # Set operations:
00082 
00083     def union(self, s):
00084         """Returns the union of this set and s."""
00085         return HashSet(self.list() + s.list())
00086 
00087     def intersection(self, s):
00088         """Returns the intersection of this set and s."""
00089         return HashSet(filter(lambda e,s=s: e in s, self.list()))
00090 
00091     def difference(self, s):
00092         """Returns the difference of this set and s."""
00093         return HashSet(filter(lambda e,s=s: e not in s, self.list()))
00094 
00095     def cartesian(self,s):
00096         """Returns the Cartesian product of this set and s."""
00097         p = []
00098         for i in self.list():
00099             for j in s.list():
00100                 p.append((i,j))
00101         return HashSet(p)
00102     
00103 
00104