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