Skip to content

Commit 9238cd0

Browse files
committed
Unify algorithm instantiation
1 parent 465057d commit 9238cd0

File tree

2 files changed

+87
-92
lines changed

2 files changed

+87
-92
lines changed

src/flc_algorithm.i

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,24 @@
99
%import "flc.i"
1010
%flc_add_header
1111

12-
%define %flc_cmp_algorithm(RETURN_TYPE, CONST, FUNCNAME)
12+
%{
13+
#include <algorithm>
14+
#include <functional>
15+
#include <numeric>
16+
%}
17+
18+
%define %flc_cmp_algorithm(RETURN_TYPE, FUNCNAME, ARGS, CALL)
1319

1420
%inline {
1521
// Operate using default "less than"
1622
template<class T>
17-
static RETURN_TYPE FUNCNAME(CONST T *DATA, size_t DATASIZE) {
18-
return std::FUNCNAME(DATA, DATA + DATASIZE);
23+
static RETURN_TYPE FUNCNAME(ARGS) {
24+
return FUNCNAME ## _impl(CALL, std::less<T>());
1925
}
2026
// Operate using user-provided function pointer
2127
template<class T>
22-
static RETURN_TYPE FUNCNAME ## _cmp(CONST T *DATA, size_t DATASIZE, bool (*cmp)(T, T)) {
23-
return std::FUNCNAME(DATA, DATA + DATASIZE, cmp);
28+
static RETURN_TYPE FUNCNAME ## _cmp(ARGS, bool (*cmp)(T, T)) {
29+
return FUNCNAME ## _impl(CALL, cmp);
2430
}
2531
}
2632

@@ -33,22 +39,18 @@ static RETURN_TYPE FUNCNAME ## _cmp(CONST T *DATA, size_t DATASIZE, bool (*cmp)(
3339
* Sorting
3440
******************************/
3541

36-
%{
37-
#include <algorithm>
38-
%}
39-
40-
%flc_cmp_algorithm(void, , sort)
41-
%flc_cmp_algorithm(bool, const, is_sorted)
42-
43-
/******************************
44-
* Finding sorted indices
45-
******************************/
46-
47-
%apply (SWIGTYPE *DATA, size_t SIZE) { (int *idx, size_t idx_size) };
42+
%apply (SWIGTYPE *DATA, size_t SIZE) { (int *IDX, size_t IDXSIZE) };
4843

4944
%{
50-
#include <numeric>
51-
#include <functional>
45+
template<class T, class Compare>
46+
static void sort_impl(T *data, size_t size, Compare cmp) {
47+
return std::sort(data, data + size, cmp);
48+
}
49+
50+
template<class T, class Compare>
51+
static bool is_sorted_impl(const T *data, size_t size, Compare cmp) {
52+
return std::is_sorted(data, data + size, cmp);
53+
}
5254

5355
template<class T, class Compare>
5456
static void argsort_impl(const T *data, size_t size,
@@ -67,24 +69,16 @@ static void argsort_impl(const T *data, size_t size,
6769
// Let the standard library do all the hard work!
6870
std::sort(index, index + size, int_sort_cmp);
6971
}
70-
%}
7172

72-
%inline %{
73-
template<class T>
74-
static void argsort(const T *DATA, size_t DATASIZE, int *idx, size_t idx_size) {
75-
return argsort_impl(DATA, DATASIZE, idx, idx_size, std::less<T>());
76-
}
77-
78-
template<class T>
79-
static void argsort_cmp(const T *DATA, size_t DATASIZE,
80-
int *idx, size_t idx_size,
81-
bool (*cmp)(T, T)) {
82-
return argsort_impl(DATA, DATASIZE, idx, idx_size, cmp);
83-
}
8473
%}
8574

86-
%flc_template_numeric(argsort, argsort)
87-
%flc_template_numeric(argsort_cmp, argsort)
75+
%flc_cmp_algorithm(void, sort, %arg(T *DATA, size_t DATASIZE),
76+
%arg(DATA, DATASIZE))
77+
%flc_cmp_algorithm(bool, is_sorted, %arg(const T *DATA, size_t DATASIZE),
78+
%arg(DATA, DATASIZE))
79+
%flc_cmp_algorithm(void, argsort, %arg(const T *DATA, size_t DATASIZE,
80+
int *IDX, size_t IDXSIZE),
81+
%arg(DATA, DATASIZE, IDX, IDXSIZE))
8882

8983
/******************************
9084
* Searching
@@ -102,21 +96,9 @@ static int binary_search_impl(const T *data, size_t size, T value, Compare cmp)
10296
}
10397
%}
10498

105-
%inline %{
106-
template<class T>
107-
static int binary_search(const T *DATA, size_t DATASIZE, T value) {
108-
return binary_search_impl(DATA, DATASIZE, value, std::less<T>());
109-
}
110-
111-
template<class T>
112-
static int binary_search_cmp(const T *DATA, size_t DATASIZE, T value,
113-
bool (*cmp)(T, T)) {
114-
return binary_search_impl(DATA, DATASIZE, value, cmp);
115-
}
116-
%}
117-
118-
%flc_template_numeric(binary_search, binary_search)
119-
%flc_template_numeric(binary_search_cmp, binary_search)
99+
%flc_cmp_algorithm(int, binary_search, %arg(const T *DATA, size_t DATASIZE,
100+
T value),
101+
%arg(DATA, DATASIZE, value))
120102

121103
/******************************
122104
* Reordering

src/generated/flc_algorithmFORTRAN_wrap.cxx

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -236,17 +236,49 @@ template <typename T> T SwigValueInit() {
236236

237237

238238
#include <algorithm>
239+
#include <functional>
240+
#include <numeric>
241+
242+
243+
template<class T, class Compare>
244+
static void sort_impl(T *data, size_t size, Compare cmp) {
245+
return std::sort(data, data + size, cmp);
246+
}
247+
248+
template<class T, class Compare>
249+
static bool is_sorted_impl(const T *data, size_t size, Compare cmp) {
250+
return std::is_sorted(data, data + size, cmp);
251+
}
252+
253+
template<class T, class Compare>
254+
static void argsort_impl(const T *data, size_t size,
255+
int *index, size_t index_size,
256+
Compare cmp) {
257+
// Fill invalid indices with zero
258+
if (size < index_size) {
259+
std::fill(index + size, index + index_size, 0);
260+
}
261+
size = std::min(size, index_size);
262+
// Fill the indices with 1 through size
263+
std::iota(index, index + size, 1);
264+
// Define a comparator that accesses the original data
265+
auto int_sort_cmp = [cmp, data](int left, int right)
266+
{ return cmp(data[left - 1], data[right - 1]); };
267+
// Let the standard library do all the hard work!
268+
std::sort(index, index + size, int_sort_cmp);
269+
}
270+
239271

240272

241273
// Operate using default "less than"
242274
template<class T>
243-
static void sort( T *DATA, size_t DATASIZE) {
244-
return std::sort(DATA, DATA + DATASIZE);
275+
static void sort(T *DATA,size_t DATASIZE) {
276+
return sort_impl(DATA,DATASIZE, std::less<T>());
245277
}
246278
// Operate using user-provided function pointer
247279
template<class T>
248-
static void sort_cmp( T *DATA, size_t DATASIZE, bool (*cmp)(T, T)) {
249-
return std::sort(DATA, DATA + DATASIZE, cmp);
280+
static void sort_cmp(T *DATA,size_t DATASIZE, bool (*cmp)(T, T)) {
281+
return sort_impl(DATA,DATASIZE, cmp);
250282
}
251283

252284

@@ -277,48 +309,27 @@ SWIGINTERN SwigArrayWrapper SwigArrayWrapper_uninitialized() {
277309

278310
// Operate using default "less than"
279311
template<class T>
280-
static bool is_sorted(const T *DATA, size_t DATASIZE) {
281-
return std::is_sorted(DATA, DATA + DATASIZE);
312+
static bool is_sorted(const T *DATA,size_t DATASIZE) {
313+
return is_sorted_impl(DATA,DATASIZE, std::less<T>());
282314
}
283315
// Operate using user-provided function pointer
284316
template<class T>
285-
static bool is_sorted_cmp(const T *DATA, size_t DATASIZE, bool (*cmp)(T, T)) {
286-
return std::is_sorted(DATA, DATA + DATASIZE, cmp);
287-
}
288-
289-
290-
#include <numeric>
291-
#include <functional>
292-
293-
template<class T, class Compare>
294-
static void argsort_impl(const T *data, size_t size,
295-
int *index, size_t index_size,
296-
Compare cmp) {
297-
// Fill invalid indices with zero
298-
if (size < index_size) {
299-
std::fill(index + size, index + index_size, 0);
300-
}
301-
size = std::min(size, index_size);
302-
// Fill the indices with 1 through size
303-
std::iota(index, index + size, 1);
304-
// Define a comparator that accesses the original data
305-
auto int_sort_cmp = [cmp, data](int left, int right)
306-
{ return cmp(data[left - 1], data[right - 1]); };
307-
// Let the standard library do all the hard work!
308-
std::sort(index, index + size, int_sort_cmp);
317+
static bool is_sorted_cmp(const T *DATA,size_t DATASIZE, bool (*cmp)(T, T)) {
318+
return is_sorted_impl(DATA,DATASIZE, cmp);
309319
}
310320

311321

322+
// Operate using default "less than"
312323
template<class T>
313-
static void argsort(const T *DATA, size_t DATASIZE, int *idx, size_t idx_size) {
314-
return argsort_impl(DATA, DATASIZE, idx, idx_size, std::less<T>());
324+
static void argsort(const T *DATA,size_t DATASIZE,int *IDX,size_t IDXSIZE
325+
) {
326+
return argsort_impl(DATA,DATASIZE,IDX,IDXSIZE, std::less<T>());
315327
}
316-
328+
// Operate using user-provided function pointer
317329
template<class T>
318-
static void argsort_cmp(const T *DATA, size_t DATASIZE,
319-
int *idx, size_t idx_size,
320-
bool (*cmp)(T, T)) {
321-
return argsort_impl(DATA, DATASIZE, idx, idx_size, cmp);
330+
static void argsort_cmp(const T *DATA,size_t DATASIZE,int *IDX,size_t IDXSIZE
331+
, bool (*cmp)(T, T)) {
332+
return argsort_impl(DATA,DATASIZE,IDX,IDXSIZE, cmp);
322333
}
323334

324335

@@ -333,15 +344,17 @@ static int binary_search_impl(const T *data, size_t size, T value, Compare cmp)
333344
}
334345

335346

347+
// Operate using default "less than"
336348
template<class T>
337-
static int binary_search(const T *DATA, size_t DATASIZE, T value) {
338-
return binary_search_impl(DATA, DATASIZE, value, std::less<T>());
349+
static int binary_search(const T *DATA,size_t DATASIZE,T value
350+
) {
351+
return binary_search_impl(DATA,DATASIZE,value, std::less<T>());
339352
}
340-
353+
// Operate using user-provided function pointer
341354
template<class T>
342-
static int binary_search_cmp(const T *DATA, size_t DATASIZE, T value,
343-
bool (*cmp)(T, T)) {
344-
return binary_search_impl(DATA, DATASIZE, value, cmp);
355+
static int binary_search_cmp(const T *DATA,size_t DATASIZE,T value
356+
, bool (*cmp)(T, T)) {
357+
return binary_search_impl(DATA,DATASIZE,value, cmp);
345358
}
346359

347360

0 commit comments

Comments
 (0)