Skip to content

Commit f40f9d2

Browse files
authored
Merge pull request #175 from herwinw/is_sparse
Add is_sparse function to array.hpp
2 parents 18cd1b1 + 0a953b2 commit f40f9d2

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

jsrc/array.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ pointer_to_values(array x) -> int64_t* {
4040
return reinterpret_cast<int64_t*>(reinterpret_cast<C*>(x) + x->kchain.k);
4141
}
4242

43+
[[nodiscard]] constexpr auto
44+
is_sparse(array x) noexcept -> bool {
45+
return (AT(x) & SPARSE) != 0;
46+
}
47+
4348
// TODO: replace with `auto` concepts
4449
template <typename T>
4550
auto

jsrc/verbs/dyadic/take_drop.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ jttk(J jt, A a, A w) {
143143
r = AR(w);
144144
s = AS(w);
145145
t = AT(w);
146-
if ((t & SPARSE) != 0) return jttks(jt, a, w);
146+
if (is_sparse(w)) return jttks(jt, a, w);
147147
DO(
148148
n, if (!u[i]) {
149149
b = 1;
@@ -196,8 +196,8 @@ jttake(J jt, A a, A w) {
196196
FPREFIP;
197197
if (!(a && w)) return 0;
198198
I wt = AT(w); // wt=type of w
199-
if ((SPARSE & AT(a)) != 0) RZ(a = jtdenseit(jt, a));
200-
if (!(SPARSE & wt)) RZ(w = jtsetfv(jt, w, w));
199+
if (is_sparse(a)) RZ(a = jtdenseit(jt, a));
200+
if (!is_sparse(w)) RZ(w = jtsetfv(jt, w, w));
201201
ar = AR(a);
202202
acr = jt->ranks >> RANKTX;
203203
acr = ar < acr ? ar : acr;
@@ -252,7 +252,7 @@ jttake(J jt, A a, A w) {
252252
}
253253
}
254254
a = s;
255-
// correct if(!(ar|wf|(SPARSE&wt)|!wcr|(AFLAG(w)&(AFNJA)))){ // if there is only 1 take axis, w has no frame
255+
// correct if(!(ar|wf|is_sparse(w)|!wcr|(AFLAG(w)&(AFNJA)))){ // if there is only 1 take axis, w has no frame
256256
// and is not atomic
257257
if (!(ar | wf | ((NOUN & ~(DIRECT | RECURSIBLE)) & wt) | !wcr |
258258
(AFLAG(w) & (AFNJA)))) { // if there is only 1 take axis, w has no frame and is not atomic NJAwhy
@@ -334,7 +334,7 @@ jtdrop(J jt, A a, A w) {
334334
n = AN(a);
335335
u = AV(a); // n=#axes to drop, u->1st axis
336336
// virtual case: scalar a
337-
// correct if(!(ar|wf|(SPARSE&wt)|!wcr|(AFLAG(w)&(AFNJA)))){ // if there is only 1 take axis, w has no frame
337+
// correct if(!(ar|wf|is_sparse(w)|!wcr|(AFLAG(w)&(AFNJA)))){ // if there is only 1 take axis, w has no frame
338338
// and is not atomic
339339
if (!(ar | wf | ((NOUN & ~(DIRECT | RECURSIBLE)) & wt) | !wcr |
340340
(AFLAG(w) & (AFNJA)))) { // if there is only 1 take axis, w has no frame and is not atomic BJAwhy
@@ -439,9 +439,9 @@ jthead(J jt, A w) {
439439
return jtfrom(jtinplace, num(0), w); // could call jtfromi directly for non-sparse w
440440
}
441441
} else {
442-
return SPARSE & AT(w) ? jtirs2(jt, jfalse, jttake(jt, jtrue, w), 0L, 0L, wcr, reinterpret_cast<AF>(jtfrom))
443-
: jtrsh0(jt, w); // cell of w is empty - create a cell of fills jt->ranks is still
444-
// set for use in take. Left rank is garbage, but that's OK
442+
return is_sparse(w) ? jtirs2(jt, jfalse, jttake(jt, jtrue, w), 0L, 0L, wcr, reinterpret_cast<AF>(jtfrom))
443+
: jtrsh0(jt, w); // cell of w is empty - create a cell of fills jt->ranks is still
444+
// set for use in take. Left rank is garbage, but that's OK
445445
}
446446
// pristinity from the called verb
447447
}
@@ -457,7 +457,7 @@ jttail(J jt, A w) {
457457
return !wcr || AS(w)[wf] ? jtfrom(jtinplace, num(-1), w)
458458
: // if cells are atoms, or if the cells are nonempty arrays, result is last cell(s)
459459
// scaf should generate virtual block here for speed
460-
SPARSE & AT(w) ? jtirs2(jt, jfalse, jttake(jt, num(-1), w), 0L, 0L, wcr, reinterpret_cast<AF>(jtfrom))
461-
: jtrsh0(jt, w);
460+
is_sparse(w) ? jtirs2(jt, jfalse, jttake(jt, num(-1), w), 0L, 0L, wcr, reinterpret_cast<AF>(jtfrom))
461+
: jtrsh0(jt, w);
462462
// pristinity from other verbs
463463
}

0 commit comments

Comments
 (0)