1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
/*
Copyright (C) 2002 Antoine Rousseau
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "m_pd.h"
#include "math.h"
/* ---------------- tabsort2 - sort a table to a table ----------------- */
typedef struct tabsort2
{
t_object x_obj; /* header */
t_symbol *x_arrayname1;
t_symbol *x_arrayname2;
t_symbol *x_arrayname3;
} t_tabsort2;
t_class *tabsort2_class;
static void *tabsort2_new(t_symbol *tab1,t_symbol *tab2,t_symbol *tab3)
{
t_tabsort2 *x;
x = (t_tabsort2 *)pd_new(tabsort2_class);
x->x_arrayname1 = tab1;
x->x_arrayname2 = tab2;
x->x_arrayname3 = tab3;
outlet_new((t_object *)x, &s_float);
return (x);
}
static void tabsort2_set1(t_tabsort2 *x, t_symbol *s)
{
x->x_arrayname1 = s;
}
static void tabsort2_set2(t_tabsort2 *x, t_symbol *s)
{
x->x_arrayname2 = s;
}
static void tabsort2_set3(t_tabsort2 *x, t_symbol *s)
{
x->x_arrayname3 = s;
}
static void tabsort2_float(t_tabsort2 *x, t_floatarg n)
{
t_garray *a;
int n1,n2,n3,i,j,h,sqn;
t_word *vec1,*vec2,*vec3;
t_float tmp;
if (!(a = (t_garray *)pd_findbyclass(x->x_arrayname1, garray_class)))
{
if (*x->x_arrayname1->s_name) pd_error(x, "tabsort2: %s: no such array",
x->x_arrayname1->s_name);
return;
}
else if (!garray_getfloatwords(a, &n1, &vec1))
{
error("%s: bad template for tabsort2", x->x_arrayname1->s_name);
return;
}
if (!(a = (t_garray *)pd_findbyclass(x->x_arrayname2, garray_class)))
{
if (*x->x_arrayname2->s_name) pd_error(x, "tabsort2: %s: no such array",
x->x_arrayname2->s_name);
return;
}
else if (!garray_getfloatwords(a, &n2, &vec2))
{
error("%s: bad template for tabsort2", x->x_arrayname2->s_name);
return;
}
if (!(a = (t_garray *)pd_findbyclass(x->x_arrayname3, garray_class)))
{
if (*x->x_arrayname3->s_name) pd_error(x, "tabsort2: %s: no such array",
x->x_arrayname3->s_name);
return;
}
else if (!garray_getfloatwords(a, &n3, &vec3))
{
error("%s: bad template for tabsort2", x->x_arrayname3->s_name);
return;
}
if(n>n1) n=n1;
if(n>n2) n=n2;
if(n>n3) n=n3;
for(i=0; i<n; vec3[i].w_float=i++);
for(i=0; i<n-1; i++)
for(j=n-1; j>i; j--)
if(vec1[(int)vec3[j-1].w_float].w_float<vec1[(int)vec3[j].w_float].w_float)
{
tmp=vec3[j].w_float;
vec3[j].w_float=vec3[j-1].w_float;
vec3[j-1].w_float=tmp;
}
sqn=(int)sqrt(n);
for(h=0; h<sqn; h++)
for(i=0; i<sqn-1; i++)
for(j=sqn-1; j>i; j--)
if(vec2[(int)vec3[h*sqn+j-1].w_float].w_float<vec2[(int)vec3[h*sqn+j].w_float].w_float)
{
tmp=vec3[h*sqn+j].w_float;
vec3[h*sqn+j].w_float=vec3[h*sqn+j-1].w_float;
vec3[h*sqn+j-1].w_float=tmp;
}
garray_redraw(a);
outlet_float(((t_object *)x)->ob_outlet,(t_float)sqn);
}
static void tabsort2_ff(t_tabsort2 *x) /* cleanup on free */
{
}
void tabsort2_setup(void )
{
tabsort2_class = class_new(gensym("tabsort2"), (t_newmethod)tabsort2_new,
(t_method)tabsort2_ff, sizeof(t_tabsort2), 0, A_DEFSYM, A_DEFSYM, A_DEFSYM, 0);
class_addmethod(tabsort2_class, (t_method)tabsort2_set1,
gensym("set1"), A_DEFSYM, 0);
class_addmethod(tabsort2_class, (t_method)tabsort2_set2,
gensym("set2"), A_DEFSYM, 0);
class_addmethod(tabsort2_class, (t_method)tabsort2_set3,
gensym("set3"), A_DEFSYM, 0);
class_addfloat(tabsort2_class, tabsort2_float);
}
|