Skip to content

Commit 57f2261

Browse files
authored
Merge pull request #187 from codereport/dev
Adding `algorithm.hpp` with `zip_find`, `zip_found` & `is_mismatched`
2 parents e0e1c36 + 3c6f076 commit 57f2261

File tree

4 files changed

+55
-14
lines changed

4 files changed

+55
-14
lines changed

jsrc/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ target_sources(j PRIVATE
1616
adverbs/ar.c
1717
adverbs/as.c
1818
adverbs/au.c
19-
array.hpp
2019
conjunctions/c.c
2120
conjunctions/ca.c
2221
conjunctions/cc.c

jsrc/algorithm.hpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
#include <tuple>
3+
#include <algorithm>
4+
5+
namespace algo {
6+
7+
/**
8+
* @brief Variadic version of `std::mismatch`
9+
*/
10+
template <typename P, typename I, typename... Is>
11+
auto zip_find(P pred, I f, I l, Is... fs) {
12+
while (f != l) {
13+
if (pred(*f, *fs...)) break;
14+
++f, ((void)++fs,...);
15+
}
16+
return std::tuple{f, fs...};
17+
}
18+
19+
/**
20+
* @brief Predicate version of `algo::zip_found`
21+
*/
22+
template <typename P, typename I, typename... Is>
23+
auto zip_found(P pred, I f, I l, Is... fs) {
24+
auto const t = zip_find(pred, f, l, fs...);
25+
return std::get<0>(t) != l;
26+
}
27+
28+
/**
29+
* @brief Predicate version of `std::mismatch`
30+
*/
31+
template <typename I, typename I2>
32+
auto is_mismatched(I f, I l, I2 f2) {
33+
return std::mismatch(f, l, f2).first != l;
34+
}
35+
36+
}

jsrc/verbs/dyadic/take_drop.cpp

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11

22
#include <algorithm>
3+
#include <functional>
34
#include <iterator>
45

6+
#include "algorithm.hpp"
57
#include "array.hpp"
68

79
/** @file */
@@ -75,7 +77,7 @@ jttks(J jt, array a, array w) { // take_sparse
7577
s = AV(y);
7678

7779
// 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;
80+
auto const b = algo::is_mismatched(u + m, u + r, s + m);
7981

8082
if (b) {
8183
jt->fill = SPA(wp, e);
@@ -87,7 +89,7 @@ jttks(J jt, array a, array w) { // take_sparse
8789
x = SPA(wp, x);
8890

8991
// TODO: rename c when we figure out what it is doing
90-
if (auto const c = std::mismatch(u, u + m, s).first != u + m; c) {
92+
if (auto const c = algo::is_mismatched(u, u + m, s); c) {
9193
A j;
9294
C *xv, *yv;
9395
I d, i, *iv, *jv, k, n, t;
@@ -104,22 +106,19 @@ jttks(J jt, array a, array w) { // take_sparse
104106
xv = CAV(x);
105107
for (i = 0; i < n; ++i) {
106108

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-
}
109+
auto const cc = algo::zip_found(
110+
[](auto a, auto b, auto c) { return 0 > a ? c < a + b : c >= a; },
111+
u, u + m, s, iv);
116112

117113
if (!cc) {
118114
++d;
119115
memcpy(yv, xv, k);
120116
yv += k;
121-
// TODO: use algorithm created above
122-
DO(m, t = u[i]; *jv++ = 0 > t ? iv[i] - (t + s[i]) : iv[i];);
117+
// TODO: create variadic `algo::transform`
118+
for (int64_t i = 0; i < m; ++i) {
119+
t = u[i];
120+
*jv++ = 0 > u[i] ? iv[i] - (u[i] + s[i]) : iv[i];
121+
}
123122
}
124123
iv += m;
125124
xv += k;

learning/LIVESTREAM_LESSONS.md

+7
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,10 @@ Prelude> let innerProduct = blackbird sum (zipWith (*))
127127
* IRS = Integral/Integrated Rank Support (optimizations for certain verbs - see [this paper](https://www.jsoftware.com/papers/rank.htm))
128128
* Discrovered [Try J](http://tryj.freeddns.org/) site set up by Nathan
129129
* Used our first C++ Standard Algorithm (`std::mismatch` x2)
130+
131+
### Livestream #9 2021-02-24
132+
133+
* Learned about J's `trace` function from Bob's video
134+
* Learned this fancy incantation `((void)++fs,...)`
135+
* Learned can't be in a deduced context then variadic template is not the last template
136+
* Added `algorithm.hpp` with `zip_find`, `zip_found` and `is_mismatched`

0 commit comments

Comments
 (0)