| 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 : Wed Aug 23 20:30:44 2017
 | 
|---|
| 13 | // Update Count     : 292
 | 
|---|
| 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
 | 
|---|
| 22 | #include <string.h>                                                                             // memcpy, memset
 | 
|---|
| 23 | #include <malloc.h>                                                                             // malloc_usable_size
 | 
|---|
| 24 | #include <math.h>                                                                               // fabsf, fabs, fabsl
 | 
|---|
| 25 | #include <complex.h>                                                                    // _Complex_I
 | 
|---|
| 26 | 
 | 
|---|
| 27 | // resize, non-array types
 | 
|---|
| 28 | forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ) {
 | 
|---|
| 29 |         size_t olen = malloc_usable_size( ptr );                        // current allocation
 | 
|---|
| 30 |     char * nptr = (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
 | 
|---|
| 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
 | 
|---|
| 34 |         } //
 | 
|---|
| 35 |     return (T *)nptr;
 | 
|---|
| 36 | } // alloc
 | 
|---|
| 37 | 
 | 
|---|
| 38 | // allocation/deallocation and constructor/destructor, non-array types
 | 
|---|
| 39 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
 | 
|---|
| 40 | T * new( Params p ) {
 | 
|---|
| 41 |         return &(*malloc()){ p };                                                               // run constructor
 | 
|---|
| 42 | } // new
 | 
|---|
| 43 | 
 | 
|---|
| 44 | forall( dtype T | sized(T) | { void ^?{}( T & ); } )
 | 
|---|
| 45 | void delete( T * ptr ) {
 | 
|---|
| 46 |         if ( ptr ) {                                                                            // ignore null
 | 
|---|
| 47 |                 ^(*ptr){};                                                                                      // run destructor
 | 
|---|
| 48 |                 free( ptr );
 | 
|---|
| 49 |         } // if
 | 
|---|
| 50 | } // delete
 | 
|---|
| 51 | 
 | 
|---|
| 52 | forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } )
 | 
|---|
| 53 | void delete( T * ptr, Params rest ) {
 | 
|---|
| 54 |         if ( ptr ) {                                                                            // ignore null
 | 
|---|
| 55 |                 ^(*ptr){};                                                                                      // run destructor
 | 
|---|
| 56 |                 free( ptr );
 | 
|---|
| 57 |         } // if
 | 
|---|
| 58 |         delete( rest );
 | 
|---|
| 59 | } // delete
 | 
|---|
| 60 | 
 | 
|---|
| 61 | 
 | 
|---|
| 62 | // allocation/deallocation and constructor/destructor, array types
 | 
|---|
| 63 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
 | 
|---|
| 64 | T * anew( size_t dim, Params p ) {
 | 
|---|
| 65 |         T *arr = alloc( dim );
 | 
|---|
| 66 |         for ( unsigned int i = 0; i < dim; i += 1 ) {
 | 
|---|
| 67 |                 (arr[i]){ p };                                                                  // run constructor
 | 
|---|
| 68 |         } // for
 | 
|---|
| 69 |         return arr;
 | 
|---|
| 70 | } // anew
 | 
|---|
| 71 | 
 | 
|---|
| 72 | forall( dtype T | sized(T) | { void ^?{}( T & ); } )
 | 
|---|
| 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
 | 
|---|
| 76 |                         ^(arr[i]){};                                                            // run destructor
 | 
|---|
| 77 |                 } // for
 | 
|---|
| 78 |                 free( arr );
 | 
|---|
| 79 |         } // if
 | 
|---|
| 80 | } // adelete
 | 
|---|
| 81 | 
 | 
|---|
| 82 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
 | 
|---|
| 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
 | 
|---|
| 86 |                         ^(arr[i]){};                                                            // run destructor
 | 
|---|
| 87 |                 } // for
 | 
|---|
| 88 |                 free( arr );
 | 
|---|
| 89 |         } // if
 | 
|---|
| 90 |         adelete( rest );
 | 
|---|
| 91 | } // adelete
 | 
|---|
| 92 | 
 | 
|---|
| 93 | //---------------------------------------
 | 
|---|
| 94 | 
 | 
|---|
| 95 | int ato( const char * ptr ) {
 | 
|---|
| 96 |         int i;
 | 
|---|
| 97 |         if ( sscanf( ptr, "%d", &i ) == EOF ) {}
 | 
|---|
| 98 |         return i;
 | 
|---|
| 99 | } // ato
 | 
|---|
| 100 | 
 | 
|---|
| 101 | unsigned int ato( const char * ptr ) {
 | 
|---|
| 102 |         unsigned int ui;
 | 
|---|
| 103 |         if ( sscanf( ptr, "%u", &ui ) == EOF ) {}
 | 
|---|
| 104 |         return ui;
 | 
|---|
| 105 | } // ato
 | 
|---|
| 106 | 
 | 
|---|
| 107 | long int ato( const char * ptr ) {
 | 
|---|
| 108 |         long int li;
 | 
|---|
| 109 |         if ( sscanf( ptr, "%ld", &li ) == EOF ) {}
 | 
|---|
| 110 |         return li;
 | 
|---|
| 111 | } // ato
 | 
|---|
| 112 | 
 | 
|---|
| 113 | unsigned long int ato( const char * ptr ) {
 | 
|---|
| 114 |         unsigned long int uli;
 | 
|---|
| 115 |         if ( sscanf( ptr, "%lu", &uli ) == EOF ) {}
 | 
|---|
| 116 |         return uli;
 | 
|---|
| 117 | } // ato
 | 
|---|
| 118 | 
 | 
|---|
| 119 | long long int ato( const char * ptr ) {
 | 
|---|
| 120 |         long long int lli;
 | 
|---|
| 121 |         if ( sscanf( ptr, "%lld", &lli ) == EOF ) {}
 | 
|---|
| 122 |         return lli;
 | 
|---|
| 123 | } // ato
 | 
|---|
| 124 | 
 | 
|---|
| 125 | unsigned long long int ato( const char * ptr ) {
 | 
|---|
| 126 |         unsigned long long int ulli;
 | 
|---|
| 127 |         if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {}
 | 
|---|
| 128 |         return ulli;
 | 
|---|
| 129 | } // ato
 | 
|---|
| 130 | 
 | 
|---|
| 131 | 
 | 
|---|
| 132 | float ato( const char * ptr ) {
 | 
|---|
| 133 |         float f;
 | 
|---|
| 134 |         if ( sscanf( ptr, "%f", &f ) == EOF ) {}
 | 
|---|
| 135 |         return f;
 | 
|---|
| 136 | } // ato
 | 
|---|
| 137 | 
 | 
|---|
| 138 | double ato( const char * ptr ) {
 | 
|---|
| 139 |         double d;
 | 
|---|
| 140 |         if ( sscanf( ptr, "%lf", &d ) == EOF ) {}
 | 
|---|
| 141 |         return d;
 | 
|---|
| 142 | } // ato
 | 
|---|
| 143 | 
 | 
|---|
| 144 | long double ato( const char * ptr ) {
 | 
|---|
| 145 |         long double ld;
 | 
|---|
| 146 |         if ( sscanf( ptr, "%Lf", &ld ) == EOF ) {}
 | 
|---|
| 147 |         return ld;
 | 
|---|
| 148 | } // ato
 | 
|---|
| 149 | 
 | 
|---|
| 150 | 
 | 
|---|
| 151 | float _Complex ato( const char * ptr ) {
 | 
|---|
| 152 |         float re, im;
 | 
|---|
| 153 |         if ( sscanf( ptr, "%g%gi", &re, &im ) == EOF ) {}
 | 
|---|
| 154 |         return re + im * _Complex_I;
 | 
|---|
| 155 | } // ato
 | 
|---|
| 156 | 
 | 
|---|
| 157 | double _Complex ato( const char * ptr ) {
 | 
|---|
| 158 |         double re, im;
 | 
|---|
| 159 |         if ( sscanf( ptr, "%lf%lfi", &re, &im ) == EOF ) {}
 | 
|---|
| 160 |         return re + im * _Complex_I;
 | 
|---|
| 161 | } // ato
 | 
|---|
| 162 | 
 | 
|---|
| 163 | long double _Complex ato( const char * ptr ) {
 | 
|---|
| 164 |         long double re, im;
 | 
|---|
| 165 |         if ( sscanf( ptr, "%Lf%Lfi", &re, &im ) == EOF ) {}
 | 
|---|
| 166 |         return re + im * _Complex_I;
 | 
|---|
| 167 | } // ato
 | 
|---|
| 168 | 
 | 
|---|
| 169 | 
 | 
|---|
| 170 | int strto( const char * sptr, char ** eptr, int base ) {
 | 
|---|
| 171 |         return (int)strtol( sptr, eptr, base );
 | 
|---|
| 172 | } // strto
 | 
|---|
| 173 | 
 | 
|---|
| 174 | unsigned int strto( const char * sptr, char ** eptr, int base ) {
 | 
|---|
| 175 |         return (unsigned int)strtoul( sptr, eptr, base );
 | 
|---|
| 176 | } // strto
 | 
|---|
| 177 | 
 | 
|---|
| 178 | long int strto( const char * sptr, char ** eptr, int base ) {
 | 
|---|
| 179 |         return strtol( sptr, eptr, base );
 | 
|---|
| 180 | } // strto
 | 
|---|
| 181 | 
 | 
|---|
| 182 | unsigned long int strto( const char * sptr, char ** eptr, int base ) {
 | 
|---|
| 183 |         return strtoul( sptr, eptr, base );
 | 
|---|
| 184 | } // strto
 | 
|---|
| 185 | 
 | 
|---|
| 186 | long long int strto( const char * sptr, char ** eptr, int base ) {
 | 
|---|
| 187 |         return strtoll( sptr, eptr, base );
 | 
|---|
| 188 | } // strto
 | 
|---|
| 189 | 
 | 
|---|
| 190 | unsigned long long int strto( const char * sptr, char ** eptr, int base ) {
 | 
|---|
| 191 |         return strtoull( sptr, eptr, base );
 | 
|---|
| 192 | } // strto
 | 
|---|
| 193 | 
 | 
|---|
| 194 | 
 | 
|---|
| 195 | float strto( const char * sptr, char ** eptr ) {
 | 
|---|
| 196 |         return strtof( sptr, eptr );
 | 
|---|
| 197 | } // strto
 | 
|---|
| 198 | 
 | 
|---|
| 199 | double strto( const char * sptr, char ** eptr ) {
 | 
|---|
| 200 |         return strtod( sptr, eptr );
 | 
|---|
| 201 | } // strto
 | 
|---|
| 202 | 
 | 
|---|
| 203 | long double strto( const char * sptr, char ** eptr ) {
 | 
|---|
| 204 |         return strtold( sptr, eptr );
 | 
|---|
| 205 | } // strto
 | 
|---|
| 206 | 
 | 
|---|
| 207 | 
 | 
|---|
| 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;
 | 
|---|
| 215 | } // strto
 | 
|---|
| 216 | 
 | 
|---|
| 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;
 | 
|---|
| 224 | } // strto
 | 
|---|
| 225 | 
 | 
|---|
| 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;
 | 
|---|
| 233 | } // strto
 | 
|---|
| 234 | 
 | 
|---|
| 235 | //---------------------------------------
 | 
|---|
| 236 | 
 | 
|---|
| 237 | forall( otype T | { int ?<?( T, T ); } )
 | 
|---|
| 238 | T * bsearch( T key, const T * arr, size_t dim ) {
 | 
|---|
| 239 |         int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
 | 
|---|
| 240 |         return (T *)bsearch( &key, arr, dim, sizeof(T), comp );
 | 
|---|
| 241 | } // bsearch
 | 
|---|
| 242 | 
 | 
|---|
| 243 | forall( otype T | { int ?<?( T, T ); } )
 | 
|---|
| 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)
 | 
|---|
| 247 | } // bsearch
 | 
|---|
| 248 | 
 | 
|---|
| 249 | forall( otype T | { int ?<?( T, T ); } )
 | 
|---|
| 250 | void qsort( const T * arr, size_t dim ) {
 | 
|---|
| 251 |         int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
 | 
|---|
| 252 |         qsort( arr, dim, sizeof(T), comp );
 | 
|---|
| 253 | } // qsort
 | 
|---|
| 254 | 
 | 
|---|
| 255 | //---------------------------------------
 | 
|---|
| 256 | 
 | 
|---|
| 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 ]; }
 | 
|---|
| 260 | forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
 | 
|---|
| 261 | [ T, T ] div( T num, T denom ) { return [ num / denom, num % denom ]; }
 | 
|---|
| 262 | 
 | 
|---|
| 263 | //---------------------------------------
 | 
|---|
| 264 | 
 | 
|---|
| 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 ); }
 | 
|---|
| 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 ); }
 | 
|---|
| 274 | 
 | 
|---|
| 275 | //---------------------------------------
 | 
|---|
| 276 | 
 | 
|---|
| 277 | void rand48seed( long int s ) { srand48( s ); }
 | 
|---|
| 278 | char rand48( void ) { return mrand48(); }
 | 
|---|
| 279 | int rand48( void ) { return mrand48(); }
 | 
|---|
| 280 | unsigned int rand48( void ) { return lrand48(); }
 | 
|---|
| 281 | long int rand48( void ) { return mrand48(); }
 | 
|---|
| 282 | unsigned long int rand48( void ) { return lrand48(); }
 | 
|---|
| 283 | float rand48( void ) { return (float)drand48(); }               // otherwise float uses lrand48
 | 
|---|
| 284 | double rand48( void ) { return drand48(); }
 | 
|---|
| 285 | float _Complex rand48( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
 | 
|---|
| 286 | double _Complex rand48( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
 | 
|---|
| 287 | long double _Complex rand48( void) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
 | 
|---|
| 288 | 
 | 
|---|
| 289 | //---------------------------------------
 | 
|---|
| 290 | 
 | 
|---|
| 291 | forall( otype T | { int ?<?( T, T ); } )
 | 
|---|
| 292 | T min( T t1, T t2 ) {
 | 
|---|
| 293 |         return t1 < t2 ? t1 : t2;
 | 
|---|
| 294 | } // min
 | 
|---|
| 295 | 
 | 
|---|
| 296 | forall( otype T | { int ?>?( T, T ); } )
 | 
|---|
| 297 | T max( T t1, T t2 ) {
 | 
|---|
| 298 |         return t1 > t2 ? t1 : t2;
 | 
|---|
| 299 | } // max
 | 
|---|
| 300 | 
 | 
|---|
| 301 | forall( otype T | { T min( T, T ); T max( T, T ); } )
 | 
|---|
| 302 | T clamp( T value, T min_val, T max_val ) {
 | 
|---|
| 303 |         return max( min_val, min( value, max_val ) );
 | 
|---|
| 304 | } // clamp
 | 
|---|
| 305 | 
 | 
|---|
| 306 | forall( otype T )
 | 
|---|
| 307 | void swap( T & t1, T & t2 ) {
 | 
|---|
| 308 |         T temp = t1;
 | 
|---|
| 309 |         t1 = t2;
 | 
|---|
| 310 |         t2 = temp;
 | 
|---|
| 311 | } // swap
 | 
|---|
| 312 | 
 | 
|---|
| 313 | // Local Variables: //
 | 
|---|
| 314 | // tab-width: 4 //
 | 
|---|
| 315 | // End: //
 | 
|---|