aboutsummaryrefslogtreecommitdiff
path: root/ninjalist.c
blob: 11f43ded399906360084508dacb1a3d6c3ac9210 (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
/* takes a map like 0 1 3 4 7 and only returns the number if it is present */
/* in the map. */
#include "m_pd.h"
#define MAXENTRIES 1024
#define LASTENTRY 1023

static t_class *ninjalist_class;

typedef struct _map
{
  t_atom map[MAXENTRIES];
  t_atom nomap[MAXENTRIES];
} t_map;

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

void ninjalist_float(t_ninjalist *x, t_floatarg fin)
{
  int arg = 0;
  float test = 0;
  x->input = arg = fin;
  test = atom_getfloatarg(arg, MAXENTRIES, x->x_map.map);
  outlet_bang(x->inst);
  outlet_float(x->value, test);
  outlet_float(x->mapped, arg);
}

void ninjalist_set(t_ninjalist *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 ninjalist_delete(t_ninjalist *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 ninjalist_insert(t_ninjalist *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 ninjalist_get(t_ninjalist *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 ninjalist_clear(t_ninjalist *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 ninjalist_map(t_ninjalist *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 *ninjalist_new(t_floatarg f) 
{
  t_ninjalist *x = (t_ninjalist *)pd_new(ninjalist_class);
  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 ninjalist_setup(void) 
{
  ninjalist_class = class_new(gensym("ninjalist"),
  (t_newmethod)ninjalist_new,
  0, sizeof(t_ninjalist),
  0, A_DEFFLOAT, 0);
  post("|^^^^^^^^^^^ninjalist^^^^^^^^^^^|");
  post("|->^^^integer map to floats^^^<-|");
  post("|^^^^^^^Edward Kelly 2012^^^^^^^|");

  class_addfloat(ninjalist_class, ninjalist_float);
  class_addmethod(ninjalist_class, (t_method)ninjalist_set, gensym("set"), A_DEFFLOAT, A_DEFFLOAT, 0);
  class_addmethod(ninjalist_class, (t_method)ninjalist_map, gensym("map"), A_GIMME, 0);
  class_addmethod(ninjalist_class, (t_method)ninjalist_clear, gensym("clear"), A_DEFFLOAT, 0);
  class_addmethod(ninjalist_class, (t_method)ninjalist_get, gensym("get"), A_DEFFLOAT, 0);
  class_addmethod(ninjalist_class, (t_method)ninjalist_delete, gensym("delete"), A_DEFFLOAT, 0);
  class_addmethod(ninjalist_class, (t_method)ninjalist_insert, gensym("insert"), A_DEFFLOAT, A_DEFFLOAT, 0);
}