aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2007-07-26 10:24:38 +0000
committerIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2007-07-26 10:24:38 +0000
commitb19be3cca1a839593c952e0bb17b11119a2e6436 (patch)
treeff255d73c29de722ac1fcc2d30d0f02dcbc5d785
parent17bbb7ac85846bcc2c29ec382013711c13dc6fa0 (diff)
added more list operating objects
svn path=/trunk/externals/zexy/; revision=8246
-rw-r--r--reference/list2lists-help.pd21
-rw-r--r--reference/listfind-help.pd26
-rw-r--r--src/list2lists.c148
-rw-r--r--src/listfind.c200
-rw-r--r--src/z_zexy.c2
-rw-r--r--src/z_zexy.h2
6 files changed, 399 insertions, 0 deletions
diff --git a/reference/list2lists-help.pd b/reference/list2lists-help.pd
new file mode 100644
index 0000000..717d82b
--- /dev/null
+++ b/reference/list2lists-help.pd
@@ -0,0 +1,21 @@
+#N canvas 385 348 685 429 10;
+#X text 497 29 part of zexy;
+#X obj 63 13 list2lists;
+#X text 136 13 - split a list into sublists;
+#X obj 88 220 print sublists;
+#X obj 88 175 list2lists;
+#X msg 88 83 1 2 3 4 5 6 7 8 9 10;
+#X text 216 154 an array of lengths of the sublists;
+#X text 226 84 a list to be split;
+#X text 186 214 outputs a number of sublists each having the length
+as specified to the object.;
+#X text 91 266 if the total length of the sublists exceeds the length
+of the input list \, the rest of the sublists is discarded;
+#X msg 143 153 4 3 0 0 -1 -2;
+#X text 94 293 zero-length sublists are "bangs";
+#X text 77 351 see also;
+#X obj 150 350 repack;
+#X text 206 351 fixed sized sublists;
+#X connect 4 0 3 0;
+#X connect 5 0 4 0;
+#X connect 10 0 4 1;
diff --git a/reference/listfind-help.pd b/reference/listfind-help.pd
new file mode 100644
index 0000000..2b51094
--- /dev/null
+++ b/reference/listfind-help.pd
@@ -0,0 +1,26 @@
+#N canvas 385 348 685 518 10;
+#X text 497 29 part of zexy;
+#X obj 63 13 listfind;
+#X text 122 14 - find sublists in lists;
+#X obj 88 175 listfind;
+#X msg 279 154 1 2 3 2 1 8 2 3 4;
+#X obj 279 128 loadbang;
+#X msg 88 71 1;
+#X text 283 174 set the list to be searched;
+#X text 85 55 the list to be found;
+#X msg 100 95 list bla;
+#X msg 114 119 list 2 3;
+#X msg 126 142 list 8;
+#X obj 88 220 print found@;
+#X text 180 223 outputs a list of (0-based) indices of all occurences
+of the sublist;
+#X text 84 281 this is similar (and hopefully compatible) to foobar's
+[list-find] but it searches not only for single elements but for entire
+sub-lists;
+#X connect 3 0 12 0;
+#X connect 4 0 3 1;
+#X connect 5 0 4 0;
+#X connect 6 0 3 0;
+#X connect 9 0 3 0;
+#X connect 10 0 3 0;
+#X connect 11 0 3 0;
diff --git a/src/list2lists.c b/src/list2lists.c
new file mode 100644
index 0000000..93972cd
--- /dev/null
+++ b/src/list2lists.c
@@ -0,0 +1,148 @@
+/******************************************************
+ *
+ * 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>
+
+//#define DEBUG
+
+#ifdef DEBUG
+# define DEBUGFUN(x) x
+#else
+# define DEBUGFUN(x)
+#endif
+
+
+/* ------------------------- list2lists ------------------------------- */
+
+/* split a list into several sublists given by their lenghts */
+
+static t_class *list2lists_class;
+
+
+typedef struct _list2lists
+{
+ t_object x_obj;
+ t_outlet *x_outlet;
+
+ int x_n;
+
+ t_inlet*x_lengin;
+ int x_lcount;
+ t_int *x_length;
+} t_list2lists;
+
+
+
+static void list2lists_list2(t_list2lists*x,t_symbol*s, int argc, t_atom*argv)
+{
+ if(x->x_length!=0) {
+ freebytes(x->x_length, sizeof(t_atom)*x->x_lcount);
+ }
+ x->x_lcount=0;
+ x->x_length=0;
+
+ DEBUGFUN(post("list of length %d", argc));
+
+ if(argc>0) {
+ int i;
+ x->x_lcount=argc;
+ x->x_length=(t_int*)getbytes((x->x_lcount)*sizeof(t_int));
+ for(i=0; i<argc; i++) {
+ x->x_length[i]=atom_getint(argv+i);
+ }
+ }
+
+ DEBUGFUN(post("list2: %d %x", x->x_lcount, x->x_length));
+}
+
+static void list2lists_output(t_list2lists*x, int argc, t_atom*argv)
+{
+ t_outlet*out=x->x_obj.ob_outlet;
+ if(argc<=0)
+ outlet_bang(out);
+ else
+ outlet_list(out, &s_list, argc, argv);
+}
+
+static void list2lists_list(t_list2lists *x, t_symbol *s, int argc, t_atom *argv)
+{
+ int i;
+ int offset=0;
+
+ if(x->x_lcount<1) {
+ outlet_anything(x->x_obj.ob_outlet, s, argc, argv);
+ return;
+ }
+
+ for(i=0; i<x->x_lcount; i++) {
+ int len=x->x_length[i];
+ if(len>argc) {
+ list2lists_output(x, argc, argv);
+ return;
+ }
+ list2lists_output(x, len, argv);
+ argv+=len;
+ argc-=len;
+ }
+}
+
+static void list2lists_free(t_list2lists *x)
+{
+ if(x->x_length) {
+ freebytes(x->x_length, x->x_lcount*sizeof(int));
+ x->x_length=0;
+ x->x_lcount=0;
+ }
+ inlet_free(x->x_lengin);
+
+}
+
+static void *list2lists_new(t_symbol *s, int argc, t_atom *argv)
+{
+ t_list2lists *x = (t_list2lists *)pd_new(list2lists_class);
+ ZEXY_USEVAR(s);
+
+ outlet_new(&x->x_obj, 0);
+ x->x_lengin=inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("list"), gensym("lst2"));
+
+ x->x_lcount=0;
+ x->x_length=0;
+
+ list2lists_list2(x, gensym("list"), argc, argv);
+
+ return (x);
+}
+
+
+static void list2lists_help(t_list2lists*x)
+{
+ post("\n%c list2lists\t\t:: split lists into multiple sublists based on matches", HEARTSYMBOL);
+}
+
+void list2lists_setup(void)
+{
+ list2lists_class = class_new(gensym("list2lists"), (t_newmethod)list2lists_new,
+ (t_method)list2lists_free, sizeof(t_list2lists), 0, A_GIMME, 0);
+ class_addlist (list2lists_class, list2lists_list);
+ class_addmethod (list2lists_class, (t_method)list2lists_list2, gensym("lst2"), A_GIMME, 0);
+
+ class_addmethod(list2lists_class, (t_method)list2lists_help, gensym("help"), A_NULL);
+ zexy_register("list2lists");
+}
diff --git a/src/listfind.c b/src/listfind.c
new file mode 100644
index 0000000..693181f
--- /dev/null
+++ b/src/listfind.c
@@ -0,0 +1,200 @@
+/******************************************************
+ *
+ * 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"
+#include <string.h>
+
+//#define DEBUG
+
+#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");
+}
diff --git a/src/z_zexy.c b/src/z_zexy.c
index 92e0aa3..a02063b 100644
--- a/src/z_zexy.c
+++ b/src/z_zexy.c
@@ -37,8 +37,10 @@ void z_zexy_setup(void)
lifop_setup(); /* lifop */
limiter_tilde_setup(); /* limiter~ */
list2int_setup(); /* list2int */
+ list2lists_setup(); /* list2lists */
list2symbol_setup(); /* list2symbol */
lister_setup(); /* lister */
+ listfind_setup(); /* listfind */
lpt_setup(); /* lpt */
makesymbol_setup(); /* makesymbol */
matchbox_setup(); /* matchbox */
diff --git a/src/z_zexy.h b/src/z_zexy.h
index 0c9b2cf..686c612 100644
--- a/src/z_zexy.h
+++ b/src/z_zexy.h
@@ -35,8 +35,10 @@ void length_setup(void); /* length */
void lifop_setup(void); /* lifop */
void limiter_tilde_setup(void); /* limiter~ */
void list2int_setup(void); /* list2int */
+void list2lists_setup(void); /* list2lists */
void list2symbol_setup(void); /* list2symbol */
void lister_setup(void); /* lister */
+void listfind_setup(void); /* listfind */
void lpt_setup(void); /* lpt */
void makesymbol_setup(void); /* makesymbol */
void matchbox_setup(void); /* matchbox */