1: #line 6 "sets.ipk"
2: class set:
3: __class_protocols__= ['sequence','mutable','set']
4: def __init__(self,*args):
5: self.s = {}
6: for e in args: self.s[e]=None
7:
8:
9: def contains(self,e):
10: return self.s.has_key(e)
11:
12:
13: def le(self,rhs):
14: for e in self.s.keys():
15: if e not in rhs: return 0
16: return 1
17:
18:
19: def ge(self,rhs):
20: for e in rhs:
21: if not self.s.has_key(e): return 0
22: return 1
23:
24: def eq(self,rhs):
25: return self.le(rhs) and self.ge(rhs)
26:
27: def ne(self,rhs):
28: for e in rhs:
29: if not self.s.has_key(e): return 1
30: for e in self.s.keys():
31: if not e in rhs: return 1
32: return 0
33:
34: def gt(self,rhs):
35: return self.ne(rhs) and self.ge(rhs)
36:
37: def lt(self,rhs):
38: return self.ne(rhs) and self.le(rhs)
39:
40: def min(self):
41: return min(self.s.keys())
42:
43: def max(self):
44: return max(self.s.keys())
45:
46: def index(self,e):
47: return self.s.keys().index(e)
48:
49: def count(self,e):
50: return self.s.has_key(e)
51:
52:
53: def insert(self,e):
54: self.s[e]=None
55:
56:
57: def remove(self,e):
58: del self.s[e]
59:
60:
61: def excise(self,e):
62: if self.s.has_key(e): del self.s[e]
63:
64:
65: def append_sequence(self,seq):
66: for e in seq: self.s[e]=None
67:
68:
69: def list(self):
70: return self.s.keys()
71:
72:
73: def tuple(self):
74: return tuple(self.s.keys())
75:
76:
77: def dict(self):
78: return self.s.copy()
79:
80:
81: def copy(self):
82: s = set()
83: s.s = self.s.copy()
84: return s
85:
86:
87: def __repr__(self):
88: keys = self.s.keys()
89: p = 'set('
90: if keys: p = p + repr(keys[0])
91: for key in keys[1:]: p = p + ', ' + repr(key)
92: p = p + ')'
93: return p
94:
95:
96: def __nonzero__(self):
97: return len(s)!=0
98:
99:
100:
101:
102: def __cmp__(self,other):
103: right = set()
104: for e in other: right.insert(e)
105: k1 = self.s.keys()
106: k1.sort()
107: k2 = right.s.keys()
108: k2.sort()
109: return cmp(k1,k2)
110:
111: def __len__(self):
112: return len(self.s)
113:
114: def __getitem__(self,index):
115: return self.s.keys()[index]
116:
117: def __delitem__(self,index):
118: k = self.s.keys()[index]
119: del self.s[k]
120:
121: def __getslice__(self,i,j):
122: return apply(set,tuple(self.s.keys()[i:j]))
123:
124: def __and__(self,right):
125: s = set()
126: for e in self.s.keys():
127: if e in right: s.insert(e)
128: return s
129:
130: def __or__(self,right):
131: s = set()
132: s.s = self.s.copy()
133: for e in right: s.s[e]=None
134: return s
135:
136: def __xor__(self,right):
137: s = set()
138: for e in right: s.insert(e)
139: for e in self.s.keys():
140: if s.s.has_key(e): del s.s[e]
141: else: s.s[e]=None
142: return s
143:
144: def __add__(self,right):
145: return self.__or__(right)
146:
147: def __sub__(self,right):
148: s = set()
149: for e in self.s.keys():
150: if e not in right: s.insert(e)
151: return s
152: