www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - EMSI Containers and HashMap of HashMaps

reply Tobias Pankrath <tobias+dlang pankrath.net> writes:
The following code segfaults, when the outer hashmap rehashes,
and I am not yet sure why.

```d
module tests.refcounted_hashmap_test;

import containers;
import std.typecons;

struct Map
{
     HashMap!(int, int) theMap;
}

unittest
{
     HashMap!(int, RefCounted!Map) mapOfMaps;
     foreach (i; 0 .. 1000)
     {
         RefCounted!Map val;
         if (i == 0) val.theMap.getOrAdd(i, i);
         mapOfMaps.getOrAdd(i, val);
     }
}
```

For your convenience: https://run.dlang.io/is/gHSlu1

I am using Refcounted() because HashMap itself is not copy-able. 
Is there another way to have HashMaps as values of HashMaps?
May 02 2021
next sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Sunday, 2 May 2021 at 07:38:03 UTC, Tobias Pankrath wrote:
 The following code segfaults, when the outer hashmap rehashes,
 and I am not yet sure why.

 ```d
 module tests.refcounted_hashmap_test;

 import containers;
 import std.typecons;

 struct Map
 {
     HashMap!(int, int) theMap;
 }

 unittest
 {
     HashMap!(int, RefCounted!Map) mapOfMaps;
     foreach (i; 0 .. 1000)
     {
         RefCounted!Map val;
         if (i == 0) val.theMap.getOrAdd(i, i);
         mapOfMaps.getOrAdd(i, val);
     }
 }
 ```

 For your convenience: https://run.dlang.io/is/gHSlu1

 I am using Refcounted() because HashMap itself is not 
 copy-able. Is there another way to have HashMaps as values of 
 HashMaps?
```d HashMap!(int, RefCounted!Map) mapOfMaps = HashMap!(int, RefCounted!Map)(1024); ```
May 02 2021
next sibling parent Tobias Pankrath <tobias+dlang pankrath.net> writes:
On Sunday, 2 May 2021 at 08:59:15 UTC, Imperatorn wrote:
 On Sunday, 2 May 2021 at 07:38:03 UTC, Tobias Pankrath wrote:
 ```d
 HashMap!(int, RefCounted!Map) mapOfMaps = HashMap!(int, 
 RefCounted!Map)(1024);
 ```
Is HashMap.init a broken state? That makes using them hard as struct members :/
May 02 2021
prev sibling parent Tobias Pankrath <tobias+dlang pankrath.net> writes:
On Sunday, 2 May 2021 at 08:59:15 UTC, Imperatorn wrote:

 ```d
 HashMap!(int, RefCounted!Map) mapOfMaps = HashMap!(int, 
 RefCounted!Map)(1024);
 ```
That only fixes it, because you are avoiding the rehash with the initial size.
May 02 2021
prev sibling parent Tobias Pankrath <tobias+dlang pankrath.net> writes:
On Sunday, 2 May 2021 at 07:38:03 UTC, Tobias Pankrath wrote:
 For your convenience: https://run.dlang.io/is/gHSlu1

 I am using Refcounted() because HashMap itself is not 
 copy-able. Is there another way to have HashMaps as values of 
 HashMaps?
I've figured it out and filed an PR https://github.com/dlang-community/containers/pull/169. Would be great if we can get a new version out with this.
May 05 2021