aboutsummaryrefslogtreecommitdiff
path: root/sieve/sieve.c
blob: 32388448a778879ef2f33d2b8ce7561be9b21d69 (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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* takes a map like 0 1 3 4 7 and only returns the number if it is present */
/* in the map, or returns the closest, or the next up or down (wrapped)*/
#include "m_pd.h"
#include <math.h>
#include <string.h>
#define MAXENTRIES 2048
#define LASTENTRY 2047

static t_class *sieve_class;

/* mode = 0 : block when absent, 1: nearest when absent, 2: shunt when absent */
typedef struct _map
{
  t_atom map[MAXENTRIES];
  t_atom nomap[MAXENTRIES];
} t_map;

typedef struct _sieve
{
  t_object x_obj;
  t_map x_map;
  t_float input, mode, max, outmap;
  t_outlet *mapped, *value, *mapout, *inst;
} t_sieve;

void sieve_float(t_sieve *x, t_floatarg fin)
{
  int i, ip, in, arg, arga, argb, argaout, argbout, argxa, argxb, itest, itesta, itestb, iresult;
  itest = itesta = itestb = iresult = arga = argb = arg = 0;
  float test, testa, testb, fresult;
  test = testa = testb = fresult = 0;
  x->input = arg = fin;
  if (x->mode == 0) /* only let through floats when the corresponding 
                       index contains != 0 */
    {
      test = fin < 0 ? 0 : atom_getfloatarg(arg, MAXENTRIES, x->x_map.map);
      if(test!=0) 
	{
	  outlet_bang(x->inst);
	  outlet_float(x->value, test);
	  outlet_float(x->mapped, arg);
	}
    }
  else if (x->mode == 1) /* find the nearest float whose (int) index is
                            != 0 */
    {
      test =  fin < 0 ? 0 : atom_getfloatarg(arg, MAXENTRIES, x->x_map.map);
      if(test!=0)
	{
	  outlet_bang(x->inst);
	  outlet_float(x->value, test);
	  outlet_float(x->mapped, arg);
	}
      else
	{
	  arga = argb = arg;
	  while(itest == 0 && (arga > -1 || argb < MAXENTRIES))
	    {
	      arga--;
	      argb++;
	      argxa = arga >= 0 ? arga : 0;
	      argxb = argb <= LASTENTRY ? argb : LASTENTRY;
	      testa = atom_getfloatarg(argxa, MAXENTRIES, x->x_map.map);
	      testb = atom_getfloatarg(argxb, MAXENTRIES, x->x_map.map);
	      itesta = testa != 0 ? 1 : 0;
	      itestb = testb != 0 ? 1 : 0;
	      itest =  fin < 0 ? 0 : itesta + itestb;
	    }
	  switch(itest)
	    {
	    case 2: /* if we find two at equal distance, output the higher */
	      if (x->mode == 1)
		{
		  outlet_float(x->value, testb);
		  outlet_float(x->mapped, argb);
		}
	      else
		{
		  outlet_float(x->value, testa);
		  outlet_float(x->mapped, arga);
		}
	    case 1:
	      iresult = itesta == 1 ? arga : argb;
	      fresult = itesta == 1 ? testa : testb;
	      outlet_float(x->value, fresult);
	      outlet_float(x->mapped, iresult);
	    case 0:
	      break;
	    }
	}
    }
  else if (x->mode==2) /* if the index is 0, find the next highest */
    {
      itest = 0;
      test =  fin < 0 ? 0 : atom_getfloatarg(arg, MAXENTRIES, x->x_map.map);
      if(test!=0)
	{
	  outlet_bang(x->inst);
	  outlet_float(x->value, test);
	  outlet_float(x->mapped, arg);
	}
      else
	{
	  arga =  arg;
	  while(itest == 0 && (x->max > 0))
	    {
	      arga = (arga + 1) <= LASTENTRY ? (arga + 1) : 0;
	      testa = atom_getfloatarg(arga, MAXENTRIES, x->x_map.map);
	      itest = testa != 0 ? 1 : 0;
	    }
	  if(x->max > 0 && fin >= 0)
	    {
	      outlet_float(x->value, testa);
	      outlet_float(x->mapped, arga);
	    }
	}
    }
  else if (x->mode == 3) /* if the index is 0, find the next lowest */
    {
      itest = 0;
      test =  fin < 0 ? 0 : atom_getfloatarg(arg, MAXENTRIES, x->x_map.map);
      if(test!=0)
	{
	  outlet_bang(x->inst);
	  outlet_float(x->value, test);
	  outlet_float(x->mapped, arg);
	}
      else
	{
	  arga =  arg;
	  while(itest == 0 && (x->max > 0))
	    {
	      argb = arga - 1;
	      arga = argb >= 0 ? argb : LASTENTRY;
	      testa = atom_getfloatarg(arga, MAXENTRIES, x->x_map.map);
	      itest = testa != 0 ? 1 : 0;
	    }
	}
      outlet_float(x->value, testa);
      outlet_float(x->mapped, arga);
    }
}

void sieve_set(t_sieve *x, t_floatarg fmap, t_floatarg fval) /* set one value 
                                                                in the array */
{
  float fvaller;
  if(fmap < MAXENTRIES && fmap >= 0)
    {
      int imap = (int)fmap;
      fvaller = fval != 0 ? 0 : 1;
      SETFLOAT(&x->x_map.map[imap], fval);
      SETFLOAT(&x->x_map.nomap[imap], fvaller);
      x->max = fmap > x->max ? fmap : x->max;
    }
}

void sieve_delete(t_sieve *x, t_floatarg loc) /* remove a value */
{
  int addloc = (int)loc + 1;
  int maxentry = (int)x->max;
  int i;
  float buffer;
  if(loc<x->max && loc>=0)
    {
      for(i=addloc;i<=maxentry;i++)
	{
	  buffer = atom_getfloatarg(i,MAXENTRIES,x->x_map.map);
	  SETFLOAT(&x->x_map.map[i-1],buffer);
	  if(buffer!=0)
	    {
	      SETFLOAT(&x->x_map.nomap[i-1],0);
	    }
	  else
	    {
	      SETFLOAT(&x->x_map.nomap[i-1],1);
	    }
	}
      SETFLOAT(&x->x_map.map[maxentry],0);
      x->max--;
    }
  else if(loc==x->max)
    {
      x->max--;
      SETFLOAT(&x->x_map.map[maxentry],0);
    }
}

void sieve_shunt(t_sieve *x, t_floatarg loc) /* move down 
                                                and decrement subsequent */
{
  int addloc = (int)loc + 1;
  int maxentry = (int)x->max;
  int i;
  float buffer, shunt;
  if(loc<x->max && loc>=0)
    {
      for(i=addloc;i<=maxentry;i++)
	{
	  buffer = atom_getfloatarg(i,MAXENTRIES,x->x_map.map);
	  shunt = buffer - 1;
	  SETFLOAT(&x->x_map.map[i-1],shunt);
	  if(shunt!=0)
	    {
	      SETFLOAT(&x->x_map.nomap[i-1],0);
	    }
	  else
	    {
	      SETFLOAT(&x->x_map.nomap[i-1],1);
	    }
	}
      SETFLOAT(&x->x_map.map[maxentry],0);
      x->max--;
    }
  else if(loc==x->max)
    {
      x->max--;
      SETFLOAT(&x->x_map.map[maxentry],0);
    }
}

void sieve_shift(t_sieve *x, t_floatarg loc) /* move up and
                                                increment subsequent */
{
  int location = (int)loc;
  int addloc;
  int maxentry = (int)x->max+1;
  int i;
  float buffer, shift;
  if(location>=0 && maxentry < MAXENTRIES)
    {
      for(i=maxentry;i>=location;i--)
	{
	  buffer = atom_getfloatarg(i-1,MAXENTRIES,x->x_map.map);
	  shift = buffer + 1;
	  SETFLOAT(&x->x_map.map[i],shift);
	  if(shift!=0)
	    {
	      SETFLOAT(&x->x_map.nomap[i],0);
	    }
	  else
	    {
	      SETFLOAT(&x->x_map.nomap[i],1);
	    }
	}
      x->max++;
    }
}

void sieve_insert(t_sieve *x, t_floatarg loc, t_floatarg val)
/* insert a value at specific location, moving subsequent values up */
{
  int location = (int)loc;
  int maxentry = (int)x->max+1;
  int i;
  float buffer;
  if(loc>=0 && maxentry < MAXENTRIES)
    {
      for(i=maxentry;i>=location;i--)
	{
	  buffer = atom_getfloatarg(i-1,MAXENTRIES,x->x_map.map);
	  SETFLOAT(&x->x_map.map[i],buffer);
	  if(buffer!=0)
	    {
	      SETFLOAT(&x->x_map.nomap[i],0);
	    }
	  else
	    {
       	      SETFLOAT(&x->x_map.nomap[i],1);
	    }
	}
      x->max++;
      SETFLOAT(&x->x_map.map[location], val);
      if(val) 
	{
	  SETFLOAT(&x->x_map.nomap[location],0);
	}
      else
	{
	  SETFLOAT(&x->x_map.nomap[location],1);
	}
    }
}

void sieve_get(t_sieve *x, t_floatarg inv) /* outlet to map or inverse */
{
  if(inv!=0) 
    {
      outlet_list(x->mapout, gensym("list"), x->max+1, x->x_map.nomap);
    }
  else outlet_list(x->mapout, gensym("list"), x->max+1, x->x_map.map);
  x->outmap = inv;
}

void sieve_clear(t_sieve *x)
{
  int i;
  for(i=0;i<MAXENTRIES;i++) 
    {
      SETFLOAT(&x->x_map.map[i], 0);
      SETFLOAT(&x->x_map.nomap[i], 1);
    }
  x->max = 0;
}

void sieve_map(t_sieve *x, t_symbol *s, int argc, t_atom *argv) /* set the whole map */
{
  int i;
  for(i=0;i<MAXENTRIES;i++) 
    {
      SETFLOAT(x->x_map.map+i, 0);
      SETFLOAT(x->x_map.nomap+i, 1);
    }
  x->max = 0;
  float arg;
  for(i=0;i<argc;i++) 
    {
      arg = atom_getfloat(argv+i);
      if(arg != 0)
	{
	  SETFLOAT(&x->x_map.map[i], arg);
	  SETFLOAT(&x->x_map.nomap[i], 0);
	  x->max = i;
	}
    }
  if (x->max > 0 && x->outmap == 0)
    {
      outlet_list(x->mapout, gensym("list"), x->max+1, x->x_map.map);
    }
  else if (x->max > 0 && x->outmap == 1)
    {
      outlet_list(x->mapout, gensym("list"), x->max+1, x->x_map.nomap);
    }
}

void sieve_mode(t_sieve *x, t_floatarg fmode)
{
  x->mode = fmode < 0 ? 0 : fmode > 3 ? 3 : fmode;
}

void sieve_debug(t_sieve *x)
{
  float ele0, ele1, ele2, ele3, ele4, ele5, ele6, ele7, ele8, ele9;
  float nle0, nle1, nle2, nle3, nle4, nle5, nle6, nle7, nle8, nle9;
  ele0 = atom_getfloatarg(0, MAXENTRIES, x->x_map.map);
  ele1 = atom_getfloatarg(1, MAXENTRIES, x->x_map.map);
  ele2 = atom_getfloatarg(2, MAXENTRIES, x->x_map.map);
  ele3 = atom_getfloatarg(3, MAXENTRIES, x->x_map.map);
  ele4 = atom_getfloatarg(4, MAXENTRIES, x->x_map.map);
  ele5 = atom_getfloatarg(5, MAXENTRIES, x->x_map.map);
  ele6 = atom_getfloatarg(6, MAXENTRIES, x->x_map.map);
  ele7 = atom_getfloatarg(7, MAXENTRIES, x->x_map.map);
  ele8 = atom_getfloatarg(8, MAXENTRIES, x->x_map.map);
  ele9 = atom_getfloatarg(9, MAXENTRIES, x->x_map.map);
  nle0 = atom_getfloatarg(0, MAXENTRIES, x->x_map.nomap);
  nle1 = atom_getfloatarg(1, MAXENTRIES, x->x_map.nomap);
  nle2 = atom_getfloatarg(2, MAXENTRIES, x->x_map.nomap);
  nle3 = atom_getfloatarg(3, MAXENTRIES, x->x_map.nomap);
  nle4 = atom_getfloatarg(4, MAXENTRIES, x->x_map.nomap);
  nle5 = atom_getfloatarg(5, MAXENTRIES, x->x_map.nomap);
  nle6 = atom_getfloatarg(6, MAXENTRIES, x->x_map.nomap);
  nle7 = atom_getfloatarg(7, MAXENTRIES, x->x_map.nomap);
  nle8 = atom_getfloatarg(8, MAXENTRIES, x->x_map.nomap);
  nle9 = atom_getfloatarg(9, MAXENTRIES, x->x_map.nomap);
  post("mode = %f, max = %f", x->mode, x->max);
  post("first 10 elements = %f, %f, %f, %f, %f, %f, %f, %f, %f, %f", ele0, ele1, ele2, ele3, ele4, ele5, ele6, ele7, ele8, ele9);
  post("first 10 elements = %f, %f, %f, %f, %f, %f, %f, %f, %f, %f", nle0, nle1, nle2, nle3, nle4, nle5, nle6, nle7, nle8, nle9);
}  

void *sieve_new(t_floatarg f) 
{
  t_sieve *x = (t_sieve *)pd_new(sieve_class);
  x->mode = f;
  x->max = 0;
  x->outmap = 0;
  int i;
  for(i=0;i<MAXENTRIES;i++) 
    {
      SETFLOAT(x->x_map.map+i, 0);
      SETFLOAT(x->x_map.nomap+i, 1);
    }
  x->mapped = outlet_new(&x->x_obj, &s_float);
  x->value = outlet_new(&x->x_obj, &s_float);
  x->mapout = outlet_new(&x->x_obj, &s_list);
  x->inst = outlet_new(&x->x_obj, &s_bang);
  return (void *)x;
}

void sieve_setup(void) 
{
  sieve_class = class_new(gensym("sieve"),
  (t_newmethod)sieve_new,
  0, sizeof(t_sieve),
  0, A_DEFFLOAT, 0);
  post("|^^^^^^^^^^^^^sieve^^^^^^^^^^^^^|");
  post("|->^^^integer map to floats^^^<-|");
  post("|^^^^^^^Edward Kelly 2006^^^^^^^|");

  class_addfloat(sieve_class, sieve_float);
  class_addmethod(sieve_class, (t_method)sieve_set, gensym("set"), A_DEFFLOAT, A_DEFFLOAT, 0);
  class_addmethod(sieve_class, (t_method)sieve_map, gensym("map"), A_GIMME, 0);
  class_addmethod(sieve_class, (t_method)sieve_clear, gensym("clear"), A_DEFFLOAT, 0);
  class_addmethod(sieve_class, (t_method)sieve_get, gensym("get"), A_DEFFLOAT, 0);
  class_addmethod(sieve_class, (t_method)sieve_delete, gensym("delete"), A_DEFFLOAT, 0);
  class_addmethod(sieve_class, (t_method)sieve_shunt, gensym("shunt"), A_DEFFLOAT, 0);
  class_addmethod(sieve_class, (t_method)sieve_shift, gensym("shift"), A_DEFFLOAT, 0);
  class_addmethod(sieve_class, (t_method)sieve_insert, gensym("insert"), A_DEFFLOAT, A_DEFFLOAT, 0);
  class_addmethod(sieve_class, (t_method)sieve_mode, gensym("mode"), A_DEFFLOAT, 0);
  class_addmethod(sieve_class, (t_method)sieve_debug, gensym("debug"), A_DEFFLOAT, 0);
}