blob: dae42d43074565ca227309fd2f3768277168e31d [file] [log] [blame]
Jeremy Hylton65d4ea02000-02-16 00:49:47 +00001import types
2
3def flatten(tup):
4 elts = []
5 for elt in tup:
6 if type(elt) == types.TupleType:
7 elts = elts + flatten(elt)
8 else:
9 elts.append(elt)
10 return elts
11
Jeremy Hylton8b6323d2000-02-04 00:28:21 +000012class Set:
13 def __init__(self):
14 self.elts = {}
Jeremy Hylton95b86142000-02-10 20:54:27 +000015 def __len__(self):
16 return len(self.elts)
Jeremy Hylton8b6323d2000-02-04 00:28:21 +000017 def add(self, elt):
18 self.elts[elt] = elt
19 def items(self):
20 return self.elts.keys()
21 def has_elt(self, elt):
22 return self.elts.has_key(elt)
Jeremy Hylton69926ea2000-02-10 00:43:22 +000023 def remove(self, elt):
24 del self.elts[elt]
Jeremy Hylton8b6323d2000-02-04 00:28:21 +000025
26class Stack:
27 def __init__(self):
28 self.stack = []
29 self.pop = self.stack.pop
Jeremy Hylton95b86142000-02-10 20:54:27 +000030 def __len__(self):
31 return len(self.stack)
Jeremy Hylton8b6323d2000-02-04 00:28:21 +000032 def push(self, elt):
33 self.stack.append(elt)
34 def top(self):
35 return self.stack[-1]