# HG changeset patch # User Bryan O'Sullivan # Date 2013-02-09 23:51:32 # Node ID ac4dbceeb14a5d2d3d1db4b52839df65d5405641 # Parent dcb27c153a40ec251f0642ec4c1580a9d2b9b441 worker: partition a list (of tasks) into equal-sized chunks diff --git a/mercurial/worker.py b/mercurial/worker.py --- a/mercurial/worker.py +++ b/mercurial/worker.py @@ -52,3 +52,16 @@ def worthwhile(ui, costperop, nops): workers = _numworkers(ui) benefit = linear - (_startupcost * workers + linear / workers) return benefit >= 0.15 + +def partition(lst, nslices): + '''partition a list into N slices of equal size''' + n = len(lst) + chunk, slop = n / nslices, n % nslices + end = 0 + for i in xrange(nslices): + start = end + end = start + chunk + if slop: + end += 1 + slop -= 1 + yield lst[start:end]