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
|
/******************************************************
*
* zexy - implementation file
*
* copyleft (c) IOhannes m zmölnig
*
* 1999:forum::für::umläute:2004
*
* institute of electronic music and acoustics (iem)
*
******************************************************
*
* license: GNU General Public License v.2
*
******************************************************/
/* 2305:forum::für::umläute:2001 */
#include "zexy.h"
#include <string.h>
/* ------------------------- lifop ------------------------------- */
/*
* a LIFO (last-in first-out) with priorities
*
* an incoming list is added to a lifo (based on its priority)
* "bang" outputs the last element of the non-empty lifo with the highest priority
*
* high priority means low numeric value
*/
static t_class *lifop_class;
typedef struct _lifop_list {
int argc;
t_atom *argv;
struct _lifop_list *next;
} t_lifop_list;
typedef struct _lifop_prioritylist {
t_float priority;
t_lifop_list *lifo_start;
struct _lifop_prioritylist *next;
} t_lifop_prioritylist;
typedef struct _lifop
{
t_object x_obj;
t_lifop_prioritylist *lifo_list;
unsigned long counter;
t_float priority; /* current priority */
t_outlet *x_out, *x_infout;
} t_lifop;
static t_lifop_prioritylist*lifop_genprioritylist(t_lifop*x, t_float priority)
{
t_lifop_prioritylist*result=0, *dummy=0;
if(x->lifo_list!=0)
{
/*
* do we already have this priority ?
* if so, just return a pointer to that lifo
* else set the dummy-pointer to the lifo BEFORE the new one
*/
dummy=x->lifo_list;
while(dummy!=0){
t_float prio=dummy->priority;
if(prio==priority)return dummy;
if(prio>priority)break;
result=dummy;
dummy=dummy->next;
}
dummy=result;
}
/* create a new priority list */
result = (t_lifop_prioritylist*)getbytes(sizeof( t_lifop_prioritylist));
result->priority=priority;
result->lifo_start=0;
/* insert it into the list of priority lists */
if(dummy==0){
/* insert at the beginning */
result->next=x->lifo_list;
x->lifo_list=result;
} else {
/* post insert into the list of LIFOs */
result->next=dummy->next;
dummy->next =result;
}
/* return the result */
return result;
}
static int add2lifo(t_lifop_prioritylist*lifoprio, int argc, t_atom *argv)
{
t_lifop_list*entry=0;
if(lifoprio==0){
error("plifo: no lifos available");
return -1;
}
/* create an entry for the lifo */
if(!(entry = (t_lifop_list*)getbytes(sizeof(t_lifop_list))))
{
error("plifo: couldn't add entry to end of lifo");
return -1;
}
if(!(entry->argv=(t_atom*)getbytes(argc*sizeof(t_atom)))){
error("plifo: couldn't add list to lifo!");
return -1;
}
memcpy(entry->argv, argv, argc*sizeof(t_atom));
entry->argc=argc;
entry->next=0;
entry->next=lifoprio->lifo_start;
lifoprio->lifo_start=entry;
return 0;
}
static t_lifop_prioritylist*getLifo(t_lifop_prioritylist*plifo)
{
if(plifo==0)return 0;
/* get the highest non-empty lifo */
while(plifo->lifo_start==0 && plifo->next!=0)plifo=plifo->next;
return plifo;
}
static void lifop_list(t_lifop *x, t_symbol *s, int argc, t_atom *argv)
{
t_lifop_prioritylist*plifo=0;
ZEXY_USEVAR(s);
if(!(plifo=lifop_genprioritylist(x, x->priority))) {
error("[lifop]: couldn't get priority lifo");
return;
}
if(!add2lifo(plifo, argc, argv))
{
x->counter++;
}
}
static void lifop_bang(t_lifop *x)
{
t_lifop_prioritylist*plifo=0;
t_lifop_list*lifo=0;
t_atom*argv=0;
int argc=0;
if(!(plifo=getLifo(x->lifo_list))){
outlet_bang(x->x_infout);
return;
}
if(!(lifo=plifo->lifo_start)){
outlet_bang(x->x_infout);
return;
}
x->counter--;
plifo->lifo_start=lifo->next;
/* get the list from the entry */
argc=lifo->argc;
argv=lifo->argv;
lifo->argc=0;
lifo->argv=0;
lifo->next=0;
/* destroy the lifo-entry (important for recursion! */
freebytes(lifo, sizeof(t_lifop_list));
/* output the list */
outlet_list(x->x_out, &s_list, argc, argv);
/* free the list */
freebytes(argv, argc*sizeof(t_atom));
}
static void lifop_query(t_lifop*x)
{
z_verbose(1, "%d elements in lifo", (int)x->counter);
outlet_float(x->x_infout, (t_float)x->counter);
}
static void lifop_clear(t_lifop *x)
{
t_lifop_prioritylist *lifo_list=x->lifo_list;
while(lifo_list){
t_lifop_prioritylist *lifo_list2=lifo_list;
t_lifop_list*lifo=lifo_list2->lifo_start;
lifo_list=lifo_list->next;
while(lifo){
t_lifop_list*lifo2=lifo;
lifo=lifo->next;
if(lifo2->argv)freebytes(lifo2->argv, lifo2->argc*sizeof(t_atom));
lifo2->argv=0;
lifo2->argc=0;
lifo2->next=0;
freebytes(lifo2, sizeof(t_lifop_list));
}
lifo_list2->priority =0;
lifo_list2->lifo_start=0;
lifo_list2->next =0;
freebytes(lifo_list2, sizeof( t_lifop_prioritylist));
}
x->lifo_list=0;
x->counter=0;
}
/* this is NOT re-entrant! */
static void lifop_dump(t_lifop*x)
{
t_lifop_prioritylist*plifo=getLifo(x->lifo_list);
if(!plifo||!plifo->lifo_start) {
outlet_bang(x->x_infout);
return;
}
while(plifo) {
t_lifop_list*lifo=plifo->lifo_start;
while(lifo) {
t_atom*argv=lifo->argv;
int argc=lifo->argc;
/* output the list */
outlet_list(x->x_out, &s_list, argc, argv);
lifo=lifo->next;
}
plifo=plifo->next;
}
}
static void lifop_free(t_lifop *x)
{
lifop_clear(x);
outlet_free(x->x_out);
outlet_free(x->x_infout);
}
static void *lifop_new(void)
{
t_lifop *x = (t_lifop *)pd_new(lifop_class);
floatinlet_new(&x->x_obj, &x->priority);
x->x_out=outlet_new(&x->x_obj, gensym("list"));
x->x_infout=outlet_new(&x->x_obj, gensym("float"));
x->lifo_list = 0;
x->priority=0;
x->counter=0;
return (x);
}
static void lifop_help(t_lifop*x)
{
post("\n%c lifop\t\t:: a Last-In-First-Out queue with priorities", HEARTSYMBOL);
}
void lifop_setup(void)
{
lifop_class = class_new(gensym("lifop"), (t_newmethod)lifop_new,
(t_method)lifop_free, sizeof(t_lifop), 0, A_NULL);
class_addbang (lifop_class, lifop_bang);
class_addlist (lifop_class, lifop_list);
class_addmethod (lifop_class, (t_method)lifop_clear, gensym("clear"), A_NULL);
class_addmethod (lifop_class, (t_method)lifop_dump, gensym("dump"), A_NULL);
class_addmethod (lifop_class, (t_method)lifop_query, gensym("info"), A_NULL);
class_addmethod (lifop_class, (t_method)lifop_help, gensym("help"), A_NULL);
zexy_register("lifop");
}
|