raiigraph
C++ RAII for igraph data structures
|
This library provides some RAII wrappers around the data structures used by the igraph C library. The aim is to make igraph a little bit more ergonomic for use in existing C++ codebases. The code here was originally developed as part of the libscran library, but has been factored out for potential re-use.
Currently, we implement wrappers for the following igraph structures:
igraph structure | raiigraph wrapper |
---|---|
igraph_vector_t | raiigraph::RealVector |
igraph_vector_int_t | raiigraph::IntVector |
igraph_vector_bool_t | raiigraph::BoolVector |
igraph_matrix_t | raiigraph::RealMatrix |
igraph_matrix_int_t | raiigraph::IntMatrix |
igraph_matrix_bool_t | raiigraph::BoolMatrix |
igraph_t | raiigraph::Graph |
Please make a PR if your favorite structure is missing.
All wrappers provide the usual RAII behavior, greatly simplifying memory management when used in existing code.
We also follow the rule of 5 convention, so copy/move assignment works as expected:
Some of the wrappers also provide convenience methods to query the underlying data structure without having to extract the raw pointer:
Check out the reference documentation for more details.
The Vector
classes provide the same methods as std::vector
and can be used as a drop-in replacement in various standard library functions:
The Matrix
classes provide views into the rows and columns that have STL-like behavior.
The RNGScope
class allows users to easily set the igraph RNG for reproducible execution. This uses RAII to restore the previous RNG when the instance goes out of scope:
This class can be used in a nested manner for easy composition:
If you're using CMake, you just need to add something like this to your CMakeLists.txt
:
Then you can link to raiigraph to make the headers available during compilation:
This assumes that igraph has already been installed and can be found by find_package()
. Users can use the RAIIGRAPH_FIND_IGRAPH
option to disable igraph discovery (e.g., to supply a custom igraph installation), in which case they will need to link to igraph manually in their target_link_libraries()
call.
To install the library, use:
Again, this assumes that igraph has already been installed and can be found by find_package()
, unless otherwise specified via RAIIGRAPH_FIND_IGRAPH
.
If you're not using CMake, the simple approach is to just copy the files in include/
- either directly or with Git submodules - and include their path during compilation with, e.g., GCC's -I
. This means that you are responsible for manually linking to igraph.