Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dumping and Loading Nested Maps/Sets #44

Closed
atom-moyer opened this issue Mar 27, 2020 · 12 comments
Closed

Dumping and Loading Nested Maps/Sets #44

atom-moyer opened this issue Mar 27, 2020 · 12 comments

Comments

@atom-moyer
Copy link

atom-moyer commented Mar 27, 2020

Hey Greg,

I would like to make an unordered unique multimap phmap::flat_hash_map<Key, phmap::flat_hash_set<Value>> where Key and Value are trivially copyable. As one would expect when I try to dump the outer map using phmap::BinaryOutputArchive ar_out( filename.c_str() ); dict.dump( ar_out ); I get an error that the inner set is not trivially copyable.

What would you suggest for serializing?

Also, side question: Is this how you would make a multimap?

Thanks,
Adam

@greg7mdp
Copy link
Owner

greg7mdp commented Mar 27, 2020

Hi Adam,

Maybe you could dump it as:

ar_out.dump(dict.size());
for (const auto& [k, v] : dict) {
    ar_out.dump(k);
    v.dump(ar_out);
}

and do the opposite when reading (read size, call reserve, and then read n [k, v] pairs and populate the dict).

Let me think about the multimap question.

greg

@atom-moyer
Copy link
Author

That worked great!

Here is the code that I ended up implementing for people in the future:

    void dump ( const std::string & filename ) {
        phmap::BinaryOutputArchive ar_out ( filename.c_str() );

        ar_out.dump( dict.size() );
        for ( auto & [k, v] : dict ) {
            ar_out.dump( k );
            v.dump( ar_out );
        }
    }


    void load ( const std::string & filename ) {
        phmap::BinaryInputArchive ar_in ( filename.c_str() );

        size_t size; Key k; phmap::flat_hash_set<Value> v;

        ar_in.load( &size );
        dict.reserve( size );

        for ( size_t idx = 0; idx < size; idx++ ) {
            ar_in.load( &k );
            v.load( ar_in );

            dict.insert_or_assign( k, v );
        }
    }

Notice that I had to remove the const in the dump for loop because v.dump( ar_out ) must not be implemented as a const member function. You might want to add that.

Adam

@greg7mdp
Copy link
Owner

Nice. I'll do the const change and add this as an example.
I would do the following change though to avoid copying the newly loaded hash_set:

    void load ( const std::string & filename ) {
        phmap::BinaryInputArchive ar_in ( filename.c_str() );

        size_t size;

        ar_in.load( &size );
        dict.reserve( size );

        for ( size_t idx = 0; idx < size; idx++ ) {
            Key k;
            phmap::flat_hash_set<Value> v;

            ar_in.load( &k );
            v.load( ar_in );

            dict.insert_or_assign( std::move(k), std::move(v) );
        }
    }

greg7mdp added a commit that referenced this issue Mar 27, 2020
@greg7mdp
Copy link
Owner

I added the example dump_nested.cc. Thanks! - greg

@mr-eyes
Copy link

mr-eyes commented Aug 12, 2022

Hi @greg7mdp

It seems that the example in https://github.com/greg7mdp/parallel-hashmap/blob/master/examples/dump_nested.cc is not working anymore to dump TriviallyCopyable types.

@greg7mdp
Copy link
Owner

Thanks for the note, @mr-eyes , I'll have a look this weekend.

@greg7mdp
Copy link
Owner

@mr-eyes sorry for that, I fixed the issue and even made the example nicer :-)

@mr-eyes
Copy link

mr-eyes commented Aug 13, 2022

@greg7mdp Thank you so much!
I have been using the parallel-hashmap for almost all the projects I am working on; Much appreciation 🙌

@greg7mdp
Copy link
Owner

@mr-eyes - thank you! It is always great to hear from happy users.

@greg7mdp
Copy link
Owner

@mr-eyes if you are using c++20, you may want to switch to the gtl library, it has all the stuff from phmap and more.

@DJuego
Copy link

DJuego commented Aug 15, 2022

Hi, I've been following this library for a while now, and although I haven't used it, I'm glad it exists, thanks for the great work! However I was unaware of the existence of the gtl library instead.

Based on your comment, could you confirm that it is an updated superset of the phpmap library, with the only limitation of requiring compatibility with c++20?

Thank you very much!

DJuego

@greg7mdp
Copy link
Owner

it is an updated superset of the phpmap library, with the only limitation of requiring compatibility with c++20?

yes, exactly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants