2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2019-2020 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Aaron LI <aly@aaronly.me>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * Calendrical Calculations, The Ultimate Edition (4th Edition)
38 * by Edward M. Reingold and Nachum Dershowitz
39 * 2018, Cambridge University Press
54 * Calculate the polynomial: c[0] + c[1] * x + ... + c[n-1] * x^(n-1)
57 poly(double x
, const double *coefs
, size_t n
)
61 for (size_t i
= 0; i
< n
; i
++) {
69 * Use bisection search to find the inverse of the given angular function
70 * $f(x) at value $y (degrees) within time interval [$a, $b].
71 * Ref: Sec.(1.8), Eq.(1.36)
74 invert_angular(double (*f
)(double), double y
, double a
, double b
)
76 static const double eps
= 1e-6;
81 if (mod_f(f(x
) - y
, 360) < 180.0)
85 } while (fabs(a
-b
) >= eps
);
92 * Like malloc(3) but exit if allocation fails.
97 void *ptr
= malloc(size
);
99 errx(1, "mcalloc(%zu): out of memory", size
);
104 * Like calloc(3) but exit if allocation fails.
107 xcalloc(size_t number
, size_t size
)
109 void *ptr
= calloc(number
, size
);
111 errx(1, "xcalloc(%zu, %zu): out of memory", number
, size
);
116 * Like realloc(3) but exit if allocation fails.
119 xrealloc(void *ptr
, size_t size
)
121 ptr
= realloc(ptr
, size
);
123 errx(1, "xrealloc: out of memory (size: %zu)", size
);
128 * Like strdup(3) but exit if fail.
131 xstrdup(const char *str
)
133 char *p
= strdup(str
);
135 errx(1, "xstrdup: out of memory (length: %zu)", strlen(str
));
141 * Linked list implementation
151 * Create a new list node with the given $name and $data.
154 list_newnode(char *name
, void *data
)
158 newp
= xcalloc(1, sizeof(*newp
));
166 * Add $newp to the front of list $listp.
169 list_addfront(struct node
*listp
, struct node
*newp
)
176 * Lookup the given $name in the list $listp.
177 * The $cmp function compares two names and return 0 if they equal.
178 * Return the associated data with the found node, otherwise NULL.
181 list_lookup(struct node
*listp
, const char *name
,
182 int (*cmp
)(const char *, const char *), void **data_out
)
184 for ( ; listp
; listp
= listp
->next
) {
185 if ((*cmp
)(name
, listp
->name
) == 0) {
187 *data_out
= listp
->data
;
196 * Free all nodes of list $listp.
199 list_freeall(struct node
*listp
,
200 void (*free_name
)(void *),
201 void (*free_data
)(void *))
209 (*free_name
)(cur
->name
);
211 (*free_data
)(cur
->data
);