[CM] How to iterate through a hash-table in C?

Iain Duncan iainduncanlists at gmail.com
Tue Dec 8 07:50:53 PST 2020


thanks Bill, I would not have found that solution on my own. GC question
time! In this case I am recursively walking a Max dictionary structure to
convert to a hash or vice versa. If I gc_protect the hash, am I good for
any dynamically created entries too during this traversal process? Or
should I be turning off the gc entirely until the traversal is done? Am I
correct in understanding that the issue is that the GC might collect items
that were made from C but not in scheme?

thanks again, getting close to a major upgrade in usefulness here!
iain

On Tue, Dec 8, 2020 at 2:45 AM <bil at ccrma.stanford.edu> wrote:

> I would use s7_iterate which underlies map and for-each:
>
>      s7_pointer iter, hash, x;
>      s7_int gc1, gc2;
>
>      hash = s7_make_hash_table(sc, 8);
>      gc1 = s7_gc_protect(sc, hash);
>      s7_hash_table_set(sc, hash, s7_make_symbol(sc, "a"),
> s7_make_integer(sc, 1));
>      s7_hash_table_set(sc, hash, s7_make_symbol(sc, "b"),
> s7_make_integer(sc, 2));
>
>      iter = s7_make_iterator(sc, hash);
>      gc2 = s7_gc_protect(sc, iter);
>
>      x = s7_iterate(sc, iter);  /* (a . 1) */
>      fprintf(stderr, "x: %s\n", s7_object_to_c_string(sc, x));
>      x = s7_iterate(sc, iter);  /* (b . 2) */
>      fprintf(stderr, "x: %s\n", s7_object_to_c_string(sc, x));
>      x = s7_iterate(sc, iter);  /* #<eof> == end */
>      fprintf(stderr, "x: %s\n", s7_object_to_c_string(sc, x));
>
>      s7_gc_unprotect_at(sc, gc1);
>      s7_gc_unprotect_at(sc, gc2);
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cm-mail.stanford.edu/pipermail/cmdist/attachments/20201208/4c5a82a0/attachment.html>


More information about the Cmdist mailing list