| [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 | 
|---|
| [f3ddc21] | 12 | // Last Modified On : Tue May  9 08:43:00 2017 | 
|---|
|  | 13 | // Update Count     : 191 | 
|---|
| [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 | 
|---|
|  | 23 | #include <string.h>                                                                             // memset | 
|---|
|  | 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 |  | 
|---|
| [f3ddc21] | 29 | forall( dtype T | sized(T) ) T * malloc( void ) {               // type-safe | 
|---|
|  | 30 | return (T *)(void *)malloc( (size_t)sizeof(T) ); | 
|---|
| [bd85400] | 31 | } // malloc | 
|---|
| [f3ddc21] | 32 |  | 
|---|
|  | 33 | forall( dtype T | sized(T) ) T * malloc( char fill ) {  // initial with fill value (like calloc) | 
|---|
|  | 34 | T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) ); | 
|---|
| [6b6597c] | 35 | return memset( ptr, (int)fill, sizeof(T) ); | 
|---|
| [bd85400] | 36 | } // malloc | 
|---|
|  | 37 |  | 
|---|
| [f3ddc21] | 38 | forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size ) { // alternative realloc | 
|---|
|  | 39 | return (T *)realloc( ptr, size ); | 
|---|
|  | 40 | } // malloc | 
|---|
|  | 41 |  | 
|---|
|  | 42 | forall( dtype T | sized(T) ) T * malloc( T * ptr, size_t size, unsigned char fill ) { // alternative realloc with fill value | 
|---|
|  | 43 | return (T *)realloc( ptr, size, fill ); | 
|---|
|  | 44 | } // malloc | 
|---|
|  | 45 |  | 
|---|
|  | 46 |  | 
|---|
|  | 47 | forall( dtype T | sized(T) ) T * calloc( size_t nmemb ) { // type-safe array initialization with fill 0 | 
|---|
| [45161b4d] | 48 | return (T *)calloc( nmemb, sizeof(T) ); | 
|---|
| [bd85400] | 49 | } // calloc | 
|---|
|  | 50 |  | 
|---|
| [f3ddc21] | 51 |  | 
|---|
|  | 52 | forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) { // type-safe | 
|---|
| [bd85400] | 53 | return (T *)(void *)realloc( (void *)ptr, size ); | 
|---|
|  | 54 | } // realloc | 
|---|
| [f3ddc21] | 55 |  | 
|---|
|  | 56 | forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size, unsigned char fill ) { // alternative realloc with fill value | 
|---|
| [bd85400] | 57 | char * nptr = (T *)(void *)realloc( (void *)ptr, size ); | 
|---|
|  | 58 | size_t unused = malloc_usable_size( nptr ); | 
|---|
|  | 59 | memset( nptr + size - unused, (int)fill, unused );  // initialize any new storage | 
|---|
|  | 60 | return nptr; | 
|---|
|  | 61 | } // realloc | 
|---|
|  | 62 |  | 
|---|
|  | 63 |  | 
|---|
| [f3ddc21] | 64 | forall( dtype T | sized(T) ) T * aligned_alloc( size_t alignment ) { // aligned allocation | 
|---|
| [bd85400] | 65 | return (T *)memalign( alignment, sizeof(T) ); | 
|---|
|  | 66 | } // aligned_alloc | 
|---|
|  | 67 |  | 
|---|
| [59239b8] | 68 | forall( dtype T | sized(T) ) T * memalign( size_t alignment ) { | 
|---|
| [bd85400] | 69 | return (T *)memalign( alignment, sizeof(T) ); | 
|---|
|  | 70 | } // memalign | 
|---|
|  | 71 |  | 
|---|
| [59239b8] | 72 | forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t alignment ) { | 
|---|
| [bd85400] | 73 | return posix_memalign( (void **)ptr, alignment, sizeof(T) ); | 
|---|
|  | 74 | } // posix_memalign | 
|---|
|  | 75 |  | 
|---|
| [f3ddc21] | 76 |  | 
|---|
|  | 77 | forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } ) //  new | 
|---|
| [627f585] | 78 | T * new( Params p ) { | 
|---|
| [02d241f] | 79 | return ((T *)malloc()){ p }; | 
|---|
| [f3ddc21] | 80 | } // new | 
|---|
| [627f585] | 81 |  | 
|---|
| [f3ddc21] | 82 | forall( dtype T | { void ^?{}(T *); } )                                 // delete | 
|---|
| [627f585] | 83 | void delete( T * ptr ) { | 
|---|
| [f3ddc21] | 84 | if ( ptr ) { | 
|---|
|  | 85 | ^ptr{}; | 
|---|
|  | 86 | free( ptr ); | 
|---|
|  | 87 | } | 
|---|
|  | 88 | } // delete | 
|---|
| [627f585] | 89 |  | 
|---|
| [bf76eab] | 90 | forall( dtype T, ttype Params | { void ^?{}(T *); void delete(Params); } ) | 
|---|
|  | 91 | void delete( T * ptr, Params rest ) { | 
|---|
|  | 92 | if ( ptr ) { | 
|---|
|  | 93 | ^ptr{}; | 
|---|
|  | 94 | free( ptr ); | 
|---|
|  | 95 | } | 
|---|
|  | 96 | delete( rest ); | 
|---|
| [f3ddc21] | 97 | } // delete | 
|---|
| [bf76eab] | 98 |  | 
|---|
| [bd85400] | 99 | //--------------------------------------- | 
|---|
|  | 100 |  | 
|---|
|  | 101 | int ato( const char * ptr ) { | 
|---|
|  | 102 | int i; | 
|---|
| [6e991d6] | 103 | if ( sscanf( ptr, "%d", &i ) == EOF ) {} | 
|---|
| [bd85400] | 104 | return i; | 
|---|
| [f3ddc21] | 105 | } // ato | 
|---|
|  | 106 |  | 
|---|
| [bd85400] | 107 | unsigned int ato( const char * ptr ) { | 
|---|
|  | 108 | unsigned int ui; | 
|---|
| [6e991d6] | 109 | if ( sscanf( ptr, "%u", &ui ) == EOF ) {} | 
|---|
| [bd85400] | 110 | return ui; | 
|---|
| [f3ddc21] | 111 | } // ato | 
|---|
|  | 112 |  | 
|---|
| [bd85400] | 113 | long int ato( const char * ptr ) { | 
|---|
|  | 114 | long int li; | 
|---|
| [6e991d6] | 115 | if ( sscanf( ptr, "%ld", &li ) == EOF ) {} | 
|---|
| [bd85400] | 116 | return li; | 
|---|
| [f3ddc21] | 117 | } // ato | 
|---|
|  | 118 |  | 
|---|
| [bd85400] | 119 | unsigned long int ato( const char * ptr ) { | 
|---|
|  | 120 | unsigned long int uli; | 
|---|
| [6e991d6] | 121 | if ( sscanf( ptr, "%lu", &uli ) == EOF ) {} | 
|---|
| [bd85400] | 122 | return uli; | 
|---|
| [f3ddc21] | 123 | } // ato | 
|---|
|  | 124 |  | 
|---|
| [bd85400] | 125 | long long int ato( const char * ptr ) { | 
|---|
|  | 126 | long long int lli; | 
|---|
| [6e991d6] | 127 | if ( sscanf( ptr, "%lld", &lli ) == EOF ) {} | 
|---|
| [bd85400] | 128 | return lli; | 
|---|
| [f3ddc21] | 129 | } // ato | 
|---|
|  | 130 |  | 
|---|
| [bd85400] | 131 | unsigned long long int ato( const char * ptr ) { | 
|---|
|  | 132 | unsigned long long int ulli; | 
|---|
| [6e991d6] | 133 | if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {} | 
|---|
| [bd85400] | 134 | return ulli; | 
|---|
| [f3ddc21] | 135 | } // ato | 
|---|
|  | 136 |  | 
|---|
| [90c3b1c] | 137 |  | 
|---|
| [bd85400] | 138 | float ato( const char * ptr ) { | 
|---|
|  | 139 | float f; | 
|---|
| [6e991d6] | 140 | if ( sscanf( ptr, "%f", &f ) == EOF ) {} | 
|---|
| [bd85400] | 141 | return f; | 
|---|
| [f3ddc21] | 142 | } // ato | 
|---|
|  | 143 |  | 
|---|
| [bd85400] | 144 | double ato( const char * ptr ) { | 
|---|
|  | 145 | double d; | 
|---|
| [6e991d6] | 146 | if ( sscanf( ptr, "%lf", &d ) == EOF ) {} | 
|---|
| [bd85400] | 147 | return d; | 
|---|
| [f3ddc21] | 148 | } // ato | 
|---|
|  | 149 |  | 
|---|
| [bd85400] | 150 | long double ato( const char * ptr ) { | 
|---|
|  | 151 | long double ld; | 
|---|
| [6e991d6] | 152 | if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {} | 
|---|
| [bd85400] | 153 | return ld; | 
|---|
| [f3ddc21] | 154 | } // ato | 
|---|
|  | 155 |  | 
|---|
| [90c3b1c] | 156 |  | 
|---|
| [bd85400] | 157 | float _Complex ato( const char * ptr ) { | 
|---|
|  | 158 | float re, im; | 
|---|
| [6e991d6] | 159 | if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {} | 
|---|
| [bd85400] | 160 | return re + im * _Complex_I; | 
|---|
| [f3ddc21] | 161 | } // ato | 
|---|
|  | 162 |  | 
|---|
| [bd85400] | 163 | double _Complex ato( const char * ptr ) { | 
|---|
|  | 164 | 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 | long double _Complex ato( const char * ptr ) { | 
|---|
|  | 170 | long double re, im; | 
|---|
| [6e991d6] | 171 | if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {} | 
|---|
| [bd85400] | 172 | return re + im * _Complex_I; | 
|---|
| [f3ddc21] | 173 | } // ato | 
|---|
|  | 174 |  | 
|---|
| [bd85400] | 175 |  | 
|---|
|  | 176 | int strto( const char * sptr, char ** eptr, int base ) { | 
|---|
|  | 177 | return (int)strtol( sptr, eptr, base ); | 
|---|
| [f3ddc21] | 178 | } // strto | 
|---|
|  | 179 |  | 
|---|
| [bd85400] | 180 | unsigned int strto( const char * sptr, char ** eptr, int base ) { | 
|---|
|  | 181 | return (unsigned int)strtoul( sptr, eptr, base ); | 
|---|
| [f3ddc21] | 182 | } // strto | 
|---|
|  | 183 |  | 
|---|
| [bd85400] | 184 | long int strto( const char * sptr, char ** eptr, int base ) { | 
|---|
|  | 185 | return strtol( sptr, eptr, base ); | 
|---|
| [f3ddc21] | 186 | } // strto | 
|---|
|  | 187 |  | 
|---|
| [bd85400] | 188 | unsigned long int strto( const char * sptr, char ** eptr, int base ) { | 
|---|
|  | 189 | return strtoul( sptr, eptr, base ); | 
|---|
| [f3ddc21] | 190 | } // strto | 
|---|
|  | 191 |  | 
|---|
| [bd85400] | 192 | long long int strto( const char * sptr, char ** eptr, int base ) { | 
|---|
|  | 193 | return strtoll( sptr, eptr, base ); | 
|---|
| [f3ddc21] | 194 | } // strto | 
|---|
|  | 195 |  | 
|---|
| [bd85400] | 196 | unsigned long long int strto( const char * sptr, char ** eptr, int base ) { | 
|---|
|  | 197 | return strtoull( sptr, eptr, base ); | 
|---|
| [f3ddc21] | 198 | } // strto | 
|---|
|  | 199 |  | 
|---|
| [90c3b1c] | 200 |  | 
|---|
| [bd85400] | 201 | float strto( const char * sptr, char ** eptr ) { | 
|---|
|  | 202 | return strtof( sptr, eptr ); | 
|---|
| [f3ddc21] | 203 | } // strto | 
|---|
|  | 204 |  | 
|---|
| [bd85400] | 205 | double strto( const char * sptr, char ** eptr ) { | 
|---|
|  | 206 | return strtod( sptr, eptr ); | 
|---|
| [f3ddc21] | 207 | } // strto | 
|---|
|  | 208 |  | 
|---|
| [bd85400] | 209 | long double strto( const char * sptr, char ** eptr ) { | 
|---|
|  | 210 | return strtold( sptr, eptr ); | 
|---|
| [f3ddc21] | 211 | } // strto | 
|---|
|  | 212 |  | 
|---|
| [90c3b1c] | 213 |  | 
|---|
| [bd85400] | 214 | float _Complex strto( const char * sptr, char ** eptr ) { | 
|---|
|  | 215 | float re, im; | 
|---|
|  | 216 | re = strtof( sptr, eptr ); | 
|---|
|  | 217 | if ( sptr == *eptr ) return 0.0; | 
|---|
|  | 218 | im = strtof( sptr, eptr ); | 
|---|
|  | 219 | if ( sptr == *eptr ) return 0.0; | 
|---|
|  | 220 | return re + im * _Complex_I; | 
|---|
| [f3ddc21] | 221 | } // strto | 
|---|
|  | 222 |  | 
|---|
| [bd85400] | 223 | double _Complex strto( const char * sptr, char ** eptr ) { | 
|---|
|  | 224 | double re, im; | 
|---|
|  | 225 | re = strtod( sptr, eptr ); | 
|---|
|  | 226 | if ( sptr == *eptr ) return 0.0; | 
|---|
|  | 227 | im = strtod( sptr, eptr ); | 
|---|
|  | 228 | if ( sptr == *eptr ) return 0.0; | 
|---|
|  | 229 | return re + im * _Complex_I; | 
|---|
| [f3ddc21] | 230 | } // strto | 
|---|
|  | 231 |  | 
|---|
| [bd85400] | 232 | long double _Complex strto( const char * sptr, char ** eptr ) { | 
|---|
|  | 233 | long double re, im; | 
|---|
|  | 234 | re = strtold( sptr, eptr ); | 
|---|
|  | 235 | if ( sptr == *eptr ) return 0.0; | 
|---|
|  | 236 | im = strtold( sptr, eptr ); | 
|---|
|  | 237 | if ( sptr == *eptr ) return 0.0; | 
|---|
|  | 238 | return re + im * _Complex_I; | 
|---|
| [f3ddc21] | 239 | } // strto | 
|---|
| [bd85400] | 240 |  | 
|---|
|  | 241 | //--------------------------------------- | 
|---|
|  | 242 |  | 
|---|
| [4040425] | 243 | forall( otype T | { int ?<?( T, T ); } ) | 
|---|
| [a797e2b1] | 244 | T * bsearch( T key, const T * arr, size_t dimension ) { | 
|---|
| [bd85400] | 245 | int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; } | 
|---|
|  | 246 | return (T *)bsearch( &key, arr, dimension, sizeof(T), comp ); | 
|---|
|  | 247 | } // bsearch | 
|---|
|  | 248 |  | 
|---|
| [707446a] | 249 | forall( otype T | { int ?<?( T, T ); } ) | 
|---|
|  | 250 | unsigned int bsearch( T key, const T * arr, size_t dimension ) { | 
|---|
| [02d241f] | 251 | T *result = bsearch( key, arr, dimension ); | 
|---|
| [707446a] | 252 | return result ? result - arr : dimension;                       // pointer subtraction includes sizeof(T) | 
|---|
|  | 253 | } // bsearch | 
|---|
|  | 254 |  | 
|---|
| [4040425] | 255 | forall( otype T | { int ?<?( T, T ); } ) | 
|---|
| [bd85400] | 256 | void qsort( const T * arr, size_t dimension ) { | 
|---|
|  | 257 | int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; } | 
|---|
|  | 258 | qsort( arr, dimension, sizeof(T), comp ); | 
|---|
|  | 259 | } // qsort | 
|---|
|  | 260 |  | 
|---|
|  | 261 | //--------------------------------------- | 
|---|
|  | 262 |  | 
|---|
| [43385ca] | 263 | forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } ) | 
|---|
|  | 264 | [ T, T ] div( T t1, T t2 ) { return [ t1 / t2, t1 % t2 ]; } | 
|---|
| [bd85400] | 265 |  | 
|---|
|  | 266 | //--------------------------------------- | 
|---|
|  | 267 |  | 
|---|
| [c3ebf37] | 268 | unsigned char abs( signed char v ) { return abs( (int)v ); } | 
|---|
|  | 269 | unsigned long int abs( long int v ) { return labs( v ); } | 
|---|
|  | 270 | unsigned long long int abs( long long int v ) { return llabs( v ); } | 
|---|
| [6e991d6] | 271 | float abs( float x ) { return fabsf( x ); } | 
|---|
|  | 272 | double abs( double x ) { return fabs( x ); } | 
|---|
|  | 273 | long double abs( long double x ) { return fabsl( x ); } | 
|---|
|  | 274 | float abs( float _Complex x ) { return cabsf( x ); } | 
|---|
|  | 275 | double abs( double _Complex x ) { return cabs( x ); } | 
|---|
|  | 276 | long double abs( long double _Complex x ) { return cabsl( x ); } | 
|---|
| [53ba273] | 277 |  | 
|---|
|  | 278 | //--------------------------------------- | 
|---|
|  | 279 |  | 
|---|
| [0438091] | 280 | void rand48seed( long int s ) { srand48( s ); } | 
|---|
| [3cfe27f] | 281 | char rand48() { return mrand48(); } | 
|---|
| [0438091] | 282 | int rand48() { return mrand48(); } | 
|---|
|  | 283 | unsigned int rand48() { return lrand48(); } | 
|---|
|  | 284 | long int rand48() { return mrand48(); } | 
|---|
|  | 285 | unsigned long int rand48() { return lrand48(); } | 
|---|
|  | 286 | float rand48() { return (float)drand48(); }                             // otherwise float uses lrand48 | 
|---|
|  | 287 | double rand48() { return drand48(); } | 
|---|
|  | 288 | float _Complex rand48() { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); } | 
|---|
|  | 289 | double _Complex rand48() { return drand48() + (double _Complex)(drand48() * _Complex_I); } | 
|---|
|  | 290 | long double _Complex rand48() { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); } | 
|---|
| [bd85400] | 291 |  | 
|---|
|  | 292 | //--------------------------------------- | 
|---|
|  | 293 |  | 
|---|
| [4040425] | 294 | forall( otype T | { int ?<?( T, T ); } ) | 
|---|
| [a797e2b1] | 295 | T min( T t1, T t2 ) { | 
|---|
| [bd85400] | 296 | return t1 < t2 ? t1 : t2; | 
|---|
|  | 297 | } // min | 
|---|
|  | 298 |  | 
|---|
| [4040425] | 299 | forall( otype T | { int ?>?( T, T ); } ) | 
|---|
| [a797e2b1] | 300 | T max( T t1, T t2 ) { | 
|---|
| [bd85400] | 301 | return t1 > t2 ? t1 : t2; | 
|---|
|  | 302 | } // max | 
|---|
|  | 303 |  | 
|---|
| [a9f2c13] | 304 | forall( otype T | { T min( T, T ); T max( T, T ); } ) | 
|---|
| [a797e2b1] | 305 | T clamp( T value, T min_val, T max_val ) { | 
|---|
| [a9f2c13] | 306 | return max( min_val, min( value, max_val ) ); | 
|---|
|  | 307 | } // clamp | 
|---|
|  | 308 |  | 
|---|
| [4040425] | 309 | forall( otype T ) | 
|---|
| [bd85400] | 310 | void swap( T * t1, T * t2 ) { | 
|---|
|  | 311 | T temp = *t1; | 
|---|
|  | 312 | *t1 = *t2; | 
|---|
|  | 313 | *t2 = temp; | 
|---|
|  | 314 | } // swap | 
|---|
|  | 315 |  | 
|---|
|  | 316 | // Local Variables: // | 
|---|
|  | 317 | // tab-width: 4 // | 
|---|
|  | 318 | // End: // | 
|---|