| 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 : Tue Jan 2 12:20:32 2018
|
|---|
| 13 | // Update Count : 441
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "stdlib"
|
|---|
| 17 |
|
|---|
| 18 | //---------------------------------------
|
|---|
| 19 |
|
|---|
| 20 | #define _XOPEN_SOURCE 600 // posix_memalign, *rand48
|
|---|
| 21 | #include <string.h> // memcpy, memset
|
|---|
| 22 | #include <malloc.h> // malloc_usable_size
|
|---|
| 23 | #include <math.h> // fabsf, fabs, fabsl
|
|---|
| 24 | #include <complex.h> // _Complex_I
|
|---|
| 25 | #include <assert.h>
|
|---|
| 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 | float _Complex strto( const char * sptr, char ** eptr ) {
|
|---|
| 96 | float re, im;
|
|---|
| 97 | char * eeptr;
|
|---|
| 98 | re = strtof( sptr, &eeptr );
|
|---|
| 99 | if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
|
|---|
| 100 | im = strtof( eeptr, &eeptr );
|
|---|
| 101 | if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
|
|---|
| 102 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0f + 0.0f * _Complex_I; }
|
|---|
| 103 | return re + im * _Complex_I;
|
|---|
| 104 | } // strto
|
|---|
| 105 |
|
|---|
| 106 | double _Complex strto( const char * sptr, char ** eptr ) {
|
|---|
| 107 | double re, im;
|
|---|
| 108 | char * eeptr;
|
|---|
| 109 | re = strtod( sptr, &eeptr );
|
|---|
| 110 | if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
|
|---|
| 111 | im = strtod( eeptr, &eeptr );
|
|---|
| 112 | if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
|
|---|
| 113 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0 + 0.0 * _Complex_I; }
|
|---|
| 114 | return re + im * _Complex_I;
|
|---|
| 115 | } // strto
|
|---|
| 116 |
|
|---|
| 117 | long double _Complex strto( const char * sptr, char ** eptr ) {
|
|---|
| 118 | long double re, im;
|
|---|
| 119 | char * eeptr;
|
|---|
| 120 | re = strtold( sptr, &eeptr );
|
|---|
| 121 | if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
|
|---|
| 122 | im = strtold( eeptr, &eeptr );
|
|---|
| 123 | if ( sptr == *eeptr ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
|
|---|
| 124 | if ( *eeptr != 'i' ) { if ( eptr != 0 ) *eptr = eeptr; return 0.0L + 0.0L * _Complex_I; }
|
|---|
| 125 | return re + im * _Complex_I;
|
|---|
| 126 | } // strto
|
|---|
| 127 |
|
|---|
| 128 | //---------------------------------------
|
|---|
| 129 |
|
|---|
| 130 | forall( otype E | { int ?<?( E, E ); } )
|
|---|
| 131 | E * bsearch( E key, const E * vals, size_t dim ) {
|
|---|
| 132 | int cmp( const void * t1, const void * t2 ) {
|
|---|
| 133 | return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
|
|---|
| 134 | } // cmp
|
|---|
| 135 | return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
|
|---|
| 136 | } // bsearch
|
|---|
| 137 |
|
|---|
| 138 | forall( otype E | { int ?<?( E, E ); } )
|
|---|
| 139 | size_t bsearch( E key, const E * vals, size_t dim ) {
|
|---|
| 140 | E * result = bsearch( key, vals, dim );
|
|---|
| 141 | return result ? result - vals : dim; // pointer subtraction includes sizeof(E)
|
|---|
| 142 | } // bsearch
|
|---|
| 143 |
|
|---|
| 144 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
|
|---|
| 145 | E * bsearch( K key, const E * vals, size_t dim ) {
|
|---|
| 146 | int cmp( const void * t1, const void * t2 ) {
|
|---|
| 147 | return *(K *)t1 < getKey( *(E *)t2 ) ? -1 : getKey( *(E *)t2 ) < *(K *)t1 ? 1 : 0;
|
|---|
| 148 | } // cmp
|
|---|
| 149 | return (E *)bsearch( &key, vals, dim, sizeof(E), cmp );
|
|---|
| 150 | } // bsearch
|
|---|
| 151 |
|
|---|
| 152 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
|
|---|
| 153 | size_t bsearch( K key, const E * vals, size_t dim ) {
|
|---|
| 154 | E * result = bsearch( key, vals, dim );
|
|---|
| 155 | return result ? result - vals : dim; // pointer subtraction includes sizeof(E)
|
|---|
| 156 | } // bsearch
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 | forall( otype E | { int ?<?( E, E ); } )
|
|---|
| 160 | size_t bsearchl( E key, const E * vals, size_t dim ) {
|
|---|
| 161 | size_t l = 0, m, h = dim;
|
|---|
| 162 | while ( l < h ) {
|
|---|
| 163 | m = (l + h) / 2;
|
|---|
| 164 | if ( (E &)(vals[m]) < key ) { // cast away const
|
|---|
| 165 | l = m + 1;
|
|---|
| 166 | } else {
|
|---|
| 167 | h = m;
|
|---|
| 168 | } // if
|
|---|
| 169 | } // while
|
|---|
| 170 | return l;
|
|---|
| 171 | } // bsearchl
|
|---|
| 172 |
|
|---|
| 173 | forall( otype E | { int ?<?( E, E ); } )
|
|---|
| 174 | E * bsearchl( E key, const E * vals, size_t dim ) {
|
|---|
| 175 | size_t posn = bsearchl( key, vals, dim );
|
|---|
| 176 | return (E *)(&vals[posn]); // cast away const
|
|---|
| 177 | } // bsearchl
|
|---|
| 178 |
|
|---|
| 179 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
|
|---|
| 180 | size_t bsearchl( K key, const E * vals, size_t dim ) {
|
|---|
| 181 | size_t l = 0, m, h = dim;
|
|---|
| 182 | while ( l < h ) {
|
|---|
| 183 | m = (l + h) / 2;
|
|---|
| 184 | if ( getKey( vals[m] ) < key ) {
|
|---|
| 185 | l = m + 1;
|
|---|
| 186 | } else {
|
|---|
| 187 | h = m;
|
|---|
| 188 | } // if
|
|---|
| 189 | } // while
|
|---|
| 190 | return l;
|
|---|
| 191 | } // bsearchl
|
|---|
| 192 |
|
|---|
| 193 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
|
|---|
| 194 | E * bsearchl( K key, const E * vals, size_t dim ) {
|
|---|
| 195 | size_t posn = bsearchl( key, vals, dim );
|
|---|
| 196 | return (E *)(&vals[posn]); // cast away const
|
|---|
| 197 | } // bsearchl
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 | forall( otype E | { int ?<?( E, E ); } )
|
|---|
| 201 | size_t bsearchu( E key, const E * vals, size_t dim ) {
|
|---|
| 202 | size_t l = 0, m, h = dim;
|
|---|
| 203 | while ( l < h ) {
|
|---|
| 204 | m = (l + h) / 2;
|
|---|
| 205 | if ( ! ( key < (E &)(vals[m]) ) ) { // cast away const
|
|---|
| 206 | l = m + 1;
|
|---|
| 207 | } else {
|
|---|
| 208 | h = m;
|
|---|
| 209 | } // if
|
|---|
| 210 | } // while
|
|---|
| 211 | return l;
|
|---|
| 212 | } // bsearchu
|
|---|
| 213 |
|
|---|
| 214 | forall( otype E | { int ?<?( E, E ); } )
|
|---|
| 215 | E * bsearchu( E key, const E * vals, size_t dim ) {
|
|---|
| 216 | size_t posn = bsearchu( key, vals, dim );
|
|---|
| 217 | return (E *)(&vals[posn]);
|
|---|
| 218 | } // bsearchu
|
|---|
| 219 |
|
|---|
| 220 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
|
|---|
| 221 | size_t bsearchu( K key, const E * vals, size_t dim ) {
|
|---|
| 222 | size_t l = 0, m, h = dim;
|
|---|
| 223 | while ( l < h ) {
|
|---|
| 224 | m = (l + h) / 2;
|
|---|
| 225 | if ( ! ( key < getKey( vals[m] ) ) ) {
|
|---|
| 226 | l = m + 1;
|
|---|
| 227 | } else {
|
|---|
| 228 | h = m;
|
|---|
| 229 | } // if
|
|---|
| 230 | } // while
|
|---|
| 231 | return l;
|
|---|
| 232 | } // bsearchu
|
|---|
| 233 |
|
|---|
| 234 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } )
|
|---|
| 235 | E * bsearchu( K key, const E * vals, size_t dim ) {
|
|---|
| 236 | size_t posn = bsearchu( key, vals, dim );
|
|---|
| 237 | return (E *)(&vals[posn]);
|
|---|
| 238 | } // bsearchu
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 | forall( otype E | { int ?<?( E, E ); } )
|
|---|
| 242 | void qsort( E * vals, size_t dim ) {
|
|---|
| 243 | int cmp( const void * t1, const void * t2 ) {
|
|---|
| 244 | return *(E *)t1 < *(E *)t2 ? -1 : *(E *)t2 < *(E *)t1 ? 1 : 0;
|
|---|
| 245 | } // cmp
|
|---|
| 246 | qsort( vals, dim, sizeof(E), cmp );
|
|---|
| 247 | } // qsort
|
|---|
| 248 |
|
|---|
| 249 | //---------------------------------------
|
|---|
| 250 |
|
|---|
| 251 | [ int, int ] div( int num, int denom ) { div_t qr = div( num, denom ); return [ qr.quot, qr.rem ]; }
|
|---|
| 252 | [ long int, long int ] div( long int num, long int denom ) { ldiv_t qr = ldiv( num, denom ); return [ qr.quot, qr.rem ]; }
|
|---|
| 253 | [ 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 ]; }
|
|---|
| 254 | forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
|
|---|
| 255 | [ T, T ] div( T num, T denom ) { return [ num / denom, num % denom ]; }
|
|---|
| 256 |
|
|---|
| 257 | //---------------------------------------
|
|---|
| 258 |
|
|---|
| 259 | extern "C" { void srandom( unsigned int seed ) { srand48( seed ); } } // override C version
|
|---|
| 260 | char random( void ) { return (unsigned long int)random(); }
|
|---|
| 261 | char random( char u ) { return random( (unsigned long int)u ); }
|
|---|
| 262 | char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); }
|
|---|
| 263 | int random( void ) { return (long int)random(); }
|
|---|
| 264 | int random( int u ) { return random( (long int)u ); }
|
|---|
| 265 | int random( int l, int u ) { return random( (long int)l, (long int)u ); }
|
|---|
| 266 | unsigned int random( void ) { return (unsigned long int)random(); }
|
|---|
| 267 | unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); }
|
|---|
| 268 | unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); }
|
|---|
| 269 | extern "C" { long int random( void ) { return mrand48(); } } // override C version
|
|---|
| 270 | long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); }
|
|---|
| 271 | long int random( long int l, long int u ) { assert( l < u ); return lrand48() % (u - l) + l; }
|
|---|
| 272 | unsigned long int random( void ) { return lrand48(); }
|
|---|
| 273 | unsigned long int random( unsigned long int u ) { return lrand48() % u; }
|
|---|
| 274 | unsigned long int random( unsigned long int l, unsigned long int u ) { assert( l < u ); return lrand48() % (u - l) + l; }
|
|---|
| 275 | float random( void ) { return (float)drand48(); } // cast otherwise float uses lrand48
|
|---|
| 276 | double random( void ) { return drand48(); }
|
|---|
| 277 | float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
|
|---|
| 278 | double _Complex random( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
|
|---|
| 279 | long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 | // Local Variables: //
|
|---|
| 283 | // tab-width: 4 //
|
|---|
| 284 | // End: //
|
|---|