-
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.
- Loading branch information
Showing
3 changed files
with
34 additions
and
31 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
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 |
---|---|---|
@@ -1,41 +1,40 @@ | ||
|
||
class RNGScope { | ||
igraph_rng_t *previous; | ||
igraph_rng_t current; | ||
|
||
constexpr const static igraph_rng_type_t *default_type = &igraph_rngtype_pcg32; | ||
|
||
public: | ||
RNGScope(igraph_uint_t seed) : RNGScope(seed, &igraph_rngtype_pcg32) {} | ||
explicit RNGScope(igraph_uint_t seed) : RNGScope(seed, default_type) {} | ||
|
||
RNGScope(igraph_uint_t seed, const igraph_rng_type_t* type) { | ||
RNGScope(igraph_uint_t seed, const igraph_rng_type_t *type) { | ||
check(igraph_rng_init(¤t, type)); | ||
|
||
auto errcode = igraph_rng_seed(¤t, seed); | ||
igraph_error_t errcode = igraph_rng_seed(¤t, seed); | ||
if (errcode != IGRAPH_SUCCESS) { | ||
igraph_rng_destroy(¤t); | ||
throw Exception(errcode); | ||
} | ||
|
||
previous = *(igraph_rng_default()); | ||
// TODO for igraph 1.0 where igraph_rng_set_default() returns the previous generator. | ||
previous = igraph_rng_default(); | ||
igraph_rng_set_default(¤t); | ||
} | ||
|
||
RNGScope(const igraph_rng_type_t* type) { | ||
check(igraph_rng_init(¤t, type)); | ||
previous = *(igraph_rng_default()); | ||
igraph_rng_set_default(¤t); | ||
} | ||
explicit RNGScope(const igraph_rng_type_t *type) : RNGScope(std::random_device{}(), type) { } | ||
|
||
RNGScope() : RNGScope(&igraph_rngtype_pcg32) {} | ||
RNGScope() : RNGScope(default_type) { } | ||
|
||
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; | ||
// 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_set_default(previous); | ||
igraph_rng_destroy(¤t); | ||
} | ||
|
||
private: | ||
igraph_rng_t previous; | ||
igraph_rng_t current; | ||
}; |