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