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