Joseph Redmon
2014-11-18 d407bffde934ea4c1ee392f24cdf26d9a987199b
src/utils.c
@@ -1,14 +1,51 @@
#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;
    float thresh = FLT_MAX;
    for(i = 0; i < k; ++i){
        float max = -FLT_MAX;
        int max_i = -1;
        for(j = 0; j < n; ++j){
            float val = a[j];
            if(val > max &&  val < thresh){
                max = val;
                max_i = j;
            }
        }
        index[i] = max_i;
        thresh = max;
    }
}
void error(char *s)
{
    fprintf(stderr, "Error: %s\n", s);