[plt-scheme] scheme_make_hash_table

From: Danny Yoo (dyoo at hkn.eecs.berkeley.edu)
Date: Wed Jan 11 05:21:58 EST 2006

On Tue, 10 Jan 2006 wayne at taxupdate.com wrote:

> I expected this code to declare a hash table, then insert 2 values in
> it.  Instead it looks like 1 value is inserted, and it's the wrong one.

Hi Wayne,


I'm getting better results with this:

/****************************************************/
#include "scheme.h"

int main(int argc, char ** args)
{
  int i;
  Scheme_Env * env;
  Scheme_Hash_Table * hash;
  scheme_set_stack_base(NULL, 1);
  env = scheme_basic_env();
  hash = scheme_make_hash_table(SCHEME_hash_ptr);
  scheme_hash_set(hash, scheme_make_byte_string("a"),
                  scheme_make_integer(2));
  scheme_hash_set(hash, scheme_make_byte_string("b"),
                  scheme_make_integer(2));
  scheme_hash_set(hash, scheme_make_byte_string("c"),
                  scheme_make_integer(3));
  printf("size: %d\n", hash->size);
  printf("count: %d\n", hash->count);
  for (i=0; i<hash->size; i++) {
    if (hash->vals[i])
      printf("key: %s, val: %d\n",
	     SCHEME_BYTE_STR_VAL(hash->keys[i]),
	     SCHEME_INT_VAL(hash->vals[i]));
  }
  return 0;
}
/****************************************************/

Here's what things look on my system:

######
mumak:~ dyoo$ mzc --cc test.c
mzc version 300.2, Copyright (c) 2004-2005 PLT Scheme Inc.
"test.c":
 [output to "./test.o"]
mumak:~ dyoo$ gcc -framework PLT_MzScheme -o test test.o
mumak:~ dyoo$ ./test
size: 7
count: 3
key: a, val: 2
key: c, val: 3
key: b, val: 2
######

I removed the eval_string stuff just because I thought calling the
scheme_hash_* API functions was nicer.  *grin*

I've also fixed the display loop to know that the hash->keys and
hash->vals point to arrays of Scheme_Object value and to use the converter
functions to extract values out of them; I think the garbage values you
were seeing were just because we were trying to treat scheme strings as c
strings.



I have to admit that, for scheme_make_hash_table, I don't totally
understand the distinction between SCHEME_hash_ptr and SCHEME_hash_string,
but I don't think we should be using SCHEME_hash_string unless we're
directly passing char*'s to scheme_hash_set.

For example, the following code also appears to work for me:

/***************************************************/
#include "scheme.h"

int main(int argc, char ** args)
{
  int i;
  Scheme_Env * env;
  Scheme_Hash_Table * hash;
  scheme_set_stack_base(NULL, 1);
  env = scheme_basic_env();
  hash = scheme_make_hash_table(SCHEME_hash_string);
  scheme_hash_set(hash, "a", scheme_make_integer(2));
  scheme_hash_set(hash, "c", scheme_make_integer(3));

  printf("size: %d\n", hash->size);
  printf("count: %d\n", hash->count);
  for (i=0; i<hash->size; i++) {
    if (hash->vals[i])
      printf("key: %s, val: %d\n",
	     hash->keys[i],
	     SCHEME_INT_VAL(hash->vals[i]));
  }
  return 0;
}
/***************************************************/

in the case where we directly pass c-string pointers off to
scheme_hash_set.  But this feels off to me, considering that
scheme_hash_set is only supposed to take Scheme_Object* keys rather than
arbitrary void*'s; I might be misreading the Inside MzScheme
documentation.


Best of wishes to you!



Posted on the users mailing list.