1
/******************************************************
3
* zexy - implementation file
5
* copyleft (c) IOhannes m zm�lnig
7
* 1999:forum::f�r::uml�ute:2004
9
* institute of electronic music and acoustics (iem)
11
******************************************************
13
* license: GNU General Public License v.2
14
******************************************************/
17
/* 1309:forum::f�r::uml�ute:2000 */
20
sort : sort a package of floats
25
/* ------------------------- sort ------------------------------- */
28
SHELL SORT: simple and easy
31
static t_class *sort_class;
43
t_outlet*indexOut, *sortedOut;
47
static void sort_dir(t_sort *x, t_float f)
49
x->ascending = (f < 0.f)?0:1;
52
static void sort_buffer(t_sort *x, int argc, t_atom *argv)
56
t_atom *atombuf = argv;
58
if (argc != x->bufsize) {
59
if (x->buffer) freebytes(x->buffer, x->bufsize * sizeof(t_float));
60
if (x->indices)freebytes(x->indices, x->bufsize * sizeof(t_int));
63
x->buffer = getbytes(x->bufsize * sizeof(t_float));
64
x->indices = getbytes(x->bufsize * sizeof(t_int));
69
*buf++ = atom_getfloat(atombuf++);
74
static void sort_list(t_sort *x, t_symbol *s, int argc, t_atom *argv)
77
t_atom *atombuf = (t_atom *)getbytes(sizeof(t_atom) * argc);
83
sort_buffer(x, argc, argv);
88
step = (step % 2)?(step+1)/2:step/2;
93
while(i--) { /* there might be some optimization in here */
94
for (n=0; n<(argc-step); n++) {
95
if (buf[n] > buf[n+step]) {
97
t_float f_tmp = buf[n];
100
idx[n] = idx[n+step];
108
for (n = 0; n < argc; n++) SETFLOAT(&atombuf[n], idx[n]);
110
for (n = 0, i=argc-1; n < argc; n++, i--) SETFLOAT(&atombuf[n], idx[i]);
112
outlet_list(x->indexOut , &s_list, n, atombuf);
115
for (n = 0; n < argc; n++) SETFLOAT(&atombuf[n], buf[n]);
117
for (n = 0, i=argc-1; n < argc; n++, i--) SETFLOAT(&atombuf[n], buf[i]);
118
outlet_list(x->sortedOut, &s_list, n, atombuf);
121
freebytes(atombuf, argc*sizeof(t_atom));
124
static void *sort_new(t_floatarg f)
126
t_sort *x = (t_sort *)pd_new(sort_class);
127
x->ascending = (f < 0.f)?0:1;
129
x->sortedOut=outlet_new(&x->x_obj, &s_list);
130
x->indexOut=outlet_new(&x->x_obj, &s_list);
135
inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("direction"));
140
static void sort_help(t_sort*x)
142
post("\n%c sort\t\t:: sort a list of numbers", HEARTSYMBOL);
144
void sort_setup(void)
146
sort_class = class_new(gensym("sort"), (t_newmethod)sort_new,
147
0, sizeof(t_sort), 0, A_DEFFLOAT, 0);
149
class_addlist (sort_class, sort_list);
150
class_addmethod (sort_class, (t_method)sort_dir, gensym("direction"), A_DEFFLOAT, 0);
151
class_addmethod(sort_class, (t_method)sort_help, gensym("help"), A_NULL);
153
zexy_register("sort");