aboutsummaryrefslogtreecommitdiff
path: root/src/index.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/index.c')
-rw-r--r--src/index.c156
1 files changed, 95 insertions, 61 deletions
diff --git a/src/index.c b/src/index.c
index 405bc19..403a3b2 100644
--- a/src/index.c
+++ b/src/index.c
@@ -1,4 +1,4 @@
-/*
+/*
* index: associative dictionary
*
* (c) 1999-2011 IOhannes m zmölnig, forum::für::umläute, institute of electronic music and acoustics (iem)
@@ -7,18 +7,18 @@
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
- *
+ *
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
- *
+ *
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-/*
+/*
(c) 2005:forum::für::umläute:2000
"index" simulates an associative index :: that is : convert a symbol to an index
@@ -32,7 +32,7 @@
TODO: "add" at a specific position (like "add 10 hallo" of "add hallo 10") (??) FIXED
TODO: "delete" from a specific position (like "delete 4" deletes the 4th element) FIXED
TODO: get the number of stored entries ("bang") FIXED
-
+
TODO: resize the array if it gets to small
*/
@@ -44,8 +44,7 @@
static t_class *index_class;
-typedef struct _index
-{
+typedef struct _index {
t_object x_obj;
int entries, maxentries;
@@ -64,34 +63,42 @@ typedef struct _index
* LATER: shouldn't this return "-1" on failure ?
*/
static int find_last(t_symbol **names, int maxentries)
-{ /* returns the index of the last entry (0..(maxentries-1)) */
- while (maxentries--) if (names[maxentries]) return maxentries;
+{
+ /* returns the index of the last entry (0..(maxentries-1)) */
+ while (maxentries--) if (names[maxentries]) {
+ return maxentries;
+ }
return 0;
}
-/* search the array for "key"
- * if it is not there, return "-1"
+/* search the array for "key"
+ * if it is not there, return "-1"
*/
static int find_item(const t_symbol *key, t_symbol **names, int maxentries)
-{ /* returns index (0..[maxentries-1?]) on success; -1 if the item could not be found */
+{
+ /* returns index (0..[maxentries-1?]) on success; -1 if the item could not be found */
int i=-1;
int max = find_last(names, maxentries);
-
+
while (++i<=max)
- if (names[i] && key==names[i]) return i;
-
+ if (names[i] && key==names[i]) {
+ return i;
+ }
+
return -1;
}
/* find the first NULL entry in the array
- * return "-1" if none can be found
+ * return "-1" if none can be found
*/
static int find_free(t_symbol **names, int maxentries)
{
int i=0;
while (i<maxentries) {
- if (!names[i]) return i;
+ if (!names[i]) {
+ return i;
+ }
i++;
}
return -1;
@@ -106,21 +113,22 @@ static void index_add(t_index *x, t_symbol *s, t_float f);
static void index_symbol(t_index *x, t_symbol *s)
{
int element;
- if ( (element = find_item(s, x->names, x->maxentries)+1) )
+ if ( (element = find_item(s, x->names, x->maxentries)+1) ) {
outlet_float(x->x_obj.ob_outlet, (t_float)element);
- else if (x->auto_mode) /* not yet stored: add automatically */
+ } else if (x->auto_mode) { /* not yet stored: add automatically */
index_add(x, s, 0);
- else outlet_float(x->x_obj.ob_outlet, 0.f); /* not yet stored but do not add */
+ } else {
+ outlet_float(x->x_obj.ob_outlet, 0.f); /* not yet stored but do not add */
+ }
}
/* output the entry at a given index */
static void index_float(t_index *x, t_float findex)
{
int iindex = (int)findex;
- if ((iindex > 0) && (iindex <= x->maxentries) && (x->names[iindex-1]))
- {
- /* TB: output symbol to outlet */
- outlet_symbol (x->x_obj.ob_outlet,x->names[iindex-1]);
+ if ((iindex > 0) && (iindex <= x->maxentries) && (x->names[iindex-1])) {
+ /* TB: output symbol to outlet */
+ outlet_symbol (x->x_obj.ob_outlet,x->names[iindex-1]);
}
}
@@ -131,14 +139,17 @@ static void index_add(t_index *x, t_symbol *s, t_float f)
int newentry=(int)f;
if (! (find_item(s, x->names, x->maxentries)+1) ) {
- if (x->auto_resize && (x->entries==x->maxentries || newentry>=x->maxentries)){
+ if (x->auto_resize && (x->entries==x->maxentries
+ || newentry>=x->maxentries)) {
/* do some resizing */
int maxentries=(newentry>x->maxentries)?newentry:(x->maxentries*2);
int i;
t_symbol**buf=(t_symbol **)getbytes(sizeof(t_symbol *) * maxentries);
- if(buf!=0){
+ if(buf!=0) {
memcpy(buf, x->names, sizeof(t_symbol *) * x->maxentries);
- for(i=x->maxentries; i<maxentries; i++)buf[i]=0;
+ for(i=x->maxentries; i<maxentries; i++) {
+ buf[i]=0;
+ }
freebytes(x->names, sizeof(t_symbol *) * x->maxentries);
@@ -148,10 +159,12 @@ static void index_add(t_index *x, t_symbol *s, t_float f)
}
if ( x->entries < x->maxentries ) {
- if(newentry>0){
+ if(newentry>0) {
newentry--;
- if(x->names[newentry]){ /* it is already taken! */
- z_verbose(1, "index :: couldn't add element '%s' at position %d (already taken)", s->s_name, newentry+1);
+ if(x->names[newentry]) { /* it is already taken! */
+ z_verbose(1,
+ "index :: couldn't add element '%s' at position %d (already taken)",
+ s->s_name, newentry+1);
outlet_float(x->x_obj.ob_outlet, -1.f);
return;
}
@@ -159,32 +172,39 @@ static void index_add(t_index *x, t_symbol *s, t_float f)
newentry=find_free(x->names, x->maxentries);
}
if (newentry + 1) {
- x->entries++;
- x->names[newentry]=s;
- outlet_float(x->x_obj.ob_outlet, (t_float)newentry+1);
+ x->entries++;
+ x->names[newentry]=s;
+ outlet_float(x->x_obj.ob_outlet, (t_float)newentry+1);
return;
- } else error("index :: couldn't find any place for new entry");
- } else error("index :: max number of elements (%d) reached !", x->maxentries);
- } else z_verbose(1, "index :: element '%s' already exists", s->s_name);
+ } else {
+ error("index :: couldn't find any place for new entry");
+ }
+ } else {
+ error("index :: max number of elements (%d) reached !", x->maxentries);
+ }
+ } else {
+ z_verbose(1, "index :: element '%s' already exists", s->s_name);
+ }
/* couldn't add the symbol to our index table */
outlet_float(x->x_obj.ob_outlet, -1.f);
}
/* delete a symbol from the map (if it is in there) */
-static void index_delete(t_index *x, t_symbol* UNUSED(s), int argc, t_atom*argv)
+static void index_delete(t_index *x, t_symbol* UNUSED(s), int argc,
+ t_atom*argv)
{
int idx=-1;
- if(argc!=1){
+ if(argc!=1) {
error("index :: delete what ?");
return;
} else {
- if(argv->a_type==A_FLOAT){
+ if(argv->a_type==A_FLOAT) {
idx=atom_getint(argv)-1;
- } else if (argv->a_type==A_SYMBOL){
+ } else if (argv->a_type==A_SYMBOL) {
idx=find_item(atom_getsymbol(argv),x->names, x->maxentries);
} else {
error("index :: delete what ?");
- return;
+ return;
}
}
@@ -223,8 +243,8 @@ static void index_dump(t_index *x)
{
t_atom ap[2];
int i=0;
- for(i=0; i<x->maxentries; i++){
- if(x->names[i]){
+ for(i=0; i<x->maxentries; i++) {
+ if(x->names[i]) {
SETSYMBOL(ap, x->names[i]);
SETFLOAT(ap+1, i+1);
outlet_list(x->x_obj.ob_outlet, 0, 2, ap);
@@ -233,12 +253,13 @@ static void index_dump(t_index *x)
}
/* compact all entries, removing all holes in the map */
-static void index_compact(t_index *x){
+static void index_compact(t_index *x)
+{
int i,j;
- for(i=0; i<x->entries; i++){
- if(!x->names[i]){
- for(j=i+1; j<x->maxentries; j++){
- if(x->names[j]){
+ for(i=0; i<x->entries; i++) {
+ if(!x->names[i]) {
+ for(j=i+1; j<x->maxentries; j++) {
+ if(x->names[j]) {
x->names[i]=x->names[j];
x->names[j]=0;
break;
@@ -248,14 +269,16 @@ static void index_compact(t_index *x){
}
}
/* sort the map alphabetically */
-static void index_sort(t_index *x){
+static void index_sort(t_index *x)
+{
int entries=x->entries;
int step=entries;
int loops=1, n;
t_symbol**buf=x->names;
- index_compact(x); /* couldn't we do it more "in-place", e.g. don't touch empty slots ? */
+ index_compact(
+ x); /* couldn't we do it more "in-place", e.g. don't touch empty slots ? */
- while(step>1){
+ while(step>1) {
int i = loops;
step+=step%2;
step>>=1;
@@ -296,10 +319,14 @@ static void *index_new(t_symbol* UNUSED(s), int argc, t_atom *argv)
if (argc--) {
maxentries = (int)atom_getfloat(argv++);
- if (argc) automod = (int)atom_getfloat(argv++);
+ if (argc) {
+ automod = (int)atom_getfloat(argv++);
+ }
}
- if (maxentries<1) maxentries=128;
+ if (maxentries<1) {
+ maxentries=128;
+ }
buf = (t_symbol **)getbytes(sizeof(t_symbol *) * maxentries);
@@ -310,7 +337,9 @@ static void *index_new(t_symbol* UNUSED(s), int argc, t_atom *argv)
x->auto_mode = !(!automod);
x->auto_resize = 1;
- while (maxentries--) buf[maxentries]=0;
+ while (maxentries--) {
+ buf[maxentries]=0;
+ }
outlet_new(&x->x_obj, gensym("float"));
@@ -351,23 +380,28 @@ static void index_helper(t_index *x)
void index_setup(void)
{
index_class = class_new(gensym("index"),
- (t_newmethod)index_new, (t_method)index_free,
- sizeof(t_index), 0, A_GIMME, 0);
+ (t_newmethod)index_new, (t_method)index_free,
+ sizeof(t_index), 0, A_GIMME, 0);
class_addsymbol(index_class, index_symbol);
class_addmethod(index_class, (t_method)index_reset, gensym("reset"), 0);
- class_addmethod(index_class, (t_method)index_delete, gensym("delete"), A_GIMME, 0);
+ class_addmethod(index_class, (t_method)index_delete, gensym("delete"),
+ A_GIMME, 0);
/* class_addmethod(index_class, (t_method)index_add, gensym("add"), A_SYMBOL, 0); */
- class_addmethod(index_class, (t_method)index_add, gensym("add"), A_SYMBOL, A_DEFFLOAT, 0);
+ class_addmethod(index_class, (t_method)index_add, gensym("add"), A_SYMBOL,
+ A_DEFFLOAT, 0);
- class_addmethod(index_class, (t_method)index_auto, gensym("auto"), A_FLOAT, 0);
- class_addmethod(index_class, (t_method)index_resize, gensym("resize"), A_FLOAT, 0);
+ class_addmethod(index_class, (t_method)index_auto, gensym("auto"),
+ A_FLOAT, 0);
+ class_addmethod(index_class, (t_method)index_resize, gensym("resize"),
+ A_FLOAT, 0);
class_addfloat(index_class, (t_method)index_float);
class_addbang(index_class, (t_method)index_bang);
class_addmethod(index_class, (t_method)index_sort, gensym("sort"), 0);
- class_addmethod(index_class, (t_method)index_compact, gensym("compact"), 0);
+ class_addmethod(index_class, (t_method)index_compact, gensym("compact"),
+ 0);
class_addmethod(index_class, (t_method)index_dump, gensym("dump"), 0);
class_addmethod(index_class, (t_method)index_helper, gensym("help"), 0);