@@ -236,17 +236,49 @@ template <typename T> T SwigValueInit() {
236
236
237
237
238
238
#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
+
239
271
240
272
241
273
// Operate using default "less than"
242
274
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>() );
245
277
}
246
278
// Operate using user-provided function pointer
247
279
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);
250
282
}
251
283
252
284
@@ -277,48 +309,27 @@ SWIGINTERN SwigArrayWrapper SwigArrayWrapper_uninitialized() {
277
309
278
310
// Operate using default "less than"
279
311
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>() );
282
314
}
283
315
// Operate using user-provided function pointer
284
316
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);
309
319
}
310
320
311
321
322
+ // Operate using default "less than"
312
323
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>());
315
327
}
316
-
328
+ // Operate using user-provided function pointer
317
329
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);
322
333
}
323
334
324
335
@@ -333,15 +344,17 @@ static int binary_search_impl(const T *data, size_t size, T value, Compare cmp)
333
344
}
334
345
335
346
347
+ // Operate using default "less than"
336
348
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>());
339
352
}
340
-
353
+ // Operate using user-provided function pointer
341
354
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);
345
358
}
346
359
347
360
0 commit comments