aboutsummaryrefslogtreecommitdiff
path: root/src/listfind.c
blob: e362bc1927e31752515a49f59b6471437ed9d6fb (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
/******************************************************
 *
 * 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
 *
 ******************************************************/

/* 2607:forum::für::umläute:2007 */


#include "zexy.h"

#if 0
# define DEBUG
#endif

#ifdef DEBUG
# define DEBUGFUN(x) x
#else
# define DEBUGFUN(x)
#endif


/* ------------------------- listfind ------------------------------- */

/* find a sublist in a list and return the index of the occurence (or indices if there are more) */

static t_class *listfind_class;


typedef struct _listfind
{
  t_object       x_obj;
  t_outlet      *x_outlet;

  int x_n;

  t_inlet*x_listin;
  int     x_argc;
  t_atom *x_argv;
} t_listfind;



static void listfind_list2(t_listfind*x,t_symbol*s, int argc, t_atom*argv)
{
  if(x->x_argv!=0) {
    freebytes(x->x_argv, sizeof(t_atom)*x->x_argc);
  }
  x->x_argc=0;
  x->x_argv=0;

  DEBUGFUN(post("list of length %d", argc));

  if(argc>0) {
    int i;
    x->x_argc=argc;
    x->x_argv=(t_atom*)getbytes((x->x_argc)*sizeof(t_atom));
    for(i=0; i<argc; i++) {
      x->x_argv[i]=argv[i];
    }
  }

  DEBUGFUN(post("list2: %d %x", x->x_argc, x->x_argv));
}

static int atom_equals(t_atom*a1, t_atom*a2) {
  if(a1->a_type!=a2->a_type) return 0;

  return(a1->a_w.w_symbol==a2->a_w.w_symbol);  
}

static int list_equals(int count, t_atom*a1, t_atom*a2) {
  int i=0;
  for(i=0; i<count; i++, a1++, a2++) {
    if(a1->a_type!=a2->a_type) 
      return 0;
    if(a1->a_w.w_symbol!=a2->a_w.w_symbol) /* is it that simple? */
      return 0;
  }
  return 1;
}

static int listfind_find(int argc, t_atom*argv, int matchc, t_atom*matchv) {
  int i=0;

  if(matchc>argc)
     return -1;
  if(matchc==0)
    return 0;

  for(i=0; i<=(argc-matchc); i++, argv++) {
    if(list_equals(matchc, argv, matchv))
      return i;
  }
  return -1;
}

static void listfind_doit(t_outlet*out, int longcount, t_atom*longlist, int patterncount, t_atom*patternlist) {
  int count=0;
  int index;
  int offset=0;

  t_atom*ap=0;
  int length=1+((patterncount>0)?(longcount/patterncount):longcount); /* we shan't have more hits than this! */
  if(length<1) {
    outlet_bang(out);
  }
  ap=(t_atom*)getbytes(length*sizeof(t_atom));

  DEBUGFUN(post("expecting no more than %d results", length));

  while((index=listfind_find(longcount-offset, longlist+offset, patterncount, patternlist))>=0) {
    offset+=index;
    SETFLOAT(ap+count, offset);

    count++;

    DEBUGFUN(post("new offset=%d", offset));
    offset++; /* proceed to the next element */
  }

  DEBUGFUN(post("got %d results", count));

  outlet_list(out, gensym("list"), count, ap);
  freebytes(ap, length*sizeof(t_atom));
}

static void listfind_list(t_listfind *x, t_symbol *s, int argc, t_atom *argv)
{
#if 0
  /* entire list hot: 
   * this is more intuitive when searching a pattern in many lists 
   */
  listfind_doit(x->x_obj.ob_outlet, argc, argv, x->x_argc, x->x_argv);

#else

  /* pattern is hot
   * this is compatible with foobar's [list-find]
   * this is more intuitive when searching different patterns in a list
   */
  listfind_doit(x->x_obj.ob_outlet, x->x_argc, x->x_argv, argc, argv);

#endif

  /* personally i think that searching 1 pattern in many lists is more useful */
}

static void listfind_free(t_listfind *x)
{ 
  if(x->x_argv) {
    freebytes(x->x_argv, x->x_argc*sizeof(int));
    x->x_argv=0;
    x->x_argc=0;
  }
  inlet_free(x->x_listin);

}

static void *listfind_new(t_symbol *s, int argc, t_atom *argv)
{
  t_listfind *x = (t_listfind *)pd_new(listfind_class);
  ZEXY_USEVAR(s);

  outlet_new(&x->x_obj, 0);
  x->x_listin=inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("list"), gensym("lst2"));

  x->x_argc=0;
  x->x_argv=0;

  listfind_list2(x, gensym("list"), argc, argv);

  return (x);
}


static void listfind_help(t_listfind*x)
{
  post("\n%c listfind\t\t:: split lists into multiple sublists based on matches", HEARTSYMBOL);
}

void listfind_setup(void)
{
  listfind_class = class_new(gensym("listfind"), (t_newmethod)listfind_new, 
                             (t_method)listfind_free, sizeof(t_listfind), 0, A_GIMME, 0);
  class_addlist    (listfind_class, listfind_list);
  class_addmethod  (listfind_class, (t_method)listfind_list2, gensym("lst2"), A_GIMME, 0);

  class_addmethod(listfind_class, (t_method)listfind_help, gensym("help"), A_NULL);
  zexy_register("listfind");
}