Joseph Redmon
2014-12-16 d6cdea49eb509af852e6279922863cf63adb4a56
src/utils.c
@@ -1,17 +1,52 @@
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include "utils.h"
char *find_replace(char *str, char *orig, char *rep)
{
    static char buffer[4096];
    char *p;
    if(!(p = strstr(str, orig)))  // Is 'orig' even in 'str'?
        return str;
    strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
    buffer[p-str] = '\0';
    sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
    return buffer;
}
float sec(clock_t clocks)
{
    return (float)clocks/CLOCKS_PER_SEC;
}
void top_k(float *a, int n, int k, int *index)
{
    int i,j;
    for(j = 0; j < k; ++j) index[j] = 0;
    for(i = 0; i < n; ++i){
        int curr = i;
        for(j = 0; j < k; ++j){
            if(a[curr] > a[index[j]]){
                int swap = curr;
                curr = index[j];
                index[j] = swap;
            }
        }
    }
}
void error(char *s)
{
    fprintf(stderr, "Error: %s\n", s);
    perror(s);
    //fprintf(stderr, "Error: %s\n", s);
    exit(0);
}
@@ -79,7 +114,7 @@
    }
    int curr = strlen(line);
    while(line[curr-1]!='\n'){
        size *= 2;
        line = realloc(line, size*sizeof(char));
@@ -121,34 +156,34 @@
int count_fields(char *line)
{
   int count = 0;
   int done = 0;
    int count = 0;
    int done = 0;
    char *c;
   for(c = line; !done; ++c){
      done = (*c == '\0');
      if(*c == ',' || done) ++count;
   }
   return count;
    for(c = line; !done; ++c){
        done = (*c == '\0');
        if(*c == ',' || done) ++count;
    }
    return count;
}
float *parse_fields(char *line, int n)
{
   float *field = calloc(n, sizeof(float));
   char *c, *p, *end;
   int count = 0;
   int done = 0;
   for(c = line, p = line; !done; ++c){
      done = (*c == '\0');
      if(*c == ',' || done){
         *c = '\0';
         field[count] = strtod(p, &end);
         if(p == c) field[count] = nan("");
         if(end != c && (end != c-1 || *end != '\r')) field[count] = nan(""); //DOS file formats!
         p = c+1;
         ++count;
      }
   }
   return field;
    float *field = calloc(n, sizeof(float));
    char *c, *p, *end;
    int count = 0;
    int done = 0;
    for(c = line, p = line; !done; ++c){
        done = (*c == '\0');
        if(*c == ',' || done){
            *c = '\0';
            field[count] = strtod(p, &end);
            if(p == c) field[count] = nan("");
            if(end != c && (end != c-1 || *end != '\r')) field[count] = nan(""); //DOS file formats!
            p = c+1;
            ++count;
        }
    }
    return field;
}
float sum_array(float *a, int n)
@@ -222,13 +257,40 @@
    return max_i;
}
// From http://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
#define TWO_PI 6.2831853071795864769252866
float rand_normal()
{
    int i;
    float sum= 0;
    for(i = 0; i < 12; ++i) sum += (float)rand()/RAND_MAX;
    return sum-6.;
    static int haveSpare = 0;
    static double rand1, rand2;
    if(haveSpare)
    {
        haveSpare = 0;
        return sqrt(rand1) * sin(rand2);
    }
    haveSpare = 1;
    rand1 = rand() / ((double) RAND_MAX);
    if(rand1 < 1e-100) rand1 = 1e-100;
    rand1 = -2 * log(rand1);
    rand2 = (rand() / ((double) RAND_MAX)) * TWO_PI;
    return sqrt(rand1) * cos(rand2);
}
/*
   float rand_normal()
   {
   int n = 12;
   int i;
   float sum= 0;
   for(i = 0; i < n; ++i) sum += (float)rand()/RAND_MAX;
   return sum-n/2.;
   }
 */
float rand_uniform()
{
    return (float)rand()/RAND_MAX;