Skip to content

Commit 1860b8d

Browse files
committed
Futher refactoring, I+LE
1 parent 9a8a452 commit 1860b8d

1 file changed

Lines changed: 30 additions & 16 deletions

File tree

jsrc/verbs/dyadic/take_drop.cpp

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,11 @@ jttk0(J jt, B b, A a, A w) {
4343
return z;
4444
}
4545

46-
static A
47-
jttks(J jt, A a, A w) {
46+
static array
47+
jttks(J jt, array a, array w) { // take_sparse
4848
PROLOG(0092);
49-
A a1, q, x, y, z;
50-
B c;
51-
I an, m, r, *s, *u, *v;
49+
array x, y, z;
50+
I an, r, *s, *u, *v;
5251
P *wp, *zp;
5352
an = AN(a);
5453
u = AV(a);
@@ -58,23 +57,26 @@ jttks(J jt, A a, A w) {
5857
v = AS(z);
5958
DO(an, v[i] = ABS(u[i]););
6059
zp = PAV(z);
61-
wp = PAV(w);
60+
wp = PAV(w); // pointer to array values
61+
6262
if (an <= r) {
6363
RZ(a = vec(INT, r, s));
6464
MCISH(AV(a), u, an);
6565
} // vec is not virtual
66-
a1 = SPA(wp, a);
67-
RZ(q = jtpaxis(jt, r, a1));
68-
m = AN(a1);
66+
67+
auto [m, q] = [&] {
68+
array const a1 = SPA(wp, a);
69+
return std::pair{AN(a1), jtpaxis(jt, r, a1)};
70+
} ();
71+
6972
RZ(a = jtfrom(jt, q, a));
7073
u = AV(a);
7174
RZ(y = jtfrom(jt, q, shape(jt, w)));
7275
s = AV(y);
7376

74-
auto const b = std::mismatch(u + m, u + r, s + m).first != u + r;
77+
// TODO: rename b when we figure out what it is doing
78+
auto const b = std::mismatch(u + m, u + r, s + m).first != u + r;
7579

76-
c = 0;
77-
DO(m, if ((c = (u[i] != s[i]))) break;);
7880
if (b) {
7981
jt->fill = SPA(wp, e);
8082
x = irs2(vec(INT, r - m, m + u), SPA(wp, x), 0L, 1L, -1L, reinterpret_cast<AF>(jttake));
@@ -83,7 +85,9 @@ jttks(J jt, A a, A w) {
8385
} // fill cannot be virtual
8486
else
8587
x = SPA(wp, x);
86-
if (c) {
88+
89+
// TODO: rename b when we figure out what it is doing
90+
if (auto const c = std::mismatch(u, u + m, s).first != u + m; c) {
8791
A j;
8892
C *xv, *yv;
8993
I d, i, *iv, *jv, k, n, t;
@@ -99,12 +103,22 @@ jttks(J jt, A a, A w) {
99103
yv = CAV(y);
100104
xv = CAV(x);
101105
for (i = 0; i < n; ++i) {
102-
c = 0;
103-
DO(m, t = u[i]; if ((c = (0 > t ? iv[i] < t + s[i] : iv[i] >= t))) break;);
104-
if (!c) {
106+
107+
// this is std::mismatch3 (or std::zip_find3)
108+
bool cc = 0;
109+
for (int64_t i = 0; i < m; ++i) {
110+
t = u[i];
111+
if (0 > t ? iv[i] < t + s[i] : iv[i] >= t) {
112+
cc = true;
113+
break;
114+
}
115+
}
116+
117+
if (!cc) {
105118
++d;
106119
memcpy(yv, xv, k);
107120
yv += k;
121+
// TODO: use algorithm created above
108122
DO(m, t = u[i]; *jv++ = 0 > t ? iv[i] - (t + s[i]) : iv[i];);
109123
}
110124
iv += m;

0 commit comments

Comments
 (0)