Re: GHashTable as set



On Tue, 2004-07-27 at 14:36, Owen Taylor wrote:
On Tue, 2004-07-27 at 07:19, Lars Clausen wrote:
[Am I right to think that GLib questions go here as well?]

I'm using a GHashTable as a set, that is I have the same key and value. 
If these keys and values are complex enough that I need to pass in the
destroy_funcs, is there anything about when what is freed I should be
aware of?  It seems to me that doing insert may sometimes case the
destroy_funcs to be called, can somebody tell me what I can expect in
that area?

If you insert a duplicated entry into the hash table, insert calls
the key destroy function on the *new* key and the value destroy
function on the *old* value. (As is described in the docs...)

So if I do g_hash_table_insert(table, item, item), and an entry matching
item for equality is found, both the old and the new entry will be
deallocated.  Good to know:)

If this is a problem for you, you probably want 
g_hash_table_replace() which has the more expected behavior
of keeping the new key and destroying the old one.

May have to try that.

Thanks,
-Lars




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]