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