Package Bio :: Package Pathway :: Package Rep :: Module HashSet
[hide private]
[frames] | no frames]

Source Code for Module Bio.Pathway.Rep.HashSet

 1  # Copyright 2001 by Tarjei Mikkelsen. All rights reserved. 
 2  # This code is part of the Biopython distribution and governed by its 
 3  # license.  Please see the LICENSE file that should have been included 
 4  # as part of this package. 
 5   
 6  # TODO - Can we replace this with the python built in set object? 
7 -class HashSet(object):
8 """A set abstraction supporting the basic set operations. 9 10 This implementation requires that all elements are hashable, 11 which implies that elements must not mutate while contained. 12 """
13 - def __init__(self, elements = []):
14 """Initializes a new HashSet.""" 15 self.__elements = {} 16 for e in elements: 17 self.__elements[e] = 1
18
19 - def __contains__(self, element):
20 """Returns true iff this set contains element.""" 21 return element in self.__elements
22
23 - def __eq__(self, set):
24 """Returns true iff x == y for all elements in self, set.""" 25 if not isinstance(set, HashSet): 26 return 0 27 for x in self.list(): 28 if not (x in set): return 0 29 for x in set.list(): 30 if not (x in self): return 0 31 return 1
32
33 - def __len__(self):
34 """Returns the number of elements in this set.""" 35 return len(self.__elements)
36
37 - def __ne__(self, set):
38 """Returns true iff this set is not equal to set.""" 39 return not self.__eq__(set)
40
41 - def __repr__(self):
42 """Returns a debugging string representation of this set.""" 43 return "HashSet(" + repr(self.list()) + ")"
44
45 - def __str__(self):
46 """Returns a string representation of this set.""" 47 return "{" + ",".join(map(str, self.list())) + "}"
48 49 # Element access: 50
51 - def add(self, element):
52 """Adds element to this set.""" 53 self.__elements[element] = 1
54
55 - def contains(self, element):
56 """Returns true iff this set contains element.""" 57 return self.__contains__(element)
58
59 - def remove(self, element):
60 """Removes element from this set.""" 61 try: 62 del self.__elements[element] 63 except KeyError: 64 pass
65
66 - def list(self):
67 """Returns the elements of this set in a list.""" 68 return self.__elements.keys()
69 70 # Information: 71
72 - def empty(self):
73 """Returns true iff this set is empty.""" 74 return len(self.__elements) == 0
75 76 # Set operations: 77
78 - def union(self, s):
79 """Returns the union of this set and s.""" 80 return HashSet(self.list() + s.list())
81
82 - def intersection(self, s):
83 """Returns the intersection of this set and s.""" 84 return HashSet(filter(lambda e,s=s: e in s, self.list()))
85
86 - def difference(self, s):
87 """Returns the difference of this set and s.""" 88 return HashSet(filter(lambda e,s=s: e not in s, self.list()))
89
90 - def cartesian(self,s):
91 """Returns the Cartesian product of this set and s.""" 92 p = [] 93 for i in self.list(): 94 for j in s.list(): 95 p.append((i,j)) 96 return HashSet(p)
97