-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from LTLA/rng_scope
Implement the RNGScope class for RAII-based RNG control.
- Loading branch information
Showing
4 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <iostream> | ||
#include <igraph.hpp> | ||
#include "ex_vector_print.hpp" | ||
|
||
int main() { | ||
// Check that we can influence the RNG. | ||
{ | ||
ig::RNGScope filled(1000); | ||
std::cout << "New RNG scope:" << std::endl; | ||
std::cout << " Draw from [0, 100]: " << igraph_rng_get_integer(igraph_rng_default(), 0, 100) << std::endl; | ||
std::cout << " Draw from [0, 100]: " << igraph_rng_get_integer(igraph_rng_default(), 0, 100) << std::endl; | ||
} | ||
|
||
// Check that the destructor correctly unsets the RNG. | ||
{ | ||
ig::RNGScope filled(1000); | ||
std::cout << "New RNG scope:" << std::endl; | ||
std::cout << " Draw from [0, 100]: " << igraph_rng_get_integer(igraph_rng_default(), 0, 100) << std::endl; | ||
|
||
{ | ||
ig::RNGScope nested; // uses a default seed and type. | ||
std::cout << " New RNG scope:" << std::endl; | ||
std::cout << " Draw from [0, 100]: " << igraph_rng_get_integer(igraph_rng_default(), 0, 100) << std::endl; | ||
std::cout << " Draw from [0, 100]: " << igraph_rng_get_integer(igraph_rng_default(), 0, 100) << std::endl; | ||
} | ||
|
||
std::cout << " Draw from [0, 100]: " << igraph_rng_get_integer(igraph_rng_default(), 0, 100) << std::endl; | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class RNGScope { | ||
public: | ||
RNGScope(igraph_uint_t seed) : RNGScope(seed, &igraph_rngtype_pcg32) {} | ||
|
||
RNGScope(igraph_uint_t seed, const igraph_rng_type_t* type) { | ||
igCheck(igraph_rng_init(¤t, type)); | ||
|
||
auto errcode = igraph_rng_seed(¤t, seed); | ||
if (errcode != IGRAPH_SUCCESS) { | ||
igraph_rng_destroy(¤t); | ||
throw igException(errcode); | ||
} | ||
|
||
previous = *(igraph_rng_default()); | ||
igraph_rng_set_default(¤t); | ||
} | ||
|
||
RNGScope(const igraph_rng_type_t* type) { | ||
igCheck(igraph_rng_init(¤t, type)); | ||
previous = *(igraph_rng_default()); | ||
igraph_rng_set_default(¤t); | ||
} | ||
|
||
RNGScope() : RNGScope(&igraph_rngtype_pcg32) {} | ||
|
||
public: | ||
// We shouldn't be copying or moving an RNGscope object, as this defeats the logic provided by scoping. | ||
RNGScope(const RNGScope&) = delete; | ||
RNGScope& operator=(const RNGScope&) = delete; | ||
RNGScope(RNGScope&&) = delete; | ||
RNGScope& operator=(RNGScope&&) = delete; | ||
|
||
~RNGScope() { | ||
igraph_rng_set_default(&previous); | ||
igraph_rng_destroy(¤t); | ||
} | ||
|
||
private: | ||
igraph_rng_t previous; | ||
igraph_rng_t current; | ||
}; |