[comp.lang.c] for t in table

djones@megatest.UUCP (Dave Jones) (01/28/89)

From article <19579@agate.BERKELEY.EDU>, by bowles@eris.berkeley.edu (Jeff A. Bowles):

...

> The only thing I really miss is something you Unix-types will recognize
> from awk (and perhaps from Algol 68?) -
> 	for (t in table)
> 		process(table[t]);
> But that's another story....
> 

It's easy enough to add, given its utility.  I have a
number of "container-classes", one of which is called "Hash".
You have to use a few more keystrokes than you would in AWK, but
it's not _too_ hard on the fingers, if you're a reasonably good
typist.

Container classes have similar initializers and iterators.
Various kinds of objects have associated with them a function which
will hash an object, and a function which will determine whether
or not two objects are equivalent. (Equivalent objects hash to
the same number.) So you're not restricted to using only a few
kinds of keys and values, as you are in AWK.

A typical kind of object would be a "binding" -- a record which
contains a key and a value.

I've used automatic variables in the example. I also have canned 
functions which will get objects from malloc space and initialize them,
if that is appropriate.)

extern int          Some_type_eq_func(); /* boolean */
extern unsigned int Some_type_hash_func();

{ 
   Hash table;
   Hash_init(&table, Some_type_eq_func, Some_type_hash_func);

  /* Code that puts things into the hash-table, and looks
  ** them up randomly has been omited... 
  */

   {
     Hash_iter next;
     Some_type *object;

     for( Hash_iter_init(&next, &table);
          object = (char*)Hash_iter_next(&next);
          object != 0
        )
        { 
          process(object);
        }

    }

    Hash_clean(&table);
}   

I also have lists, queues, stacks, avl_trees, priority-queues, and
so on, all of which have iterators.