[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 | //
|
---|
[bb82c03] | 7 | // stdlib --
|
---|
[bd85400] | 8 | //
|
---|
| 9 | // Author : Peter A. Buhr
|
---|
| 10 | // Created On : Thu Jan 28 17:12:35 2016
|
---|
[b89c7c2] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[c354108] | 12 | // Last Modified On : Fri Aug 14 23:38:50 2020
|
---|
| 13 | // Update Count : 504
|
---|
[bd85400] | 14 | //
|
---|
| 15 |
|
---|
[53a6c2a] | 16 | #pragma once
|
---|
[17e5e2b] | 17 |
|
---|
[2026bb6] | 18 | #include "bits/defs.hfa"
|
---|
[d6b03b7] | 19 | #include "bits/align.hfa"
|
---|
[2026bb6] | 20 |
|
---|
[d46ed6e] | 21 | #include <stdlib.h> // *alloc, strto*, ato*
|
---|
[4e7c0fc0] | 22 | #include <heap.hfa>
|
---|
[d6b03b7] | 23 |
|
---|
[ca7949b] | 24 | // Reduce includes by explicitly defining these routines.
|
---|
[3ce0d440] | 25 | extern "C" {
|
---|
[4e7c0fc0] | 26 | void * memalign( size_t alignment, size_t size ); // malloc.h
|
---|
| 27 | void * pvalloc( size_t size ); // malloc.h
|
---|
[b9c04946] | 28 | void * memset( void * dest, int fill, size_t size ); // string.h
|
---|
[57fc7d8] | 29 | void * memcpy( void * dest, const void * src, size_t size ); // string.h
|
---|
[3ce0d440] | 30 | } // extern "C"
|
---|
[e672372] | 31 |
|
---|
[bd85400] | 32 | //---------------------------------------
|
---|
| 33 |
|
---|
[45161b4d] | 34 | #ifndef EXIT_FAILURE
|
---|
| 35 | #define EXIT_FAILURE 1 // failing exit status
|
---|
| 36 | #define EXIT_SUCCESS 0 // successful exit status
|
---|
| 37 | #endif // ! EXIT_FAILURE
|
---|
| 38 |
|
---|
| 39 | //---------------------------------------
|
---|
| 40 |
|
---|
[c354108] | 41 | #include "common.hfa"
|
---|
| 42 |
|
---|
| 43 | //---------------------------------------
|
---|
| 44 |
|
---|
[b0a0ee4] | 45 | // Macro because of returns
|
---|
| 46 | #define $VAR_ALLOC( allocation, alignment ) \
|
---|
| 47 | if ( _Alignof(T) <= libAlign() ) return (T *)(void *)allocation( (size_t)sizeof(T) ); /* C allocation */ \
|
---|
| 48 | else return (T *)alignment( _Alignof(T), sizeof(T) )
|
---|
| 49 |
|
---|
| 50 | #define $ARRAY_ALLOC( allocation, alignment, dim ) \
|
---|
| 51 | if ( _Alignof(T) <= libAlign() ) return (T *)(void *)allocation( dim, (size_t)sizeof(T) ); /* C allocation */ \
|
---|
| 52 | else return (T *)alignment( _Alignof(T), dim, sizeof(T) )
|
---|
| 53 |
|
---|
| 54 | #define $RE_SPECIALS( ptr, size, allocation, alignment ) \
|
---|
| 55 | if ( unlikely( size == 0 ) || unlikely( ptr == 0p ) ) { \
|
---|
| 56 | if ( unlikely( size == 0 ) ) free( ptr ); \
|
---|
| 57 | $VAR_ALLOC( malloc, memalign ); \
|
---|
| 58 | } /* if */
|
---|
| 59 |
|
---|
[74b19fb] | 60 | static inline forall( dtype T | sized(T) ) {
|
---|
[ca7949b] | 61 | // Cforall safe equivalents, i.e., implicit size specification
|
---|
[3ce0d440] | 62 |
|
---|
[74b19fb] | 63 | T * malloc( void ) {
|
---|
[b0a0ee4] | 64 | $VAR_ALLOC( malloc, memalign );
|
---|
[74b19fb] | 65 | } // malloc
|
---|
| 66 |
|
---|
[856fe3e] | 67 | T * aalloc( size_t dim ) {
|
---|
[b0a0ee4] | 68 | $ARRAY_ALLOC( aalloc, amemalign, dim );
|
---|
[856fe3e] | 69 | } // aalloc
|
---|
| 70 |
|
---|
[74b19fb] | 71 | T * calloc( size_t dim ) {
|
---|
[b0a0ee4] | 72 | $ARRAY_ALLOC( calloc, cmemalign, dim );
|
---|
[74b19fb] | 73 | } // calloc
|
---|
| 74 |
|
---|
[b89c7c2] | 75 | T * resize( T * ptr, size_t size ) { // CFA resize, eliminate return-type cast
|
---|
[b0a0ee4] | 76 | $RE_SPECIALS( ptr, size, malloc, memalign );
|
---|
[60062be] | 77 | if ( _Alignof(T) <= libAlign() ) return (T *)(void *)resize( (void *)ptr, size ); // CFA resize
|
---|
| 78 | else return (T *)(void *)resize( (void *)ptr, _Alignof(T), size ); // CFA resize
|
---|
[856fe3e] | 79 | } // resize
|
---|
| 80 |
|
---|
[d74369b] | 81 | T * realloc( T * ptr, size_t size ) { // CFA realloc, eliminate return-type cast
|
---|
[b0a0ee4] | 82 | $RE_SPECIALS( ptr, size, malloc, memalign );
|
---|
[60062be] | 83 | if ( _Alignof(T) <= libAlign() ) return (T *)(void *)realloc( (void *)ptr, size ); // C realloc
|
---|
| 84 | else return (T *)(void *)realloc( (void *)ptr, _Alignof(T), size ); // CFA realloc
|
---|
[74b19fb] | 85 | } // realloc
|
---|
| 86 |
|
---|
| 87 | T * memalign( size_t align ) {
|
---|
[cafb687] | 88 | return (T *)memalign( align, sizeof(T) ); // C memalign
|
---|
[74b19fb] | 89 | } // memalign
|
---|
| 90 |
|
---|
[856fe3e] | 91 | T * amemalign( size_t align, size_t dim ) {
|
---|
| 92 | return (T *)amemalign( align, dim, sizeof(T) ); // CFA amemalign
|
---|
| 93 | } // amemalign
|
---|
| 94 |
|
---|
[d74369b] | 95 | T * cmemalign( size_t align, size_t dim ) {
|
---|
| 96 | return (T *)cmemalign( align, dim, sizeof(T) ); // CFA cmemalign
|
---|
| 97 | } // cmemalign
|
---|
| 98 |
|
---|
[74b19fb] | 99 | T * aligned_alloc( size_t align ) {
|
---|
[cafb687] | 100 | return (T *)aligned_alloc( align, sizeof(T) ); // C aligned_alloc
|
---|
[74b19fb] | 101 | } // aligned_alloc
|
---|
| 102 |
|
---|
| 103 | int posix_memalign( T ** ptr, size_t align ) {
|
---|
| 104 | return posix_memalign( (void **)ptr, align, sizeof(T) ); // C posix_memalign
|
---|
| 105 | } // posix_memalign
|
---|
[ada0246d] | 106 |
|
---|
| 107 | T * valloc( void ) {
|
---|
| 108 | return (T *)valloc( sizeof(T) ); // C valloc
|
---|
| 109 | } // valloc
|
---|
| 110 |
|
---|
| 111 | T * pvalloc( void ) {
|
---|
| 112 | return (T *)pvalloc( sizeof(T) ); // C pvalloc
|
---|
| 113 | } // pvalloc
|
---|
[cfbc703d] | 114 | } // distribution
|
---|
[74b19fb] | 115 |
|
---|
[ceb7db8] | 116 | /*
|
---|
| 117 | FIX ME : fix alloc interface after Ticker Number 214 is resolved, define and add union to S_fill. Then, modify postfix-fill functions to support T * with nmemb, char, and T object of any size. Finally, change alloc_internal.
|
---|
| 118 | Or, just follow the instructions below for that.
|
---|
| 119 |
|
---|
| 120 | 1. Replace the current forall-block that contains defintions of S_fill and S_realloc with following:
|
---|
| 121 | forall( dtype T | sized(T) ) {
|
---|
| 122 | union U_fill { char c; T * a; T t; };
|
---|
| 123 | struct S_fill { char tag; char c; size_t size; T * at; char t[50]; };
|
---|
| 124 | struct S_realloc { inline T *; };
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | 2. Replace all current postfix-fill functions with following for updated S_fill:
|
---|
| 128 | S_fill(T) ?`fill( char a ) { S_fill(T) ret = {'c'}; ret.fill.c = a; return ret; }
|
---|
| 129 | S_fill(T) ?`fill( T a ) { S_fill(T) ret = {'t'}; memcpy(&ret.fill.t, &a, sizeof(T)); return ret; }
|
---|
| 130 | S_fill(T) ?`fill( T a[], size_t nmemb ) { S_fill(T) ret = {'a', nmemb}; ret.fill.a = a; return ret; }
|
---|
| 131 |
|
---|
| 132 | 3. Replace the $alloc_internal function which is outside ttype forall-block with following function:
|
---|
| 133 | T * $alloc_internal( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill) {
|
---|
| 134 | T * ptr = NULL;
|
---|
| 135 | size_t size = sizeof(T);
|
---|
| 136 | size_t copy_end = 0;
|
---|
| 137 |
|
---|
| 138 | if(Resize) {
|
---|
| 139 | ptr = (T*) (void *) resize( (int *)Resize, Align, Dim * size );
|
---|
| 140 | } else if (Realloc) {
|
---|
| 141 | if (Fill.tag != '0') copy_end = min(malloc_size( Realloc ), Dim * size);
|
---|
| 142 | ptr = (T*) (void *) realloc( (int *)Realloc, Align, Dim * size );
|
---|
| 143 | } else {
|
---|
| 144 | ptr = (T*) (void *) memalign( Align, Dim * size );
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | if(Fill.tag == 'c') {
|
---|
| 148 | memset( (char *)ptr + copy_end, (int)Fill.fill.c, Dim * size - copy_end );
|
---|
| 149 | } else if(Fill.tag == 't') {
|
---|
| 150 | for ( int i = copy_end; i <= Dim * size - size ; i += size ) {
|
---|
| 151 | memcpy( (char *)ptr + i, &Fill.fill.t, size );
|
---|
| 152 | }
|
---|
| 153 | } else if(Fill.tag == 'a') {
|
---|
| 154 | memcpy( (char *)ptr + copy_end, Fill.fill.a, min(Dim * size - copy_end, size * Fill.nmemb) );
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | return ptr;
|
---|
| 158 | } // $alloc_internal
|
---|
| 159 | */
|
---|
| 160 |
|
---|
| 161 | typedef struct S_align { inline size_t; } T_align;
|
---|
| 162 | typedef struct S_resize { inline void *; } T_resize;
|
---|
| 163 |
|
---|
| 164 | forall( dtype T ) {
|
---|
| 165 | struct S_fill { char tag; char c; size_t size; T * at; char t[50]; };
|
---|
| 166 | struct S_realloc { inline T *; };
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | static inline T_align ?`align ( size_t a ) { return (T_align){a}; }
|
---|
| 170 | static inline T_resize ?`resize ( void * a ) { return (T_resize){a}; }
|
---|
[cfbc703d] | 171 | static inline forall( dtype T | sized(T) ) {
|
---|
[74b19fb] | 172 |
|
---|
[ceb7db8] | 173 | S_fill(T) ?`fill ( T t ) {
|
---|
| 174 | S_fill(T) ret = { 't' };
|
---|
| 175 | size_t size = sizeof(T);
|
---|
| 176 | if(size > sizeof(ret.t)) { printf("ERROR: const object of size greater than 50 bytes given for dynamic memory fill\n"); exit(1); }
|
---|
| 177 | memcpy( &ret.t, &t, size );
|
---|
| 178 | return ret;
|
---|
| 179 | }
|
---|
| 180 | S_fill(T) ?`fill ( char c ) { return (S_fill(T)){ 'c', c }; }
|
---|
| 181 | S_fill(T) ?`fill ( T * a ) { return (S_fill(T)){ 'T', '0', 0, a }; }
|
---|
| 182 | S_fill(T) ?`fill ( T a[], size_t nmemb ) { return (S_fill(T)){ 'a', '0', nmemb * sizeof(T), a }; }
|
---|
| 183 |
|
---|
| 184 | S_realloc(T) ?`realloc ( T * a ) { return (S_realloc(T)){a}; }
|
---|
| 185 |
|
---|
| 186 | T * $alloc_internal( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill) {
|
---|
| 187 | T * ptr = NULL;
|
---|
| 188 | size_t size = sizeof(T);
|
---|
| 189 | size_t copy_end = 0;
|
---|
| 190 |
|
---|
| 191 | if(Resize) {
|
---|
| 192 | ptr = (T*) (void *) resize( (int *)Resize, Align, Dim * size );
|
---|
| 193 | } else if (Realloc) {
|
---|
| 194 | if (Fill.tag != '0') copy_end = min(malloc_size( Realloc ), Dim * size);
|
---|
| 195 | ptr = (T*) (void *) realloc( (int *)Realloc, Align, Dim * size );
|
---|
[cfbc703d] | 196 | } else {
|
---|
[ceb7db8] | 197 | ptr = (T*) (void *) memalign( Align, Dim * size );
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | if(Fill.tag == 'c') {
|
---|
| 201 | memset( (char *)ptr + copy_end, (int)Fill.c, Dim * size - copy_end );
|
---|
| 202 | } else if(Fill.tag == 't') {
|
---|
| 203 | for ( int i = copy_end; i <= Dim * size - size ; i += size ) {
|
---|
| 204 | memcpy( (char *)ptr + i, &Fill.t, size );
|
---|
| 205 | }
|
---|
| 206 | } else if(Fill.tag == 'a') {
|
---|
| 207 | memcpy( (char *)ptr + copy_end, Fill.at, min(Dim * size - copy_end, Fill.size) );
|
---|
| 208 | } else if(Fill.tag == 'T') {
|
---|
| 209 | for ( int i = copy_end; i <= Dim * size - size ; i += size ) {
|
---|
| 210 | memcpy( (char *)ptr + i, Fill.at, size );
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | return ptr;
|
---|
| 215 | } // $alloc_internal
|
---|
| 216 |
|
---|
| 217 | forall( ttype TT | { T * $alloc_internal( void *, T *, size_t, size_t, S_fill(T), TT ); } ) {
|
---|
| 218 |
|
---|
| 219 | T * $alloc_internal( void * , T * Realloc, size_t Align, size_t Dim, S_fill(T) Fill, T_resize Resize, TT rest) {
|
---|
| 220 | return $alloc_internal( Resize, (T*)0p, Align, Dim, Fill, rest);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | T * $alloc_internal( void * Resize, T * , size_t Align, size_t Dim, S_fill(T) Fill, S_realloc(T) Realloc, TT rest) {
|
---|
| 224 | return $alloc_internal( (void*)0p, Realloc, Align, Dim, Fill, rest);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | T * $alloc_internal( void * Resize, T * Realloc, size_t , size_t Dim, S_fill(T) Fill, T_align Align, TT rest) {
|
---|
| 228 | return $alloc_internal( Resize, Realloc, Align, Dim, Fill, rest);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | T * $alloc_internal( void * Resize, T * Realloc, size_t Align, size_t Dim, S_fill(T) , S_fill(T) Fill, TT rest) {
|
---|
| 232 | return $alloc_internal( Resize, Realloc, Align, Dim, Fill, rest);
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | T * alloc( TT all ) {
|
---|
| 236 | return $alloc_internal( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), (size_t)1, (S_fill(T)){'0'}, all);
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | T * alloc( size_t dim, TT all ) {
|
---|
| 240 | return $alloc_internal( (void*)0p, (T*)0p, (_Alignof(T) > libAlign() ? _Alignof(T) : libAlign()), dim, (S_fill(T)){'0'}, all);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | } // distribution TT
|
---|
| 244 |
|
---|
| 245 | } // distribution T
|
---|
[3ce0d440] | 246 |
|
---|
| 247 | static inline forall( dtype T | sized(T) ) {
|
---|
[ca7949b] | 248 | // Cforall safe initialization/copy, i.e., implicit size specification, non-array types
|
---|
[b9c04946] | 249 | T * memset( T * dest, char fill ) {
|
---|
| 250 | return (T *)memset( dest, fill, sizeof(T) );
|
---|
[3ce0d440] | 251 | } // memset
|
---|
| 252 |
|
---|
| 253 | T * memcpy( T * dest, const T * src ) {
|
---|
| 254 | return (T *)memcpy( dest, src, sizeof(T) );
|
---|
| 255 | } // memcpy
|
---|
| 256 | } // distribution
|
---|
| 257 |
|
---|
| 258 | static inline forall( dtype T | sized(T) ) {
|
---|
[ca7949b] | 259 | // Cforall safe initialization/copy, i.e., implicit size specification, array types
|
---|
[b9c04946] | 260 | T * amemset( T dest[], char fill, size_t dim ) {
|
---|
| 261 | return (T *)(void *)memset( dest, fill, dim * sizeof(T) ); // C memset
|
---|
| 262 | } // amemset
|
---|
[3ce0d440] | 263 |
|
---|
[b9c04946] | 264 | T * amemcpy( T dest[], const T src[], size_t dim ) {
|
---|
[3ce0d440] | 265 | return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
|
---|
[b9c04946] | 266 | } // amemcpy
|
---|
[3ce0d440] | 267 | } // distribution
|
---|
[f3fc631f] | 268 |
|
---|
[ca7949b] | 269 | // Cforall allocation/deallocation and constructor/destructor, non-array types
|
---|
[aca65621] | 270 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
|
---|
[aabb846] | 271 | forall( dtype T | { void ^?{}( T & ); } ) void delete( T * ptr );
|
---|
| 272 | forall( dtype T, ttype Params | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );
|
---|
[627f585] | 273 |
|
---|
[ca7949b] | 274 | // Cforall allocation/deallocation and constructor/destructor, array types
|
---|
[aca65621] | 275 | forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
|
---|
| 276 | forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
|
---|
| 277 | forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );
|
---|
[6065b3aa] | 278 |
|
---|
[bd85400] | 279 | //---------------------------------------
|
---|
| 280 |
|
---|
[57fc7d8] | 281 | static inline {
|
---|
[e3fea42] | 282 | int strto( const char sptr[], char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
|
---|
| 283 | unsigned int strto( const char sptr[], char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
|
---|
| 284 | long int strto( const char sptr[], char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
|
---|
| 285 | unsigned long int strto( const char sptr[], char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
|
---|
| 286 | long long int strto( const char sptr[], char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
|
---|
| 287 | unsigned long long int strto( const char sptr[], char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }
|
---|
| 288 |
|
---|
| 289 | float strto( const char sptr[], char ** eptr ) { return strtof( sptr, eptr ); }
|
---|
| 290 | double strto( const char sptr[], char ** eptr ) { return strtod( sptr, eptr ); }
|
---|
| 291 | long double strto( const char sptr[], char ** eptr ) { return strtold( sptr, eptr ); }
|
---|
[57fc7d8] | 292 | } // distribution
|
---|
[e672372] | 293 |
|
---|
[e3fea42] | 294 | float _Complex strto( const char sptr[], char ** eptr );
|
---|
| 295 | double _Complex strto( const char sptr[], char ** eptr );
|
---|
| 296 | long double _Complex strto( const char sptr[], char ** eptr );
|
---|
[bd85400] | 297 |
|
---|
[57fc7d8] | 298 | static inline {
|
---|
[e3fea42] | 299 | int ato( const char sptr[] ) { return (int)strtol( sptr, 0p, 10 ); }
|
---|
| 300 | unsigned int ato( const char sptr[] ) { return (unsigned int)strtoul( sptr, 0p, 10 ); }
|
---|
| 301 | long int ato( const char sptr[] ) { return strtol( sptr, 0p, 10 ); }
|
---|
| 302 | unsigned long int ato( const char sptr[] ) { return strtoul( sptr, 0p, 10 ); }
|
---|
| 303 | long long int ato( const char sptr[] ) { return strtoll( sptr, 0p, 10 ); }
|
---|
| 304 | unsigned long long int ato( const char sptr[] ) { return strtoull( sptr, 0p, 10 ); }
|
---|
| 305 |
|
---|
| 306 | float ato( const char sptr[] ) { return strtof( sptr, 0p ); }
|
---|
| 307 | double ato( const char sptr[] ) { return strtod( sptr, 0p ); }
|
---|
| 308 | long double ato( const char sptr[] ) { return strtold( sptr, 0p ); }
|
---|
| 309 |
|
---|
| 310 | float _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
|
---|
| 311 | double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
|
---|
| 312 | long double _Complex ato( const char sptr[] ) { return strto( sptr, 0p ); }
|
---|
[57fc7d8] | 313 | } // distribution
|
---|
[e672372] | 314 |
|
---|
[bd85400] | 315 | //---------------------------------------
|
---|
| 316 |
|
---|
[3ce0d440] | 317 | forall( otype E | { int ?<?( E, E ); } ) {
|
---|
| 318 | E * bsearch( E key, const E * vals, size_t dim );
|
---|
| 319 | size_t bsearch( E key, const E * vals, size_t dim );
|
---|
| 320 | E * bsearchl( E key, const E * vals, size_t dim );
|
---|
| 321 | size_t bsearchl( E key, const E * vals, size_t dim );
|
---|
| 322 | E * bsearchu( E key, const E * vals, size_t dim );
|
---|
| 323 | size_t bsearchu( E key, const E * vals, size_t dim );
|
---|
| 324 | } // distribution
|
---|
[9c47a47] | 325 |
|
---|
[3ce0d440] | 326 | forall( otype K, otype E | { int ?<?( K, K ); K getKey( const E & ); } ) {
|
---|
| 327 | E * bsearch( K key, const E * vals, size_t dim );
|
---|
| 328 | size_t bsearch( K key, const E * vals, size_t dim );
|
---|
| 329 | E * bsearchl( K key, const E * vals, size_t dim );
|
---|
| 330 | size_t bsearchl( K key, const E * vals, size_t dim );
|
---|
| 331 | E * bsearchu( K key, const E * vals, size_t dim );
|
---|
| 332 | size_t bsearchu( K key, const E * vals, size_t dim );
|
---|
| 333 | } // distribution
|
---|
[bd85400] | 334 |
|
---|
[b9c04946] | 335 | forall( otype E | { int ?<?( E, E ); } ) {
|
---|
| 336 | void qsort( E * vals, size_t dim );
|
---|
| 337 | } // distribution
|
---|
| 338 |
|
---|
[bd85400] | 339 | //---------------------------------------
|
---|
| 340 |
|
---|
[bbe1a87] | 341 | extern "C" { // override C version
|
---|
| 342 | void srandom( unsigned int seed );
|
---|
[4e7c0fc0] | 343 | long int random( void ); // GENERATES POSITIVE AND NEGATIVE VALUES
|
---|
| 344 | // For positive values, use unsigned int, e.g., unsigned int r = random() % 100U;
|
---|
[bbe1a87] | 345 | } // extern "C"
|
---|
| 346 |
|
---|
| 347 | static inline {
|
---|
| 348 | long int random( long int l, long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
|
---|
| 349 | long int random( long int u ) { if ( u < 0 ) return random( u, 0 ); else return random( 0, u ); } // [0,u)
|
---|
| 350 | unsigned long int random( void ) { return lrand48(); }
|
---|
| 351 | unsigned long int random( unsigned long int u ) { return lrand48() % u; } // [0,u)
|
---|
[4e7c0fc0] | 352 | unsigned long int random( unsigned long int l, unsigned long int u ) { if ( u < l ) [u, l] = [l, u]; return lrand48() % (u - l) + l; } // [l,u)
|
---|
[bbe1a87] | 353 |
|
---|
| 354 | char random( void ) { return (unsigned long int)random(); }
|
---|
| 355 | char random( char u ) { return random( (unsigned long int)u ); } // [0,u)
|
---|
| 356 | char random( char l, char u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
|
---|
| 357 | int random( void ) { return (long int)random(); }
|
---|
| 358 | int random( int u ) { return random( (long int)u ); } // [0,u]
|
---|
| 359 | int random( int l, int u ) { return random( (long int)l, (long int)u ); } // [l,u)
|
---|
| 360 | unsigned int random( void ) { return (unsigned long int)random(); }
|
---|
| 361 | unsigned int random( unsigned int u ) { return random( (unsigned long int)u ); } // [0,u]
|
---|
| 362 | unsigned int random( unsigned int l, unsigned int u ) { return random( (unsigned long int)l, (unsigned long int)u ); } // [l,u)
|
---|
| 363 | } // distribution
|
---|
| 364 |
|
---|
| 365 | float random( void ); // [0.0, 1.0)
|
---|
| 366 | double random( void ); // [0.0, 1.0)
|
---|
| 367 | float _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
|
---|
| 368 | double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
|
---|
| 369 | long double _Complex random( void ); // [0.0, 1.0)+[0.0, 1.0)i
|
---|
[bd85400] | 370 |
|
---|
| 371 | //---------------------------------------
|
---|
| 372 |
|
---|
[2026bb6] | 373 | extern bool threading_enabled(void) OPTIONAL_THREAD;
|
---|
| 374 |
|
---|
[bd85400] | 375 | // Local Variables: //
|
---|
| 376 | // mode: c //
|
---|
| 377 | // tab-width: 4 //
|
---|
| 378 | // End: //
|
---|