File: algorithm.h

package info (click to toggle)
rcpp 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,480 kB
  • sloc: cpp: 27,436; ansic: 7,778; sh: 53; makefile: 2
file content (462 lines) | stat: -rw-r--r-- 16,157 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
//
// algorithm.h: Rcpp R/C++ interface class library -- data frames
//
// Copyright (C) 2016 - 2024  Daniel C. Dillon
// Copyright (C) 2025         Daniel C. Dillon and IƱaki Ucar
//
// This file is part of Rcpp.
//
// Rcpp is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Rcpp is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.

#ifndef Rcpp__Algorithm_h
#define Rcpp__Algorithm_h

namespace Rcpp {
namespace algorithm {

namespace helpers {
    typedef struct {char a[1];} CTYPE_CHAR;
    typedef struct {char a[2];} CTYPE_SHORT;
    typedef struct {char a[3];} CTYPE_INT;
    typedef struct {char a[4];} CTYPE_LONG;
    typedef struct {char a[5];} CTYPE_LONG_LONG;
    typedef struct {char a[6];} CTYPE_FLOAT;
    typedef struct {char a[7];} CTYPE_DOUBLE;
    typedef struct {char a[8];} CTYPE_LONG_DOUBLE;
    typedef struct {char a[9];} CTYPE_STRING;
    typedef struct {char a[10];} CTYPE_UNSIGNED_CHAR;
    typedef struct {char a[11];} CTYPE_UNSIGNED_SHORT;
    typedef struct {char a[12];} CTYPE_UNSIGNED_INT;
    typedef struct {char a[13];} CTYPE_UNSIGNED_LONG;
    typedef struct {char a[14];} CTYPE_UNSIGNED_LONG_LONG;
    typedef struct {char a[128];} CTYPE_UNKNOWN;

    template< std::size_t I >
    struct ctype_helper { static const bool value = false; };

    template<>
    struct ctype_helper< sizeof(CTYPE_CHAR) > { typedef char type; static const bool value = true; };

    template<>
    struct ctype_helper< sizeof(CTYPE_SHORT) > { typedef short type; static const bool value = true; };

    template<>
    struct ctype_helper< sizeof(CTYPE_INT) > { typedef int type; static const bool value = true; };

    template<>
    struct ctype_helper< sizeof(CTYPE_LONG) > { typedef long type; static const bool value = true; };

    template<>
    struct ctype_helper< sizeof(CTYPE_LONG_LONG) > { typedef rcpp_long_long_type type; static const bool value = true; };

    template<>
    struct ctype_helper< sizeof(CTYPE_FLOAT) > { typedef float type; static const bool value = true; };

    template<>
    struct ctype_helper< sizeof(CTYPE_DOUBLE) > { typedef double type; static const bool value = true; };

    template<>
    struct ctype_helper< sizeof(CTYPE_LONG_DOUBLE) > { typedef long double type; static const bool value = true; };

    template<>
    struct ctype_helper< sizeof(CTYPE_STRING) > { typedef std::string type; static const bool value = true; };

    template<>
    struct ctype_helper< sizeof(CTYPE_UNSIGNED_CHAR) > { typedef unsigned char type; static const bool value = true; };

    template<>
    struct ctype_helper< sizeof(CTYPE_UNSIGNED_SHORT) > { typedef unsigned short type; static const bool value = true; };

    template<>
    struct ctype_helper< sizeof(CTYPE_UNSIGNED_INT) > { typedef unsigned int type; static const bool value = true; };

    template<>
    struct ctype_helper< sizeof(CTYPE_UNSIGNED_LONG) > { typedef unsigned long type; static const bool value = true; };

    template<>
    struct ctype_helper< sizeof(CTYPE_UNSIGNED_LONG_LONG) > { typedef rcpp_ulong_long_type type; static const bool value = true; };

    template< typename T >
    struct ctype
    {
        static CTYPE_CHAR test(const char &);
        static CTYPE_SHORT test(const short &);
        static CTYPE_INT test(const int &);
        static CTYPE_LONG test(const long &);
        static CTYPE_LONG_LONG test(const rcpp_long_long_type &);
        static CTYPE_FLOAT test(const float &);
        static CTYPE_DOUBLE test(const double &);
        static CTYPE_LONG_DOUBLE test(const long double &);
        static CTYPE_STRING test(const std::string &);
        static CTYPE_UNSIGNED_CHAR test(const unsigned char &);
        static CTYPE_UNSIGNED_SHORT test(const unsigned short &);
        static CTYPE_UNSIGNED_INT test(const unsigned int &);
        static CTYPE_UNSIGNED_LONG test(const unsigned long &);
        static CTYPE_UNSIGNED_LONG_LONG test(const rcpp_ulong_long_type &);
        static CTYPE_UNKNOWN test(...);

        static T make();

        typedef typename ctype_helper< sizeof(test(make())) >::type type;
    };

    template< typename T >
    struct decays_to_ctype
    {
        static CTYPE_CHAR test(const char &);
        static CTYPE_SHORT test(const short &);
        static CTYPE_INT test(const int &);
        static CTYPE_LONG test(const long &);
        static CTYPE_LONG_LONG test(const rcpp_long_long_type &);
        static CTYPE_FLOAT test(const float &);
        static CTYPE_DOUBLE test(const double &);
        static CTYPE_LONG_DOUBLE test(const long double &);
        static CTYPE_STRING test(const std::string &);
        static CTYPE_UNSIGNED_CHAR test(const unsigned char &);
        static CTYPE_UNSIGNED_SHORT test(const unsigned short &);
        static CTYPE_UNSIGNED_INT test(const unsigned int &);
        static CTYPE_UNSIGNED_LONG test(const unsigned long &);
        static CTYPE_UNSIGNED_LONG_LONG test(const rcpp_ulong_long_type &);
        static CTYPE_UNKNOWN test(...);

        static T make();

        static const bool value = ctype_helper< sizeof(test(make())) >::value;
    };

    template< typename T >
    struct rtype_helper {
    };

    template<>
    struct rtype_helper< double > {
        typedef double type;
        static constexpr int RTYPE = REALSXP;
        static inline double NA() { return NA_REAL; }
        static inline constexpr double ZERO() { return 0.0; }
        static inline constexpr double ONE() { return 1.0; }
    };

    template<>
    struct rtype_helper< int > {
        typedef int type;
        static constexpr int RTYPE = INTSXP;
        static inline int NA() { return NA_INTEGER; }
        static inline constexpr int ZERO() { return 0; }
        static inline constexpr int ONE() { return 1; }
    };

    template< typename T >
    struct rtype {
        typedef typename rtype_helper< typename ctype< T >::type >::type type;
        typedef rtype_helper< typename ctype< T >::type > helper_type;
        static constexpr int RTYPE = helper_type::RTYPE;
        static inline T NA() { return helper_type::NA(); }
        static inline constexpr T ZERO() { return helper_type::ZERO(); }
        static inline constexpr T ONE() { return helper_type::ONE(); }
    };

    struct log {
        template< typename T >
        inline double operator()(T val) {
            if (!Vector< rtype< T >::RTYPE >::is_na(val)) {
                return std::log(val);
            }

            return rtype< double >::NA();
        }
    };

    struct exp {
        template< typename T >
        inline double operator()(T val) {
            if (!Vector< rtype< T >::RTYPE >::is_na(val)) {
                return std::exp(val);
            }

            return rtype< double >::NA();
        }
    };

    struct sqrt {
        template< typename T >
        inline double operator()(T val) {
            if (!Vector< rtype< T >::RTYPE >::is_na(val)) {
                return std::sqrt(val);
            }

            return rtype< double >::NA();
        }
    };
} // namespace helpers

template< typename InputIterator >
typename traits::enable_if< helpers::decays_to_ctype< typename std::iterator_traits< InputIterator >::value_type >::value,
    typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type
        sum(InputIterator begin, InputIterator end) {

    typedef typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type value_type;
    typedef typename helpers::rtype< value_type > rtype;

    if (begin != end) {
         value_type start = rtype::ZERO();

        while (begin != end) {
            if (!Vector< rtype::RTYPE >::is_na(*begin)) {
                start += *begin++;
            } else {
                return rtype::NA();
            }
        }

        return start;
    }

    return rtype::ZERO();
}

template< typename InputIterator >
typename traits::enable_if< helpers::decays_to_ctype< typename std::iterator_traits< InputIterator >::value_type >::value,
    typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type
        sum_nona(InputIterator begin, InputIterator end) {

    typedef typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type value_type;
    typedef typename helpers::rtype< value_type > rtype;

    if (begin != end) {
         value_type start = rtype::ZERO();

        while (begin != end) {
            start += *begin++;
        }

        return start;
    }

    return rtype::ZERO();
}

template< typename InputIterator >
typename traits::enable_if< helpers::decays_to_ctype< typename std::iterator_traits< InputIterator >::value_type >::value,
    typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type
        prod(InputIterator begin, InputIterator end) {

    typedef typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type value_type;
    typedef typename helpers::rtype< value_type > rtype;

    if (begin != end) {
        value_type start = rtype::ONE();

        while (begin != end) {
            if (!Vector< rtype::RTYPE >::is_na(*begin)) {
                start *= *begin++;
            } else {
                return rtype::NA();
            }
        }

        return start;
    }

    return rtype::ONE();
}

template< typename InputIterator >
typename traits::enable_if< helpers::decays_to_ctype< typename std::iterator_traits< InputIterator >::value_type >::value,
    typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type
        prod_nona(InputIterator begin, InputIterator end) {

    typedef typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type value_type;
    typedef typename helpers::rtype< value_type > rtype;

    if (begin != end) {
        value_type start = rtype::ONE();

        while (begin != end) {
            start *= *begin++;
        }

        return start;
    }

    return rtype::ONE();
}

template< typename InputIterator >
typename traits::enable_if< helpers::decays_to_ctype< typename std::iterator_traits< InputIterator >::value_type >::value,
    typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type
        max(InputIterator begin, InputIterator end) {

    typedef typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type value_type;
    typedef typename helpers::rtype< value_type > rtype;

    if (begin != end) {
        value_type max = *begin;

	while (begin != end) {
            if (!Vector< rtype::RTYPE >::is_na(*begin)) {
                max = std::max(max, *begin++);
            } else {
                return rtype::NA();
            }
        }

        return max;
    }

    return std::numeric_limits< typename rtype::type >::infinity() * -rtype::ONE();
}

template< typename InputIterator >
typename traits::enable_if< helpers::decays_to_ctype< typename std::iterator_traits< InputIterator >::value_type >::value,
    typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type
        max_nona(InputIterator begin, InputIterator end) {

    typedef typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type value_type;
    typedef typename helpers::rtype< value_type > rtype;

    if (begin != end) {
        value_type max = *begin;

	while (begin != end) {
            max = std::max(max, *begin++);
        }

        return max;
    }

    return std::numeric_limits< typename rtype::type >::infinity() * -rtype::ONE();
}

template< typename InputIterator >
typename traits::enable_if< helpers::decays_to_ctype< typename std::iterator_traits< InputIterator >::value_type >::value,
    typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type
        min(InputIterator begin, InputIterator end) {

    typedef typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type value_type;
    typedef typename helpers::rtype< value_type > rtype;

    if (begin != end) {
        value_type min = *begin;

	while (begin != end) {
            if (!Vector< rtype::RTYPE >::is_na(*begin)) {
                min = std::min(min, *begin++);
            } else {
                return rtype::NA();
            }
        }

        return min;
    }

    return std::numeric_limits< typename rtype::type >::infinity();
}

template< typename InputIterator >
typename traits::enable_if< helpers::decays_to_ctype< typename std::iterator_traits< InputIterator >::value_type >::value,
    typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type >::type
        min_nona(InputIterator begin, InputIterator end) {

    typedef typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type value_type;
    typedef typename helpers::rtype< value_type > rtype;

    if (begin != end) {
        value_type min = *begin;

	while (begin != end) {
            min = std::min(min, *begin++);
        }

        return min;
    }

    return std::numeric_limits< typename rtype::type >::infinity();
}

// for REALSXP
template< typename InputIterator >
typename traits::enable_if< helpers::decays_to_ctype< typename std::iterator_traits< InputIterator >::value_type >::value
    && traits::same_type< typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type, double >::value, double >::type
        mean(InputIterator begin, InputIterator end)
{
    if (begin != end)
    {
        std::size_t n = end - begin;
        long double s = std::accumulate(begin, end, 0.0L);
        s /= n;

        if (R_FINITE((double) s)) {
            long double t = 0.0L;
            while (begin != end) {
                t += *begin++ - s;
            }

            s += t / n;
        }

        return (double) s;
    }

    return helpers::rtype< double >::NA();
}

// for LGLSXP and INTSXP
template< typename InputIterator >
typename traits::enable_if< helpers::decays_to_ctype< typename std::iterator_traits< InputIterator >::value_type >::value
    && traits::same_type< typename helpers::ctype< typename std::iterator_traits< InputIterator >::value_type >::type, int >::value, double >::type
        mean(InputIterator begin, InputIterator end)
{
    if (begin != end)
    {
        std::size_t n = end - begin;
        long double s = std::accumulate(begin, end, 0.0L);
        s /= n;

        if (R_FINITE((double) s)) {
            long double t = 0.0L;
            while (begin != end) {
                if (*begin == helpers::rtype< int >::NA()) return helpers::rtype< double >::NA();
                t += *begin++ - s;
            }

            s += t / n;
        }

        return (double) s;
    }

    return helpers::rtype< double >::NA();
}

template< typename InputIterator, typename OutputIterator >
void log(InputIterator begin, InputIterator end, OutputIterator out) {
    std::transform(begin, end, out, helpers::log());
}

template< typename InputIterator, typename OutputIterator >
void exp(InputIterator begin, InputIterator end, OutputIterator out) {
    std::transform(begin, end, out, helpers::exp());
}

template< typename InputIterator, typename OutputIterator >
void sqrt(InputIterator begin, InputIterator end, OutputIterator out) {
    std::transform(begin, end, out, helpers::sqrt());
}

} // namespace algorithm
} // namespace Rcpp

#endif