pritishn's blog

By pritishn, 4 years ago, In English

I know it's slightly off-topic but I couldn't find any answers on other sites.
I was actually doing a project in which I think using a trie will be really helpful for processing the queries.

But if I keep remaking the trie everytime from scratch when I load the app then it's gonna be slow. So is there anyway I can store the contents of a trie ( wrapped in a class) into a file and load it directly everytime I need it?

I have done something like this using classes before , but we know trie needs dynamic memory allocation. So will it work if I wrap it in a class and put it in a file?
Also can anyone who has development experience tell how do people generally handle these query-based data-structures in apps? Do they rebuild it on the database everytime?

  • Vote: I like it
  • +51
  • Vote: I do not like it

»
4 years ago, # |
Rev. 2   Vote: I like it +39 Vote: I do not like it

There is no one short or even one correct answer google for serialization. Something like this.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks a LOT !
    I didn't know it was called "serialization". The link you provided cleared my doubts, thanks a lot again!

»
4 years ago, # |
  Vote: I like it +22 Vote: I do not like it

This process is often called serialization/deserialization, or marshalling/unmarshalling.

There is no common solution, it must be implemented for each structure. Javas ObjectInputStream/ObjectOutputStream are often used to do these things in Java. I assume there are some tutorials arround covering common patterns in this field.

The more or less same can be done in C++, but the STL does not support much. For example see https://www.codeguru.com/cpp/cpp/algorithms/general/an-introduction-to-object-serialization-in-c.html

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks for the link. Yeah , I was also thinking of shifting to Java for this purpose. :)

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +11 Vote: I do not like it

      While STL does not support serialization, it can be done with Boost's serialization library or with cereal

»
4 years ago, # |
Rev. 2   Vote: I like it +16 Vote: I do not like it

For serializing trie in C++ this may help

Credits: kartik8800

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +9 Vote: I do not like it

    That's exactly what I was looking for. XD
    Time to ctrl+c and ctrl+v.XD

  • »
    »
    4 years ago, # ^ |
    Rev. 3   Vote: I like it +16 Vote: I do not like it

    Ah that's a wonderful library, thanks for sharing!
    I'd definitely star that guy's repo :P

    P.S. that noob forgot to free the memory by writing a suitable destructor smh xD

»
4 years ago, # |
  Vote: I like it +6 Vote: I do not like it

Just for the sake of completeness, if you're using Python, you can note that Python has a module called pickle for serialization of data (and this is used quite a lot in machine learning). You can read more about this here.

»
4 years ago, # |
  Vote: I like it +3 Vote: I do not like it

use https://github.com/tlwg/libdatrie (or its bindings) if possible. There are builtin methods for trie serialization/deserialization