Statistics
| Revision:

root / tmp / org.txm.statsengine.r.core.win32 / res / win32 / include / R_ext / Itermacros.h @ 2486

History | View | Annotate | Download (4.8 kB)

1
/*
2
 *  R : A Computer Language for Statistical Data Analysis
3
 *  Copyright (C) 2001-12  The R Core Team.
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU Lesser General Public License as published by
7
 *  the Free Software Foundation; either version 2.1 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Lesser General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Lesser General Public License
16
 *  along with this program; if not, a copy is available at
17
 *  http://www.r-project.org/Licenses/
18
 */
19

    
20
/*
21
  Macros to help defining vectorized functions with proper recycling
22
  and periodic interrupt checks.
23
 */
24

    
25
#ifndef  R_EXT_ITERMACROS_H_
26
#define  R_EXT_ITERMACROS_H_
27

    
28
#define LOOP_WITH_INTERRUPT_CHECK(LOOP, ncheck, n, ...) do {                \
29
        for (size_t __intr_threshold__ = ncheck;                        \
30
             TRUE;                                                        \
31
             __intr_threshold__ += ncheck) {                                \
32
            size_t __intr_end__ = n < __intr_threshold__ ?                \
33
                n : __intr_threshold__;                                        \
34
            LOOP(__intr_end__, __VA_ARGS__);                                \
35
            if (__intr_end__ == n) break;                                \
36
            else R_CheckUserInterrupt();                                \
37
        }                                                                \
38
    } while (0)
39

    
40
#define R_ITERATE_CORE(n, i, loop_body) do {        \
41
        for (; i < n; ++i) { loop_body }        \
42
    } while (0)
43

    
44
#define R_ITERATE(n, i, loop_body) do {                \
45
        i = 0;                                        \
46
        R_ITERATE_CORE(n, i, loop_body);                \
47
    } while (0)
48

    
49
#define R_ITERATE_CHECK(ncheck, n, i, loop_body) do {                        \
50
        i = 0;                                                                \
51
        LOOP_WITH_INTERRUPT_CHECK(R_ITERATE_CORE, ncheck, n, i, loop_body); \
52
    } while (0)
53

    
54
#define MOD_ITERATE_CORE(n, n1, n2, i, i1, i2, loop_body) do {        \
55
        for (; i < n;                                                        \
56
             i1 = (++i1 == n1) ? 0 : i1,                                \
57
                 i2 = (++i2 == n2) ? 0 : i2,                                \
58
                 ++i) {                                                        \
59
            loop_body                                                        \
60
                }                                                        \
61
    } while (0)
62

    
63
#define MOD_ITERATE(n, n1, n2, i, i1, i2, loop_body) do {        \
64
        i = i1 = i2 = 0;                                        \
65
        MOD_ITERATE_CORE(n, n1, n2, i, i1, i2, loop_body);        \
66
    } while (0)
67

    
68
#define MOD_ITERATE_CHECK(ncheck, n, n1, n2, i, i1, i2, loop_body) do {        \
69
        i = i1 = i2 = 0;                                                \
70
        LOOP_WITH_INTERRUPT_CHECK(MOD_ITERATE_CORE, ncheck, n,                \
71
                                  n1, n2, i, i1, i2, loop_body);        \
72
    } while (0)
73

    
74
#define MOD_ITERATE3_CORE(n, n1, n2, n3, i, i1, i2, i3, loop_body) do {        \
75
        for (; i < n;                                                        \
76
             i1 = (++i1 == n1) ? 0 : i1,                                \
77
                 i2 = (++i2 == n2) ? 0 : i2,                                \
78
                 i3 = (++i3 == n3) ? 0 : i3,                                \
79
                 ++i) {                                                        \
80
            loop_body                                                        \
81
                }                                                        \
82
    } while (0)
83

    
84
#define MOD_ITERATE3(n, n1, n2, n3, i, i1, i2, i3, loop_body) do {        \
85
        i = i1 = i2 = i3 = 0;                                                \
86
        MOD_ITERATE3_CORE(n, n1, n2, n3, i, i1, i2, i3, loop_body);        \
87
    } while (0)
88

    
89
#define MOD_ITERATE3_CHECK(ncheck, n, n1, n2, n3, i, i1, i2, i3, loop_body) \
90
    do {                                                                \
91
        i = i1 = i2 = i3 = 0;                                                \
92
        LOOP_WITH_INTERRUPT_CHECK(MOD_ITERATE3_CORE, ncheck, n,                \
93
                                  n1, n2, n3, i, i1, i2, i3, loop_body); \
94
    } while (0)
95

    
96
#define MOD_ITERATE4_CORE(n, n1, n2, n3, n4, i, i1, i2, i3, i4, loop_body) \
97
    do {                                                                \
98
        for (; i < n;                                                        \
99
             i1 = (++i1 == n1) ? 0 : i1,                                \
100
                 i2 = (++i2 == n2) ? 0 : i2,                                \
101
                 i3 = (++i3 == n3) ? 0 : i3,                                \
102
                 i4 = (++i4 == n4) ? 0 : i4,                                \
103
                 ++i) {                                                        \
104
            loop_body                                                        \
105
                }                                                        \
106
    } while (0)
107

    
108
#define MOD_ITERATE4(n, n1, n2, n3, n4, i, i1, i2, i3, i4, loop_body) do { \
109
        i = i1 = i2 = i3 = i4 = 0;                                        \
110
        MOD_ITERATE4_CORE(n, n1, n2, n3, n4, i, i1, i2, i3, i4, loop_body); \
111
    } while (0)
112

    
113
#define MOD_ITERATE4_CHECK(ncheck, n, n1, n2, n3, n4, i, i1, i2, i3, i4, \
114
                           loop_body)                                        \
115
    do {                                                                \
116
        i = i1 = i2 = i3 = i4 = 0;                                        \
117
        LOOP_WITH_INTERRUPT_CHECK(MOD_ITERATE4_CORE, ncheck, n,        \
118
                                  n1, n2, n3, n4,                        \
119
                                  i, i1, i2, i3, i4, loop_body);        \
120
    } while (0)
121

    
122
#define MOD_ITERATE5_CORE(n, n1, n2, n3, n4, n5, i, i1, i2, i3, i4, i5, \
123
                          loop_body)                                        \
124
    do {                                                                \
125
        for (; i < n;                                                        \
126
             i1 = (++i1 == n1) ? 0 : i1,                                \
127
                 i2 = (++i2 == n2) ? 0 : i2,                                \
128
                 i3 = (++i3 == n3) ? 0 : i3,                                \
129
                 i4 = (++i4 == n4) ? 0 : i4,                                \
130
                 i5 = (++i5 == n5) ? 0 : i5,                                \
131
                 ++i) {                                                        \
132
            loop_body                                                        \
133
                }                                                        \
134
    } while (0)
135

    
136
#define MOD_ITERATE5(n, n1, n2, n3, n4, n5, i, i1, i2, i3, i4, i5, loop_body) \
137
    do {                                                                \
138
        i = i1 = i2 = i3 = i4 = i5 = 0;                                        \
139
        MOD_ITERATE5_CORE(n, n1, n2, n3, n4, n5, i, i1, i2, i3, i4, i5, \
140
                          loop_body);                                        \
141
    } while (0)
142

    
143
#define MOD_ITERATE5_CHECK(ncheck, n, n1, n2, n3, n4, n5, \
144
                           i, i1, i2, i3, i4, i5,                        \
145
                           loop_body)                                        \
146
    do {                                                                \
147
        i = i1 = i2 = i3 = i4 = i5 = 0;                                        \
148
        LOOP_WITH_INTERRUPT_CHECK(MOD_ITERATE5_CORE, ncheck, n,        \
149
                                  n1, n2, n3, n4, n5,                        \
150
                                  i, i1, i2, i3, i4, i5, loop_body);        \
151
    } while (0)
152

    
153
#endif /* R_EXT_ITERMACROS_H_ */