Mercurial > hg > smallbox
annotate util/ksvd utils/ompbox utils/omputils.c @ 137:9207d56c5547 ivand_dev
New ompbox in utils for testing purposes
author | Ivan Damnjanovic lnx <ivan.damnjanovic@eecs.qmul.ac.uk> |
---|---|
date | Thu, 21 Jul 2011 14:07:41 +0100 |
parents | |
children |
rev | line source |
---|---|
ivan@137 | 1 /************************************************************************** |
ivan@137 | 2 * |
ivan@137 | 3 * File name: omputils.c |
ivan@137 | 4 * |
ivan@137 | 5 * Ron Rubinstein |
ivan@137 | 6 * Computer Science Department |
ivan@137 | 7 * Technion, Haifa 32000 Israel |
ivan@137 | 8 * ronrubin@cs |
ivan@137 | 9 * |
ivan@137 | 10 * Last Updated: 18.8.2009 |
ivan@137 | 11 * |
ivan@137 | 12 *************************************************************************/ |
ivan@137 | 13 |
ivan@137 | 14 #include "omputils.h" |
ivan@137 | 15 #include <math.h> |
ivan@137 | 16 |
ivan@137 | 17 |
ivan@137 | 18 const char FULL_GAMMA_STR[] = "full"; |
ivan@137 | 19 const char SPARSE_GAMMA_STR[] = "sparse"; |
ivan@137 | 20 |
ivan@137 | 21 |
ivan@137 | 22 /* convert seconds to hours, minutes and seconds */ |
ivan@137 | 23 |
ivan@137 | 24 void secs2hms(double sectot, int *hrs, int *mins, double *secs) |
ivan@137 | 25 { |
ivan@137 | 26 *hrs = (int)(floor(sectot/3600)+1e-2); |
ivan@137 | 27 sectot = sectot - 3600*(*hrs); |
ivan@137 | 28 *mins = (int)(floor(sectot/60)+1e-2); |
ivan@137 | 29 *secs = sectot - 60*(*mins); |
ivan@137 | 30 } |
ivan@137 | 31 |
ivan@137 | 32 |
ivan@137 | 33 /* quicksort, public-domain C implementation by Darel Rex Finley. */ |
ivan@137 | 34 /* modification: sorts the array data[] as well, according to the values in the array vals[] */ |
ivan@137 | 35 |
ivan@137 | 36 #define MAX_LEVELS 300 |
ivan@137 | 37 |
ivan@137 | 38 void quicksort(mwIndex vals[], double data[], mwIndex n) { |
ivan@137 | 39 |
ivan@137 | 40 long piv, beg[MAX_LEVELS], end[MAX_LEVELS], i=0, L, R, swap ; |
ivan@137 | 41 double datapiv; |
ivan@137 | 42 |
ivan@137 | 43 beg[0]=0; |
ivan@137 | 44 end[0]=n; |
ivan@137 | 45 |
ivan@137 | 46 while (i>=0) { |
ivan@137 | 47 |
ivan@137 | 48 L=beg[i]; |
ivan@137 | 49 R=end[i]-1; |
ivan@137 | 50 |
ivan@137 | 51 if (L<R) { |
ivan@137 | 52 |
ivan@137 | 53 piv=vals[L]; |
ivan@137 | 54 datapiv=data[L]; |
ivan@137 | 55 |
ivan@137 | 56 while (L<R) |
ivan@137 | 57 { |
ivan@137 | 58 while (vals[R]>=piv && L<R) |
ivan@137 | 59 R--; |
ivan@137 | 60 if (L<R) { |
ivan@137 | 61 vals[L]=vals[R]; |
ivan@137 | 62 data[L++]=data[R]; |
ivan@137 | 63 } |
ivan@137 | 64 |
ivan@137 | 65 while (vals[L]<=piv && L<R) |
ivan@137 | 66 L++; |
ivan@137 | 67 if (L<R) { |
ivan@137 | 68 vals[R]=vals[L]; |
ivan@137 | 69 data[R--]=data[L]; |
ivan@137 | 70 } |
ivan@137 | 71 } |
ivan@137 | 72 |
ivan@137 | 73 vals[L]=piv; |
ivan@137 | 74 data[L]=datapiv; |
ivan@137 | 75 |
ivan@137 | 76 beg[i+1]=L+1; |
ivan@137 | 77 end[i+1]=end[i]; |
ivan@137 | 78 end[i++]=L; |
ivan@137 | 79 |
ivan@137 | 80 if (end[i]-beg[i] > end[i-1]-beg[i-1]) { |
ivan@137 | 81 swap=beg[i]; beg[i]=beg[i-1]; beg[i-1]=swap; |
ivan@137 | 82 swap=end[i]; end[i]=end[i-1]; end[i-1]=swap; |
ivan@137 | 83 } |
ivan@137 | 84 } |
ivan@137 | 85 else { |
ivan@137 | 86 i--; |
ivan@137 | 87 } |
ivan@137 | 88 } |
ivan@137 | 89 } |