Блог пользователя pritishn

Автор pritishn, 4 года назад, По-английски

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?

  • Проголосовать: нравится
  • +51
  • Проголосовать: не нравится

»
4 года назад, # |
Rev. 2   Проголосовать: нравится +39 Проголосовать: не нравится

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

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

»
4 года назад, # |
  Проголосовать: нравится +22 Проголосовать: не нравится

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 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      4 года назад, # ^ |
        Проголосовать: нравится +11 Проголосовать: не нравится

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

»
4 года назад, # |
Rev. 2   Проголосовать: нравится +16 Проголосовать: не нравится

For serializing trie in C++ this may help

Credits: kartik8800

  • »
    »
    4 года назад, # ^ |
      Проголосовать: нравится +9 Проголосовать: не нравится

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

  • »
    »
    4 года назад, # ^ |
    Rev. 3   Проголосовать: нравится +16 Проголосовать: не нравится

    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 года назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится

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 года назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

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