1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // algorithm.c --
|
---|
8 | //
|
---|
9 | // Author : Peter A. Buhr
|
---|
10 | // Created On : Thu Jan 28 17:10:29 2016
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Thu Jun 1 21:52:57 2017
|
---|
13 | // Update Count : 280
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "stdlib"
|
---|
17 |
|
---|
18 | //---------------------------------------
|
---|
19 |
|
---|
20 | extern "C" {
|
---|
21 | #define _XOPEN_SOURCE 600 // posix_memalign, *rand48
|
---|
22 | #include <stdlib.h> // malloc, free, calloc, realloc, memalign, posix_memalign, bsearch
|
---|
23 | #include <string.h> // memcpy, memset
|
---|
24 | #include <malloc.h> // malloc_usable_size
|
---|
25 | #include <math.h> // fabsf, fabs, fabsl
|
---|
26 | #include <complex.h> // _Complex_I
|
---|
27 | } // extern "C"
|
---|
28 |
|
---|
29 | // resize, non-array types
|
---|
30 | forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ) {
|
---|
31 | size_t olen = malloc_usable_size( ptr ); // current allocation
|
---|
32 | char * nptr = (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
|
---|
33 | size_t nlen = malloc_usable_size( nptr ); // new allocation
|
---|
34 | if ( nlen > olen ) { // larger ?
|
---|
35 | memset( nptr + olen, (int)fill, nlen - olen ); // initialize added storage
|
---|
36 | } //
|
---|
37 | return (T *)nptr;
|
---|
38 | } // alloc
|
---|
39 |
|
---|
40 | // allocation/deallocation and constructor/destructor, non-array types
|
---|
41 | forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } )
|
---|
42 | T * new( Params p ) {
|
---|
43 | return (malloc()){ p }; // run constructor
|
---|
44 | } // new
|
---|
45 |
|
---|
46 | forall( dtype T | { void ^?{}( T * ); } )
|
---|
47 | void delete( T * ptr ) {
|
---|
48 | if ( ptr ) { // ignore null
|
---|
49 | ^ptr{}; // run destructor
|
---|
50 | free( ptr );
|
---|
51 | } // if
|
---|
52 | } // delete
|
---|
53 |
|
---|
54 | forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } )
|
---|
55 | void delete( T * ptr, Params rest ) {
|
---|
56 | if ( ptr ) { // ignore null
|
---|
57 | ^ptr{}; // run destructor
|
---|
58 | free( ptr );
|
---|
59 | } // if
|
---|
60 | delete( rest );
|
---|
61 | } // delete
|
---|
62 |
|
---|
63 |
|
---|
64 | // allocation/deallocation and constructor/destructor, array types
|
---|
65 | forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } )
|
---|
66 | T * anew( size_t dim, Params p ) {
|
---|
67 | T *arr = alloc( dim );
|
---|
68 | for ( unsigned int i = 0; i < dim; i += 1 ) {
|
---|
69 | (&arr[i]){ p }; // run constructor
|
---|
70 | } // for
|
---|
71 | return arr;
|
---|
72 | } // anew
|
---|
73 |
|
---|
74 | forall( dtype T | sized(T) | { void ^?{}( T * ); } )
|
---|
75 | void adelete( size_t dim, T arr[] ) {
|
---|
76 | if ( arr ) { // ignore null
|
---|
77 | for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned
|
---|
78 | ^(&arr[i]){}; // run destructor
|
---|
79 | } // for
|
---|
80 | free( arr );
|
---|
81 | } // if
|
---|
82 | } // adelete
|
---|
83 |
|
---|
84 | forall( dtype T | sized(T) | { void ^?{}( T * ); }, ttype Params | { void adelete( Params ); } )
|
---|
85 | void adelete( size_t dim, T arr[], Params rest ) {
|
---|
86 | if ( arr ) { // ignore null
|
---|
87 | for ( int i = dim - 1; i >= 0; i -= 1 ) { // reverse allocation order, must be unsigned
|
---|
88 | ^(&arr[i]){}; // run destructor
|
---|
89 | } // for
|
---|
90 | free( arr );
|
---|
91 | } // if
|
---|
92 | adelete( rest );
|
---|
93 | } // adelete
|
---|
94 |
|
---|
95 | //---------------------------------------
|
---|
96 |
|
---|
97 | int ato( const char * ptr ) {
|
---|
98 | int i;
|
---|
99 | if ( sscanf( ptr, "%d", &i ) == EOF ) {}
|
---|
100 | return i;
|
---|
101 | } // ato
|
---|
102 |
|
---|
103 | unsigned int ato( const char * ptr ) {
|
---|
104 | unsigned int ui;
|
---|
105 | if ( sscanf( ptr, "%u", &ui ) == EOF ) {}
|
---|
106 | return ui;
|
---|
107 | } // ato
|
---|
108 |
|
---|
109 | long int ato( const char * ptr ) {
|
---|
110 | long int li;
|
---|
111 | if ( sscanf( ptr, "%ld", &li ) == EOF ) {}
|
---|
112 | return li;
|
---|
113 | } // ato
|
---|
114 |
|
---|
115 | unsigned long int ato( const char * ptr ) {
|
---|
116 | unsigned long int uli;
|
---|
117 | if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}
|
---|
118 | return uli;
|
---|
119 | } // ato
|
---|
120 |
|
---|
121 | long long int ato( const char * ptr ) {
|
---|
122 | long long int lli;
|
---|
123 | if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}
|
---|
124 | return lli;
|
---|
125 | } // ato
|
---|
126 |
|
---|
127 | unsigned long long int ato( const char * ptr ) {
|
---|
128 | unsigned long long int ulli;
|
---|
129 | if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {}
|
---|
130 | return ulli;
|
---|
131 | } // ato
|
---|
132 |
|
---|
133 |
|
---|
134 | float ato( const char * ptr ) {
|
---|
135 | float f;
|
---|
136 | if ( sscanf( ptr, "%f", &f ) == EOF ) {}
|
---|
137 | return f;
|
---|
138 | } // ato
|
---|
139 |
|
---|
140 | double ato( const char * ptr ) {
|
---|
141 | double d;
|
---|
142 | if ( sscanf( ptr, "%lf", &d ) == EOF ) {}
|
---|
143 | return d;
|
---|
144 | } // ato
|
---|
145 |
|
---|
146 | long double ato( const char * ptr ) {
|
---|
147 | long double ld;
|
---|
148 | if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {}
|
---|
149 | return ld;
|
---|
150 | } // ato
|
---|
151 |
|
---|
152 |
|
---|
153 | float _Complex ato( const char * ptr ) {
|
---|
154 | float re, im;
|
---|
155 | if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {}
|
---|
156 | return re + im * _Complex_I;
|
---|
157 | } // ato
|
---|
158 |
|
---|
159 | double _Complex ato( const char * ptr ) {
|
---|
160 | double re, im;
|
---|
161 | if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {}
|
---|
162 | return re + im * _Complex_I;
|
---|
163 | } // ato
|
---|
164 |
|
---|
165 | long double _Complex ato( const char * ptr ) {
|
---|
166 | long double re, im;
|
---|
167 | if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {}
|
---|
168 | return re + im * _Complex_I;
|
---|
169 | } // ato
|
---|
170 |
|
---|
171 |
|
---|
172 | int strto( const char * sptr, char ** eptr, int base ) {
|
---|
173 | return (int)strtol( sptr, eptr, base );
|
---|
174 | } // strto
|
---|
175 |
|
---|
176 | unsigned int strto( const char * sptr, char ** eptr, int base ) {
|
---|
177 | return (unsigned int)strtoul( sptr, eptr, base );
|
---|
178 | } // strto
|
---|
179 |
|
---|
180 | long int strto( const char * sptr, char ** eptr, int base ) {
|
---|
181 | return strtol( sptr, eptr, base );
|
---|
182 | } // strto
|
---|
183 |
|
---|
184 | unsigned long int strto( const char * sptr, char ** eptr, int base ) {
|
---|
185 | return strtoul( sptr, eptr, base );
|
---|
186 | } // strto
|
---|
187 |
|
---|
188 | long long int strto( const char * sptr, char ** eptr, int base ) {
|
---|
189 | return strtoll( sptr, eptr, base );
|
---|
190 | } // strto
|
---|
191 |
|
---|
192 | unsigned long long int strto( const char * sptr, char ** eptr, int base ) {
|
---|
193 | return strtoull( sptr, eptr, base );
|
---|
194 | } // strto
|
---|
195 |
|
---|
196 |
|
---|
197 | float strto( const char * sptr, char ** eptr ) {
|
---|
198 | return strtof( sptr, eptr );
|
---|
199 | } // strto
|
---|
200 |
|
---|
201 | double strto( const char * sptr, char ** eptr ) {
|
---|
202 | return strtod( sptr, eptr );
|
---|
203 | } // strto
|
---|
204 |
|
---|
205 | long double strto( const char * sptr, char ** eptr ) {
|
---|
206 | return strtold( sptr, eptr );
|
---|
207 | } // strto
|
---|
208 |
|
---|
209 |
|
---|
210 | float _Complex strto( const char * sptr, char ** eptr ) {
|
---|
211 | float re, im;
|
---|
212 | re = strtof( sptr, eptr );
|
---|
213 | if ( sptr == *eptr ) return 0.0;
|
---|
214 | im = strtof( sptr, eptr );
|
---|
215 | if ( sptr == *eptr ) return 0.0;
|
---|
216 | return re + im * _Complex_I;
|
---|
217 | } // strto
|
---|
218 |
|
---|
219 | double _Complex strto( const char * sptr, char ** eptr ) {
|
---|
220 | double re, im;
|
---|
221 | re = strtod( sptr, eptr );
|
---|
222 | if ( sptr == *eptr ) return 0.0;
|
---|
223 | im = strtod( sptr, eptr );
|
---|
224 | if ( sptr == *eptr ) return 0.0;
|
---|
225 | return re + im * _Complex_I;
|
---|
226 | } // strto
|
---|
227 |
|
---|
228 | long double _Complex strto( const char * sptr, char ** eptr ) {
|
---|
229 | long double re, im;
|
---|
230 | re = strtold( sptr, eptr );
|
---|
231 | if ( sptr == *eptr ) return 0.0;
|
---|
232 | im = strtold( sptr, eptr );
|
---|
233 | if ( sptr == *eptr ) return 0.0;
|
---|
234 | return re + im * _Complex_I;
|
---|
235 | } // strto
|
---|
236 |
|
---|
237 | //---------------------------------------
|
---|
238 |
|
---|
239 | forall( otype T | { int ?<?( T, T ); } )
|
---|
240 | T * bsearch( T key, const T * arr, size_t dim ) {
|
---|
241 | int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
|
---|
242 | return (T *)bsearch( &key, arr, dim, sizeof(T), comp );
|
---|
243 | } // bsearch
|
---|
244 |
|
---|
245 | forall( otype T | { int ?<?( T, T ); } )
|
---|
246 | unsigned int bsearch( T key, const T * arr, size_t dim ) {
|
---|
247 | T *result = bsearch( key, arr, dim );
|
---|
248 | return result ? result - arr : dim; // pointer subtraction includes sizeof(T)
|
---|
249 | } // bsearch
|
---|
250 |
|
---|
251 | forall( otype T | { int ?<?( T, T ); } )
|
---|
252 | void qsort( const T * arr, size_t dim ) {
|
---|
253 | int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
|
---|
254 | qsort( arr, dim, sizeof(T), comp );
|
---|
255 | } // qsort
|
---|
256 |
|
---|
257 | //---------------------------------------
|
---|
258 |
|
---|
259 | forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
|
---|
260 | [ T, T ] div( T t1, T t2 ) { return [ t1 / t2, t1 % t2 ]; }
|
---|
261 |
|
---|
262 | //---------------------------------------
|
---|
263 |
|
---|
264 | unsigned char abs( signed char v ) { return abs( (int)v ); }
|
---|
265 | unsigned long int abs( long int v ) { return labs( v ); }
|
---|
266 | unsigned long long int abs( long long int v ) { return llabs( v ); }
|
---|
267 | float abs( float x ) { return fabsf( x ); }
|
---|
268 | double abs( double x ) { return fabs( x ); }
|
---|
269 | long double abs( long double x ) { return fabsl( x ); }
|
---|
270 | float abs( float _Complex x ) { return cabsf( x ); }
|
---|
271 | double abs( double _Complex x ) { return cabs( x ); }
|
---|
272 | long double abs( long double _Complex x ) { return cabsl( x ); }
|
---|
273 |
|
---|
274 | //---------------------------------------
|
---|
275 |
|
---|
276 | void rand48seed( long int s ) { srand48( s ); }
|
---|
277 | char rand48( void ) { return mrand48(); }
|
---|
278 | int rand48( void ) { return mrand48(); }
|
---|
279 | unsigned int rand48( void ) { return lrand48(); }
|
---|
280 | long int rand48( void ) { return mrand48(); }
|
---|
281 | unsigned long int rand48( void ) { return lrand48(); }
|
---|
282 | float rand48( void ) { return (float)drand48(); } // otherwise float uses lrand48
|
---|
283 | double rand48( void ) { return drand48(); }
|
---|
284 | float _Complex rand48( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
|
---|
285 | double _Complex rand48( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
|
---|
286 | long double _Complex rand48( void) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
|
---|
287 |
|
---|
288 | //---------------------------------------
|
---|
289 |
|
---|
290 | forall( otype T | { int ?<?( T, T ); } )
|
---|
291 | T min( T t1, T t2 ) {
|
---|
292 | return t1 < t2 ? t1 : t2;
|
---|
293 | } // min
|
---|
294 |
|
---|
295 | forall( otype T | { int ?>?( T, T ); } )
|
---|
296 | T max( T t1, T t2 ) {
|
---|
297 | return t1 > t2 ? t1 : t2;
|
---|
298 | } // max
|
---|
299 |
|
---|
300 | forall( otype T | { T min( T, T ); T max( T, T ); } )
|
---|
301 | T clamp( T value, T min_val, T max_val ) {
|
---|
302 | return max( min_val, min( value, max_val ) );
|
---|
303 | } // clamp
|
---|
304 |
|
---|
305 | forall( otype T )
|
---|
306 | void swap( T * t1, T * t2 ) {
|
---|
307 | T temp = *t1;
|
---|
308 | *t1 = *t2;
|
---|
309 | *t2 = temp;
|
---|
310 | } // swap
|
---|
311 |
|
---|
312 | // Local Variables: //
|
---|
313 | // tab-width: 4 //
|
---|
314 | // End: //
|
---|