forked from Zhangtaining/cell_research
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting_cells_multithread.py
64 lines (51 loc) · 1.61 KB
/
sorting_cells_multithread.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
import sys
import threading
import time
from modules.multithread.MultiThreadCell import MultiThreadCell
#VALUE_LIST = [28, 34, 6, 20, 7, 89, 34, 18, 29, 51]
VALUE_LIST = range(50,0,-1)
threadLock = threading.Lock()
def create_cells_based_on_value_list(value_list):
if len(value_list) == 0:
return []
cells = []
start_cell = MultiThreadCell(0, 0, threadLock, None)
current_cell = start_cell
for i in range(0, len(value_list)):
cell = MultiThreadCell(i + 1, VALUE_LIST[i], threadLock, start_cell)
cells.append(cell)
cell.left_neighbor = current_cell
current_cell.right_neighbor = cell
current_cell = cell
return cells, start_cell
def get_values_as_arr(start_ptr):
p = start_ptr.right_neighbor
values = []
while p:
values.append(p.value)
p = p.right_neighbor
return values
def print_current_list(start_ptr):
threadLock.acquire()
values = get_values_as_arr(start_ptr)
print(values)
threadLock.release()
def sort_cells(cells, start_ptr):
for cell in cells:
cell.start()
def get_current_monotonicity(arr, index):
monotonicity_value = 0
prev = arr[0][index]
for i in range(1, len(arr)):
if arr[i][index] < prev:
monotonicity_value += 1
prev = arr[i][index]
return monotonicity_value
def main(argv):
cells, start_ptr = create_cells_based_on_value_list(VALUE_LIST)
sort_cells(cells, start_ptr)
while True:
print_current_list(start_ptr)
time.sleep(0.0001)
if __name__ == "__main__":
main(sys.argv[1:])