Download code

Jump to: navigation, search

Back to Parallel_prefix_(Python)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

parprefix.py

 1 # The authors of this work have released all rights to it and placed it
 2 # in the public domain under the Creative Commons CC0 1.0 waiver
 3 # (http://creativecommons.org/publicdomain/zero/1.0/).
 4 # 
 5 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 # 
13 # Retrieved from: http://en.literateprograms.org/Parallel_prefix_(Python)?oldid=16590
14 
15 from itertools import imap
16 vect = imap
17 
18 def scan(op,a):
19         print a
20         if len(a) > 1:
21                 a[1::2] = vect(op,a[0::2],a[1::2])
22                 a[1::2] = scan(op,a[1::2])
23                 a[2::2] = vect(op,a[1::2],a[2::2])
24         print a
25         return a
26 def segmented_sum(xps,yps):
27         x,xf = xps
28         y,yf = yps
29         if yf:  return (  y,yf)
30         else:   return (x+y,xf)
31 if __name__ == "__main__":
32         import operator
33         scan(operator.add,range(0,16))
34         scan(operator.mul,range(1,17))
35         scan(operator.add,list("hi world"))
36         print [x for (x,f) in
37                 scan(segmented_sum,zip([7,6,5,4,3,2,1,0],
38                                        [1,0,1,0,0,1,0,0]))]


hijacker
hijacker
hijacker
hijacker