Max 5 API Reference
00001 /** 00002 @file 00003 minimum - output the minimum of a group of numbers 00004 00005 updated 3/22/09 ajm: new API 00006 00007 @ingroup examples 00008 */ 00009 00010 #include "ext.h" 00011 #include "ext_obex.h" 00012 00013 typedef struct minimum 00014 { 00015 struct object m_ob; 00016 t_atom *m_args; 00017 long m_count; 00018 short m_incount; 00019 short m_outtype; 00020 void *m_out2; 00021 void *m_out; 00022 } t_minimum; 00023 00024 typedef struct _big_atom { // so we have double precision 00025 long a_type; 00026 long w_long; 00027 double w_float; 00028 } t_big_atom; 00029 00030 void *minimum_class; 00031 00032 void minimum_compare(t_big_atom *min, t_atom *newone, long *i, long *minIndex); 00033 void minimum_bang(t_minimum *x); 00034 void minimum_int(t_minimum *x, long n); 00035 void minimum_in1(t_minimum *x, long n); 00036 void minimum_float(t_minimum *x, double f); 00037 void minimum_ft1(t_minimum *x, double f); 00038 void minimum_list(t_minimum *x, t_symbol *s, short ac, t_atom *av); 00039 void minimum_assist(t_minimum *x, void *b, long m, long a, char *s); 00040 void minimum_inletinfo(t_minimum *x, void *b, long a, char *t); 00041 void *minimum_new(t_symbol *s, short ac, t_atom *av); 00042 void minimum_free(t_minimum *x); 00043 void minimum_resize(t_minimum *x, long size); 00044 00045 int main() 00046 { 00047 t_class *c; 00048 00049 c = class_new("minimum", (method)minimum_new, (method)minimum_free, (short)sizeof(t_minimum), 0L, A_GIMME, 0); 00050 class_addmethod(c, (method)minimum_bang, "bang", 0); 00051 class_addmethod(c, (method)minimum_int, "int", A_LONG, 0); 00052 class_addmethod(c, (method)minimum_in1, "in1", A_LONG, 0); 00053 class_addmethod(c, (method)minimum_float, "float", A_FLOAT, 0); 00054 class_addmethod(c, (method)minimum_ft1, "ft1", A_FLOAT, 0); 00055 class_addmethod(c, (method)minimum_list,"list",A_GIMME,0); 00056 class_addmethod(c, (method)minimum_assist,"assist",A_CANT,0); 00057 class_addmethod(c, (method)minimum_inletinfo, "inletinfo", A_CANT, 0); 00058 class_register(CLASS_BOX, c); 00059 minimum_class = c; 00060 00061 return 0; 00062 } 00063 00064 void minimum_compare(t_big_atom *min, t_atom *newone, long *i, long *minIndex) 00065 { 00066 double a, b; 00067 00068 if (atom_gettype(min)==A_NOTHING) { 00069 min->a_type = newone->a_type; 00070 min->w_long = atom_getlong(newone); 00071 min->w_float = atom_getfloat(newone); 00072 return; 00073 } 00074 if (atom_gettype(min)==A_FLOAT) 00075 a = min->w_float; 00076 else 00077 a = (double)min->w_long; 00078 if (atom_gettype(newone) == A_FLOAT) 00079 b = atom_getfloat(newone); 00080 else 00081 b = (double)atom_getlong(newone); 00082 00083 if (a <= b) { 00084 min->a_type = A_FLOAT; 00085 min->w_float = a; 00086 } else { 00087 *minIndex = *i; 00088 min->a_type = A_FLOAT; 00089 min->w_float = b; 00090 } 00091 } 00092 00093 void minimum_bang(t_minimum *x) 00094 { 00095 long i; 00096 t_big_atom themin; 00097 long minIndex; 00098 long res; 00099 double fres; 00100 00101 minIndex = 0; 00102 themin.a_type = A_NOTHING; 00103 for (i=0; i < x->m_count; i++) 00104 minimum_compare(&themin,x->m_args+i, &i, &minIndex); 00105 00106 outlet_int(x->m_out2, minIndex); 00107 if (x->m_outtype==A_LONG) { 00108 if (atom_gettype(&themin)==A_LONG) 00109 res = themin.w_long; 00110 else 00111 res = (long)themin.w_float; 00112 outlet_int(x->m_out,res); 00113 } else { 00114 if (atom_gettype(&themin)==A_FLOAT) 00115 fres = themin.w_float; 00116 else 00117 fres = (float)themin.w_long; 00118 outlet_float(x->m_out,fres); 00119 } 00120 } 00121 00122 void minimum_int(t_minimum *x, long n) 00123 { 00124 atom_setlong(x->m_args,n); 00125 minimum_bang(x); 00126 } 00127 00128 void minimum_in1(t_minimum *x, long n) 00129 { 00130 t_atom tmp; 00131 00132 tmp = x->m_args[0]; 00133 minimum_resize(x,2); 00134 x->m_args[0] = tmp; 00135 atom_setlong(x->m_args+1,n); 00136 x->m_count = 2; 00137 } 00138 00139 00140 void minimum_float(t_minimum *x, double f) 00141 { 00142 atom_setfloat(x->m_args,f); 00143 minimum_bang(x); 00144 } 00145 00146 void minimum_ft1(t_minimum *x, double f) 00147 { 00148 t_atom tmp; 00149 00150 tmp = x->m_args[0]; 00151 minimum_resize(x,2); 00152 x->m_args[0] = tmp; 00153 atom_setfloat(x->m_args+1,f); 00154 x->m_count = 2; 00155 } 00156 00157 void minimum_list(t_minimum *x, t_symbol *s, short ac, t_atom *av) 00158 { 00159 short i; 00160 00161 minimum_resize(x,ac); 00162 for (i=0; i < ac; i++,av++) { 00163 if (atom_gettype(av)==A_LONG) 00164 atom_setlong(x->m_args+i,atom_getlong(av)); 00165 else if (atom_gettype(av)==A_FLOAT) 00166 atom_setfloat(x->m_args+i,atom_getfloat(av)); 00167 } 00168 x->m_count = ac; 00169 minimum_bang(x); 00170 } 00171 00172 void minimum_inletinfo(t_minimum *x, void *b, long a, char *t) 00173 { 00174 if (a) 00175 *t = 1; 00176 } 00177 00178 void minimum_assist(t_minimum *x, void *b, long m, long a, char *s) 00179 { 00180 if (m == ASSIST_OUTLET) 00181 if (a == 0) 00182 sprintf(s,"Minimum Value"); 00183 if (a == 1) 00184 sprintf(s, "Index of the Minimum Value"); 00185 else { 00186 switch (a) { 00187 case 0: 00188 sprintf(s,"Compares Left and Right Inlets"); 00189 break; 00190 case 1: 00191 sprintf(s,"Value to be Compared"); 00192 break; 00193 } 00194 } 00195 } 00196 00197 void *minimum_new(t_symbol *s, short ac, t_atom *av) 00198 { 00199 t_minimum *x; 00200 00201 x = object_alloc(minimum_class); 00202 x->m_count = 0; 00203 x->m_args = NULL; 00204 minimum_resize(x,2); 00205 x->m_out2 = intout(x); 00206 if (ac) { 00207 x->m_args[1] = *av; 00208 if (atom_gettype(av)==A_LONG) { 00209 x->m_args[0].a_type = x->m_outtype = A_LONG; 00210 x->m_out = intout(x); 00211 x->m_args[0].a_w.w_long = 0; 00212 intin(x,1); 00213 } else if (atom_gettype(av)==A_FLOAT) { 00214 x->m_args[0].a_type = x->m_outtype = A_FLOAT; 00215 x->m_out = floatout(x); 00216 x->m_args[0].a_w.w_float = 0; 00217 floatin(x,1); 00218 } else { 00219 x->m_outtype = A_LONG; 00220 intin(x,1); 00221 x->m_out = intout(x); 00222 atom_setlong(x->m_args+1,0L); 00223 atom_setlong(x->m_args,0L); 00224 } 00225 } else { 00226 x->m_outtype = A_LONG; 00227 intin(x,1); 00228 x->m_out = intout(x); 00229 atom_setlong(x->m_args+1,0L); 00230 atom_setlong(x->m_args,0L); 00231 } 00232 return x; 00233 } 00234 00235 void minimum_free(t_minimum *x) 00236 { 00237 if (x->m_args) 00238 sysmem_freeptr(x->m_args); 00239 } 00240 00241 void minimum_resize(t_minimum *x, long size) 00242 { 00243 if (size!=x->m_count) { 00244 if (x->m_args) 00245 sysmem_freeptr(x->m_args); 00246 if (size) 00247 x->m_args = (t_atom *)sysmem_newptr(size*sizeof(t_atom)); 00248 else 00249 x->m_args = NULL; 00250 x->m_count = size; 00251 } 00252 }
Copyright © 2008, Cycling '74