Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
1 total  XML / RSS feed 

C macro that implements count_if, sum, max_elem

// C macro that does count_if, sum, max_elem.

#define COUNT_IF(ITERATION, CONDITION, COUNT)  {COUNT =0; ITERATION {if(CONDITION) COUNT++;}}  
/* ex: COUNT_IF( for(i=0;i++;i<10), i%2==0, evens)  [counting the number of evens.]  */

#define SUM(ITERATION, EXPRESSION, SUMM)  {SUMM =0; ITERATION {SUMM +=(EXPRESSION);}}  
/* if ITERATION is an empty iteration, the sum is 0
SUM( for(i=0;i<60;i++) if(zik[i]==sol[i]) , score[i], actual_score ) */


#define MAX_ELEM(ITERATION, EXPRESSION, MAX_VALUE, POSITION)  { \
        bool first233496 = true;                                   \
        ITERATION {                                               \
                if(first233496){ first233496 = false; POSITION ; MAX_VALUE =(EXPRESSION); } \
                else if((EXPRESSION)> MAX_VALUE ){ POSITION ; MAX_VALUE =(EXPRESSION); }   \
        }                                               \
}
/* if ITERATION is an empty iteration, this macro does not work.
ex: MAX_ELEM(for(x=0;x<5;x++) for(y=0;y<5;y++) , x-2*y , maximum , {m_x=x;m_y=y;} );  [ Set (m_x,m_y,maximum) := (x,y,x-2*y) when x-2*y achieves maximum value] 
ex: MAX_ELEM( ON(i,10), (10-v[i])*v[i], d, k=i); */
« Newer Snippets
Older Snippets »
1 total  XML / RSS feed