release 0.1.15
[liba.git] / src / a.c
blob3f503df5b6c5335f36f5e7d67ecdd57cf6b93a7c
1 #define LIBA_A_C
2 #include "a/a.h"
3 #include <string.h>
4 #include <stdlib.h>
5 #if defined(A_HAVE_MIMALLOC_H)
6 #include <mimalloc-override.h>
7 #endif /* A_HAVE_MIMALLOC_H */
9 void *a_copy(void *__restrict dst, void const *__restrict src, a_size siz) { return memcpy(dst, src, siz); }
11 void *a_move(void *dst, void const *src, a_size siz) { return memmove(dst, src, siz); }
13 void *a_fill(void *ptr, a_size siz, int val) { return memset(ptr, val, siz); }
15 void *a_zero(void *ptr, a_size siz) { return memset(ptr, 0, siz); }
17 void a_swap(void *lhs_, void *rhs_, a_size siz)
19 a_byte *lhs = (a_byte *)lhs_;
20 a_byte *rhs = (a_byte *)rhs_;
21 for (a_byte byte; siz; --siz, ++lhs, ++rhs)
23 byte = *lhs;
24 *lhs = *rhs;
25 *rhs = byte;
29 a_u32 a_hash_bkdr(void const *str_, a_u32 val)
31 a_byte const *str = (a_byte const *)str_;
32 if (str)
34 for (; *str; ++str)
36 val = val * 131 + *str;
39 return val;
42 a_u32 a_hash_bkdr_(void const *ptr_, a_size siz, a_u32 val)
44 a_byte const *ptr = (a_byte const *)ptr_;
45 for (; siz; --siz, ++ptr)
47 val = val * 131 + *ptr;
49 return val;
52 a_u32 a_hash_sdbm(void const *str_, a_u32 val)
54 a_byte const *str = (a_byte const *)str_;
55 if (str)
57 for (; *str; ++str)
59 val = val * 65599 + *str;
62 return val;
65 a_u32 a_hash_sdbm_(void const *ptr_, a_size siz, a_u32 val)
67 a_byte const *ptr = (a_byte const *)ptr_;
68 for (; siz; --siz, ++ptr)
70 val = val * 65599 + *ptr;
72 return val;
75 void a_float_push_fore(a_float *p, a_size n, a_float x)
77 if (n--)
79 a_move(p + 1, p, sizeof(a_float) * n);
80 p[0] = x;
84 void a_float_push_back(a_float *p, a_size n, a_float x)
86 if (n--)
88 a_move(p, p + 1, sizeof(a_float) * n);
89 p[n] = x;
93 void a_float_push_fore_(a_float *block_p, a_size block_n,
94 a_float const *cache_p, a_size cache_n)
96 a_size const n = A_MIN(cache_n, block_n);
97 if (n)
99 a_size const m = block_n - n;
100 cache_p = cache_p + cache_n - n;
101 a_move(block_p + n, block_p, sizeof(a_float) * m);
102 a_copy(block_p, cache_p, sizeof(a_float) * n);
106 void a_float_push_back_(a_float *block_p, a_size block_n,
107 a_float const *cache_p, a_size cache_n)
109 a_size const n = A_MIN(cache_n, block_n);
110 if (n)
112 a_size const m = block_n - n;
113 cache_p = cache_p + cache_n - n;
114 a_move(block_p, block_p + n, sizeof(a_float) * m);
115 a_copy(block_p + m, cache_p, sizeof(a_float) * n);
119 void a_float_roll_fore(a_float *p, a_size n)
121 if (n--)
123 a_float x = p[0];
124 a_move(p, p + 1, sizeof(a_float) * n);
125 p[n] = x;
129 void a_float_roll_back(a_float *p, a_size n)
131 if (n--)
133 a_float x = p[n];
134 a_move(p + 1, p, sizeof(a_float) * n);
135 p[0] = x;
139 void a_float_roll_fore_(a_float *block_p, a_size block_n,
140 a_float *shift_p, a_size shift_n)
142 a_size const shift = shift_n % block_n;
143 a_size const start = block_n - shift;
144 a_copy(shift_p, block_p, sizeof(a_float) * shift);
145 a_move(block_p, block_p + shift, sizeof(a_float) * start);
146 a_copy(block_p + start, shift_p, sizeof(a_float) * shift);
149 void a_float_roll_back_(a_float *block_p, a_size block_n,
150 a_float *shift_p, a_size shift_n)
152 a_size const shift = shift_n % block_n;
153 a_size const start = block_n - shift;
154 a_copy(shift_p, block_p + start, sizeof(a_float) * shift);
155 a_move(block_p + shift, block_p, sizeof(a_float) * start);
156 a_copy(block_p, shift_p, sizeof(a_float) * shift);
159 a_float a_float_mean(a_float const *p, a_size n)
161 a_float res = 0;
162 a_float const inv = 1 / (a_float)n;
163 for (; n; --n, ++p) { res += *p * inv; }
164 return res;
167 a_float a_float_sum(a_float const *p, a_size n)
169 a_float res = 0;
170 for (; n; --n, ++p) { res += *p; }
171 return res;
174 a_float a_float_sum1(a_float const *p, a_size n)
176 a_float res = 0;
177 for (; n; --n, ++p) { res += A_ABS(*p); }
178 return res;
181 a_float a_float_sum2(a_float const *p, a_size n)
183 a_float res = 0;
184 for (; n; --n, ++p) { res += A_SQ(*p); }
185 return res;
188 A_ALLOC((*a_alloc), addr, size) = a_alloc_;
189 A_ALLOC(a_alloc_, addr, size)
191 if (size)
193 if (addr)
195 return realloc(addr, size);
197 return malloc(size);
199 free(addr);
200 return A_NULL;