www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Custom hash table key is const, how to call dtors?

reply Marco Leise <Marco.Leise gmx.de> writes:
Usually I want the keys to be declared "immutable" to signal
that their content must not change in order to provide stable
hashes. But when you remove items from the table you need to
call a const/immutable dtor that needs to be written for
everything that can be a hash table key.

What do you put into such a const/immutable dtor?
How do others deal with this?

-- 
Marco
Feb 05 2016
parent reply cy <dlang verge.info.tm> writes:
On Friday, 5 February 2016 at 22:18:50 UTC, Marco Leise wrote:
 But when you remove items from the table you need to call a 
 const/immutable dtor that needs to be written for everything 
 that can be a hash table key.
You need to write destructors for hash keys? How would you use string literals as keys then? Could you provide an example maybe...?
Feb 05 2016
parent reply Marco Leise <Marco.Leise gmx.de> writes:
Am Sat, 06 Feb 2016 03:38:54 +0000
schrieb cy <dlang verge.info.tm>:

 On Friday, 5 February 2016 at 22:18:50 UTC, Marco Leise wrote:
 But when you remove items from the table you need to call a 
 const/immutable dtor that needs to be written for everything 
 that can be a hash table key.
You need to write destructors for hash keys? How would you use string literals as keys then? Could you provide an example maybe...?
No, but they could have dtors because they contain malloc'd data. E.g. string literals that don't live on the GC heap. -- Marco
Feb 05 2016
parent reply cy <dlang verge.info.tm> writes:
On Saturday, 6 February 2016 at 03:57:16 UTC, Marco Leise wrote:

 No, but they could have dtors because they contain malloc'd 
 data. E.g. string literals that don't live on the GC heap.
Character arrays allocated with glibc malloc are immutable? News to me...
Feb 06 2016
parent Marco Leise <Marco.Leise gmx.de> writes:
Am Sun, 07 Feb 2016 01:05:28 +0000
schrieb cy <dlang verge.info.tm>:

 On Saturday, 6 February 2016 at 03:57:16 UTC, Marco Leise wrote:
 
 No, but they could have dtors because they contain malloc'd 
 data. E.g. string literals that don't live on the GC heap.
Character arrays allocated with glibc malloc are immutable? News to me...
err... well... you got a point there, but then new string(100) is probably allocated with malloc, too deep down in druntime. immutable really means that there is no mutable reference to the data. At any point in your code you can cast something to immutable when you that no mutable references will exist thereafter. We do this all the time during construction of immutable stuff, because when something is newly created, there is only one unique reference that is turned immutable after construction and you are set. You can go the same route with other MM schemes such as malloc, just that without a GC you are responsible for not freeing the immutable data as long as there are references to it. For example (and this applies to Ds GC'd AA, too) you must not delete entries while you iterate over the keys. There is no way to say "Hey I just borrowed the list of keys, please disallow any writes to it." For now, issues related to dtor-constness need to be fixed. Then working with immutable data structures is a lot less of a mine field. -- Marco
Feb 06 2016