-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_net.py
executable file
·86 lines (70 loc) · 2.92 KB
/
sort_net.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
def wire(name=None, debug=None):
comparators = list()
def connect(comparator):
if debug:
print(f'wire({name}): adding comparator {comparator["get_name"]()}')
comparators.append(comparator)
def get_comparators():
return comparators
def is_set():
return not link['value'] is None
def get_value():
return link['value']
def get_name():
return link['name']
def set_value(src_comparator, value):
if debug:
if src_comparator is None:
print(f'wire({name}): setting value {value} by comparator None')
else:
print(f'wire({name}): setting value {value} by comparator {src_comparator["get_name"]()}')
link['value'] = value
for comparator in comparators:
if comparator is not src_comparator:
if debug:
print(f'wire({name}): sending update to comparator {comparator["get_name"]()}')
comparator['update'](link)
# A dispatch dictionary
link = { 'name': name,
'value': None,
'connect': connect,
'set': set_value,
'get': get_value,
'is_set': is_set,
'get_name': get_name,
'comparators': get_comparators }
return link
def comparator(inn, inx, outx, outn, inn_f, inx_f, outx_f, outn_f, name=None, debug=None):
def update(src_wire):
if debug:
print(f'comparator({name}): got update from wire {src_wire["get_name"]()}')
if src_wire in (inn, inx) and inn['is_set']() and inx['is_set']():
outx_val = outx_f(inn['get'](), inx['get']())
if debug:
print(f'comparator({name}): setting value {outx_val} to wire {outx["get_name"]()}')
outx['set'](node, outx_val)
outn_val = outn_f(inn['get'](), inx['get']())
if debug:
print(f'comparator({name}): setting value {outn_val} to wire {outn["get_name"]()}')
outn['set'](node, outn_val)
elif src_wire in (outn, outx) and outn['is_set']() and outx['is_set']():
inx_val = inx_f(outn['get'](), outx['get']())
if debug:
print(f'comparator({name}): setting value {inx_val} to wire {inx["get_name"]()}')
inx['set'](node, inx_val)
inn_val = inn_f(outn['get'](), outx['get']())
if debug:
print(f'comparator({name}): setting value {inn_val} to wire {inn["get_name"]()}')
inn['set'](node, inn_val)
def get_name():
return node['name']
# A dispatch dictionary
node = { 'name': name,
'get_name': get_name,
'update': update }
for conn in (inn, inx, outx, outn):
if debug:
print(f'comparator({name}): sending connect to wire {conn["get_name"]()}')
conn['connect'](node)
return node