persistent_table(7rheolef)

πŸ“¦
version 7.2 Β· 4 versions
, in debian trixie, ubuntu noble, ubuntu questing

✨ persistent_table ✨

okay bestie, so this is persistent_table(7rheolef) β€” a whole manual page, and it's genuinely such a glow-up for your code. sit with me, this one's a bop πŸ’–


🌸 NAME 🌸

persistent_table β€” persistent data base (rheolef-7.2) πŸ’…


πŸŽ€ DESCRIPTION πŸŽ€

so here's the vibe: you've got these big objects that take forever to set up, and then after all that they just get used in read-only mode β€” via accessors, no modifiers, periodt. this class is a convenient way to implement a persistent data base of exactly those. iconic behavior, honestly.

examples of such objects in scientific computing? finite element meshes (peep geo(2)) β€” long to load from file, and they want a lot of memory 😭 β€” or high-order polynomial basis (peep basis(2)) β€” long to initialize, like, e.g. Vandermonde matrix inversion, no big deal. the way that these objects get created independently in different parts of a code… bestie, both memory size and computation time could be saved by just reusing them when they was already created. we love efficiency, we love not doing the same work twice 🫢

the aim of the persistent_table class is to automate the implementation of a persistent data base for a generic object, that could be e.g. a finite element mesh or a polynomial basis. and the best part? it requires very few modification of a pre-existing object. slay.

the implementation of the persistent_table class bases on those of smart_pointer(7) class for handling reference counting. when shared object in the data base are not modifiable, the idea is to use the smart_pointer_nocopy class. otherwise, when the object has to be modified, the name of the object β€” that is used as a key in an hashtable β€” should also be modified, in order to address the new modified object. iykyk.

here is a small minimalist example of the class πŸ‘‘


πŸ¦‹ EXAMPLE πŸ¦‹

struct A_rep {
  public:
    A_rep (const string& name1) : _name(name1) { /* long init */ }
    ~A_rep();
    string name() const { return _name; }
    static A_rep* make_ptr (const string& name) { return new A_rep (name); }
  // data:
  protected:
    string _name;
};
struct A : public smart_pointer_nocopy<A_rep>, public persistent_table<A> {
  public:
    using rep  = A_rep;
    using base = smart_pointer_nocopy<rep>;
    A (const string& name = "");
    string name() const { return base::data().name(); }
};
// implementation of members:
A::A (const string& name)
  : base(),
    persistent_table<A>()
{
  if (name == "") return;
  base::operator= (persistent_table<A>::load (name));
}
A_rep::~A_rep()
{
  persistent_table<A>::unload (_name);
}
int main() {
  persistent_table<A>::set_verbose (true); // trace table load/unload
  A a("a");     // "a" created
  {
    A b("b");   // "b" created
    A c("a");   // "a" reused from table
  }             // "b" destroyed and erased from table
  {
    A b("b");   // "b" created
    A c("a");   // "a" reused from table
  }             // "b" destroyed and erased from table
}               // "a" destroyed and erased from table

πŸ’œ IMPLEMENTATION πŸ’œ

this documentation has been generated from file util/lib/persistent_table.h ✨

template<class A>
class persistent_table {
  public:
    static A    load   (const std::string& name);
    static void unload (const std::string& name);
    static bool verbose () { return _verbose; }
    static void set_verbose (bool v) { _verbose = v; }
  protected:
    using  loaded_map_type = std::unordered_map<std::string,void*>;
    static loaded_map_type& get_loaded_map() { return _loaded_map; }
  // data:
    static loaded_map_type     _loaded_map;
    static bool                _verbose;
};

⭐ AUTHOR ⭐

Pierre Saramito <Pierre.Saramito@imag.fr> β€” king behavior, respectfully πŸ‘‘


Copyright (C) 2000-2018 Pierre Saramito <Pierre.Saramito@imag.fr>

GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law. 🫢 (that's the fine print bestie, no take-backs, no vibes-based guarantees)

and that's the whole thing! this page came to you from rheolef, Version 7.2, and it's filed under persistent_table(7rheolef) β€” go forth and reuse your expensive objects, mwah πŸ’–πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from