-
Notifications
You must be signed in to change notification settings - Fork 1
/
colors_or_cyt.pyx
60 lines (47 loc) · 1.67 KB
/
colors_or_cyt.pyx
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
from cpython cimport array
import numpy as np
from numpy cimport ndarray, int64_t
cpdef ndarray[int64_t] update_connected(ndarray[int64_t] items, connected,
int[:] cols, int N):
cdef int idx
cols[:] = 0
for idx in connected:
cols[items[idx]] = 1
connected = {i for i in range(N) if cols[items[i]]}
a_connected = np.array(list(connected), dtype='int')
return a_connected
cpdef _colors_or(ndarray[int64_t] left,
ndarray[int64_t] right):
cdef:
int[:] cols = array.array('i', right)
int N
N = len(left)
todo = np.ones(N)
for idx in range(N):
if not todo[idx]:
continue
connected = {idx}
prev_size = 1
for it in range(N):
connected = update_connected(right, connected,
cols,
N)
connected = update_connected(left, connected,
cols,
N)
size = len(connected)
if size == prev_size:
l_col = left[idx]
r_col = right[idx]
a_connected = np.array(list(connected),
dtype='int')
left[a_connected] = l_col
right[a_connected] = r_col
todo[a_connected] = False
break
prev_size = size
return left
def colors_or(l_left, l_right):
left = np.array(l_left, dtype='int')
right = np.array(l_right, dtype='int')
return _colors_or(left, right)