include/tkr/tkr_set.h

filter: Statementstmt 91.2% 259 / 284 Branchbran 80.1% 170 / 212 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.4% 429 / 496 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/tkr/tkr_walk_sv.h ↑ Summary include/tkr/tkr_ical.h »
1 #ifndef TKR_SET_H
2 #define TKR_SET_H
3  
4 /* A set: rules (RRULEs, then the EXRULEs read from iCalendar), dates added
5 * (RDATE) and dates removed (EXDATE), one zone, start, gap and overlap
6 * choice for all, packed in one buffer behind a blessed reference: a
7 * header, the rules, the dates. Never changed after it is made. Its
8 * occurrences are the merge of each rule's, in order, with the added dates
9 * in and the removed ones and the EXRULEs' taken out. An added whole day
10 * is its midnight in the set's zone, resolved when the set is made; a
11 * removed whole day takes every occurrence of that day in the set's zone,
12 * so it is kept as the day's wall midnight and compared by day. */
13  
14 #define TKR_SET_MAGIC 0x53524b54u
15 #define TKR_SET_MAX_RULES 16
16 #define TKR_SET_MAX_DATES 10000
17  
18 typedef struct tkr_date {
19 int64_t at; /* an instant; a removed whole day: its wall midnight, seconds as if UTC */
20 uint8_t is_day;
21 } tkr_date;
22  
23 typedef struct tkr_set {
24 uint32_t magic;
25 uint8_t n_rules, n_exrules, gap, overlap;
26 uint32_t n_rdates, n_exdates;
27 time_keeper_zone zone;
28 time_keeper_instant start;
29 int locale;
30 uint8_t has_start, start_is_day, pad[6];
31 } tkr_set;
32  
33 #define TKR_SET_HDR ((sizeof(tkr_set) + 7) & ~(size_t)7)
34  
35 130986 static size_t tkr_set_size(unsigned nrules, unsigned ndates) { return TKR_SET_HDR + nrules * sizeof(tkr_rule) + ndates * sizeof(tkr_date); }
36 48773422 static tkr_rule *tkr_set_rules(const tkr_set *s) { return (tkr_rule *)((char *)s + TKR_SET_HDR); }
37 39132175 static tkr_date *tkr_set_rdates(const tkr_set *s) { return (tkr_date *)(tkr_set_rules(s) + s->n_rules + s->n_exrules); }
38 19546494 static tkr_date *tkr_set_exdates(const tkr_set *s) { return tkr_set_rdates(s) + s->n_rdates; }
39  
40 static int
41 6745 tkr_date_cmp(const void *a, const void *b)
42 {
43 6745 const tkr_date *x = (const tkr_date *)a, *y = (const tkr_date *)b;
44 6745 ▶ return x->at < y->at ? -1 : x->at > y->at ? 1 : (int)x->is_day - (int)y->is_day;
Branch: 2 branches
BranchTrueFalseTotal
return x->at < y->at ? -1 : x->at > y->at ? 1 : (int)x->is_day - (int)y->is_day; 4424 2321 6745
return x->at < y->at ? -1 : x->at > y->at ? 1 : (int)x->is_day - (int)y->is_day; 60 4364 4424
45 }
46  
47 /* The wall time of an instant in a zone, and the instant of a wall time
48 * (forward over a gap, the earlier of an overlap). */
49 static int64_t
50 5022022 tkr_zone_wall(pTHX_ const time_keeper_zone *z, int64_t inst)
51 {
52 tkr_rule probe;
53 5022022 tkr_rule_init(&probe);
54 5022022 probe.zone = *z;
55 5022022 return tkr_wall_of(aTHX_ &probe, inst, NULL);
56 }
57  
58 static int64_t
59 533 tkr_zone_instant(pTHX_ const time_keeper_zone *z, int64_t wall, const char *what)
60 {
61 tkr_rule probe;
62 int64_t out[2];
63 533 tkr_rule_init(&probe);
64 533 probe.zone = *z;
65 533 probe.gap = TKR_GAP_FORWARD;
66 533 probe.overlap = TKR_OVERLAP_EARLIER;
67 533 ▶ if (tkr_resolve(aTHX_ &probe, wall, out) < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (tkr_resolve(aTHX_ &probe, wall, out) < 1) 0 533 533
68 0 croak("%s: a date outside the years Time::Keeper holds", what);
69 533 return out[0];
70 }
71  
72 /* Everything a set is made of, before it is packed. */
73 typedef struct tkr_set_parts {
74 tkr_rule rules[TKR_SET_MAX_RULES];
75 unsigned n_rules, n_exrules;
76 tkr_date *rdates, *exdates;
77 unsigned n_rdates, n_exdates, rcap, xcap;
78 time_keeper_zone zone;
79 time_keeper_instant start;
80 int locale, has_start, start_is_day, gap, overlap;
81 } tkr_set_parts;
82  
83 static void
84 1534 tkr_parts_init(tkr_set_parts *p)
85 {
86 1534 memset(p, 0, sizeof *p);
87 1534 p->zone.kind = TIME_KEEPER_ZONE_UTC;
88 1534 p->gap = TKR_GAP_FORWARD;
89 1534 p->overlap = TKR_OVERLAP_EARLIER;
90 1534 }
91  
92 static void
93 1529 tkr_parts_free(tkr_set_parts *p)
94 {
95 1529 ▶ if (p->rdates) Safefree(p->rdates);
Branch: 1 branch
BranchTrueFalseTotal
if (p->rdates) Safefree(p->rdates); 1088 441 1529
96 1529 ▶ if (p->exdates) Safefree(p->exdates);
Branch: 1 branch
BranchTrueFalseTotal
if (p->exdates) Safefree(p->exdates); 1163 366 1529
97 1529 p->rdates = p->exdates = NULL;
98 1529 }
99  
100 /* A date as it is stored: an instant, or a removed day's wall midnight. */
101 static void
102 5140 tkr_parts_add_raw(pTHX_ tkr_set_parts *p, int ex, int64_t at, int is_day, const char *what)
103 {
104 5140 ▶ tkr_date **list = ex ? &p->exdates : &p->rdates;
Branch: 1 branch
BranchTrueFalseTotal
tkr_date **list = ex ? &p->exdates : &p->rdates; 2636 2504 5140
105 5140 ▶ unsigned *n = ex ? &p->n_exdates : &p->n_rdates, *cap = ex ? &p->xcap : &p->rcap;
Branch: 2 branches
BranchTrueFalseTotal
unsigned *n = ex ? &p->n_exdates : &p->n_rdates, *cap = ex ? &p->xcap : &p->rcap; 2636 2504 5140
unsigned *n = ex ? &p->n_exdates : &p->n_rdates, *cap = ex ? &p->xcap : &p->rcap; 2636 2504 5140
106 5140 ▶ if (*n >= TKR_SET_MAX_DATES) {
Branch: 1 branch
BranchTrueFalseTotal
if (*n >= TKR_SET_MAX_DATES) { 0 5140 5140
107 0 tkr_parts_free(p);
108 0 ▶ croak("%s: more than %d dates %s", what, TKR_SET_MAX_DATES, ex ? "removed" : "added");
Branch: 1 branch
BranchTrueFalseTotal
croak("%s: more than %d dates %s", what, TKR_SET_MAX_DATES, ex ? "removed" : "added"); 0 0 0
109 }
110 5140 ▶ if (*n >= *cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (*n >= *cap) { 2257 2883 5140
111 2257 ▶ *cap = *cap ? *cap * 2 : 16;
Branch: 1 branch
BranchTrueFalseTotal
*cap = *cap ? *cap * 2 : 16; 6 2251 2257
112 2257 Renew(*list, *cap, tkr_date);
113 }
114 5140 (*list)[*n].at = at;
115 5140 (*list)[*n].is_day = (uint8_t)is_day;
116 5140 (*n)++;
117 5140 }
118  
119 /* A date as it was given: an instant, or a whole day as its wall midnight
120 * in the set's zone (an added day becomes the instant of that midnight). */
121 static void
122 845 tkr_parts_add_date(pTHX_ tkr_set_parts *p, int ex, int64_t at, int is_day, const char *what)
123 {
124 845 ▶ if (is_day && !ex)
Branch: 2 branches
BranchTrueFalseTotal
if (is_day && !ex) 845 0 845
if (is_day && !ex) 302 543 845
125 302 at = tkr_zone_instant(aTHX_ &p->zone, at, what);
126 845 tkr_parts_add_raw(aTHX_ p, ex, at, is_day, what);
127 845 }
128  
129 static void
130 1869 tkr_parts_add_rule(pTHX_ tkr_set_parts *p, const tkr_rule *r, int ex, const char *what)
131 {
132 1869 ▶ if (p->n_rules + p->n_exrules >= TKR_SET_MAX_RULES) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->n_rules + p->n_exrules >= TKR_SET_MAX_RULES) { 2 1867 1869
133 2 tkr_parts_free(p);
134 2 croak("%s: more than %d rules in a set", what, TKR_SET_MAX_RULES);
135 }
136 1867 ▶ if (ex) {
Branch: 1 branch
BranchTrueFalseTotal
if (ex) { 4 1863 1867
137 4 p->rules[p->n_rules + p->n_exrules] = *r;
138 4 p->n_exrules++;
139 }
140 else {
141 1863 memmove(&p->rules[p->n_rules + 1], &p->rules[p->n_rules], p->n_exrules * sizeof(tkr_rule));
142 1863 p->rules[p->n_rules] = *r;
143 1863 p->n_rules++;
144 }
145 1867 }
146  
147 static int
148 316 tkr_zone_eq(const time_keeper_zone *a, const time_keeper_zone *b)
149 {
150 316 ▶ return a->kind == b->kind && (a->kind == TIME_KEEPER_ZONE_UTC || a->kind == TIME_KEEPER_ZONE_FLOATING || a->value == b->value);
Branch: 4 branches
BranchTrueFalseTotal
return a->kind == b->kind && (a->kind == TIME_KEEPER_ZONE_UTC || a->kind == TIME_KEEPER_ZONE_FLOATING || a->value == b->value); 316 0 316
return a->kind == b->kind && (a->kind == TIME_KEEPER_ZONE_UTC || a->kind == TIME_KEEPER_ZONE_FLOATING || a->value == b->value); 316 0 316
return a->kind == b->kind && (a->kind == TIME_KEEPER_ZONE_UTC || a->kind == TIME_KEEPER_ZONE_FLOATING || a->value == b->value); 316 0 316
return a->kind == b->kind && (a->kind == TIME_KEEPER_ZONE_UTC || a->kind == TIME_KEEPER_ZONE_FLOATING || a->value == b->value); 315 1 316
151 }
152  
153 /* A rule joining a set from Perl: it must share the set's zone, and an
154 * RRULE its start; a cron keeps its own bound. */
155 static void
156 316 tkr_parts_join_rule(pTHX_ tkr_set_parts *p, const tkr_rule *r, const char *what)
157 {
158 316 ▶ if (r->flags & TKR_F_EVERY) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->flags & TKR_F_EVERY) { 0 316 316
159 0 tkr_parts_free(p);
160 0 croak("%s: an @every cron has no occurrences of its own, only the next after a moment", what);
161 }
162 316 ▶ if (!tkr_zone_eq(&p->zone, &r->zone)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tkr_zone_eq(&p->zone, &r->zone)) { 1 315 316
163 1 tkr_parts_free(p);
164 1 croak("%s: the rules of a set share one zone", what);
165 }
166 315 ▶ if (!(r->flags & TKR_F_CRON) && p->has_start && (r->start.sec != p->start.sec || r->start.nsec != p->start.nsec)) {
Branch: 4 branches
BranchTrueFalseTotal
if (!(r->flags & TKR_F_CRON) && p->has_start && (r->start.sec != p->start.sec || r->start.nsec != p->start.nsec)) { 315 0 315
if (!(r->flags & TKR_F_CRON) && p->has_start && (r->start.sec != p->start.sec || r->start.nsec != p->start.nsec)) { 315 0 315
if (!(r->flags & TKR_F_CRON) && p->has_start && (r->start.sec != p->start.sec || r->start.nsec != p->start.nsec)) { 314 1 315
if (!(r->flags & TKR_F_CRON) && p->has_start && (r->start.sec != p->start.sec || r->start.nsec != p->start.nsec)) { 0 314 314
167 1 tkr_parts_free(p);
168 1 croak("%s: the rules of a set share one start", what);
169 }
170 314 ▶ if (!(r->flags & TKR_F_CRON) && !p->has_start) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(r->flags & TKR_F_CRON) && !p->has_start) { 314 0 314
if (!(r->flags & TKR_F_CRON) && !p->has_start) { 0 314 314
171 0 p->start = r->start;
172 0 p->has_start = 1;
173 }
174 314 tkr_parts_add_rule(aTHX_ p, r, 0, what);
175 313 }
176  
177 /* The set, packed and blessed. The dates are sorted and deduplicated; every
178 * rule takes the set's zone, locale, gap and overlap, and an RRULE its start. */
179 static SV *
180 1510 tkr_set_new_sv(pTHX_ HV *stash, tkr_set_parts *p, const char *what)
181 {
182 1510 unsigned i, nr = p->n_rules + p->n_exrules, w;
183 size_t size;
184 SV *body;
185 tkr_set *s;
186 tkr_rule *rules;
187 tkr_date *d;
188 1510 ▶ if (!nr && !p->n_rdates) {
Branch: 2 branches
BranchTrueFalseTotal
if (!nr && !p->n_rdates) { 1 1509 1510
if (!nr && !p->n_rdates) { 0 1 1
189 0 tkr_parts_free(p);
190 0 croak("%s: nothing recurs: no rule and no date added", what);
191 }
192 1510 size = tkr_set_size(nr, p->n_rdates + p->n_exdates);
193 1510 body = newSV(size + 1);
194 1510 SvPOK_on(body);
195 1510 s = (tkr_set *)SvPVX(body);
196 1510 memset(s, 0, size);
197 1510 s->magic = TKR_SET_MAGIC;
198 1510 s->n_rules = (uint8_t)p->n_rules;
199 1510 s->n_exrules = (uint8_t)p->n_exrules;
200 1510 s->gap = (uint8_t)p->gap;
201 1510 s->overlap = (uint8_t)p->overlap;
202 1510 s->zone = p->zone;
203 1510 s->start = p->start;
204 1510 s->locale = p->locale;
205 1510 s->has_start = (uint8_t)(p->has_start != 0);
206 1510 s->start_is_day = (uint8_t)(p->start_is_day != 0);
207 1510 rules = tkr_set_rules(s);
208 3337 ▶ for (i = 0; i < nr; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nr; i++) { 1827 1510 3337
209 1827 rules[i] = p->rules[i];
210 1827 rules[i].zone = p->zone;
211 1827 rules[i].locale = p->locale;
212 1827 rules[i].gap = (uint8_t)p->gap;
213 1827 rules[i].overlap = (uint8_t)p->overlap;
214 1827 ▶ if (p->has_start && !(rules[i].flags & TKR_F_CRON)) {
Branch: 2 branches
BranchTrueFalseTotal
if (p->has_start && !(rules[i].flags & TKR_F_CRON)) { 1817 10 1827
if (p->has_start && !(rules[i].flags & TKR_F_CRON)) { 1817 0 1817
215 1817 rules[i].start = p->start;
216 1817 rules[i].flags |= TKR_F_HAS_START;
217 }
218 }
219 1510 ▶ if (p->n_rdates)
Branch: 1 branch
BranchTrueFalseTotal
if (p->n_rdates) 1088 422 1510
220 1088 qsort(p->rdates, p->n_rdates, sizeof(tkr_date), tkr_date_cmp);
221 1510 ▶ if (p->n_exdates)
Branch: 1 branch
BranchTrueFalseTotal
if (p->n_exdates) 1163 347 1510
222 1163 qsort(p->exdates, p->n_exdates, sizeof(tkr_date), tkr_date_cmp);
223 1510 d = tkr_set_rdates(s);
224 4014 ▶ for (i = w = 0; i < p->n_rdates; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = w = 0; i < p->n_rdates; i++) 2504 1510 4014
225 2504 ▶ if (!w || tkr_date_cmp(&p->rdates[i], &d[w - 1]) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!w || tkr_date_cmp(&p->rdates[i], &d[w - 1]) != 0) 1416 1088 2504
if (!w || tkr_date_cmp(&p->rdates[i], &d[w - 1]) != 0) 1414 2 1416
226 2502 d[w++] = p->rdates[i];
227 1510 s->n_rdates = w;
228 1510 d = tkr_set_rdates(s) + w;
229 4146 ▶ for (i = w = 0; i < p->n_exdates; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = w = 0; i < p->n_exdates; i++) 2636 1510 4146
230 2636 ▶ if (!w || tkr_date_cmp(&p->exdates[i], &d[w - 1]) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!w || tkr_date_cmp(&p->exdates[i], &d[w - 1]) != 0) 1473 1163 2636
if (!w || tkr_date_cmp(&p->exdates[i], &d[w - 1]) != 0) 1445 28 1473
231 2608 d[w++] = p->exdates[i];
232 1510 s->n_exdates = w;
233 1510 SvCUR_set(body, tkr_set_size(nr, s->n_rdates + s->n_exdates));
234 1510 tkr_parts_free(p);
235 1510 return sv_bless(newRV_noinc(body), stash);
236 }
237  
238 static tkr_set *
239 127966 tkr_set_of(pTHX_ SV *sv, const char *what)
240 {
241 tkr_set *s;
242 127966 ▶ if (!sv || !SvROK(sv) || !sv_derived_from(sv, "Time::Keeper::Recurrence::Set") || !SvPOK(SvRV(sv))
Branch: 4 branches
BranchTrueFalseTotal
if (!sv || !SvROK(sv) || !sv_derived_from(sv, "Time::Keeper::Recurrence::Set") || !SvPOK(SvRV(sv)) 127966 0 127966
if (!sv || !SvROK(sv) || !sv_derived_from(sv, "Time::Keeper::Recurrence::Set") || !SvPOK(SvRV(sv)) 127966 0 127966
if (!sv || !SvROK(sv) || !sv_derived_from(sv, "Time::Keeper::Recurrence::Set") || !SvPOK(SvRV(sv)) 127966 0 127966
if (!sv || !SvROK(sv) || !sv_derived_from(sv, "Time::Keeper::Recurrence::Set") || !SvPOK(SvRV(sv)) 127966 0 127966
243 127966 ▶ || SvCUR(SvRV(sv)) < TKR_SET_HDR)
Branch: 1 branch
BranchTrueFalseTotal
|| SvCUR(SvRV(sv)) < TKR_SET_HDR) 0 127966 127966
244 0 croak("%s: not a Time::Keeper::Recurrence::Set", what);
245 127966 s = (tkr_set *)SvPVX(SvRV(sv));
246 127966 ▶ if (s->magic != TKR_SET_MAGIC || SvCUR(SvRV(sv)) != tkr_set_size(s->n_rules + s->n_exrules, s->n_rdates + s->n_exdates))
Branch: 2 branches
BranchTrueFalseTotal
if (s->magic != TKR_SET_MAGIC || SvCUR(SvRV(sv)) != tkr_set_size(s->n_rules + s->n_exrules, s->n_rdates + s->n_exdates)) 127966 0 127966
if (s->magic != TKR_SET_MAGIC || SvCUR(SvRV(sv)) != tkr_set_size(s->n_rules + s->n_exrules, s->n_rdates + s->n_exdates)) 0 127966 127966
247 0 croak("%s: a damaged Time::Keeper::Recurrence::Set", what);
248 127966 return s;
249 }
250  
251 /* The parts of a set, back out of it, so with() can add to them. */
252 static void
253 299 tkr_parts_from_set(pTHX_ tkr_set_parts *p, const tkr_set *s, const char *what)
254 {
255 unsigned i;
256 299 tkr_parts_init(p);
257 299 p->zone = s->zone;
258 299 p->start = s->start;
259 299 p->locale = s->locale;
260 299 p->has_start = s->has_start;
261 299 p->start_is_day = s->start_is_day;
262 299 p->gap = s->gap;
263 299 p->overlap = s->overlap;
264 613 ▶ for (i = 0; i < (unsigned)s->n_rules; i++) tkr_parts_add_rule(aTHX_ p, &tkr_set_rules(s)[i], 0, what);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (unsigned)s->n_rules; i++) tkr_parts_add_rule(aTHX_ p, &tkr_set_rules(s)[i], 0, what); 314 299 613
265 299 ▶ for (i = 0; i < (unsigned)s->n_exrules; i++) tkr_parts_add_rule(aTHX_ p, &tkr_set_rules(s)[s->n_rules + i], 1, what);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (unsigned)s->n_exrules; i++) tkr_parts_add_rule(aTHX_ p, &tkr_set_rules(s)[s->n_rules + i], 1, what); 0 299 299
266 842 ▶ for (i = 0; i < s->n_rdates; i++) tkr_parts_add_raw(aTHX_ p, 0, tkr_set_rdates(s)[i].at, tkr_set_rdates(s)[i].is_day, what);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->n_rdates; i++) tkr_parts_add_raw(aTHX_ p, 0, tkr_set_rdates(s)[i].at, tkr_set_rdates(s)[i].is_day, what); 543 299 842
267 867 ▶ for (i = 0; i < s->n_exdates; i++) tkr_parts_add_raw(aTHX_ p, 1, tkr_set_exdates(s)[i].at, tkr_set_exdates(s)[i].is_day, what);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->n_exdates; i++) tkr_parts_add_raw(aTHX_ p, 1, tkr_set_exdates(s)[i].at, tkr_set_exdates(s)[i].is_day, what); 568 299 867
268 299 }
269  
270 static void
271 1085 tkr_parts_from_rule(pTHX_ tkr_set_parts *p, const tkr_rule *r, const char *what)
272 {
273 1085 tkr_parts_init(p);
274 1085 ▶ if (r->flags & TKR_F_EVERY)
Branch: 1 branch
BranchTrueFalseTotal
if (r->flags & TKR_F_EVERY) 2 1083 1085
275 2 croak("%s: an @every cron has no occurrences of its own, only the next after a moment", what);
276 1083 p->zone = r->zone;
277 1083 p->start = r->start;
278 1083 p->locale = r->locale;
279 1083 ▶ p->has_start = (r->flags & TKR_F_HAS_START) != 0 && !(r->flags & TKR_F_CRON);
Branch: 2 branches
BranchTrueFalseTotal
p->has_start = (r->flags & TKR_F_HAS_START) != 0 && !(r->flags & TKR_F_CRON); 1077 6 1083
p->has_start = (r->flags & TKR_F_HAS_START) != 0 && !(r->flags & TKR_F_CRON); 1076 1 1077
280 1083 p->gap = r->gap;
281 1083 p->overlap = r->overlap;
282 1083 tkr_parts_add_rule(aTHX_ p, r, 0, what);
283 1083 }
284  
285 /* ---- the merge --------------------------------------------------------------- */
286  
287 /* Is an instant removed: an EXDATE to the second, an EXDATE of its whole
288 * day in the set's zone, or an occurrence of an EXRULE. */
289 static int
290 19545083 tkr_set_excluded(pTHX_ const tkr_set *s, int64_t inst, int *rc)
291 {
292 19545083 const tkr_date *x = tkr_set_exdates(s);
293 unsigned i;
294 19545083 int64_t day = INT64_MIN;
295 38675206 ▶ for (i = 0; i < s->n_exdates; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->n_exdates; i++) { 19137519 19537687 38675206
296 19137519 ▶ if (x[i].is_day) {
Branch: 1 branch
BranchTrueFalseTotal
if (x[i].is_day) { 5021688 14115831 19137519
297 5021688 ▶ if (day == INT64_MIN)
Branch: 1 branch
BranchTrueFalseTotal
if (day == INT64_MIN) 5021683 5 5021688
298 5021683 day = tkr_floor_div(tkr_zone_wall(aTHX_ &s->zone, inst), 86400) * 86400;
299 5021688 ▶ if (x[i].at == day)
Branch: 1 branch
BranchTrueFalseTotal
if (x[i].at == day) 1562 5020126 5021688
300 1562 return 1;
301 }
302 14115831 ▶ else if (x[i].at == inst)
Branch: 1 branch
BranchTrueFalseTotal
else if (x[i].at == inst) 5834 14109997 14115831
303 5834 return 1;
304 }
305 19537718 ▶ for (i = 0; i < s->n_exrules; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->n_exrules; i++) { 45 19537673 19537718
306 int64_t got;
307 45 int r = tkr_next_after(aTHX_ &tkr_set_rules(s)[s->n_rules + i], inst, 1, &got, NULL);
308 45 ▶ if (r == TKR_W_FAIL) {
Branch: 1 branch
BranchTrueFalseTotal
if (r == TKR_W_FAIL) { 0 45 45
309 0 *rc = r;
310 14 return 1;
311 }
312 45 ▶ if (r == TKR_W_GOT && got == inst)
Branch: 2 branches
BranchTrueFalseTotal
if (r == TKR_W_GOT && got == inst) 45 0 45
if (r == TKR_W_GOT && got == inst) 14 31 45
313 14 return 1;
314 }
315 19537673 return 0;
316 }
317  
318 typedef struct tkr_setiter {
319 SV *set_sv; /* held: an iterator outlives the object it came from */
320 tkr_walk *walks[TKR_SET_MAX_RULES];
321 int64_t heads[TKR_SET_MAX_RULES];
322 unsigned rd_i;
323 int64_t from, last, fail_wall;
324 int inclusive, from_start, done;
325 } tkr_setiter;
326  
327 /* A rule's next occurrence past the iterator's from, into heads[i]. A
328 * cron's start is a bound, never an occurrence. */
329 static int
330 19973724 tkr_setiter_pull(pTHX_ tkr_setiter *it, unsigned i)
331 {
332 int64_t got;
333 int rc;
334 19973724 const tkr_rule *r = &it->walks[i]->rule;
335 20077207 ▶ while ((rc = tkr_walk_next(aTHX_ it->walks[i], &got)) == TKR_W_GOT) {
Branch: 1 branch
BranchTrueFalseTotal
while ((rc = tkr_walk_next(aTHX_ it->walks[i], &got)) == TKR_W_GOT) { 20077185 22 20077207
336 20077185 ▶ if (got > it->from || (it->inclusive && got == it->from && !((r->flags & TKR_F_CRON) && it->from_start))) {
Branch: 5 branches
BranchTrueFalseTotal
if (got > it->from || (it->inclusive && got == it->from && !((r->flags & TKR_F_CRON) && it->from_start))) { 127570 19949615 20077185
if (got > it->from || (it->inclusive && got == it->from && !((r->flags & TKR_F_CRON) && it->from_start))) { 69240 58330 127570
if (got > it->from || (it->inclusive && got == it->from && !((r->flags & TKR_F_CRON) && it->from_start))) { 24087 45153 69240
if (got > it->from || (it->inclusive && got == it->from && !((r->flags & TKR_F_CRON) && it->from_start))) { 0 24087 24087
if (got > it->from || (it->inclusive && got == it->from && !((r->flags & TKR_F_CRON) && it->from_start))) { 0 0 0
337 19973702 it->heads[i] = got;
338 19973702 return TKR_W_GOT;
339 }
340 }
341 22 it->heads[i] = INT64_MAX;
342 22 ▶ if (rc == TKR_W_FAIL)
Branch: 1 branch
BranchTrueFalseTotal
if (rc == TKR_W_FAIL) 0 22 22
343 0 it->fail_wall = it->walks[i]->fail_wall;
344 22 return rc;
345 }
346  
347 static void
348 43603 tkr_setiter_free(pTHX_ tkr_setiter *it)
349 {
350 unsigned i;
351 741251 ▶ for (i = 0; i < TKR_SET_MAX_RULES; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < TKR_SET_MAX_RULES; i++) 697648 43603 741251
352 697648 ▶ if (it->walks[i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (it->walks[i]) { 56457 641191 697648
353 56457 tkr_walk_free(it->walks[i]);
354 56457 Safefree(it->walks[i]);
355 56457 it->walks[i] = NULL;
356 }
357 43603 ▶ if (it->set_sv)
Branch: 1 branch
BranchTrueFalseTotal
if (it->set_sv) 43603 0 43603
358 43603 SvREFCNT_dec(it->set_sv);
359 43603 it->set_sv = NULL;
360 43603 }
361  
362 static int
363 43603 tkr_setiter_init(pTHX_ tkr_setiter *it, SV *set_sv, int64_t from, int inclusive, int from_start)
364 {
365 43603 const tkr_set *s = tkr_set_of(aTHX_ set_sv, "Time::Keeper::Recurrence::Set");
366 43603 const tkr_date *rd = tkr_set_rdates(s);
367 unsigned i;
368 43603 memset(it, 0, sizeof *it);
369 43603 it->set_sv = SvREFCNT_inc_simple_NN(set_sv);
370 43603 it->from = from;
371 43603 it->inclusive = inclusive;
372 43603 it->from_start = from_start;
373 43603 it->last = INT64_MIN;
374 100060 ▶ for (i = 0; i < s->n_rules; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->n_rules; i++) { 56457 43603 100060
375 56457 const tkr_rule *r = &tkr_set_rules(s)[i];
376 int rc;
377 56457 Newxz(it->walks[i], 1, tkr_walk);
378 56457 ▶ if (r->flags & TKR_F_EVERY) {
Branch: 1 branch
BranchTrueFalseTotal
if (r->flags & TKR_F_EVERY) { 0 56457 56457
379 0 it->heads[i] = INT64_MAX; /* an @every cron has no occurrences of its own */
380 0 it->walks[i]->rule = *r;
381 0 continue;
382 }
383 56457 ▶ tkr_walk_init(aTHX_ it->walks[i], r, tkr_wall_of(aTHX_ r, from, NULL) - tkr_lookback(aTHX_ r, from), from_start && (r->flags & TKR_F_HAS_START));
Branch: 2 branches
BranchTrueFalseTotal
tkr_walk_init(aTHX_ it->walks[i], r, tkr_wall_of(aTHX_ r, from, NULL) - tkr_lookback(aTHX_ r, from), from_start && (r->flags & TKR_F_HAS_START)); 1370 55087 56457
tkr_walk_init(aTHX_ it->walks[i], r, tkr_wall_of(aTHX_ r, from, NULL) - tkr_lookback(aTHX_ r, from), from_start && (r->flags & TKR_F_HAS_START)); 1370 0 1370
384 56457 rc = tkr_setiter_pull(aTHX_ it, i);
385 56457 ▶ if (rc < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) { 0 56457 56457
386 0 tkr_setiter_free(aTHX_ it);
387 0 return rc;
388 }
389 }
390 70685 ▶ for (it->rd_i = 0; it->rd_i < s->n_rdates; it->rd_i++)
Branch: 1 branch
BranchTrueFalseTotal
for (it->rd_i = 0; it->rd_i < s->n_rdates; it->rd_i++) 54863 15822 70685
391 54863 ▶ if (rd[it->rd_i].at > from || (inclusive && rd[it->rd_i].at == from))
Branch: 3 branches
BranchTrueFalseTotal
if (rd[it->rd_i].at > from || (inclusive && rd[it->rd_i].at == from)) 28266 26597 54863
if (rd[it->rd_i].at > from || (inclusive && rd[it->rd_i].at == from)) 15487 12779 28266
if (rd[it->rd_i].at > from || (inclusive && rd[it->rd_i].at == from)) 14303 1184 15487
392 break;
393 43603 return TKR_W_GOT;
394 }
395  
396 /* The next occurrence of the set, in order: the least of every rule's head
397 * and the next added date, unless it was given out already or is removed. */
398 static int
399 19517691 tkr_setiter_next(pTHX_ tkr_setiter *it, int64_t *inst)
400 {
401 19517691 const tkr_set *s = (const tkr_set *)SvPVX(SvRV(it->set_sv));
402 19517691 const tkr_date *rd = tkr_set_rdates(s);
403 405037 for (;;) {
404 19922728 int64_t best = INT64_MAX;
405 19922728 int which = -1, rc = TKR_W_GOT, excluded;
406 unsigned i;
407 19922728 ▶ if (it->done)
Branch: 1 branch
BranchTrueFalseTotal
if (it->done) 1 19922727 19922728
408 19517691 return TKR_W_END;
409 43537082 ▶ for (i = 0; i < s->n_rules; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->n_rules; i++) 23614355 19922727 43537082
410 23614355 ▶ if (it->heads[i] < best) {
Branch: 1 branch
BranchTrueFalseTotal
if (it->heads[i] < best) { 21065660 2548695 23614355
411 21065660 best = it->heads[i];
412 21065660 which = (int)i;
413 }
414 19922727 ▶ if (it->rd_i < s->n_rdates && rd[it->rd_i].at < best) {
Branch: 2 branches
BranchTrueFalseTotal
if (it->rd_i < s->n_rdates && rd[it->rd_i].at < best) { 67448 19855279 19922727
if (it->rd_i < s->n_rdates && rd[it->rd_i].at < best) { 5439 62009 67448
415 5439 best = rd[it->rd_i].at;
416 5439 which = -2;
417 }
418 19922727 ▶ if (which == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (which == -1) { 21 19922706 19922727
419 21 it->done = 1;
420 21 return TKR_W_END;
421 }
422 19922706 ▶ if (which == -2)
Branch: 1 branch
BranchTrueFalseTotal
if (which == -2) 5439 19917267 19922706
423 5439 it->rd_i++;
424 else {
425 19917267 rc = tkr_setiter_pull(aTHX_ it, (unsigned)which);
426 19917267 ▶ if (rc < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) 0 19917267 19917267
427 0 return rc;
428 }
429 19922706 ▶ if (best == it->last)
Branch: 1 branch
BranchTrueFalseTotal
if (best == it->last) 398761 19523945 19922706
430 405037 continue;
431 19523945 excluded = tkr_set_excluded(aTHX_ s, best, &rc);
432 19523945 ▶ if (rc < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) 0 19523945 19523945
433 0 return rc;
434 19523945 ▶ if (excluded)
Branch: 1 branch
BranchTrueFalseTotal
if (excluded) 6276 19517669 19523945
435 6276 continue;
436 19517669 it->last = best;
437 19517669 *inst = best;
438 19517669 return TKR_W_GOT;
439 }
440 }
441  
442 /* The last occurrence of the set strictly before an instant: the greatest
443 * of every rule's previous and the last added date before it, stepping
444 * back past the removed ones. */
445 static int
446 20004 tkr_set_previous(pTHX_ SV *set_sv, int64_t from, int64_t *inst, int64_t *detail)
447 {
448 20004 const tkr_set *s = tkr_set_of(aTHX_ set_sv, "Time::Keeper::Recurrence::Set->previous");
449 20004 const tkr_date *rd = tkr_set_rdates(s);
450 20004 unsigned guard = 0;
451 1134 for (;;) {
452 21138 int64_t best = INT64_MIN;
453 unsigned i;
454 int rc, excluded;
455 48427 ▶ for (i = 0; i < s->n_rules; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->n_rules; i++) { 27289 21138 48427
456 int64_t got;
457 27289 const tkr_rule *r = &tkr_set_rules(s)[i];
458 27289 ▶ if (r->flags & TKR_F_EVERY)
Branch: 1 branch
BranchTrueFalseTotal
if (r->flags & TKR_F_EVERY) 0 27289 27289
459 0 continue;
460 27289 rc = tkr_previous_before(aTHX_ r, from, &got, detail);
461 27289 ▶ if (rc < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) 0 27289 27289
462 0 return rc;
463 27289 ▶ if (rc == TKR_W_GOT && got > best)
Branch: 2 branches
BranchTrueFalseTotal
if (rc == TKR_W_GOT && got > best) 27289 0 27289
if (rc == TKR_W_GOT && got > best) 23170 4119 27289
464 23170 best = got;
465 }
466 44784 ▶ for (i = s->n_rdates; i > 0; i--)
Branch: 1 branch
BranchTrueFalseTotal
for (i = s->n_rdates; i > 0; i--) 32929 11855 44784
467 32929 ▶ if (rd[i - 1].at < from) {
Branch: 1 branch
BranchTrueFalseTotal
if (rd[i - 1].at < from) { 9283 23646 32929
468 9283 ▶ if (rd[i - 1].at > best)
Branch: 1 branch
BranchTrueFalseTotal
if (rd[i - 1].at > best) 1186 8097 9283
469 1186 best = rd[i - 1].at;
470 9283 break;
471 }
472 21138 ▶ if (best == INT64_MIN)
Branch: 1 branch
BranchTrueFalseTotal
if (best == INT64_MIN) 0 21138 21138
473 0 return TKR_W_END;
474 21138 excluded = tkr_set_excluded(aTHX_ s, best, &rc);
475 21138 ▶ if (rc < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (rc < 0) 0 21138 21138
476 0 return rc;
477 21138 ▶ if (!excluded) {
Branch: 1 branch
BranchTrueFalseTotal
if (!excluded) { 20004 1134 21138
478 20004 *inst = best;
479 20004 return TKR_W_GOT;
480 }
481 1134 from = best;
482 1134 ▶ if (++guard > TKR_SET_MAX_DATES + 1000)
Branch: 1 branch
BranchTrueFalseTotal
if (++guard > TKR_SET_MAX_DATES + 1000) 0 1134 1134
483 0 return TKR_W_END;
484 }
485 }
486  
487 #endif
« include/tkr/tkr_walk_sv.h ↑ Summary include/tkr/tkr_ical.h »