Created
February 23, 2012 21:21
-
-
Save cuzzo/1895113 to your computer and use it in GitHub Desktop.
Priority Bucket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
class PriItem(object): | |
def __init__(self, index, priority, greed=1): | |
self.index = index | |
self.priority = priority | |
self.greed = greed | |
def insert(self): | |
return [self.index]*self.greed | |
class PriBucket(object): | |
def __init__(self, p=None): | |
self.bucket = [] | |
if p: | |
self.add(p) | |
def add(self, p): | |
if isinstance(p, list): | |
self.bucket.extend(p) | |
else: | |
self.bucket.append(p) | |
def spit(self, l): | |
for p in self.bucket: | |
p.oPri = p.priority | |
rList = [] | |
while len(rList) < l: | |
p = self.bucket.pop(0) | |
p.priority -= 1 | |
if p.priority <= 0: | |
rList.extend(p.insert()) | |
p.priority = p.oPri | |
self.bucket.append(p) | |
return rList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment