//
// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
//
// The contents of this file are covered under the licence agreement in the
// file "LICENCE" distributed with Cforall.
//
// stdlib --
//
// Author           : Peter A. Buhr
// Created On       : Thu Jan 28 17:12:35 2016
// Last Modified By : Peter A. Buhr
// Last Modified On : Sat Dec 23 18:21:42 2017
// Update Count     : 267
//

#pragma once

//#define _XOPEN_SOURCE 600								// posix_memalign, *rand48
#include <stdlib.h>										// strto*, *abs

//---------------------------------------

#ifndef EXIT_FAILURE
#define	EXIT_FAILURE	1								// failing exit status
#define	EXIT_SUCCESS	0								// successful exit status
#endif // ! EXIT_FAILURE

//---------------------------------------

// allocation, non-array types
static inline forall( dtype T | sized(T) ) T * malloc( void ) {
	// printf( "* malloc\n" );
	return (T *)(void *)malloc( (size_t)sizeof(T) );	// C malloc
} // malloc

// static inline forall( dtype T | sized(T) ) T & malloc( void ) {
// 	int & p = *(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
// 	printf( "& malloc %p\n", &p );
// 	return p;
// //	return (T &)*(T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
// } // malloc

extern "C" { void * calloc( size_t dim, size_t size ); } // default C routine
static inline forall( dtype T | sized(T) ) T * calloc( size_t dim ) {
	//printf( "X2\n" );
	return (T *)(void *)calloc( dim, sizeof(T) );		// C cmalloc
}

extern "C" { void * realloc( void * ptr, size_t size ); } // default C routine for void *
static inline forall( dtype T | sized(T) ) T * realloc( T * ptr, size_t size ) {
	//printf( "X3\n" );
	return (T *)(void *)realloc( (void *)ptr, size );
}

extern "C" { void * memalign( size_t align, size_t size ); } // use default C routine for void *
static inline forall( dtype T | sized(T) ) T * memalign( size_t align ) {
	//printf( "X4\n" );
	return (T *)memalign( align, sizeof(T) );
} // memalign

static inline forall( dtype T | sized(T) ) T * aligned_alloc( size_t align ) {
	//printf( "X5\n" );
	return (T *)memalign( align, sizeof(T) );
} // aligned_alloc

extern "C" { int posix_memalign( void ** ptr, size_t align, size_t size ); } // use default C routine for void *
static inline forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t align ) {
	//printf( "X6\n" );
	return posix_memalign( (void **)ptr, align, sizeof(T) );
} // posix_memalign


extern "C" { void * memset( void * dest, int c, size_t size ); } // use default C routine for void *

static inline forall( dtype T | sized(T) ) T * alloc( void ) {
	//printf( "X7\n" );
	return (T *)(void *)malloc( (size_t)sizeof(T) );	// C malloc
} // alloc
static inline forall( dtype T | sized(T) ) T * alloc( char fill ) {
	//printf( "X8\n" );
	T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );	// C malloc
    return (T *)memset( ptr, (int)fill, sizeof(T) );	// initial with fill value
} // alloc

static inline forall( dtype T | sized(T) ) T * alloc( size_t dim ) {
	//printf( "X9\n" );
	return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
} // alloc
static inline forall( dtype T | sized(T) ) T * alloc( size_t dim, char fill ) {
	//printf( "X10\n" );
	T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
    return (T *)memset( ptr, (int)fill, dim * sizeof(T) );
} // alloc

static inline forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim ) {
	//printf( "X11\n" );
	return (T *)(void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
} // alloc
forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );

static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align ) {
	//printf( "X13\n" );
	return (T *)memalign( align, sizeof(T) );
} // align_alloc
static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill ) {
	//printf( "X14\n" );
    T * ptr = (T *)memalign( align, sizeof(T) );
    return (T *)memset( ptr, (int)fill, sizeof(T) );
} // align_alloc

static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim ) {
	//printf( "X15\n" );
	return (T *)memalign( align, dim * sizeof(T) );
} // align_alloc
static inline forall( dtype T | sized(T) ) T * align_alloc( size_t align, size_t dim, char fill ) {
	//printf( "X16\n" );
    T * ptr = (T *)memalign( align, dim * sizeof(T) );
    return (T *)memset( ptr, (int)fill, dim * sizeof(T) );
} // align_alloc


// data, non-array types
static inline forall( dtype T | sized(T) ) T * memset( T * dest, char c ) {
	//printf( "X17\n" );
	return (T *)memset( dest, c, sizeof(T) );
} // memset
extern "C" { void * memcpy( void * dest, const void * src, size_t size ); } // use default C routine for void *
static inline forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src ) {
	//printf( "X18\n" );
	return (T *)memcpy( dest, src, sizeof(T) );
} // memcpy

// data, array types
static inline forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ) {
	//printf( "X19\n" );
	return (T *)(void *)memset( dest, c, dim * sizeof(T) );	// C memset
} // memset
static inline forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ) {
	//printf( "X20\n" );
	return (T *)(void *)memcpy( dest, src, dim * sizeof(T) ); // C memcpy
} // memcpy

// allocation/deallocation and constructor/destructor, non-array types
forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );
forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );
forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) void delete( T * ptr, Params rest );

// allocation/deallocation and constructor/destructor, array types
forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );
forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );
forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) void adelete( size_t dim, T arr[], Params rest );

//---------------------------------------

static inline int strto( const char * sptr, char ** eptr, int base ) { return (int)strtol( sptr, eptr, base ); }
static inline unsigned int strto( const char * sptr, char ** eptr, int base ) { return (unsigned int)strtoul( sptr, eptr, base ); }
static inline long int strto( const char * sptr, char ** eptr, int base ) { return strtol( sptr, eptr, base ); }
static inline unsigned long int strto( const char * sptr, char ** eptr, int base ) { return strtoul( sptr, eptr, base ); }
static inline long long int strto( const char * sptr, char ** eptr, int base ) { return strtoll( sptr, eptr, base ); }
static inline unsigned long long int strto( const char * sptr, char ** eptr, int base ) { return strtoull( sptr, eptr, base ); }

static inline float strto( const char * sptr, char ** eptr ) { return strtof( sptr, eptr ); }
static inline double strto( const char * sptr, char ** eptr ) { return strtod( sptr, eptr ); }
static inline long double strto( const char * sptr, char ** eptr ) { return strtold( sptr, eptr ); }

float _Complex strto( const char * sptr, char ** eptr );
double _Complex strto( const char * sptr, char ** eptr );
long double _Complex strto( const char * sptr, char ** eptr );

static inline int ato( const char * sptr ) {return (int)strtol( sptr, 0, 10 ); }
static inline unsigned int ato( const char * sptr ) { return (unsigned int)strtoul( sptr, 0, 10 ); }
static inline long int ato( const char * sptr ) { return strtol( sptr, 0, 10 ); }
static inline unsigned long int ato( const char * sptr ) { return strtoul( sptr, 0, 10 ); }
static inline long long int ato( const char * sptr ) { return strtoll( sptr, 0, 10 ); }
static inline unsigned long long int ato( const char * sptr ) { return strtoull( sptr, 0, 10 ); }

static inline float ato( const char * sptr ) { return strtof( sptr, 0 ); }
static inline double ato( const char * sptr ) { return strtod( sptr, 0 ); }
static inline long double ato( const char * sptr ) { return strtold( sptr, 0 ); }

static inline float _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
static inline double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }
static inline long double _Complex ato( const char * sptr ) { return strto( sptr, NULL ); }

//---------------------------------------

forall( otype T | { int ?<?( T, T ); } )
T * bsearch( T key, const T * arr, size_t dim );

forall( otype T | { int ?<?( T, T ); } )
unsigned int bsearch( T key, const T * arr, size_t dim );


forall( otype T | { int ?<?( T, T ); } )
void qsort( const T * arr, size_t dim );

//---------------------------------------

[ int, int ] div( int num, int denom );
[ long int, long int ] div( long int num, long int denom );
[ long long int, long long int ] div( long long int num, long long int denom );
forall( otype T | { T ?/?( T, T ); T ?%?( T, T ); } )
[ T, T ] div( T num, T demon );

//---------------------------------------

static inline unsigned char abs( signed char v ) { return abs( (int)v ); }
extern "C" { int abs( int ); }							// use default C routine for int
static inline unsigned long int abs( long int v ) { return labs( v ); }
static inline unsigned long long int abs( long long int v ) { return llabs( v ); }

extern "C" {
double fabs( double );
float fabsf( float );
long double fabsl( long double );
} // extern "C"
static inline float abs( float x ) { return fabsf( x ); }
static inline double abs( double x ) { return fabs( x ); }
static inline long double abs( long double x ) { return fabsl( x ); }

extern "C" {
double cabs( double _Complex );
float cabsf( float _Complex );
long double cabsl( long double _Complex );
} // extern "C"
static inline float abs( float _Complex x ) { return cabsf( x ); }
static inline double abs( double _Complex x ) { return cabs( x ); }
static inline long double abs( long double _Complex x ) { return cabsl( x ); }

forall( otype T | { void ?{}( T &, zero_t ); int ?<?( T, T ); T -?( T ); } )
T abs( T );

//---------------------------------------

void random_seed( long int s );
char random( void );
char random( char u );
char random( char l, char u );
int random( void );
int random( int u );
int random( int l, int u );
unsigned int random( void );
unsigned int random( unsigned int u );
unsigned int random( unsigned int l, unsigned int u );
extern "C" { long int random( void ); }
long int random( long int u );
long int random( long int l, long int u );
unsigned long int random( void );
unsigned long int random( unsigned long int u );
unsigned long int random( unsigned long int l, unsigned long int u );
float random( void );
double random( void );
float _Complex random( void );
double _Complex random( void );
long double _Complex random( void );

//---------------------------------------

forall( otype T | { int ?<?( T, T ); } )
static inline T min( T t1, T t2 ) { return t1 < t2 ? t1 : t2; }

forall( otype T | { int ?>?( T, T ); } )
static inline T max( T t1, T t2 ) { return t1 > t2 ? t1 : t2; }

forall( otype T | { T min( T, T ); T max( T, T ); } )
static inline T clamp( T value, T min_val, T max_val ) { return max( min_val, min( value, max_val ) ); }

forall( otype T )
static inline void swap( T & v1, T & v2 ) { T temp = v1; v1 = v2; v2 = temp; }

// Local Variables: //
// mode: c //
// tab-width: 4 //
// End: //
