Recursive data structures with stdlib hash maps

I’m trying to understand whether it is possible / feasible to define recursive data structures using stdlib’s hash maps. For the most basic functionality, I would need a way to store maps and arrays, where the latter can be implemented as associative array using a map, as values.

However, the copy_other routine uses a sourced allocation, which means any data structure stored in a stdlib hash map will be copied in and later copied out this way:

For a recursive data structure this can be quite an issue, depending on the represented data, for example I found deep copy of a 2 MB JSON to be possible however quite expensive (TOML Fortran 0.3.0 - #3 by awvwgk).

I wonder whether there is a better way to create recursive data structures based on what we have available in stdlib.

2 Likes

My solution to this problem in rojff was to use move_alloc. In that way you can build up quite large recursive data structures without any copying. It gets really monotonous and tricky to get right if you do it all in one procedure, but there’s an example of it in the test suite. Check out the move_construct_with(out)_errors test/construction_method_test.f90 · main · Brad Richardson / rojff · GitLab

2 Likes

But this requires that we change how stdlib hash maps work, i.e. recursive data structures are currently not possible there.

I took a quick look, and I actually don’t see how one puts data into or fetches it from the stdlib hash map. I did see one type with a class(*) component, which would let you have recursive data structures (unless you mean the constructed data structure would have circular references, in which case I don’t think that’s possible).

Is there a tutorial, or user-guide for the stdlib hash map somewhere?

I remember stumbling onto this a while back

1 Like

Not per se. There is only the description of the API .

I used the stdlib hashmaps a few times and I was happy with its API and performances (in comparison to other fpmized hashmap projects). However, as it is still experimental, the API can still be changed and improved

Off-topic, but I read the code of rojff. Very clean and nice. I like how you approached the error handling here. Definetely learned something :slight_smile:

1 Like