Q: hash parameter passed to c++ ruby extension is incomplete

Y

Yuri Danielyan

I'm trying to pass following hash to my c++ ruby extension:
h1 = {"G804988.19" => 0, "G804988.21" => 0, "NH3812.06" => 0,
"NH3812.10" => 0}
myext.SomeFunction(h1)

The hash looks fine from script:
h1.each_pair {| key, value | printf("%s - %s\n", key, value) }

The above statement shows that all 4 hash entries are there.


But in the extension I can only see 3 items.
Code is like below:

extern "C" VALUE SomeFunction(VALUE vSelf,VALUE vh1)
{
struct st_table *pTblHash;

pTblHash = RHASH(vh1)->tbl;
}

The pTblHash->num_entries is correct and equals to 4.
But in the pTblHash->bins array there are only 3 not empty pointers to
hash elements.

If I use one symbol shorter hash keys for one of entries (G80498.21
instead of G804988.21), for example:
h1 = {"G804988.19" => 0, "G80498.21" => 0, "NH3812.06" => 0, "NH3812.10"
=> 0}

then problem disappears.

I'm debugging under Windows XP and Ruby 1.8.

I'll appreciate any help.
Thank you.
Yuri
 
H

Heesob Park

Hi,

2009/3/11 Yuri Danielyan said:
I'm trying to pass following hash to my c++ ruby extension:
h1 =3D {"G804988.19" =3D> 0, "G804988.21" =3D> 0, "NH3812.06" =3D> 0,
"NH3812.10" =3D> 0}
myext.SomeFunction(h1)

The hash looks fine from script:
h1.each_pair {| key, value | printf("%s - %s\n", =C2=A0key, value) }

The above statement shows that all 4 hash entries are there.


But in the extension I can only see 3 items.
Code is like below:

extern "C" VALUE SomeFunction(VALUE vSelf,VALUE vh1)
{
=C2=A0struct st_table =C2=A0*pTblHash;

=C2=A0pTblHash =3D RHASH(vh1)->tbl;
}

The pTblHash->num_entries is correct and equals to 4.
But in the pTblHash->bins array there are only 3 not empty pointers to
hash elements.

If I use one symbol shorter hash keys for one of entries (G80498.21
instead of G804988.21), for example:
h1 =3D {"G804988.19" =3D> 0, "G80498.21" =3D> 0, "NH3812.06" =3D> 0, "NH3= 812.10"
=3D> 0}

then problem disappears.

I'm debugging under Windows XP and Ruby 1.8.
I guess the problem is due to the Collision resolution [1].

The st_table, num_entries and bins are highly implementation dependent
so using it directly in extension code is not recommended.

In my experience, almost all extension codes required hash could be
done with following rb_hash_xxx functions
rb_hash_new
rb_hash_delete
rb_hash_foreach
rb_hash_aref
rb_hash_aset


[1] http://en.wikipedia.org/wiki/Hash_table#Collision_resolution

Regards,

Park Heesob
 
Y

Yuri Danielyan

Heesob said:
I guess the problem is due to the Collision resolution [1].

The st_table, num_entries and bins are highly implementation dependent
so using it directly in extension code is not recommended.

rb_hash_foreach

Thank you
Actually I started with using rb_hash_foreach, but sometimes I don't see
this function in intern.h (probably older Ruby versions), so I turned to
direct usage.
But after your reply I thought that maybe there are two items under same
bin and I can reach it somehow.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top