-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.cpp
59 lines (52 loc) · 1.4 KB
/
utility.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <gsl/gsl_errno.h>
#include <sstream>
#include "bct.h"
/*
* Catches GSL errors and throws BCT exceptions.
*/
void BCT_NAMESPACE::gsl_error_handler(const char* reason, const char* file, int line, int gsl_errno) {
std::stringstream what;
what << reason << " in " << file << ", line " << line << ".";
throw bct_exception(what.str());
}
/*
* Overloaded convenience function for freeing GSL vectors and matrices.
*/
void BCT_NAMESPACE::gsl_free(VECTOR_T* v) { VECTOR_ID(free)(v); }
void BCT_NAMESPACE::gsl_free(MATRIX_T* m) { MATRIX_ID(free)(m); }
void BCT_NAMESPACE::gsl_free(std::vector<MATRIX_T*>& m) {
for (int i = 0; i < (int)m.size(); i++) {
if (m[i] != NULL) {
MATRIX_ID(free)(m[i]);
m[i] = NULL;
}
}
}
void BCT_NAMESPACE::gsl_free(gsl_permutation* p) { gsl_permutation_free(p); }
/*
* Initializes the BCT library for external use.
*/
void BCT_NAMESPACE::init() {
gsl_set_error_handler(gsl_error_handler);
}
/*
* Returns the number of edges in a directed graph.
*/
int BCT_NAMESPACE::number_of_edges_dir(const MATRIX_T* m) {
return nnz(m);
}
/*
* Returns the number of edges in an undirected graph.
*/
int BCT_NAMESPACE::number_of_edges_und(const MATRIX_T* m) {
MATRIX_T* triu_m = triu(m);
int ret = nnz(triu_m);
MATRIX_ID(free)(triu_m);
return ret;
}
/*
* Returns the number of nodes in a graph.
*/
int BCT_NAMESPACE::number_of_nodes(const MATRIX_T* m) {
return (int)m->size1;
}