Commit fa9122a2 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

inference: add constexpr

parent 4a8d6f3c
...@@ -131,7 +131,6 @@ public: ...@@ -131,7 +131,6 @@ public:
{ {
_empty_blocks.clear(); _empty_blocks.clear();
_candidate_blocks.clear(); _candidate_blocks.clear();
_candidate_blocks.push_back(null_group);
for (auto r : vertices_range(_bg)) for (auto r : vertices_range(_bg))
{ {
if (_wr[r] == 0) if (_wr[r] == 0)
...@@ -811,7 +810,7 @@ public: ...@@ -811,7 +810,7 @@ public:
if (_deg_corr) if (_deg_corr)
{ {
get<1>(_degs[u].front())++; get<1>(_degs[u].front())++;
if (graph_tool::is_directed(_g)) if constexpr (is_directed_::apply<g_t>::type::value)
get<0>(_degs[v].front())++; get<0>(_degs[v].front())++;
else else
get<1>(_degs[v].front())++; get<1>(_degs[v].front())++;
...@@ -828,7 +827,7 @@ public: ...@@ -828,7 +827,7 @@ public:
if (_deg_corr) if (_deg_corr)
{ {
get<1>(_degs[u].front())--; get<1>(_degs[u].front())--;
if (graph_tool::is_directed(_g)) if constexpr (is_directed_::apply<g_t>::type::value)
get<0>(_degs[v].front())--; get<0>(_degs[v].front())--;
else else
get<1>(_degs[v].front())--; get<1>(_degs[v].front())--;
...@@ -983,7 +982,7 @@ public: ...@@ -983,7 +982,7 @@ public:
if (t == u) if (t == u)
{ {
t = v; t = v;
if (!graph_tool::is_directed(_g)) if constexpr (!is_directed_::apply<g_t>::type::value)
{ {
assert(w % 2 == 0); assert(w % 2 == 0);
w /= 2; w /= 2;
...@@ -1022,7 +1021,7 @@ public: ...@@ -1022,7 +1021,7 @@ public:
} }
} }
if (graph_tool::is_directed(_g)) if constexpr (is_directed_::apply<g_t>::type::value)
{ {
ns_u.clear(); ns_u.clear();
ns_v.clear(); ns_v.clear();
...@@ -1183,7 +1182,7 @@ public: ...@@ -1183,7 +1182,7 @@ public:
size_t kout = out_degreeS()(v, _g, _eweight); size_t kout = out_degreeS()(v, _g, _eweight);
size_t kin = kout; size_t kin = kout;
if (graph_tool::is_directed(_g)) if constexpr (is_directed_::apply<g_t>::type::value)
kin = in_degreeS()(v, _g, _eweight); kin = in_degreeS()(v, _g, _eweight);
int dwr = _vweight[v]; int dwr = _vweight[v];
...@@ -1195,7 +1194,7 @@ public: ...@@ -1195,7 +1194,7 @@ public:
auto vt = [&](auto mrp, auto mrm, auto nr) auto vt = [&](auto mrp, auto mrm, auto nr)
{ {
assert(mrp >= 0 && mrm >=0 && nr >= 0); assert(mrp >= 0 && mrm >=0 && nr >= 0);
if (exact) if constexpr (exact)
return vterm_exact(mrp, mrm, nr, _deg_corr, _bg); return vterm_exact(mrp, mrm, nr, _deg_corr, _bg);
else else
return vterm(mrp, mrm, nr, _deg_corr, _bg); return vterm(mrp, mrm, nr, _deg_corr, _bg);
...@@ -1249,11 +1248,11 @@ public: ...@@ -1249,11 +1248,11 @@ public:
else else
deltap[s] += _eweight[e]; deltap[s] += _eweight[e];
} }
if (!graph_tool::is_directed(_g)) if constexpr (!is_directed_::apply<g_t>::type::value)
deltal /= 2; deltal /= 2;
vector<int> deltam(num_vertices(_bg), 0); vector<int> deltam(num_vertices(_bg), 0);
if (graph_tool::is_directed(_g)) if (is_directed_::apply<g_t>::type::value)
{ {
for (auto e : in_edges_range(v, _g)) for (auto e : in_edges_range(v, _g))
{ {
...@@ -1289,7 +1288,7 @@ public: ...@@ -1289,7 +1288,7 @@ public:
int ers = (r != null_group) ? get_beprop(r, s, _mrs, _emat) : 0; int ers = (r != null_group) ? get_beprop(r, s, _mrs, _emat) : 0;
int enrs = (nr != null_group) ? get_beprop(nr, s, _mrs, _emat) : 0; int enrs = (nr != null_group) ? get_beprop(nr, s, _mrs, _emat) : 0;
if (!graph_tool::is_directed(_g)) if (!is_directed_::apply<g_t>::type::value)
{ {
if (s != nr && s != r) if (s != nr && s != r)
{ {
...@@ -1727,10 +1726,10 @@ public: ...@@ -1727,10 +1726,10 @@ public:
// Sample node placement // Sample node placement
size_t sample_block(size_t v, double c, double d, rng_t& rng) size_t sample_block(size_t v, double c, double d, rng_t& rng)
{ {
// attempt random block // attempt new block
size_t s; size_t s;
std::bernoulli_distribution new_r(d); std::bernoulli_distribution new_r(d);
if (d > 0 && new_r(rng) && (_candidate_blocks.size() - 1 < num_vertices(_g))) if (d > 0 && new_r(rng) && (_candidate_blocks.size() < num_vertices(_g)))
{ {
if (_empty_blocks.empty()) if (_empty_blocks.empty())
add_block(); add_block();
...@@ -1752,8 +1751,8 @@ public: ...@@ -1752,8 +1751,8 @@ public:
double p_rand = 0; double p_rand = 0;
if (c > 0) if (c > 0)
{ {
size_t B = _candidate_blocks.size() - 1; size_t B = _candidate_blocks.size();
if (graph_tool::is_directed(_g)) if (is_directed_::apply<g_t>::type::value)
p_rand = c * B / double(_mrp[t] + _mrm[t] + c * B); p_rand = c * B / double(_mrp[t] + _mrm[t] + c * B);
else else
p_rand = c * B / double(_mrp[t] + c * B); p_rand = c * B / double(_mrp[t] + c * B);
...@@ -1773,16 +1772,12 @@ public: ...@@ -1773,16 +1772,12 @@ public:
} }
else else
{ {
s = uniform_sample(_candidate_blocks.begin() + 1, s = uniform_sample(_candidate_blocks, rng);
_candidate_blocks.end(),
rng);
} }
} }
else else
{ {
s = uniform_sample(_candidate_blocks.begin() + 1, s = uniform_sample(_candidate_blocks, rng);
_candidate_blocks.end(),
rng);
} }
return s; return s;
...@@ -1800,7 +1795,7 @@ public: ...@@ -1800,7 +1795,7 @@ public:
double get_move_prob(size_t v, size_t r, size_t s, double c, double d, double get_move_prob(size_t v, size_t r, size_t s, double c, double d,
bool reverse, MEntries& m_entries) bool reverse, MEntries& m_entries)
{ {
size_t B = _candidate_blocks.size() - 1; size_t B = _candidate_blocks.size();
if (reverse) if (reverse)
{ {
...@@ -1828,13 +1823,13 @@ public: ...@@ -1828,13 +1823,13 @@ public:
size_t kout = 0, kin = 0; size_t kout = 0, kin = 0;
degs_op(v, _vweight, _eweight, _degs, _g, degs_op(v, _vweight, _eweight, _degs, _g,
[&] (size_t din, size_t dout, auto c) [&] ([[maybe_unused]] size_t din, size_t dout, auto c)
{ {
kout += dout * c; kout += dout * c;
if (graph_tool::is_directed(this->_g)) if constexpr (is_directed_::apply<g_t>::type::value)
kin += din * c; kin += din * c;
}); });
if (!graph_tool::is_directed(_g)) if constexpr (!is_directed_::apply<g_t>::type::value)
kin = kout; kin = kout;
m_entries.get_mes(_emat); m_entries.get_mes(_emat);
...@@ -1855,7 +1850,7 @@ public: ...@@ -1855,7 +1850,7 @@ public:
int mst = mts; int mst = mts;
int mtm = mtp; int mtm = mtp;
if (graph_tool::is_directed(_g)) if constexpr (is_directed_::apply<g_t>::type::value)
{ {
mst = 0; mst = 0;
const auto& me = m_entries.get_me(s, t, _emat); const auto& me = m_entries.get_me(s, t, _emat);
...@@ -1868,7 +1863,7 @@ public: ...@@ -1868,7 +1863,7 @@ public:
{ {
int dts = m_entries.get_delta(t, s); int dts = m_entries.get_delta(t, s);
int dst = dts; int dst = dts;
if (graph_tool::is_directed(_g)) if constexpr (is_directed_::apply<g_t>::type::value)
dst = m_entries.get_delta(s, t); dst = m_entries.get_delta(s, t);
mts += dts; mts += dts;
...@@ -1887,7 +1882,7 @@ public: ...@@ -1887,7 +1882,7 @@ public:
} }
} }
if (graph_tool::is_directed(_g)) if constexpr (is_directed_::apply<g_t>::type::value)
{ {
p += ew * ((mts + mst + c) / (mtp + mtm + c * B)); p += ew * ((mts + mst + c) / (mtp + mtm + c * B));
} }
...@@ -1907,7 +1902,7 @@ public: ...@@ -1907,7 +1902,7 @@ public:
sum_prob(e, target(e, _g)); sum_prob(e, target(e, _g));
} }
if (graph_tool::is_directed(_g)) if constexpr (is_directed_::apply<g_t>::type::value)
{ {
for (auto e : in_edges_range(v, _g)) for (auto e : in_edges_range(v, _g))
{ {
...@@ -2131,7 +2126,7 @@ public: ...@@ -2131,7 +2126,7 @@ public:
auto& m = uc.second; auto& m = uc.second;
if (m > 1) if (m > 1)
{ {
if (u == v && !graph_tool::is_directed(_g)) if (u == v && !is_directed_::apply<g_t>::type::value)
{ {
assert(m % 2 == 0); assert(m % 2 == 0);
S += lgamma_fast(m/2 + 1) + m * log(2) / 2; S += lgamma_fast(m/2 + 1) + m * log(2) / 2;
...@@ -2181,7 +2176,7 @@ public: ...@@ -2181,7 +2176,7 @@ public:
} }
else else
{ {
if (!graph_tool::is_directed(this->_g)) if constexpr (!is_directed_::apply<g_t>::type::value)
{ {
if (Add) if (Add)
degs[1] = {kin, kout + 2}; degs[1] = {kin, kout + 2};
...@@ -2210,7 +2205,7 @@ public: ...@@ -2210,7 +2205,7 @@ public:
[&] (size_t kin, size_t kout, auto) [&] (size_t kin, size_t kout, auto)
{ {
degs[0] = {kin, kout}; degs[0] = {kin, kout};
if (!graph_tool::is_directed(this->_g)) if constexpr (!is_directed_::apply<g_t>::type::value)
{ {
if (Add) if (Add)
degs[1] = {kin, kout + 1}; degs[1] = {kin, kout + 1};
...@@ -2250,7 +2245,7 @@ public: ...@@ -2250,7 +2245,7 @@ public:
{ {
degs[2] = {kin, kout}; degs[2] = {kin, kout};
if (!graph_tool::is_directed(this->_g)) if constexpr (!is_directed_::apply<g_t>::type::value)
{ {
if (Add) if (Add)
degs[3] = {kin, kout + 1}; degs[3] = {kin, kout + 1};
......
...@@ -34,7 +34,7 @@ template <class Edge, class Graph> ...@@ -34,7 +34,7 @@ template <class Edge, class Graph>
inline typename graph_traits<Graph>::vertex_descriptor inline typename graph_traits<Graph>::vertex_descriptor
get_source(const Edge& e, const Graph &g) get_source(const Edge& e, const Graph &g)
{ {
if (graph_tool::is_directed(g)) if constexpr (is_directed_::apply<Graph>::type::value)
return source(e, g); return source(e, g);
return std::min(source(e, g), target(e, g)); return std::min(source(e, g), target(e, g));
} }
...@@ -43,7 +43,7 @@ template <class Edge, class Graph> ...@@ -43,7 +43,7 @@ template <class Edge, class Graph>
inline typename graph_traits<Graph>::vertex_descriptor inline typename graph_traits<Graph>::vertex_descriptor
get_target(const Edge& e, const Graph &g) get_target(const Edge& e, const Graph &g)
{ {
if (graph_tool::is_directed(g)) if constexpr (is_directed_::apply<Graph>::type::value)
return target(e, g); return target(e, g);
return std::max(source(e, g), target(e, g)); return std::max(source(e, g), target(e, g));
} }
...@@ -221,7 +221,7 @@ public: ...@@ -221,7 +221,7 @@ public:
// update the half-edge lists // update the half-edge lists
for (auto e : out_edges_range(v, g)) for (auto e : out_edges_range(v, g))
remove_edge(e, b, g); remove_edge(e, b, g);
if (graph_tool::is_directed(g)) if constexpr (is_directed_::apply<Graph>::type::value)
{ {
for (auto e : in_edges_range(v, g)) for (auto e : in_edges_range(v, g))
remove_edge(e, b, g); remove_edge(e, b, g);
...@@ -236,7 +236,7 @@ public: ...@@ -236,7 +236,7 @@ public:
//update the half-edge lists //update the half-edge lists
for (auto e : out_edges_range(v, g)) for (auto e : out_edges_range(v, g))
insert_edge(e, eweight[e], b, g); insert_edge(e, eweight[e], b, g);
if (graph_tool::is_directed(g)) if constexpr (is_directed_::apply<Graph>::type::value)
{ {
for (auto e : in_edges_range(v, g)) for (auto e : in_edges_range(v, g))
insert_edge(e, eweight[e], b, g); insert_edge(e, eweight[e], b, g);
......
...@@ -131,7 +131,7 @@ public: ...@@ -131,7 +131,7 @@ public:
{ {
_r_out_field.resize(B, _null); _r_out_field.resize(B, _null);
_nr_out_field.resize(B, _null); _nr_out_field.resize(B, _null);
if (is_directed_::apply<Graph>::type::value) if constexpr (is_directed_::apply<Graph>::type::value)
{ {
_r_in_field.resize(B, _null); _r_in_field.resize(B, _null);
_nr_in_field.resize(B, _null); _nr_in_field.resize(B, _null);
...@@ -146,7 +146,7 @@ public: ...@@ -146,7 +146,7 @@ public:
{ {
_r_out_field.resize(B, _null); _r_out_field.resize(B, _null);
_nr_out_field.resize(B, _null); _nr_out_field.resize(B, _null);
if (is_directed_::apply<Graph>::type::value) if constexpr (is_directed_::apply<Graph>::type::value)
{ {
_r_in_field.resize(B, _null); _r_in_field.resize(B, _null);
_nr_in_field.resize(B, _null); _nr_in_field.resize(B, _null);
...@@ -160,7 +160,7 @@ public: ...@@ -160,7 +160,7 @@ public:
size_t& get_field_rnr(size_t s, size_t t) size_t& get_field_rnr(size_t s, size_t t)
{ {
auto& out_field = First ? _r_out_field : _nr_out_field; auto& out_field = First ? _r_out_field : _nr_out_field;
if (is_directed_::apply<Graph>::type::value) if constexpr (is_directed_::apply<Graph>::type::value)
{ {
auto& in_field = (First ? _r_in_field : _nr_in_field); auto& in_field = (First ? _r_in_field : _nr_in_field);
return (Source || s == t) ? out_field[t] : in_field[s]; return (Source || s == t) ? out_field[t] : in_field[s];
...@@ -192,7 +192,7 @@ public: ...@@ -192,7 +192,7 @@ public:
f = _entries.size(); f = _entries.size();
_entries.emplace_back(s, t); _entries.emplace_back(s, t);
_delta.emplace_back(); _delta.emplace_back();
if (sizeof...(delta) > 0) if constexpr (sizeof...(delta) > 0)
_edelta.emplace_back(); _edelta.emplace_back();
} }
...@@ -315,7 +315,7 @@ void modify_entries(Vertex v, Vertex r, Vertex nr, Vprop& _b, Graph& g, ...@@ -315,7 +315,7 @@ void modify_entries(Vertex v, Vertex r, Vertex nr, Vprop& _b, Graph& g,
typedef typename graph_traits<Graph>::vertex_descriptor vertex_t; typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
auto& eself_weight = m_entries._self_eweight; auto& eself_weight = m_entries._self_eweight;
int self_weight = 0; int self_weight = 0;
if (!graph_tool::is_directed(g) && sizeof...(Eprops) > 0) if constexpr (!is_directed_::apply<Graph>::type::value && sizeof...(Eprops) > 0)
{ {
tuple_apply([&](auto&... vals) tuple_apply([&](auto&... vals)
{ {
...@@ -350,7 +350,7 @@ void modify_entries(Vertex v, Vertex r, Vertex nr, Vprop& _b, Graph& g, ...@@ -350,7 +350,7 @@ void modify_entries(Vertex v, Vertex r, Vertex nr, Vprop& _b, Graph& g,
(nr, s, ew, make_vadapter(eprops, e)...); (nr, s, ew, make_vadapter(eprops, e)...);
} }
if ((u == v || is_loop(v)) && !graph_tool::is_directed(g)) if ((u == v || is_loop(v)) && !is_directed_::apply<Graph>::type::value)
{ {
self_weight += ew; self_weight += ew;
tuple_op(eself_weight, [&](auto&& x, auto&& val){ x += val; }, tuple_op(eself_weight, [&](auto&& x, auto&& val){ x += val; },
...@@ -358,9 +358,9 @@ void modify_entries(Vertex v, Vertex r, Vertex nr, Vprop& _b, Graph& g, ...@@ -358,9 +358,9 @@ void modify_entries(Vertex v, Vertex r, Vertex nr, Vprop& _b, Graph& g,
} }
} }
if (self_weight > 0 && self_weight % 2 == 0 && !graph_tool::is_directed(g)) if (self_weight > 0 && self_weight % 2 == 0 && !is_directed_::apply<Graph>::type::value)
{ {
if (sizeof...(Eprops) > 0) if constexpr (sizeof...(Eprops) > 0)
{ {
tuple_apply([&](auto&... vals) tuple_apply([&](auto&... vals)
{ {
...@@ -378,16 +378,16 @@ void modify_entries(Vertex v, Vertex r, Vertex nr, Vprop& _b, Graph& g, ...@@ -378,16 +378,16 @@ void modify_entries(Vertex v, Vertex r, Vertex nr, Vprop& _b, Graph& g,
} }
else else
{ {
if (Add) if constexpr (Add)
m_entries.template insert_delta_rnr<false, true, false> m_entries.template insert_delta_rnr<false, true, false>
(nr, nr, self_weight / 2); (nr, nr, self_weight / 2);
if (Remove) if constexpr (Remove)
m_entries.template insert_delta_rnr<true, true, true> m_entries.template insert_delta_rnr<true, true, true>
(r, r, self_weight / 2); (r, r, self_weight / 2);
} }
} }
if (graph_tool::is_directed(g)) if constexpr (is_directed_::apply<Graph>::type::value)
{ {
for (auto e : in_edges_range(v, g)) for (auto e : in_edges_range(v, g))
{ {
...@@ -399,10 +399,10 @@ void modify_entries(Vertex v, Vertex r, Vertex nr, Vprop& _b, Graph& g, ...@@ -399,10 +399,10 @@ void modify_entries(Vertex v, Vertex r, Vertex nr, Vprop& _b, Graph& g,
vertex_t s = _b[u]; vertex_t s = _b[u];
int ew = eweights[e]; int ew = eweights[e];
if (Remove) if constexpr (Remove)
m_entries.template insert_delta_rnr<true, false, false> m_entries.template insert_delta_rnr<true, false, false>
(s, r, ew, make_vadapter(eprops, e)...); (s, r, ew, make_vadapter(eprops, e)...);
if (Add) if constexpr (Add)
{ {
if (s != r) if (s != r)
m_entries.template insert_delta_rnr<false, false, true> m_entries.template insert_delta_rnr<false, false, true>
...@@ -496,7 +496,7 @@ double entries_dS(MEntries& m_entries, Eprop& mrs, EMat& emat, BGraph& bg) ...@@ -496,7 +496,7 @@ double entries_dS(MEntries& m_entries, Eprop& mrs, EMat& emat, BGraph& bg)
if (me != emat.get_null_edge()) if (me != emat.get_null_edge())
ers = mrs[me]; ers = mrs[me];
assert(int(ers) + d >= 0); assert(int(ers) + d >= 0);
if (exact) if constexpr (exact)
dS += eterm_exact(r, s, ers + d, bg) - eterm_exact(r, s, ers, bg); dS += eterm_exact(r, s, ers + d, bg) - eterm_exact(r, s, ers, bg);
else else
dS += eterm(r, s, ers + d, bg) - eterm(r, s, ers, bg); dS += eterm(r, s, ers + d, bg) - eterm(r, s, ers, bg);
......
...@@ -56,39 +56,38 @@ struct entropy_args_t ...@@ -56,39 +56,38 @@ struct entropy_args_t
// exact microcanonical deg-corr entropy // exact microcanonical deg-corr entropy
template <class Graph> template <class Graph>
inline double eterm_exact(size_t r, size_t s, size_t mrs, const Graph& g) inline double eterm_exact(size_t r, size_t s, size_t mrs, const Graph&)
{ {
double val = lgamma_fast(mrs + 1); double val = lgamma_fast(mrs + 1);
if (graph_tool::is_directed(g) || r != s) if (is_directed_::apply<Graph>::type::value || r != s)
{ {
return -val; return -val;
} }
else else
{