/* Version 0.0
** From "Wayne's Little DSA Library" (DSA == Data Structures and
** Algorithms) Feel free to change, modify, or burn these sources, but if
** you modify them please don't distribute your changes without a clear
** indication that you've done so.  If you think your change is spiffy,
** send it to me and maybe I'll include it in the next release.
**
** Wayne Hayes, wayne@csri.utoronto.ca (preffered), or wayne@csri.toronto.edu
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include <sys/time.h>   /*getrusage doesn't seem to work.*/
#include <sys/resource.h>
#include <unistd.h>
/*#include <../ucbinclude/sys/rusage.h>*/

#include "misc.h"

const foint ABSTRACT_ERROR = {(1 << (8*sizeof(void*)-1))};

static FILE *tty;

void Warning(const char *fmt, ...)
{
    va_list ap;
    fflush(stdout);
    fprintf(stderr, "Warning: ");
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    fprintf(stderr, "\n");
    if(!isatty(fileno(stderr)))
    {
	if(!tty)
	    if(!(tty = fopen("/dev/tty", "w")))
		return;
	fprintf(tty, "Warning: ");
	va_start(ap, fmt);
	vfprintf(tty, fmt, ap);
	va_end(ap);
	fprintf(tty, "\n");
    }
}

void Apology(const char *fmt, ...)
{
    va_list ap;
    fflush(stdout);
    fprintf(stderr, "Sorry, fatal limitation: ");
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    fprintf(stderr, "\n");
    if(!isatty(fileno(stderr)))
    {
	if(!tty)
	    tty = fopen("/dev/tty", "w");
	fprintf(tty, "Sorry, fatal limitation: ");
	va_start(ap, fmt);
	vfprintf(tty, fmt, ap);
	va_end(ap);
	fprintf(tty, "\n");
    }
    assert(false);
    exit(1);
}

void Fatal(const char *fmt, ...)
{
    va_list ap;
    fflush(stdout);
    fprintf(stderr, "Fatal Error: ");
    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
    fprintf(stderr, "\n");
    if(!isatty(fileno(stderr)))
    {
	if(!tty)
	    tty = fopen("/dev/tty", "w");
	fprintf(tty, "Fatal Error: ");
	va_start(ap, fmt);
	vfprintf(tty, fmt, ap);
	va_end(ap);
	fprintf(tty, "\n");
    }
    assert(false);
    exit(1);
}

/* A malloc that exits if system calloc fails.
*/
void *Malloc(int n)
{
    void *p;
    assert(n>=0);
    p = (void*)malloc(n);
    if(!p && n)
	Fatal("malloc failed");
    return p;
}
void *Calloc(int n, int m)
{
    void *p;
    assert(n>=0 && m>=0);
    p = (void*)calloc(n, m);
    if(!p && n && m)
	Fatal("calloc failed");
    return p;
}

void *Realloc(void *ptr, int newSize)
{
    void *p;
    assert(newSize>=0);
    p = (void*) realloc(ptr, newSize);
    if(!p)
	Fatal("realloc failed");
    return p;
}

void *Memdup(void *v, int n)
{
    void *r = Malloc(n);
    memcpy(r, v, n);
    return r;
}


/* return current user time used in seconds */
double uTime(void)
{
#if 1
	struct rusage rUsage;
	getrusage(RUSAGE_SELF, &rUsage);
	return rUsage.ru_utime.tv_sec + 1e-6*rUsage.ru_utime.tv_usec;
#else
	return -1;
#endif
}

char *Int2BitString(char word[33], unsigned i)
{
    int b, k = 0;
    assert(sizeof(unsigned) == 4);
    for(b=31; b >= 0; b--)
	word[k++] = '0' + !!(i & (1<<b));
    word[k] = '\0';
    return word;
}


void PrintArray(FILE *fp, int n, int *array)
{
    int i;
    if(!n)
	return;
    for(i=0; i<n; i++)
	fprintf(fp, "%d ", array[i]);
    fprintf(fp, "\n");
}

double IntPow(double a, int n)
{
    double result;
    if(n == 1)
	return a;
    if(n == 0)
	return a == 0.0 ? 0/0 : 1;
    if(n < 0)
	return 1/IntPow(a,-n);

    result = IntPow(a, n/2);
    if(n & 1)
	return a * result * result;
    else
	return result * result;
}


int Log2(int n)
{
    int log2 = 0;
    assert(n != 0);
    while((n /= 2))
	++log2;
    return log2;
}
