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 : Tue May 30 09:07:56 2017
|
---|
13 | // Update Count : 237
|
---|
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 * realloc( T * ptr, size_t size, char fill ) { // alternative realloc with fill value
|
---|
31 | //printf( "X6\n" );
|
---|
32 | size_t olen = malloc_usable_size( ptr ); // current allocation
|
---|
33 | char * nptr = (void *)realloc( (void *)ptr, size ); // C realloc
|
---|
34 | size_t nlen = malloc_usable_size( nptr ); // new allocation
|
---|
35 | if ( nlen > olen ) { // larger ?
|
---|
36 | memset( nptr + olen, (int)fill, nlen - olen ); // initialize added storage
|
---|
37 | } //
|
---|
38 | return (T *)nptr;
|
---|
39 | } // realloc
|
---|
40 |
|
---|
41 | // allocation/deallocation and constructor/destructor
|
---|
42 | forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } )
|
---|
43 | T * new( Params p ) {
|
---|
44 | return ((T *)malloc()){ p };
|
---|
45 | } // new
|
---|
46 |
|
---|
47 | forall( dtype T | { void ^?{}( T * ); } )
|
---|
48 | void delete( T * ptr ) {
|
---|
49 | if ( ptr ) {
|
---|
50 | ^ptr{}; // run destructor
|
---|
51 | free( ptr );
|
---|
52 | } // if
|
---|
53 | } // delete
|
---|
54 |
|
---|
55 | forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } )
|
---|
56 | void delete( T * ptr, Params rest ) {
|
---|
57 | if ( ptr ) {
|
---|
58 | ^ptr{}; // run destructor
|
---|
59 | free( ptr );
|
---|
60 | } // if
|
---|
61 | delete( rest );
|
---|
62 | } // delete
|
---|
63 |
|
---|
64 | //---------------------------------------
|
---|
65 |
|
---|
66 | int ato( const char * ptr ) {
|
---|
67 | int i;
|
---|
68 | if ( sscanf( ptr, "%d", &i ) == EOF ) {}
|
---|
69 | return i;
|
---|
70 | } // ato
|
---|
71 |
|
---|
72 | unsigned int ato( const char * ptr ) {
|
---|
73 | unsigned int ui;
|
---|
74 | if ( sscanf( ptr, "%u", &ui ) == EOF ) {}
|
---|
75 | return ui;
|
---|
76 | } // ato
|
---|
77 |
|
---|
78 | long int ato( const char * ptr ) {
|
---|
79 | long int li;
|
---|
80 | if ( sscanf( ptr, "%ld", &li ) == EOF ) {}
|
---|
81 | return li;
|
---|
82 | } // ato
|
---|
83 |
|
---|
84 | unsigned long int ato( const char * ptr ) {
|
---|
85 | unsigned long int uli;
|
---|
86 | if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}
|
---|
87 | return uli;
|
---|
88 | } // ato
|
---|
89 |
|
---|
90 | long long int ato( const char * ptr ) {
|
---|
91 | long long int lli;
|
---|
92 | if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}
|
---|
93 | return lli;
|
---|
94 | } // ato
|
---|
95 |
|
---|
96 | unsigned long long int ato( const char * ptr ) {
|
---|
97 | unsigned long long int ulli;
|
---|
98 | if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {}
|
---|
99 | return ulli;
|
---|
100 | } // ato
|
---|
101 |
|
---|
102 |
|
---|
103 | float ato( const char * ptr ) {
|
---|
104 | float f;
|
---|
105 | if ( sscanf( ptr, "%f", &f ) == EOF ) {}
|
---|
106 | return f;
|
---|
107 | } // ato
|
---|
108 |
|
---|
109 | double ato( const char * ptr ) {
|
---|
110 | double d;
|
---|
111 | if ( sscanf( ptr, "%lf", &d ) == EOF ) {}
|
---|
112 | return d;
|
---|
113 | } // ato
|
---|
114 |
|
---|
115 | long double ato( const char * ptr ) {
|
---|
116 | long double ld;
|
---|
117 | if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {}
|
---|
118 | return ld;
|
---|
119 | } // ato
|
---|
120 |
|
---|
121 |
|
---|
122 | float _Complex ato( const char * ptr ) {
|
---|
123 | float re, im;
|
---|
124 | if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {}
|
---|
125 | return re + im * _Complex_I;
|
---|
126 | } // ato
|
---|
127 |
|
---|
128 | double _Complex ato( const char * ptr ) {
|
---|
129 | double re, im;
|
---|
130 | if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {}
|
---|
131 | return re + im * _Complex_I;
|
---|
132 | } // ato
|
---|
133 |
|
---|
134 | long double _Complex ato( const char * ptr ) {
|
---|
135 | long double re, im;
|
---|
136 | if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {}
|
---|
137 | return re + im * _Complex_I;
|
---|
138 | } // ato
|
---|
139 |
|
---|
140 |
|
---|
141 | int strto( const char * sptr, char ** eptr, int base ) {
|
---|
142 | return (int)strtol( sptr, eptr, base );
|
---|
143 | } // strto
|
---|
144 |
|
---|
145 | unsigned int strto( const char * sptr, char ** eptr, int base ) {
|
---|
146 | return (unsigned int)strtoul( sptr, eptr, base );
|
---|
147 | } // strto
|
---|
148 |
|
---|
149 | long int strto( const char * sptr, char ** eptr, int base ) {
|
---|
150 | return strtol( sptr, eptr, base );
|
---|
151 | } // strto
|
---|
152 |
|
---|
153 | unsigned long int strto( const char * sptr, char ** eptr, int base ) {
|
---|
154 | return strtoul( sptr, eptr, base );
|
---|
155 | } // strto
|
---|
156 |
|
---|
157 | long long int strto( const char * sptr, char ** eptr, int base ) {
|
---|
158 | return strtoll( sptr, eptr, base );
|
---|
159 | } // strto
|
---|
160 |
|
---|
161 | unsigned long long int strto( const char * sptr, char ** eptr, int base ) {
|
---|
162 | return strtoull( sptr, eptr, base );
|
---|
163 | } // strto
|
---|
164 |
|
---|
165 |
|
---|
166 | float strto( const char * sptr, char ** eptr ) {
|
---|
167 | return strtof( sptr, eptr );
|
---|
168 | } // strto
|
---|
169 |
|
---|
170 | double strto( const char * sptr, char ** eptr ) {
|
---|
171 | return strtod( sptr, eptr );
|
---|
172 | } // strto
|
---|
173 |
|
---|
174 | long double strto( const char * sptr, char ** eptr ) {
|
---|
175 | return strtold( sptr, eptr );
|
---|
176 | } // strto
|
---|
177 |
|
---|
178 |
|
---|
179 | float _Complex strto( const char * sptr, char ** eptr ) {
|
---|
180 | float re, im;
|
---|
181 | re = strtof( sptr, eptr );
|
---|
182 | if ( sptr == *eptr ) return 0.0;
|
---|
183 | im = strtof( sptr, eptr );
|
---|
184 | if ( sptr == *eptr ) return 0.0;
|
---|
185 | return re + im * _Complex_I;
|
---|
186 | } // strto
|
---|
187 |
|
---|
188 | double _Complex strto( const char * sptr, char ** eptr ) {
|
---|
189 | double re, im;
|
---|
190 | re = strtod( sptr, eptr );
|
---|
191 | if ( sptr == *eptr ) return 0.0;
|
---|
192 | im = strtod( sptr, eptr );
|
---|
193 | if ( sptr == *eptr ) return 0.0;
|
---|
194 | return re + im * _Complex_I;
|
---|
195 | } // strto
|
---|
196 |
|
---|
197 | long double _Complex strto( const char * sptr, char ** eptr ) {
|
---|
198 | long double re, im;
|
---|
199 | re = strtold( sptr, eptr );
|
---|
200 | if ( sptr == *eptr ) return 0.0;
|
---|
201 | im = strtold( sptr, eptr );
|
---|
202 | if ( sptr == *eptr ) return 0.0;
|
---|
203 | return re + im * _Complex_I;
|
---|
204 | } // strto
|
---|
205 |
|
---|
206 | //---------------------------------------
|
---|
207 |
|
---|
208 | forall( otype T | { int ?<?( T, T ); } )
|
---|
209 | T * bsearch( T key, const T * arr, size_t dim ) {
|
---|
210 | int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
|
---|
211 | return (T *)bsearch( &key, arr, dim, sizeof(T), comp );
|
---|
212 | } // bsearch
|
---|
213 |
|
---|
214 | forall( otype T | { int ?<?( T, T ); } )
|
---|
215 | unsigned int bsearch( T key, const T * arr, size_t dim ) {
|
---|
216 | T *result = bsearch( key, arr, dim );
|
---|
217 | return result ? result - arr : dim; // pointer subtraction includes sizeof(T)
|
---|
218 | } // bsearch
|
---|
219 |
|
---|
220 | forall( otype T | { int ?<?( T, T ); } )
|
---|
221 | void qsort( const T * arr, size_t dim ) {
|
---|
222 | int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
|
---|
223 | qsort( arr, dim, sizeof(T), comp );
|
---|
224 | } // qsort
|
---|
225 |
|
---|
226 | //---------------------------------------
|
---|
227 |
|
---|
228 | forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
|
---|
229 | [ T, T ] div( T t1, T t2 ) { return [ t1 / t2, t1 % t2 ]; }
|
---|
230 |
|
---|
231 | //---------------------------------------
|
---|
232 |
|
---|
233 | unsigned char abs( signed char v ) { return abs( (int)v ); }
|
---|
234 | unsigned long int abs( long int v ) { return labs( v ); }
|
---|
235 | unsigned long long int abs( long long int v ) { return llabs( v ); }
|
---|
236 | float abs( float x ) { return fabsf( x ); }
|
---|
237 | double abs( double x ) { return fabs( x ); }
|
---|
238 | long double abs( long double x ) { return fabsl( x ); }
|
---|
239 | float abs( float _Complex x ) { return cabsf( x ); }
|
---|
240 | double abs( double _Complex x ) { return cabs( x ); }
|
---|
241 | long double abs( long double _Complex x ) { return cabsl( x ); }
|
---|
242 |
|
---|
243 | //---------------------------------------
|
---|
244 |
|
---|
245 | void rand48seed( long int s ) { srand48( s ); }
|
---|
246 | char rand48( void ) { return mrand48(); }
|
---|
247 | int rand48( void ) { return mrand48(); }
|
---|
248 | unsigned int rand48( void ) { return lrand48(); }
|
---|
249 | long int rand48( void ) { return mrand48(); }
|
---|
250 | unsigned long int rand48( void ) { return lrand48(); }
|
---|
251 | float rand48( void ) { return (float)drand48(); } // otherwise float uses lrand48
|
---|
252 | double rand48( void ) { return drand48(); }
|
---|
253 | float _Complex rand48( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
|
---|
254 | double _Complex rand48( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
|
---|
255 | long double _Complex rand48( void) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
|
---|
256 |
|
---|
257 | //---------------------------------------
|
---|
258 |
|
---|
259 | forall( otype T | { int ?<?( T, T ); } )
|
---|
260 | T min( T t1, T t2 ) {
|
---|
261 | return t1 < t2 ? t1 : t2;
|
---|
262 | } // min
|
---|
263 |
|
---|
264 | forall( otype T | { int ?>?( T, T ); } )
|
---|
265 | T max( T t1, T t2 ) {
|
---|
266 | return t1 > t2 ? t1 : t2;
|
---|
267 | } // max
|
---|
268 |
|
---|
269 | forall( otype T | { T min( T, T ); T max( T, T ); } )
|
---|
270 | T clamp( T value, T min_val, T max_val ) {
|
---|
271 | return max( min_val, min( value, max_val ) );
|
---|
272 | } // clamp
|
---|
273 |
|
---|
274 | forall( otype T )
|
---|
275 | void swap( T * t1, T * t2 ) {
|
---|
276 | T temp = *t1;
|
---|
277 | *t1 = *t2;
|
---|
278 | *t2 = temp;
|
---|
279 | } // swap
|
---|
280 |
|
---|
281 | // Local Variables: //
|
---|
282 | // tab-width: 4 //
|
---|
283 | // End: //
|
---|