6 def classify(requests, partition):
8 Takes an iterable over requests and a classification, and classifies the requests
9 returning a mapping from their classification (bitmap of applicable partitions) to
14 requests: iterable over sets of viable candidates for a request
16 partition: sequence of sets of candidates that forms a partition
17 over all available candidates.
23 rv = collections.defaultdict(list)
25 for request in requests:
26 rv[getClass(request, partition)].append(request)
30 def getClass(set, partition):
32 map("01".__getitem__, [
39 def isSubclass(superclass, subclass):
41 Returns True iff 'superclass' includes all elements of 'subclass'
43 >>> isSubclass("1100","1000")
45 >>> isSubclass("1100","1100")
47 >>> isSubclass("0000","0001")
50 for superbit, subbit in itertools.izip(superclass, subclass):
51 if subbit and not superbit:
56 def classContains(clz, partIndex):
57 return clz[partIndex] == "1"
59 def classCardinality(clz, partition = None):
61 return sum(itertools.imap("1".__eq__, clz))
63 return sum(len(part) for bit,part in zip(clz,partition)
66 def classMembers(clz, partition):
67 return reduce(operator.or_, classComponents(clz, partition), set())
69 def classComponents(clz, partition):
72 for i,bit in enumerate(clz)