| 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 : Fri Feb  5 15:41:24 2016
 | 
|---|
| 13 | // Update Count     : 128
 | 
|---|
| 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 <stdio.h>
 | 
|---|
| 26 | #include <math.h>                                                                               // fabsf, fabs, fabsl
 | 
|---|
| 27 | #include <complex.h>                                                                    // _Complex_I, cabsf, cabs, cabsl
 | 
|---|
| 28 | } // extern "C"
 | 
|---|
| 29 | 
 | 
|---|
| 30 | forall( type T ) T * memset( T * ptr, unsigned char fill ) { // use default value '\0' for fill
 | 
|---|
| 31 |         printf( "memset1\n" );
 | 
|---|
| 32 |     return (T *)memset( ptr, (int)fill, malloc_usable_size( ptr ) );
 | 
|---|
| 33 | } // memset
 | 
|---|
| 34 | forall( type T ) T * memset( T * ptr ) {                                // remove when default value available
 | 
|---|
| 35 |         printf( "memset2\n" );
 | 
|---|
| 36 |     return (T *)memset( ptr, 0, malloc_usable_size( ptr ) );
 | 
|---|
| 37 | } // memset
 | 
|---|
| 38 | 
 | 
|---|
| 39 | forall( type T ) T * malloc( void ) {
 | 
|---|
| 40 |         printf( "malloc1\n" );
 | 
|---|
| 41 |     return (T *)malloc( sizeof(T) );
 | 
|---|
| 42 | } // malloc
 | 
|---|
| 43 | forall( type T ) T * malloc( size_t size ) {
 | 
|---|
| 44 |         printf( "malloc2\n" );
 | 
|---|
| 45 |     return (T *)(void *)malloc( size );
 | 
|---|
| 46 | } // malloc
 | 
|---|
| 47 | forall( type T ) T * malloc( char fill ) {
 | 
|---|
| 48 |         printf( "malloc3\n" );
 | 
|---|
| 49 |         T * ptr = (T *)malloc( sizeof(T) );
 | 
|---|
| 50 |     return memset( ptr );
 | 
|---|
| 51 | } // malloc
 | 
|---|
| 52 | 
 | 
|---|
| 53 | forall( type T ) T * calloc( size_t size ) {
 | 
|---|
| 54 |         printf( "calloc\n" );
 | 
|---|
| 55 |     return (T *)calloc( size, sizeof(T) );
 | 
|---|
| 56 | } // calloc
 | 
|---|
| 57 | 
 | 
|---|
| 58 | forall( type T ) T * realloc( T * ptr, size_t size ) {
 | 
|---|
| 59 |         printf( "realloc1\n" );
 | 
|---|
| 60 |     return (T *)(void *)realloc( (void *)ptr, size );
 | 
|---|
| 61 | } // realloc
 | 
|---|
| 62 | forall( type T ) T * realloc( T * ptr, size_t size, unsigned char fill ) {
 | 
|---|
| 63 |         printf( "realloc2\n" );
 | 
|---|
| 64 |     char * nptr = (T *)(void *)realloc( (void *)ptr, size );
 | 
|---|
| 65 |     size_t unused = malloc_usable_size( nptr );
 | 
|---|
| 66 |     memset( nptr + size - unused, (int)fill, unused );  // initialize any new storage
 | 
|---|
| 67 |     return nptr;
 | 
|---|
| 68 | } // realloc
 | 
|---|
| 69 | 
 | 
|---|
| 70 | forall( type T ) T * malloc( T * ptr, size_t size ) {
 | 
|---|
| 71 |         printf( "malloc4\n" );
 | 
|---|
| 72 |     return (T *)realloc( ptr, size );
 | 
|---|
| 73 | } // malloc
 | 
|---|
| 74 | forall( type T ) T * malloc( T * ptr, size_t size, unsigned char fill ) {
 | 
|---|
| 75 |         printf( "malloc5\n" );
 | 
|---|
| 76 |     return (T *)realloc( ptr, size, fill );
 | 
|---|
| 77 | } // malloc
 | 
|---|
| 78 | 
 | 
|---|
| 79 | forall( type T ) T * aligned_alloc( size_t alignment ) {
 | 
|---|
| 80 |         printf( "aligned_alloc\n" );
 | 
|---|
| 81 |     return (T *)memalign( alignment, sizeof(T) );
 | 
|---|
| 82 | } // aligned_alloc
 | 
|---|
| 83 | 
 | 
|---|
| 84 | forall( type T ) T * memalign( size_t alignment ) {
 | 
|---|
| 85 |         printf( "memalign\n" );
 | 
|---|
| 86 |     return (T *)memalign( alignment, sizeof(T) );
 | 
|---|
| 87 | } // memalign
 | 
|---|
| 88 | 
 | 
|---|
| 89 | forall( type T ) int posix_memalign( T ** ptr, size_t alignment ) {
 | 
|---|
| 90 |         printf( "posix_memalign\n" );
 | 
|---|
| 91 |     return posix_memalign( (void **)ptr, alignment, sizeof(T) );
 | 
|---|
| 92 | } // posix_memalign
 | 
|---|
| 93 | 
 | 
|---|
| 94 | //---------------------------------------
 | 
|---|
| 95 | 
 | 
|---|
| 96 | int ato( const char * ptr ) {
 | 
|---|
| 97 |         int i;
 | 
|---|
| 98 |         if ( sscanf( ptr, "%d", &i ) == EOF ) {}                        // check return code
 | 
|---|
| 99 |         return i;
 | 
|---|
| 100 | }
 | 
|---|
| 101 | unsigned int ato( const char * ptr ) {
 | 
|---|
| 102 |         unsigned int ui;
 | 
|---|
| 103 |         if ( sscanf( ptr, "%u", &ui ) == EOF ) {}                       // check return code
 | 
|---|
| 104 |         return ui;
 | 
|---|
| 105 | }
 | 
|---|
| 106 | long int ato( const char * ptr ) {
 | 
|---|
| 107 |         long int li;
 | 
|---|
| 108 |         if ( sscanf( ptr, "%ld", &li ) == EOF ) {};                     // check return code
 | 
|---|
| 109 |         return li;
 | 
|---|
| 110 | }
 | 
|---|
| 111 | unsigned long int ato( const char * ptr ) {
 | 
|---|
| 112 |         unsigned long int uli;
 | 
|---|
| 113 |         if ( sscanf( ptr, "%lu", &uli ) == EOF ) {};            // check return code
 | 
|---|
| 114 |         return uli;
 | 
|---|
| 115 | }
 | 
|---|
| 116 | long long int ato( const char * ptr ) {
 | 
|---|
| 117 |         long long int lli;
 | 
|---|
| 118 |         if ( sscanf( ptr, "%lld", &lli ) == EOF ) {};           // check return code
 | 
|---|
| 119 |         return lli;
 | 
|---|
| 120 | }
 | 
|---|
| 121 | unsigned long long int ato( const char * ptr ) {
 | 
|---|
| 122 |         unsigned long long int ulli;
 | 
|---|
| 123 |         if ( sscanf( ptr, "%llu", &ulli ) == EOF ) {};          // check return code
 | 
|---|
| 124 |         return ulli;
 | 
|---|
| 125 | }
 | 
|---|
| 126 | float ato( const char * ptr ) {
 | 
|---|
| 127 |         float f;
 | 
|---|
| 128 |         if ( sscanf( ptr, "%f", &f ) == EOF ) {};                       // check return code
 | 
|---|
| 129 |         return f;
 | 
|---|
| 130 | }
 | 
|---|
| 131 | double ato( const char * ptr ) {
 | 
|---|
| 132 |         double d;
 | 
|---|
| 133 |         if ( sscanf( ptr, "%lf", &d ) == EOF ) {};                      // check return code
 | 
|---|
| 134 |         return d;
 | 
|---|
| 135 | }
 | 
|---|
| 136 | long double ato( const char * ptr ) {
 | 
|---|
| 137 |         long double ld;
 | 
|---|
| 138 |         printf( "FRED " );
 | 
|---|
| 139 |         if ( sscanf( ptr, "%.32Lf", &ld ) == EOF ) {};          // check return code
 | 
|---|
| 140 |         return ld;
 | 
|---|
| 141 | }
 | 
|---|
| 142 | float _Complex ato( const char * ptr ) {
 | 
|---|
| 143 |         float re, im;
 | 
|---|
| 144 |         if ( sscanf( ptr, "%g%g", &re, &im ) == EOF ) {};       // check return code
 | 
|---|
| 145 |         return re + im * _Complex_I;
 | 
|---|
| 146 | }
 | 
|---|
| 147 | double _Complex ato( const char * ptr ) {
 | 
|---|
| 148 |         double re, im;
 | 
|---|
| 149 |         if ( sscanf( ptr, "%.16lg%.16lg", &re, &im ) == EOF ) {}; // check return code
 | 
|---|
| 150 |         return re + im * _Complex_I;
 | 
|---|
| 151 | }
 | 
|---|
| 152 | long double _Complex ato( const char * ptr ) {
 | 
|---|
| 153 |         long double re, im;
 | 
|---|
| 154 |         if ( sscanf( ptr, "%.32Lg%.32Lg", &re, &im ) == EOF ) {}; // check return code
 | 
|---|
| 155 |         return re + im * _Complex_I;
 | 
|---|
| 156 | }       
 | 
|---|
| 157 | 
 | 
|---|
| 158 | int strto( const char * sptr, char ** eptr, int base ) {
 | 
|---|
| 159 |         return (int)strtol( sptr, eptr, base );
 | 
|---|
| 160 | }
 | 
|---|
| 161 | unsigned int strto( const char * sptr, char ** eptr, int base ) {
 | 
|---|
| 162 |         return (unsigned int)strtoul( sptr, eptr, base );
 | 
|---|
| 163 | }
 | 
|---|
| 164 | long int strto( const char * sptr, char ** eptr, int base ) {
 | 
|---|
| 165 |         return strtol( sptr, eptr, base );
 | 
|---|
| 166 | }
 | 
|---|
| 167 | unsigned long int strto( const char * sptr, char ** eptr, int base ) {
 | 
|---|
| 168 |         return strtoul( sptr, eptr, base );
 | 
|---|
| 169 | }
 | 
|---|
| 170 | long long int strto( const char * sptr, char ** eptr, int base ) {
 | 
|---|
| 171 |         return strtoll( sptr, eptr, base );
 | 
|---|
| 172 | }
 | 
|---|
| 173 | unsigned long long int strto( const char * sptr, char ** eptr, int base ) {
 | 
|---|
| 174 |         return strtoull( sptr, eptr, base );
 | 
|---|
| 175 | }
 | 
|---|
| 176 | float strto( const char * sptr, char ** eptr ) {
 | 
|---|
| 177 |         return strtof( sptr, eptr );
 | 
|---|
| 178 | }
 | 
|---|
| 179 | double strto( const char * sptr, char ** eptr ) {
 | 
|---|
| 180 |         return strtod( sptr, eptr );
 | 
|---|
| 181 | }
 | 
|---|
| 182 | long double strto( const char * sptr, char ** eptr ) {
 | 
|---|
| 183 |         return strtold( sptr, eptr );
 | 
|---|
| 184 | }
 | 
|---|
| 185 | float _Complex strto( const char * sptr, char ** eptr ) {
 | 
|---|
| 186 |         float re, im;
 | 
|---|
| 187 |         re = strtof( sptr, eptr );
 | 
|---|
| 188 |         if ( sptr == *eptr ) return 0.0;
 | 
|---|
| 189 |         im = strtof( sptr, eptr );
 | 
|---|
| 190 |         if ( sptr == *eptr ) return 0.0;
 | 
|---|
| 191 |         return re + im * _Complex_I;
 | 
|---|
| 192 | }
 | 
|---|
| 193 | double _Complex strto( const char * sptr, char ** eptr ) {
 | 
|---|
| 194 |         double re, im;
 | 
|---|
| 195 |         re = strtod( sptr, eptr );
 | 
|---|
| 196 |         if ( sptr == *eptr ) return 0.0;
 | 
|---|
| 197 |         im = strtod( sptr, eptr );
 | 
|---|
| 198 |         if ( sptr == *eptr ) return 0.0;
 | 
|---|
| 199 |         return re + im * _Complex_I;
 | 
|---|
| 200 | }
 | 
|---|
| 201 | long double _Complex strto( const char * sptr, char ** eptr ) {
 | 
|---|
| 202 |         long double re, im;
 | 
|---|
| 203 |         re = strtold( sptr, eptr );
 | 
|---|
| 204 |         if ( sptr == *eptr ) return 0.0;
 | 
|---|
| 205 |         im = strtold( sptr, eptr );
 | 
|---|
| 206 |         if ( sptr == *eptr ) return 0.0;
 | 
|---|
| 207 |         return re + im * _Complex_I;
 | 
|---|
| 208 | }
 | 
|---|
| 209 | 
 | 
|---|
| 210 | //---------------------------------------
 | 
|---|
| 211 | 
 | 
|---|
| 212 | forall( type T | { int ?<?( T, T ); } )
 | 
|---|
| 213 | T * bsearch( const T key, const T * arr, size_t dimension ) {
 | 
|---|
| 214 |         int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
 | 
|---|
| 215 |         return (T *)bsearch( &key, arr, dimension, sizeof(T), comp );
 | 
|---|
| 216 | } // bsearch
 | 
|---|
| 217 | 
 | 
|---|
| 218 | forall( type T | { int ?<?( T, T ); } )
 | 
|---|
| 219 | void qsort( const T * arr, size_t dimension ) {
 | 
|---|
| 220 |         int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
 | 
|---|
| 221 |         qsort( arr, dimension, sizeof(T), comp );
 | 
|---|
| 222 | } // qsort
 | 
|---|
| 223 | 
 | 
|---|
| 224 | //---------------------------------------
 | 
|---|
| 225 | 
 | 
|---|
| 226 | forall( type T | { T ?/?( T, T ); T ?%?( T, T ); } )
 | 
|---|
| 227 | [ T, T ] div( T t1, T t2 ) { /* return [ t1 / t2, t1 % t2 ]; */ }
 | 
|---|
| 228 | 
 | 
|---|
| 229 | //---------------------------------------
 | 
|---|
| 230 | 
 | 
|---|
| 231 | char abs( char v ) { return abs( (int)v ); }
 | 
|---|
| 232 | long int abs( long int v ) { return labs( v ); }
 | 
|---|
| 233 | long long int abs( long long int v ) { return llabs( v ); }
 | 
|---|
| 234 | float abs( float v ) { return fabsf( v ); }
 | 
|---|
| 235 | double abs( double v ) { return fabs( v ); }
 | 
|---|
| 236 | long double abs( long double v ) { return fabsl( v ); }
 | 
|---|
| 237 | float _Complex abs( float _Complex v ) { return cabsf( v ); }
 | 
|---|
| 238 | double _Complex abs( double _Complex v ) { return cabs( v ); }
 | 
|---|
| 239 | long double _Complex abs( long double _Complex v ) { return cabsl( v ); }
 | 
|---|
| 240 | 
 | 
|---|
| 241 | //---------------------------------------
 | 
|---|
| 242 | 
 | 
|---|
| 243 | void randseed( long int s ) { srand48( s ); }
 | 
|---|
| 244 | char random() { return lrand48(); }
 | 
|---|
| 245 | int random() { return mrand48(); }
 | 
|---|
| 246 | unsigned int random() { return lrand48(); }
 | 
|---|
| 247 | long int random() { return mrand48(); }
 | 
|---|
| 248 | unsigned long int random() { return lrand48(); }
 | 
|---|
| 249 | float random() { return (float)drand48(); }                             // otherwise float uses lrand48
 | 
|---|
| 250 | double random() { return drand48(); }
 | 
|---|
| 251 | float _Complex random() { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
 | 
|---|
| 252 | double _Complex random() { return drand48() + (double _Complex)(drand48() * _Complex_I); }
 | 
|---|
| 253 | long double _Complex random() { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
 | 
|---|
| 254 | 
 | 
|---|
| 255 | //---------------------------------------
 | 
|---|
| 256 | 
 | 
|---|
| 257 | forall( type T | { int ?<?( T, T ); } )
 | 
|---|
| 258 | T min( const T t1, const T t2 ) {
 | 
|---|
| 259 |         return t1 < t2 ? t1 : t2;
 | 
|---|
| 260 | } // min
 | 
|---|
| 261 | 
 | 
|---|
| 262 | forall( type T | { int ?>?( T, T ); } )
 | 
|---|
| 263 | T max( const T t1, const T t2 ) {
 | 
|---|
| 264 |         return t1 > t2 ? t1 : t2;
 | 
|---|
| 265 | } // max
 | 
|---|
| 266 | 
 | 
|---|
| 267 | forall( type T )
 | 
|---|
| 268 | void swap( T * t1, T * t2 ) {
 | 
|---|
| 269 |         T temp = *t1;
 | 
|---|
| 270 |         *t1 = *t2;
 | 
|---|
| 271 |         *t2 = temp;
 | 
|---|
| 272 | } // swap
 | 
|---|
| 273 | 
 | 
|---|
| 274 | // Local Variables: //
 | 
|---|
| 275 | // tab-width: 4 //
 | 
|---|
| 276 | // End: //
 | 
|---|