aboutsummaryrefslogtreecommitdiff
path: root/src/mtx_find.c
blob: 8ff27964dd7a234190d707869a3d457a4ef6dd55 (plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 *  iemmatrix
 *
 *  objects for manipulating simple matrices
 *  mostly refering to matlab/octave matrix functions
 *
 * Copyright (c) 2005, Franz Zotter
 * IEM, Graz, Austria
 *
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.
 *
 */


#include "iemmatrix.h"

static t_class *mtx_find_class;
static t_symbol *row_sym;
static t_symbol *col_sym;
static t_symbol *col_sym2;
static t_symbol *mtx_sym;
static t_symbol *mtx_sym2;

typedef struct _MTXfind_ MTXfind;
struct _MTXfind_
{
   t_object x_obj;
   int size;
   int outsize;
   //int find_dimension;
   t_symbol *find_mode;
   int find_direction;

   t_outlet *list_outlet;

   t_atom *list_out;
   t_atom *list_in;
};

static void deleteMTXFind (MTXfind *mtx_find_obj) 
{
   if (mtx_find_obj->list_out)
      freebytes (mtx_find_obj->list_out, sizeof(t_atom)*(mtx_find_obj->size+2));
}

static void mTXSetFindDirection (MTXfind *mtx_find_obj, t_float c_dir)
{
   int direction = (int) c_dir;
   if ((direction != -1) && (direction != 1))
      direction = 1;
   mtx_find_obj->find_direction = direction;
}
/*
static void mTXSetFindDimension (MTXfind *mtx_find_obj, t_float c_dim)
{
   int dimension = (int) c_dim;
   dimension = (dimension > 0)?dimension:0;
   dimension = (dimension < 3)?dimension:3;
   mtx_find_obj->find_dimension = dimension;
}
*/
static void mTXSetFindMode (MTXfind *mtx_find_obj, t_symbol *c_dim)
{
   mtx_find_obj->find_mode = c_dim;
}

static void *newMTXFind (t_symbol *s, int argc, t_atom *argv)
{
   MTXfind *mtx_find_obj = (MTXfind *) pd_new (mtx_find_class);
//   int c_dim = 0;
//   int c_dir = 1;

   mTXSetFindMode (mtx_find_obj, gensym(":"));
   mTXSetFindDirection (mtx_find_obj, 1);
   if (argc>=1) {
      if (argv[0].a_type == A_SYMBOL) {
	 mTXSetFindMode (mtx_find_obj, atom_getsymbol (argv));
	 if (argc>=2) {
	    if (argv[1].a_type != A_SYMBOL)
	       mTXSetFindDirection (mtx_find_obj, atom_getfloat (argv+1));
	    else
	       post("mtx_find: 2nd arg ignored. supposed to be float");
	 }
      }
      else {
	 mTXSetFindDirection (mtx_find_obj, atom_getfloat (argv));
	 if (argc>=2) {
	    if (argv[1].a_type == A_SYMBOL)
	       mTXSetFindMode (mtx_find_obj, atom_getsymbol (argv+1));
	    else
	       post("mtx_find: 2nd arg ignored. supposed to be symbolic, e.g. \"row\", \"col\", \":\", \"mtx\"");
	 }
      }
   }
/*
   switch ((argc>2)?2:argc) {
      case 2:
	 c_dir = atom_getint(argv+1);
      case 1:
	 c_dim = atom_getint(argv);
   }
   mTXSetFindDimension (mtx_find_obj, (t_float) c_dim);
   mTXSetFindDirection (mtx_find_obj, (t_float) c_dir);
   */

   mtx_find_obj->list_outlet = outlet_new (&mtx_find_obj->x_obj, gensym("matrix"));

   error("[mtx_find]: this object is likely to change! not really for use yet");
   return ((void *) mtx_find_obj);
} 

static void mTXFindBang (MTXfind *mtx_find_obj)
{
   if (mtx_find_obj->list_out) 
      outlet_anything(mtx_find_obj->list_outlet, gensym("matrix"), 
	    mtx_find_obj->outsize+2, mtx_find_obj->list_out);
}
/*
static void copyList (int size, t_atom *x, t_atom *y)
{
   while(size--)
 *y++=*x++;
 }
 */
static int findPreviousNonZero (const int n, t_atom *x, int offset)
{
   x+=offset;
   for (; offset > n; offset--, x--) 
      if (atom_getfloat(x))
	 return offset;
   return -1;
}
static int findPreviousNonZeroStep (const int step, t_atom *x, int offset)
{
   x += offset;
   for (; offset > 0; offset-=step, x-=step) 
      if (atom_getfloat(x))
	 return offset;
   return -1;
}
static int findNextNonZero (const int n, t_atom *x, int offset)
{
   x+=offset;
   for (; offset < n; offset++, x++) 
      if (atom_getfloat(x))
	 return offset;
   return -1;
}
static int findNextNonZeroStep (const int n, const int step, t_atom *x, int offset)
{
   x += offset;
   for (; offset < n; offset+=step, x+=step) 
      if (atom_getfloat(x))
	 return offset;
   return -1;
}

static void findFirstNonZeroRow (const int rows, const int columns, t_atom *x, t_atom *y)
{
   int offset;
   int pos;
   const int size = rows*columns;
   for (offset = 0; offset < size; y++, offset+=columns) {
      pos = findNextNonZero(offset+columns,x,offset)+1;
      SETFLOAT(y,(t_float)pos);
   }
}
static void findLastNonZeroRow (const int rows, const int columns, t_atom *x, t_atom *y)
{
   int offset;
   int pos;
   const int size = rows*columns;
   for (offset = columns-1; offset < size; y++, offset+=columns) {
      pos = findPreviousNonZero(offset-columns,x,offset)+1;
      SETFLOAT(y,(t_float)pos);
   }
}
static void findFirstNonZeroColumn (const int rows, const int columns, t_atom *x, t_atom *y)
{
   int offset;
   int pos;
   const int size = rows*columns;
   for (offset = 0; offset < columns; y++, offset++) {
      pos = findNextNonZeroStep(size,columns,x,offset)+1;
      SETFLOAT(y,(t_float)pos);
   }
}
static void findLastNonZeroColumn (const int rows, const int columns, t_atom *x, t_atom *y)
{
   int offset;
   int pos;
   const int size = rows*columns;
   for (offset = size-columns; offset < size; y++, offset++) {
      pos = findPreviousNonZeroStep(columns,x,offset)+1;
      SETFLOAT(y,(t_float)pos);
   }
}

static int findAllNonZeros (int n, t_atom *x, t_atom *y)
{
   int outsize = 0;
   int pos = 0;
   while ((pos = findNextNonZero(n,x,pos)) != -1) {
      pos++;
      SETFLOAT(y,(t_float)pos);
      y++;
      outsize++;
   }
   return outsize;
}

static void zeroFloatList (int n, t_atom *x)
{
   for (;n--;x++)
      SETFLOAT(x,0);
}
static void findReplaceNonZerosWithIndex (int n, t_atom *x, t_atom *y)
{
   int pos = 0;
   zeroFloatList(n,y);
   while ((pos = findNextNonZero(n,x,pos)) != -1) {
      SETFLOAT(y+pos,(t_float)pos+1);
      pos++;
   }
}

static void mTXFindMatrix (MTXfind *mtx_find_obj, t_symbol *s, 
      int argc, t_atom *argv)
{
   int rows = atom_getint (argv++);
   int columns = atom_getint (argv++);
   int size = rows * columns;
   int list_size = argc - 2;
   t_atom *list_in = argv;
   t_atom *list_out = mtx_find_obj->list_out;
   int rows_out;
   int columns_out;

   // size check
   if (!size) {
      post("mtx_find: invalid dimensions");
      return;
   }
   else if (list_size<size) {
      post("mtx_find: sparse matrix not yet supported: use \"mtx_check\"");
      return;
   }
   
   if (size != mtx_find_obj->size) {
      if (!list_out)
	 list_out = (t_atom *) getbytes (sizeof (t_atom) * (size + 2));
      else
	 list_out = (t_atom *) resizebytes (list_out,
	       sizeof (t_atom) * (mtx_find_obj->size+2),
	       sizeof (t_atom) * (size + 2));
   }

   mtx_find_obj->size = size;
   mtx_find_obj->list_out = list_out;

   // main part
   list_out += 2;
   //copyList (size, argv, list_out);
   rows_out = 1;
   if (mtx_find_obj->find_mode == row_sym) {
      if (mtx_find_obj->find_direction == -1)
	 findLastNonZeroRow (rows, columns, list_in, list_out);
      else
	 findFirstNonZeroRow (rows, columns, list_in, list_out);
      rows_out = rows;
      columns_out = 1;
   }
   else if ((mtx_find_obj->find_mode == col_sym)||
	 (mtx_find_obj->find_mode == col_sym2)) {
      if (mtx_find_obj->find_direction == -1)
	 findLastNonZeroColumn (rows, columns, list_in, list_out);
      else
	 findFirstNonZeroColumn (rows, columns, list_in, list_out);
      columns_out = columns;
      rows_out = 1;
   }
   else if ((mtx_find_obj->find_mode == mtx_sym)||
	 (mtx_find_obj->find_mode == mtx_sym2)) {
      findReplaceNonZerosWithIndex (size, list_in, list_out);
      rows_out = rows;
      columns_out = columns;
   }
   else {
      columns_out = findAllNonZeros (size, list_in, list_out); 
      rows_out = 1;
   }
   /*
   switch (mtx_find_obj->find_dimension) {
      case 0:
	 columns_out = findAllNonZeros (size, list_in, list_out); 
	 rows_out = 1;
	 break;
      case 3:
	 findReplaceNonZerosWithIndex (size, list_in, list_out);
	 rows_out = rows;
	 columns_out = columns;
	 break;
      case 2:
	 if (mtx_find_obj->find_direction == -1)
	    findLastNonZeroColumn (rows, columns, list_in, list_out);
	 else
	    findFirstNonZeroColumn (rows, columns, list_in, list_out);
	 columns_out = columns;
	 rows_out = 1;
	 break;
      case 1:
	 if (mtx_find_obj->find_direction == -1)
	    findLastNonZeroRow (rows, columns, list_in, list_out);
	 else
	    findFirstNonZeroRow (rows, columns, list_in, list_out);
	 rows_out = rows;
	 columns_out = 1;
	 break;
   }
   */
   mtx_find_obj->outsize = columns_out * rows_out;
   list_out = mtx_find_obj->list_out;

   SETSYMBOL(list_out, gensym("matrix"));
   SETFLOAT(list_out, rows_out);
   SETFLOAT(&list_out[1], columns_out);
   outlet_anything(mtx_find_obj->list_outlet, gensym("matrix"), 
	 mtx_find_obj->outsize+2, list_out);
}

void mtx_find_setup (void)
{
   mtx_find_class = class_new 
      (gensym("mtx_find"),
       (t_newmethod) newMTXFind,
       (t_method) deleteMTXFind,
       sizeof (MTXfind),
       CLASS_DEFAULT, A_GIMME, 0);
   class_addbang (mtx_find_class, (t_method) mTXFindBang);
   class_addmethod (mtx_find_class, (t_method) mTXFindMatrix, gensym("matrix"), A_GIMME,0);
//   class_addmethod (mtx_find_class, (t_method) mTXSetFindDimension, gensym("dimension"), A_DEFFLOAT,0);
   class_addmethod (mtx_find_class, (t_method) mTXSetFindMode, gensym("mode"), A_DEFSYMBOL,0);
   class_addmethod (mtx_find_class, (t_method) mTXSetFindDirection, gensym("direction"), A_DEFFLOAT,0);

   row_sym = gensym("row");
   col_sym = gensym("col");
   col_sym2 = gensym("columns");
   mtx_sym = gensym("mtx");
   mtx_sym2 = gensym ("matrix");
}

void iemtx_find_setup(void){
  mtx_find_setup();
}