~ubuntu-branches/ubuntu/natty/pd-zexy/natty

« back to all changes in this revision

Viewing changes to src/sort.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard, IOhannes m zmölnig, Jonas Smedegaard
  • Date: 2010-08-20 12:17:41 UTC
  • mfrom: (2.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100820121741-4kxozn8b9rhee9fr
Tags: 2.2.3-1
* New upstream version

[ IOhannes m zmölnig ]
* Adopt package, on behalf of Multimedia Team.
  Closes: #546964
* Simply debian/rules with CDBS, and don't unconditionally strip
  binaries.
  Closes: #437763
* Install into /usr/lib/pd/extra/zexy/. Document usage in REAME.Debian
  and warn about change in NEWS.
* git'ify package. Add Vcs-* stanzas to control file.
* Use dpkg source format 3.0 (quilt). Drop build-dependency on quilt.

[ Jonas Smedegaard ]
* Enable CDBS copyright-check routine.
* Add copyright and licensing header to debian/rules.
* Add myself as uploader.
* Rewrite debian/copyright using rev. 135 of draft DEP5 format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************
 
2
 *
 
3
 * zexy - implementation file
 
4
 *
 
5
 * copyleft (c) IOhannes m zm�lnig
 
6
 *
 
7
 *   1999:forum::f�r::uml�ute:2004
 
8
 *
 
9
 *   institute of electronic music and acoustics (iem)
 
10
 *
 
11
 ******************************************************
 
12
 *
 
13
 * license: GNU General Public License v.2
 
14
 ******************************************************/
 
15
 
 
16
 
 
17
/* 1309:forum::f�r::uml�ute:2000 */
 
18
 
 
19
/*
 
20
  sort :  sort a package of floats
 
21
*/
 
22
 
 
23
#include "zexy.h"
 
24
 
 
25
/* ------------------------- sort ------------------------------- */
 
26
 
 
27
/*
 
28
  SHELL SORT: simple and easy
 
29
*/
 
30
 
 
31
static t_class *sort_class;
 
32
 
 
33
typedef struct _sort
 
34
{
 
35
  t_object x_obj;
 
36
 
 
37
  int bufsize;
 
38
  t_float *buffer;
 
39
  t_int   *indices;
 
40
 
 
41
  int ascending;
 
42
 
 
43
  t_outlet*indexOut, *sortedOut;
 
44
} t_sort;
 
45
 
 
46
 
 
47
static void sort_dir(t_sort *x, t_float f)
 
48
{
 
49
  x->ascending = (f < 0.f)?0:1;
 
50
}
 
51
 
 
52
static void sort_buffer(t_sort *x, int argc, t_atom *argv)
 
53
{
 
54
  int n = argc;
 
55
  t_float *buf;
 
56
  t_atom *atombuf = argv;
 
57
 
 
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));
 
61
 
 
62
    x->bufsize = argc;
 
63
    x->buffer = getbytes(x->bufsize * sizeof(t_float));
 
64
    x->indices = getbytes(x->bufsize * sizeof(t_int));
 
65
  }
 
66
 
 
67
  buf = x->buffer;
 
68
  while (n--){
 
69
    *buf++ = atom_getfloat(atombuf++);
 
70
    x->indices[n] = n;
 
71
  }
 
72
}
 
73
 
 
74
static void sort_list(t_sort *x, t_symbol *s, int argc, t_atom *argv)
 
75
{
 
76
  int step = argc, n;
 
77
  t_atom *atombuf = (t_atom *)getbytes(sizeof(t_atom) * argc);
 
78
  t_float *buf;
 
79
  t_int   *idx;
 
80
 
 
81
  int i, loops = 1;
 
82
 
 
83
  sort_buffer(x, argc, argv);
 
84
  buf = x->buffer;
 
85
  idx = x->indices;
 
86
 
 
87
  while (step > 1) {
 
88
    step = (step % 2)?(step+1)/2:step/2;
 
89
 
 
90
    i = loops;
 
91
    loops += 2;
 
92
 
 
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]) {
 
96
          t_int   i_tmp = idx[n];
 
97
          t_float f_tmp = buf[n];
 
98
          buf[n]        = buf[n+step];
 
99
          buf[n+step]   = f_tmp;
 
100
          idx[n]        = idx[n+step];
 
101
          idx[n+step]   = i_tmp;
 
102
        }
 
103
      }
 
104
    }
 
105
  }
 
106
 
 
107
  if (x->ascending) 
 
108
    for (n = 0; n < argc; n++) SETFLOAT(&atombuf[n], idx[n]);
 
109
  else
 
110
    for (n = 0, i=argc-1; n < argc; n++, i--) SETFLOAT(&atombuf[n], idx[i]);
 
111
 
 
112
  outlet_list(x->indexOut , &s_list, n, atombuf);
 
113
 
 
114
  if (x->ascending) 
 
115
    for (n = 0; n < argc; n++) SETFLOAT(&atombuf[n], buf[n]);
 
116
  else
 
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);
 
119
 
 
120
 
 
121
  freebytes(atombuf, argc*sizeof(t_atom));
 
122
}
 
123
 
 
124
static void *sort_new(t_floatarg f)
 
125
{
 
126
  t_sort *x = (t_sort *)pd_new(sort_class);
 
127
  x->ascending = (f < 0.f)?0:1;
 
128
 
 
129
  x->sortedOut=outlet_new(&x->x_obj, &s_list);
 
130
  x->indexOut=outlet_new(&x->x_obj, &s_list);
 
131
 
 
132
  x->bufsize = 0;
 
133
  x->buffer = NULL;
 
134
 
 
135
  inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("direction"));
 
136
 
 
137
  return (x);
 
138
}
 
139
 
 
140
static void sort_help(t_sort*x)
 
141
{
 
142
  post("\n%c sort\t\t:: sort a list of numbers", HEARTSYMBOL);
 
143
}
 
144
void sort_setup(void)
 
145
{
 
146
  sort_class = class_new(gensym("sort"), (t_newmethod)sort_new, 
 
147
                         0, sizeof(t_sort), 0, A_DEFFLOAT,  0);
 
148
  
 
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);
 
152
 
 
153
  zexy_register("sort");
 
154
}