changeset 18637:ac4dbceeb14a

worker: partition a list (of tasks) into equal-sized chunks
author Bryan O'Sullivan <bryano@fb.com>
date Sat, 09 Feb 2013 15:51:32 -0800
parents dcb27c153a40
children 047110c0e2a8
files mercurial/worker.py
diffstat 1 files changed, 13 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/worker.py	Sat Feb 09 15:51:26 2013 -0800
+++ b/mercurial/worker.py	Sat Feb 09 15:51:32 2013 -0800
@@ -52,3 +52,16 @@
     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]