#include "util.h"

/* this allocates memory and makes sure that the allocation worked */
void *safe_malloc(unsigned int size) {
  void *r;
  assert(size > 0);
  r = malloc(size);
  if (r == NULL) {
    fprintf(stderr, "Sorry, out of memory.  Exiting.\n");
    exit(1);
  } else {
    return r;
  }
}

/* rounds a double into an integer */
BITS round(double a) {                                            
  if (a-(BITS)a >= .5) {                                          
    a++;                                                          
  }                                                               
  return (BITS)a;
}
                                                                  
/* returns a random floating point value between min and max */
double randomFloat(double min, double max) {                      
  double res;                                                     
  res = rand()*(max-min);                                         
  res /= RAND_MAX;                                                
  return res+min;                                                 
}                                                                 
                                                                  
/* returns a random unsigned int between min and max, inclusive */
BITS randomInt(BITS min, BITS max) {                              
  return (BITS)(randomFloat((double)min, 1+(double)max));         
}                                                                 
                                                                  
/* returns 1 if the event occured given rate, and 0 otherwise */
BITS event(double rate) {                                         
  if (randomFloat(0, 1) <= rate) {              
    return 1;                                                     
  } else {                                                        
    return 0;                                                     
  }                                                               
}                                                                 
                                                                  
/* returns the value of a gaussian evaluated at str
 * where the gaussian has variance var, mean avg and amplitude amp */
double gaussian(BITS str, double var, double avg, double amp) {   
  double temp;                                                    
  temp = str-avg;                                                 
  temp *= temp;                                                   
  return amp*exp(-var*temp);                                      
}                                                                 

/* prints str in binary to file output */
void printBinary(FILE* output, BITS str) {                       
  int i;                                           
  BITS mask = 1;                                   
  BITS numBits = BITS_PER_BYTE * sizeof(BITS);     
  for (i=numBits-1; i>=0; i--) {                   
    fprintf(output, "%u", mask & (str >> i));               
    if (i%8 == 0) {
      fprintf(output, " ");
    }
  }                                                
}                                                  

