PageRenderTime 53ms CodeModel.GetById 9ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 0ms

/src/hierarchical/C/util.cpp

http://github.com/beechung/Latent-Factor-Models
C++ | 211 lines | 178 code | 28 blank | 5 comment | 43 complexity | f4130a0e8add273a0a9c76504f257a9c MD5 | raw file
  1#include "util.h"
  2#include <math.h>
  3
  4
  5void error(const char *fmt, ...)
  6{
  7	char buf[BUFSIZ];
  8	va_list ap;
  9	va_start(ap,fmt);
 10	vsprintf(buf,fmt,ap);
 11	va_end(ap);
 12	fprintf(stderr, buf);
 13    exit(-1);
 14}
 15
 16void error2(const char *filename, int lineno, const char *fmt, ...){
 17	char buf[BUFSIZ];
 18	va_list ap;
 19	va_start(ap,fmt);
 20	vsprintf(buf,fmt,ap);
 21	va_end(ap);
 22	fprintf(stderr, "ERROR in file %s at line %d: ", filename, lineno);
 23	fprintf(stderr, buf);
 24	fprintf(stderr, "\n");
 25    exit(-1);
 26}
 27
 28void info(FILE* fp, const int verbose, const char* fmt, ...){
 29	char buf[BUFSIZ];
 30	va_list ap;
 31	va_start(ap,fmt);
 32	vsprintf(buf,fmt,ap);
 33	va_end(ap);
 34	int n = (verbose-1)*3;
 35	if(n<0) n=0;
 36	std::string str = indent(buf, n);
 37	fprintf(fp, str.c_str());
 38}
 39
 40CReverseIndex::CReverseIndex(const int* indexArray, const int arrayLength, const int nIndexValues){
 41
 42    revIndex = Malloc(int*, nIndexValues);
 43    num      = Malloc(int, nIndexValues);
 44    index_space = Malloc(int, arrayLength);
 45    size = nIndexValues;
 46    
 47    for(int i=0; i<nIndexValues; i++) num[i] = 0;
 48    
 49    for(int k=0; k<arrayLength; k++){
 50        int i = indexArray[k];
 51#ifdef SANITY_CHECK
 52        CHK_C_INDEX(i, nIndexValues);
 53#endif
 54        num[i]++;
 55    }
 56    
 57    revIndex[0] = index_space;
 58    for(int i=1; i<nIndexValues; i++){
 59        revIndex[i] = &(revIndex[i-1][num[i-1]]);
 60    }
 61    
 62    int *current = Malloc(int, nIndexValues);
 63    for(int i=0; i<nIndexValues; i++) current[i] = 0;
 64
 65    for(int k=0; k<arrayLength; k++){
 66        int i = indexArray[k];
 67        revIndex[i][current[i]] = k;
 68        current[i]++;
 69    }
 70#ifdef SANITY_CHECK
 71    for(int i=0; i<nIndexValues; i++){
 72        if(current[i] != num[i]) STOP_HERE("sanity check error");
 73        for(int j=0; j<num[i]; j++){
 74            if(i != indexArray[revIndex[i][j]]) STOP_HERE("sanity check error");
 75        }
 76    }
 77#endif
 78    free(current);
 79}
 80
 81CReverseIndex::~CReverseIndex(){
 82    free(revIndex);
 83    free(num);
 84    free(index_space);
 85}
 86
 87void tokenize(const std::string& str, std::vector<std::string>& tokens, const std::string& delimiters)
 88{
 89    // Skip delimiters at beginning.
 90    std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
 91    // Find first "non-delimiter".
 92    std::string::size_type pos     = str.find_first_of(delimiters, lastPos);
 93
 94    tokens.clear();
 95    
 96    while (std::string::npos != pos || std::string::npos != lastPos)
 97    {
 98        // Found a token, add it to the vector.
 99        tokens.push_back(str.substr(lastPos, pos - lastPos));
100        // Skip delimiters.  Note the "not_of"
101        lastPos = str.find_first_not_of(delimiters, pos);
102        // Find next "non-delimiter"
103        pos = str.find_first_of(delimiters, lastPos);
104    }
105}
106
107void print_matrix(double **matrix, const int nrow, const int ncol, FILE* fp, const char* sep, const char* format){
108    for(int i=0; i<nrow; i++){
109        for(int j=0; j<ncol; j++){
110            if(j>0) fprintf(fp, sep);
111            fprintf(fp, format, matrix[i][j]);
112        }
113        fprintf(fp, "\n");
114    }
115}
116
117void print_row_vector(double *vector, const int num, FILE* fp, const char* sep, const char* format){
118    for(int i=0; i<num; i++){
119        if(i>0) fprintf(fp, sep);
120        fprintf(fp, format, vector[i]);
121    }
122}
123
124void print_col_vector(double *vector, const int num, FILE* fp, const char* sep, const char* format){
125    for(int i=0; i<num; i++){
126        fprintf(fp, format, vector[i]);
127        fprintf(fp, "\n");
128    }
129}
130
131
132void print_matrix(int **matrix, const int nrow, const int ncol, FILE* fp, const char* sep, const char* format){
133    for(int i=0; i<nrow; i++){
134        for(int j=0; j<ncol; j++){
135            if(j>0) fprintf(fp, sep);
136            fprintf(fp, format, matrix[i][j]);
137        }
138        fprintf(fp, "\n");
139    }
140}
141
142void print_row_vector(int *vector, const int num, FILE* fp, const char* sep, const char* format){
143    for(int i=0; i<num; i++){
144        if(i>0) fprintf(fp, sep);
145        fprintf(fp, format, vector[i]);
146    }
147}
148
149void print_col_vector(int *vector, const int num, FILE* fp, const char* sep, const char* format){
150    for(int i=0; i<num; i++){
151        fprintf(fp, format, vector[i]);
152        fprintf(fp, "\n");
153    }
154}
155
156
157
158std::string indent(const char* input, int n){
159	int len = strlen(input);
160	std::string out = "";
161	char *buf = (char*) malloc((len+1) * sizeof(char));
162	char *space = (char*) malloc((n+1) * sizeof(char));
163	for(int i=0; i<n; i++) space[i] = ' ';
164	space[n] = '\0';
165	int k=0;
166	for(int i=0; input[i] != '\0'; i++){
167		buf[k] = input[i];
168		k++;
169		if(input[i] == '\n'){
170			buf[k] = '\0';
171			if(k > 1) out += space;
172			out += buf;
173			k=0;
174		}
175	}
176	buf[k] = '\0';
177	if(k > 1 || (k==1 && buf[0] != '\n')) out += space;
178	out += buf;
179	free(buf);
180	free(space);
181	return out;
182}
183
184double* get_doubleArray(const char* list, int* num, const char* sep){
185    std::string temp(list);
186    std::vector<std::string> tokens;
187    tokenize(temp, tokens, sep);
188    (*num) = tokens.size();
189    double *output = new double[*num];
190    for(unsigned int i=0; i<tokens.size(); i++){
191    	int status = parse_double(tokens[i].c_str(), &(output[i]));
192    	if(status != 0) error("ERROR when parsing '%s': It is expected to be a number!\n", tokens[i].c_str());
193    }
194    return output;
195}
196
197int toLinearIndex(const int *dim, const int *dimSize, int ndim){
198	int index = dim[0];
199	for(int i=1; i<ndim; i++){
200		index = (index*dimSize[i])+dim[i];
201	}
202	return index;
203}
204
205void fillInDimIndex(int index, int *dim, const int *dimSize, int ndim){
206	int temp = index;
207	for(int i=ndim-1; i>=0; i--){
208		dim[i] = temp % dimSize[i];
209		temp /= dimSize[i];
210	}
211}