[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
|
---|
[e672372] | 12 | // Last Modified On : Sun Dec 24 13:00:15 2017
|
---|
| 13 | // Update Count : 344
|
---|
[bd85400] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #include "stdlib"
|
---|
| 17 |
|
---|
| 18 | //---------------------------------------
|
---|
| 19 |
|
---|
| 20 | #define _XOPEN_SOURCE 600 // posix_memalign, *rand48
|
---|
[f3fc631f] | 21 | #include <string.h> // memcpy, memset
|
---|
[bd85400] | 22 | #include <malloc.h> // malloc_usable_size
|
---|
| 23 | #include <math.h> // fabsf, fabs, fabsl
|
---|
[6e991d6] | 24 | #include <complex.h> // _Complex_I
|
---|
[e672372] | 25 | #include <assert.h>
|
---|
[bd85400] | 26 |
|
---|
[f3fc631f] | 27 | // resize, non-array types
|
---|
[6065b3aa] | 28 | forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ) {
|
---|
[f3fc631f] | 29 | size_t olen = malloc_usable_size( ptr ); // current allocation
|
---|
[6065b3aa] | 30 | char * nptr = (void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
|
---|
[f3fc631f] | 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
|
---|
[aca65621] | 34 | } //
|
---|
[f3fc631f] | 35 | return (T *)nptr;
|
---|
[6065b3aa] | 36 | } // alloc
|
---|
[f3ddc21] | 37 |
|
---|
[6065b3aa] | 38 | // allocation/deallocation and constructor/destructor, non-array types
|
---|
[aca65621] | 39 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
|
---|
[627f585] | 40 | T * new( Params p ) {
|
---|
[a493682] | 41 | return &(*malloc()){ p }; // run constructor
|
---|
[f3ddc21] | 42 | } // new
|
---|
[627f585] | 43 |
|
---|
[83a071f9] | 44 | forall( dtype T | sized(T) | { void ^?{}( T & ); } )
|
---|
[627f585] | 45 | void delete( T * ptr ) {
|
---|
[6065b3aa] | 46 | if ( ptr ) { // ignore null
|
---|
[83a071f9] | 47 | ^(*ptr){}; // run destructor
|
---|
[f3ddc21] | 48 | free( ptr );
|
---|
[f3fc631f] | 49 | } // if
|
---|
[f3ddc21] | 50 | } // delete
|
---|
[627f585] | 51 |
|
---|
[83a071f9] | 52 | forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } )
|
---|
[bf76eab] | 53 | void delete( T * ptr, Params rest ) {
|
---|
[6065b3aa] | 54 | if ( ptr ) { // ignore null
|
---|
[83a071f9] | 55 | ^(*ptr){}; // run destructor
|
---|
[bf76eab] | 56 | free( ptr );
|
---|
[f3fc631f] | 57 | } // if
|
---|
[bf76eab] | 58 | delete( rest );
|
---|
[f3ddc21] | 59 | } // delete
|
---|
[bf76eab] | 60 |
|
---|
[6065b3aa] | 61 |
|
---|
| 62 | // allocation/deallocation and constructor/destructor, array types
|
---|
[aca65621] | 63 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } )
|
---|
[6065b3aa] | 64 | T * anew( size_t dim, Params p ) {
|
---|
| 65 | T *arr = alloc( dim );
|
---|
| 66 | for ( unsigned int i = 0; i < dim; i += 1 ) {
|
---|
[a493682] | 67 | (arr[i]){ p }; // run constructor
|
---|
[6065b3aa] | 68 | } // for
|
---|
| 69 | return arr;
|
---|
| 70 | } // anew
|
---|
| 71 |
|
---|
[aca65621] | 72 | forall( dtype T | sized(T) | { void ^?{}( T & ); } )
|
---|
[6065b3aa] | 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
|
---|
[a493682] | 76 | ^(arr[i]){}; // run destructor
|
---|
[6065b3aa] | 77 | } // for
|
---|
| 78 | free( arr );
|
---|
| 79 | } // if
|
---|
| 80 | } // adelete
|
---|
| 81 |
|
---|
[aca65621] | 82 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } )
|
---|
[6065b3aa] | 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
|
---|
[a493682] | 86 | ^(arr[i]){}; // run destructor
|
---|
[6065b3aa] | 87 | } // for
|
---|
| 88 | free( arr );
|
---|
| 89 | } // if
|
---|
| 90 | adelete( rest );
|
---|
| 91 | } // adelete
|
---|
| 92 |
|
---|
[bd85400] | 93 | //---------------------------------------
|
---|
| 94 |
|
---|
| 95 | float _Complex strto( const char * sptr, char ** eptr ) {
|
---|
| 96 | float re, im;
|
---|
[e672372] | 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; }
|
---|
[bd85400] | 103 | return re + im * _Complex_I;
|
---|
[f3ddc21] | 104 | } // strto
|
---|
| 105 |
|
---|
[bd85400] | 106 | double _Complex strto( const char * sptr, char ** eptr ) {
|
---|
| 107 | double re, im;
|
---|
[e672372] | 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; }
|
---|
[bd85400] | 114 | return re + im * _Complex_I;
|
---|
[f3ddc21] | 115 | } // strto
|
---|
| 116 |
|
---|
[bd85400] | 117 | long double _Complex strto( const char * sptr, char ** eptr ) {
|
---|
| 118 | long double re, im;
|
---|
[e672372] | 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; }
|
---|
[bd85400] | 125 | return re + im * _Complex_I;
|
---|
[f3ddc21] | 126 | } // strto
|
---|
[bd85400] | 127 |
|
---|
| 128 | //---------------------------------------
|
---|
| 129 |
|
---|
[4040425] | 130 | forall( otype T | { int ?<?( T, T ); } )
|
---|
[f3fc631f] | 131 | T * bsearch( T key, const T * arr, size_t dim ) {
|
---|
[bd85400] | 132 | int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
|
---|
[f3fc631f] | 133 | return (T *)bsearch( &key, arr, dim, sizeof(T), comp );
|
---|
[bd85400] | 134 | } // bsearch
|
---|
| 135 |
|
---|
[707446a] | 136 | forall( otype T | { int ?<?( T, T ); } )
|
---|
[f3fc631f] | 137 | unsigned int bsearch( T key, const T * arr, size_t dim ) {
|
---|
[e672372] | 138 | T * result = bsearch( key, arr, dim );
|
---|
[f3fc631f] | 139 | return result ? result - arr : dim; // pointer subtraction includes sizeof(T)
|
---|
[707446a] | 140 | } // bsearch
|
---|
| 141 |
|
---|
[4040425] | 142 | forall( otype T | { int ?<?( T, T ); } )
|
---|
[f3fc631f] | 143 | void qsort( const T * arr, size_t dim ) {
|
---|
[bd85400] | 144 | int comp( const void * t1, const void * t2 ) { return *(T *)t1 < *(T *)t2 ? -1 : *(T *)t2 < *(T *)t1 ? 1 : 0; }
|
---|
[f3fc631f] | 145 | qsort( arr, dim, sizeof(T), comp );
|
---|
[bd85400] | 146 | } // qsort
|
---|
| 147 |
|
---|
| 148 | //---------------------------------------
|
---|
| 149 |
|
---|
[5ea26ed] | 150 | [ int, int ] div( int num, int denom ) { div_t qr = div( num, denom ); return [ qr.quot, qr.rem ]; }
|
---|
| 151 | [ long int, long int ] div( long int num, long int denom ) { ldiv_t qr = ldiv( num, denom ); return [ qr.quot, qr.rem ]; }
|
---|
| 152 | [ 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 ]; }
|
---|
[43385ca] | 153 | forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
|
---|
[5ea26ed] | 154 | [ T, T ] div( T num, T denom ) { return [ num / denom, num % denom ]; }
|
---|
[bd85400] | 155 |
|
---|
| 156 | //---------------------------------------
|
---|
| 157 |
|
---|
[e672372] | 158 | void random_seed( long int s ) { srand48( s ); srandom( s ); } // call srandom to harmonize with C-lib random
|
---|
| 159 | char random( void ) { return (unsigned long int)random(); }
|
---|
| 160 | char random( char u ) { return random( (unsigned long int)u ); }
|
---|
| 161 | char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); }
|
---|
| 162 | int random( void ) { return (long int)random(); }
|
---|
| 163 | int random( int u ) { return random( (long int)u ); }
|
---|
| 164 | int random( int l, int u ) { return random( (long int)l, (long int)u ); }
|
---|
| 165 | unsigned int random( void ) { return (unsigned long int)random(); }
|
---|
| 166 | unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); }
|
---|
| 167 | unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); }
|
---|
| 168 | //extern "C" { long int random() { return mrand48(); } }
|
---|
| 169 | long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); }
|
---|
| 170 | long int random( long int l, long int u ) { assert( l < u ); return lrand48() % (u - l) + l; }
|
---|
[70e4895d] | 171 | unsigned long int random( void ) { return lrand48(); }
|
---|
| 172 | unsigned long int random( unsigned long int u ) { return lrand48() % u; }
|
---|
[e672372] | 173 | unsigned long int random( unsigned long int l, unsigned long int u ) { assert( l < u ); return lrand48() % (u - l) + l; }
|
---|
| 174 | float random( void ) { return (float)drand48(); } // cast otherwise float uses lrand48
|
---|
[70e4895d] | 175 | double random( void ) { return drand48(); }
|
---|
| 176 | float _Complex random( void ) { return (float)drand48() + (float _Complex)(drand48() * _Complex_I); }
|
---|
| 177 | double _Complex random( void ) { return drand48() + (double _Complex)(drand48() * _Complex_I); }
|
---|
[e672372] | 178 | long double _Complex random( void ) { return (long double)drand48() + (long double _Complex)(drand48() * _Complex_I); }
|
---|
[a9f2c13] | 179 |
|
---|
[bd85400] | 180 |
|
---|
| 181 | // Local Variables: //
|
---|
| 182 | // tab-width: 4 //
|
---|
| 183 | // End: //
|
---|