From 0df9d25c46e39fa5f532c023261784fdcdd5d25d Mon Sep 17 00:00:00 2001
From: Joseph Redmon <pjreddie@gmail.com>
Date: Thu, 11 Jun 2015 22:51:17 +0000
Subject: [PATCH] new cfg files for classification
---
src/utils.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 54 insertions(+), 5 deletions(-)
diff --git a/src/utils.c b/src/utils.c
index 8261682..bd2f54d 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -2,11 +2,39 @@
#include <stdlib.h>
#include <string.h>
#include <math.h>
+#include <unistd.h>
#include <float.h>
#include <limits.h>
#include "utils.h"
+
+char *basecfg(char *cfgfile)
+{
+ char *c = cfgfile;
+ char *next;
+ while((next = strchr(c, '/')))
+ {
+ c = next+1;
+ }
+ c = copy_string(c);
+ next = strchr(c, '_');
+ if (next) *next = 0;
+ next = strchr(c, '.');
+ if (next) *next = 0;
+ return c;
+}
+
+int alphanum_to_int(char c)
+{
+ return (c < 58) ? c - 48 : c-87;
+}
+char int_to_alphanum(int i)
+{
+ if (i == 36) return '.';
+ return (i < 10) ? i + 48 : i + 87;
+}
+
void pm(int M, int N, float *A)
{
int i,j;
@@ -45,11 +73,11 @@
void top_k(float *a, int n, int k, int *index)
{
int i,j;
- for(j = 0; j < k; ++j) index[j] = 0;
+ for(j = 0; j < k; ++j) index[j] = -1;
for(i = 0; i < n; ++i){
int curr = i;
for(j = 0; j < k; ++j){
- if(a[curr] > a[index[j]]){
+ if((index[j] < 0) || a[curr] > a[index[j]]){
int swap = curr;
curr = index[j];
index[j] = swap;
@@ -148,6 +176,27 @@
return line;
}
+void read_all(int fd, char *buffer, size_t bytes)
+{
+ size_t n = 0;
+ while(n < bytes){
+ int next = read(fd, buffer + n, bytes-n);
+ if(next <= 0) error("read failed");
+ n += next;
+ }
+}
+
+void write_all(int fd, char *buffer, size_t bytes)
+{
+ size_t n = 0;
+ while(n < bytes){
+ size_t next = write(fd, buffer + n, bytes-n);
+ if(next <= 0) error("write failed");
+ n += next;
+ }
+}
+
+
char *copy_string(char *s)
{
char *copy = malloc(strlen(s)+1);
@@ -227,10 +276,10 @@
return variance;
}
-float constrain(float a, float max)
+float constrain(float min, float max, float a)
{
- if(a > abs(max)) return abs(max);
- if(a < -abs(max)) return -abs(max);
+ if (a < min) return min;
+ if (a > max) return max;
return a;
}
--
Gitblit v1.10.0