[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 |
---|
[f3fc631f] | 12 | // Last Modified On : Tue May 30 09:07:56 2017 |
---|
| 13 | // Update Count : 237 |
---|
[bd85400] | 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 |
---|
[f3fc631f] | 23 | #include <string.h> // memcpy, memset |
---|
[bd85400] | 24 | #include <malloc.h> // malloc_usable_size |
---|
| 25 | #include <math.h> // fabsf, fabs, fabsl |
---|
[6e991d6] | 26 | #include <complex.h> // _Complex_I |
---|
[bd85400] | 27 | } // extern "C" |
---|
| 28 | |
---|
[f3fc631f] | 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; |
---|
[bd85400] | 39 | } // realloc |
---|
[f3ddc21] | 40 | |
---|
[f3fc631f] | 41 | // allocation/deallocation and constructor/destructor |
---|
| 42 | forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } ) |
---|
[627f585] | 43 | T * new( Params p ) { |
---|
[02d241f] | 44 | return ((T *)malloc()){ p }; |
---|
[f3ddc21] | 45 | } // new |
---|
[627f585] | 46 | |
---|
[f3fc631f] | 47 | forall( dtype T | { void ^?{}( T * ); } ) |
---|
[627f585] | 48 | void delete( T * ptr ) { |
---|
[f3ddc21] | 49 | if ( ptr ) { |
---|
[f3fc631f] | 50 | ^ptr{}; // run destructor |
---|
[f3ddc21] | 51 | free( ptr ); |
---|
[f3fc631f] | 52 | } // if |
---|
[f3ddc21] | 53 | } // delete |
---|
[627f585] | 54 | |
---|
[f3fc631f] | 55 | forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } ) |
---|
[bf76eab] | 56 | void delete( T * ptr, Params rest ) { |
---|
| 57 | if ( ptr ) { |
---|
[f3fc631f] | 58 | ^ptr{}; // run destructor |
---|
[bf76eab] | 59 | free( ptr ); |
---|
[f3fc631f] | 60 | } // if |
---|
[bf76eab] | 61 | delete( rest ); |
---|
[f3ddc21] | 62 | } // delete |
---|
[bf76eab] | 63 | |
---|
[bd85400] | 64 | //--------------------------------------- |
---|
| 65 | |
---|
| 66 | int ato( const char * ptr ) { |
---|
| 67 | int i; |
---|
[6e991d6] | 68 | if ( sscanf( ptr, "%d", &i ) == EOF ) {} |
---|
[bd85400] | 69 | return i; |
---|
[f3ddc21] | 70 | } // ato |
---|
| 71 | |
---|
[bd85400] | 72 | unsigned int ato( const char * ptr ) { |
---|
| 73 | unsigned int ui; |
---|
[6e991d6] | 74 | if ( sscanf( ptr, "%u", &ui ) == EOF ) {} |
---|
[bd85400] | 75 | return ui; |
---|
[f3ddc21] | 76 | } // ato |
---|
| 77 | |
---|
[bd85400] | 78 | long int ato( const char * ptr ) { |
---|
| 79 | long int li; |
---|
[6e991d6] | 80 | if ( sscanf( ptr, "%ld", &li ) == EOF ) {} |
---|
[bd85400] | 81 | return li; |
---|
[f3ddc21] | 82 | } // ato |
---|
| 83 | |
---|
[bd85400] | 84 | unsigned long int ato( const char * ptr ) { |
---|
| 85 | unsigned long int uli; |
---|
[6e991d6] | 86 | if ( sscanf( ptr, "%lu", &uli ) == EOF ) {} |
---|
[bd85400] | 87 | return uli; |
---|
[f3ddc21] | 88 | } // ato |
---|
| 89 | |
---|
[bd85400] | 90 | long long int ato( const char * ptr ) { |
---|
| 91 | long long int lli; |
---|
[6e991d6] | 92 | if ( sscanf( ptr, "%lld", &lli ) == EOF ) {} |
---|
[bd85400] | 93 | return lli; |
---|
[f3ddc21] | 94 | } // ato |
---|
| 95 | |
---|
[bd85400] | 96 | unsigned long long int ato( const char * ptr ) { |
---|
| 97 | unsigned long long int ulli; |
---|
[6e991d6] | 98 | if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {} |
---|
[bd85400] | 99 | return ulli; |
---|
[f3ddc21] | 100 | } // ato |
---|
| 101 | |
---|
[90c3b1c] | 102 | |
---|
[bd85400] | 103 | float ato( const char * ptr ) { |
---|
| 104 | float f; |
---|
[6e991d6] | 105 | if ( sscanf( ptr, "%f", &f ) == EOF ) {} |
---|
[bd85400] | 106 | return f; |
---|
[f3ddc21] | 107 | } // ato |
---|
| 108 | |
---|
[bd85400] | 109 | double ato( const char * ptr ) { |
---|
| 110 | double d; |
---|
[6e991d6] | 111 | if ( sscanf( ptr, "%lf", &d ) == EOF ) {} |
---|
[bd85400] | 112 | return d; |
---|
[f3ddc21] | 113 | } // ato |
---|
| 114 | |
---|
[bd85400] | 115 | long double ato( const char * ptr ) { |
---|
| 116 | long double ld; |
---|
[6e991d6] | 117 | if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {} |
---|
[bd85400] | 118 | return ld; |
---|
[f3ddc21] | 119 | } // ato |
---|
| 120 | |
---|
[90c3b1c] | 121 | |
---|
[bd85400] | 122 | float _Complex ato( const char * ptr ) { |
---|
| 123 | float re, im; |
---|
[6e991d6] | 124 | if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {} |
---|
[bd85400] | 125 | return re + im * _Complex_I; |
---|
[f3ddc21] | 126 | } // ato |
---|
| 127 | |
---|
[bd85400] | 128 | double _Complex ato( const char * ptr ) { |
---|
| 129 | double re, im; |
---|
[6e991d6] | 130 | if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {} |
---|
[bd85400] | 131 | return re + im * _Complex_I; |
---|
[f3ddc21] | 132 | } // ato |
---|
| 133 | |
---|
[bd85400] | 134 | long double _Complex ato( const char * ptr ) { |
---|
| 135 | long double re, im; |
---|
[6e991d6] | 136 | if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {} |
---|
[bd85400] | 137 | return re + im * _Complex_I; |
---|
[f3ddc21] | 138 | } // ato |
---|
| 139 | |
---|
[bd85400] | 140 | |
---|
| 141 | int strto( const char * sptr, char ** eptr, int base ) { |
---|
| 142 | return (int)strtol( sptr, eptr, base ); |
---|
[f3ddc21] | 143 | } // strto |
---|
| 144 | |
---|
[bd85400] | 145 | unsigned int strto( const char * sptr, char ** eptr, int base ) { |
---|
| 146 | return (unsigned int)strtoul( sptr, eptr, base ); |
---|
[f3ddc21] | 147 | } // strto |
---|
| 148 | |
---|
[bd85400] | 149 | long int strto( const char * sptr, char ** eptr, int base ) { |
---|
| 150 | return strtol( sptr, eptr, base ); |
---|
[f3ddc21] | 151 | } // strto |
---|
| 152 | |
---|
[bd85400] | 153 | unsigned long int strto( const char * sptr, char ** eptr, int base ) { |
---|
| 154 | return strtoul( sptr, eptr, base ); |
---|
[f3ddc21] | 155 | } // strto |
---|
| 156 | |
---|
[bd85400] | 157 | long long int strto( const char * sptr, char ** eptr, int base ) { |
---|
| 158 | return strtoll( sptr, eptr, base ); |
---|
[f3ddc21] | 159 | } // strto |
---|
| 160 | |
---|
[bd85400] | 161 | unsigned long long int strto( const char * sptr, char ** eptr, int base ) { |
---|
| 162 | return strtoull( sptr, eptr, base ); |
---|
[f3ddc21] | 163 | } // strto |
---|
| 164 | |
---|
[90c3b1c] | 165 | |
---|
[bd85400] | 166 | float strto( const char * sptr, char ** eptr ) { |
---|
| 167 | return strtof( sptr, eptr ); |
---|
[f3ddc21] | 168 | } // strto |
---|
| 169 | |
---|
[bd85400] | 170 | double strto( const char * sptr, char ** eptr ) { |
---|
| 171 | return strtod( sptr, eptr ); |
---|
[f3ddc21] | 172 | } // strto |
---|
| 173 | |
---|
[bd85400] | 174 | long double strto( const char * sptr, char ** eptr ) { |
---|
| 175 | return strtold( sptr, eptr ); |
---|
[f3ddc21] | 176 | } // strto |
---|
| 177 | |
---|
[90c3b1c] | 178 | |
---|
[bd85400] | 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; |
---|
[f3ddc21] | 186 | } // strto |
---|
| 187 | |
---|
[bd85400] | 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; |
---|
[f3ddc21] | 195 | } // strto |
---|
| 196 | |
---|
[bd85400] | 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; |
---|
[f3ddc21] | 204 | } // strto |
---|
[bd85400] | 205 | |
---|
| 206 | //--------------------------------------- |
---|
| 207 | |
---|
[4040425] | 208 | forall( otype T | { int ?<?( T, T ); } ) |
---|
[f3fc631f] | 209 | T * bsearch( T key, const T * arr, size_t dim ) { |
---|
[bd85400] | 210 | int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; } |
---|
[f3fc631f] | 211 | return (T *)bsearch( &key, arr, dim, sizeof(T), comp ); |
---|
[bd85400] | 212 | } // bsearch |
---|
| 213 | |
---|
[707446a] | 214 | forall( otype T | { int ?<?( T, T ); } ) |
---|
[f3fc631f] | 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) |
---|
[707446a] | 218 | } // bsearch |
---|
| 219 | |
---|
[4040425] | 220 | forall( otype T | { int ?<?( T, T ); } ) |
---|
[f3fc631f] | 221 | void qsort( const T * arr, size_t dim ) { |
---|
[bd85400] | 222 | int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; } |
---|
[f3fc631f] | 223 | qsort( arr, dim, sizeof(T), comp ); |
---|
[bd85400] | 224 | } // qsort |
---|
| 225 | |
---|
| 226 | //--------------------------------------- |
---|
| 227 | |
---|
[43385ca] | 228 | forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } ) |
---|
| 229 | [ T, T ] div( T t1, T t2 ) { return [ t1 / t2, t1 % t2 ]; } |
---|
[bd85400] | 230 | |
---|
| 231 | //--------------------------------------- |
---|
| 232 | |
---|
[c3ebf37] | 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 ); } |
---|
[6e991d6] | 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 ); } |
---|
[53ba273] | 242 | |
---|
| 243 | //--------------------------------------- |
---|
| 244 | |
---|
[0438091] | 245 | void rand48seed( long int s ) { srand48( s ); } |
---|
[8dc51c8] | 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); } |
---|
[bd85400] | 256 | |
---|
| 257 | //--------------------------------------- |
---|
| 258 | |
---|
[4040425] | 259 | forall( otype T | { int ?<?( T, T ); } ) |
---|
[a797e2b1] | 260 | T min( T t1, T t2 ) { |
---|
[bd85400] | 261 | return t1 < t2 ? t1 : t2; |
---|
| 262 | } // min |
---|
| 263 | |
---|
[4040425] | 264 | forall( otype T | { int ?>?( T, T ); } ) |
---|
[a797e2b1] | 265 | T max( T t1, T t2 ) { |
---|
[bd85400] | 266 | return t1 > t2 ? t1 : t2; |
---|
| 267 | } // max |
---|
| 268 | |
---|
[a9f2c13] | 269 | forall( otype T | { T min( T, T ); T max( T, T ); } ) |
---|
[a797e2b1] | 270 | T clamp( T value, T min_val, T max_val ) { |
---|
[a9f2c13] | 271 | return max( min_val, min( value, max_val ) ); |
---|
| 272 | } // clamp |
---|
| 273 | |
---|
[4040425] | 274 | forall( otype T ) |
---|
[bd85400] | 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: // |
---|