##// END OF EJS Templates
don't create lists...
MinRK -
Show More
@@ -56,9 +56,9 b' else:'
56 arrayModules.append({'module':numarray,
56 arrayModules.append({'module':numarray,
57 'type':numarray.numarraycore.NumArray})
57 'type':numarray.numarraycore.NumArray})
58
58
59 class Map:
59 class Map(object):
60 """A class for partitioning a sequence using a map."""
60 """A class for partitioning a sequence using a map."""
61
61
62 def getPartition(self, seq, p, q):
62 def getPartition(self, seq, p, q):
63 """Returns the pth partition of q partitions of seq."""
63 """Returns the pth partition of q partitions of seq."""
64
64
@@ -66,25 +66,24 b' class Map:'
66 if p<0 or p>=q:
66 if p<0 or p>=q:
67 print "No partition exists."
67 print "No partition exists."
68 return
68 return
69
69
70 remainder = len(seq)%q
70 N = len(seq)
71 basesize = len(seq)//q
71 remainder = N % q
72 hi = []
72 basesize = N // q
73 lo = []
73
74 for n in range(q):
74 if p < remainder:
75 if n < remainder:
75 low = p * (basesize + 1)
76 lo.append(n * (basesize + 1))
76 high = low + basesize + 1
77 hi.append(lo[-1] + basesize + 1)
77 else:
78 else:
78 low = p * basesize + remainder
79 lo.append(n*basesize + remainder)
79 high = low + basesize
80 hi.append(lo[-1] + basesize)
81
80
82 try:
81 try:
83 result = seq[lo[p]:hi[p]]
82 result = seq[low:high]
84 except TypeError:
83 except TypeError:
85 # some objects (iterators) can't be sliced,
84 # some objects (iterators) can't be sliced,
86 # use islice:
85 # use islice:
87 result = list(islice(seq, lo[p], hi[p]))
86 result = list(islice(seq, low, high))
88
87
89 return result
88 return result
90
89
General Comments 0
You need to be logged in to leave comments. Login now