Skip to content

Instantly share code, notes, and snippets.

@cuzzo
Created February 23, 2012 21:21
Show Gist options
  • Save cuzzo/1895113 to your computer and use it in GitHub Desktop.
Save cuzzo/1895113 to your computer and use it in GitHub Desktop.
Priority Bucket
#! /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