码迷,mamicode.com
首页 > 编程语言 > 详细

KahnProcessNetwork的Python实现

时间:2014-11-20 13:33:49      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   sp   for   strong   on   

用Pytho实现了一个Kahn Process Network:

bubuko.com,布布扣

思路:

用Python的list模拟queue。

每个channel一个queue

用一个list (fgLog)来记录所有push到fg channel的值用于最后的显示

channel的queue设置为全局变量

代码实现:

h1f=[];fg=[];gh1=[];gh0=[];h0f=[]
fgLog = []

h1FirstRun = True
h0FirstRun = True

fLastChoose = 0
gLastChoose = 0

def h1():
    global h1FirstRun
    if h1FirstRun:
        h1FirstRun = False
        global h1f
        h1f.append(1)
    else:
        global gh1
        if len(gh1) != 0:
            value = gh1.pop(0)
            global h1f
            h1f.append(value)

def h0():
    global h0FirstRun
    if h0FirstRun:
        h0FirstRun = False
        global h0f
        h0f.append(0)
    else:
        global gh0
        if len(gh0) != 0:
            value = gh0.pop(0)
            global h0f
            h0f.append(value)

def f():
    global fLastChoose
    if fLastChoose == 0:
        global h1f
        if len(h1f) != 0:
            value = h1f.pop(0)
            global fg
            global fgLog
            fg.append(value)
            fgLog.append(value)
            fLastChoose = 1
    else:
        global h0f
        if len(h0f) != 0:
            value = h0f.pop(0)
            global fg
            global fgLog
            fg.append(value)
            fgLog.append(value)
            fLastChoose = 0
def g():
    global fg
    if len(fg) != 0:
        global gLastChoose
        value = fg.pop(0)
        if gLastChoose == 0:
            global gh1
            gh1.append(value)
            gLastChoose = 1
        else:
            global gh0
            gh0.append(value)
            gLastChoose = 0

if __name__ == __main__:
    runOrder = order3
    print runOrder
    if runOrder == order1:
        for i in range(50):
            h1();h0();f();g()
    elif runOrder == order2:
        for i in range(50):
            h1();h1();h1();g();f();h0();h0();
    elif runOrder == order3:
        for i in range(50):
            f();f();g();h1();h0();h1();h0();h0();g()
    print fgLog
            
            
    
        

 

KahnProcessNetwork的Python实现

标签:style   blog   http   color   os   sp   for   strong   on   

原文地址:http://www.cnblogs.com/instant7/p/4110361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!