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

Remove dense_hash_map/set cruft

This implements a general wrapper for dense_hash_map and dense_hash_set,
that falls back to unordered_map and unordered_set, if sparsehash is not
enabled.
parent 9774522c
......@@ -56,6 +56,7 @@ libgraph_tool_core_la_include_HEADERS = \
graph_python_interface.hh \
graph_selectors.hh \
graph_util.hh \
hash_map_wrap.hh \
histogram.hh \
mpl_nested_loop.hh \
numpy_bind.hh \
......
......@@ -20,13 +20,9 @@
#include "config.h"
#include <unordered_set>
#include "hash_map_wrap.hh"
#include <boost/mpl/if.hpp>
#ifdef HAVE_SPARSEHASH
#include SPARSEHASH_INCLUDE(dense_hash_set)
#endif
#ifndef __clang__
#include <ext/numeric>
using __gnu_cxx::power;
......@@ -42,12 +38,6 @@ namespace graph_tool
{
using namespace boost;
#ifdef HAVE_SPARSEHASH
using google::dense_hash_set;
#else
using std::unordered_set;
#endif
// calculates the number of triangles to which v belongs
template <class Graph>
pair<int,int>
......@@ -55,18 +45,11 @@ get_triangles(typename graph_traits<Graph>::vertex_descriptor v, const Graph &g)
{
typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
#ifdef HAVE_SPARSEHASH
typedef dense_hash_set<vertex_t, std::hash<vertex_t>> set_t;
#else
typedef unordered_set<vertex_t> set_t;
#endif
typedef gt_hash_set<vertex_t> set_t;
set_t neighbour_set;
#ifdef HAVE_SPARSEHASH
neighbour_set.set_empty_key(numeric_limits<vertex_t>::max());
neighbour_set.resize(out_degree(v, g));
#endif
neighbour_set.resize(out_degree(v, g));
size_t triangles = 0;
......
......@@ -603,11 +603,7 @@ struct get_deg_entropy_term_overlap
void operator()(Graph& g, Vprop b, overlap_stats_t& overlap_stats, size_t N,
double& S) const
{
#ifdef HAVE_SPARSEHASH
typedef dense_hash_map<int, int, std::hash<int>> map_t;
#else
typedef unordered_map<int, int> map_t;
#endif
typedef gt_hash_map<int, int> map_t;
map_t in_hist, out_hist;
......
......@@ -18,21 +18,16 @@
#ifndef GRAPH_BLOCKMODEL_HH
#define GRAPH_BLOCKMODEL_HH
#include "config.h"
#include <cmath>
#include <iostream>
#include <queue>
#include <boost/math/special_functions/zeta.hpp>
#include <boost/functional/hash.hpp>
#include "config.h"
#include <unordered_set>
#include <unordered_map>
#include <tuple>
#ifdef HAVE_SPARSEHASH
#include SPARSEHASH_INCLUDE(dense_hash_set)
#include SPARSEHASH_INCLUDE(dense_hash_map)
#endif
#include "hash_map_wrap.hh"
#include "../generation/sampler.hh"
#include "../generation/dynamic_sampler.hh"
......@@ -47,11 +42,6 @@ double spence(double);
namespace graph_tool
{
#ifdef HAVE_SPARSEHASH
using google::dense_hash_set;
using google::dense_hash_map;
#endif
using namespace boost;
template <class Key>
......@@ -382,11 +372,7 @@ class partition_stats_t
{
public:
#ifdef HAVE_SPARSEHASH
typedef dense_hash_map<pair<size_t,size_t>, int, std::hash<pair<size_t,size_t>>> map_t;
#else
typedef unordered_map<pair<size_t,size_t>, int> map_t;
#endif
typedef gt_hash_map<pair<size_t,size_t>, int> map_t;
partition_stats_t() : _enabled(false) {}
......@@ -396,17 +382,6 @@ public:
: _enabled(true), _N(N), _E(0), _B(B), _hist(B), _total(B), _ep(B),
_em(B), _edges_dl(edges_dl)
{
#ifdef HAVE_SPARSEHASH
for (size_t r = 0; r < B; ++r)
{
_hist[r].set_empty_key(make_pair(numeric_limits<size_t>::max(),
numeric_limits<size_t>::max()));
_hist[r].set_deleted_key(make_pair(numeric_limits<size_t>::max() - 1,
numeric_limits<size_t>::max() - 1));
}
#endif
for (auto v : vertices_range(g))
{
auto r = b[v];
......@@ -719,12 +694,8 @@ struct get_ehash_t
{
typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
typedef typename graph_traits<Graph>::edge_descriptor edge_t;
#ifdef HAVE_SPARSEHASH
typedef dense_hash_map<vertex_t, edge_t, std::hash<vertex_t>> map_t;
#else
typedef unordered_map<vertex_t, edge_t> map_t;
#endif
typedef vector<map_t> type;
typedef gt_hash_map<vertex_t, edge_t> ehash_t;
typedef std::vector<ehash_t> type;
};
};
......@@ -782,25 +753,11 @@ struct create_ehash
void operator()(Graph& g, boost::any& oemap) const
{
typedef typename get_ehash_t::apply<Graph>::type emat_t;
typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
emat_t emat(num_vertices(g));
#ifdef HAVE_SPARSEHASH
for (auto v : vertices_range(g))
{
emat[v].set_empty_key(numeric_limits<vertex_t>::max());
emat[v].set_deleted_key(numeric_limits<vertex_t>::max() - 1);
}
#endif
for (auto e : edges_range(g))
put_me(source(e, g), target(e, g), e, emat, g);
#ifdef HAVE_SPARSEHASH
for (auto v : vertices_range(g))
emat[v].resize(0);
#endif
oemap = emat;
}
};
......
......@@ -23,25 +23,15 @@
#include <queue>
#include <boost/math/special_functions/zeta.hpp>
#include <boost/functional/hash.hpp>
#include <tuple>
#include "config.h"
#include <unordered_set>
#include <unordered_map>
#include <tuple>
#ifdef HAVE_SPARSEHASH
#include SPARSEHASH_INCLUDE(dense_hash_set)
#include SPARSEHASH_INCLUDE(dense_hash_map)
#endif
#include "hash_map_wrap.hh"
namespace graph_tool
{
#ifdef HAVE_SPARSEHASH
using google::dense_hash_set;
using google::dense_hash_map;
#endif
using namespace boost;
// this will label each edge covariate in a consecutive range [0, C-1]
......@@ -104,17 +94,7 @@ struct split_graph
size_t u_r;
if (block_map.size() <= l + 1)
{
size_t n = block_map.size();
block_map.resize(l + 2);
#ifdef HAVE_SPARSEHASH
for (size_t i = n; i < block_map.size(); ++i)
{
block_map[i].set_empty_key(numeric_limits<size_t>::max());
block_map[i].set_deleted_key(numeric_limits<size_t>::max() - 1);
}
#endif
}
auto& bmap = block_map[l + 1];
auto riter = bmap.find(r);
if (riter == bmap.end())
......@@ -150,12 +130,7 @@ struct split_graph
}
};
#ifdef HAVE_SPARSEHASH
typedef vector<dense_hash_map<size_t, size_t, std::hash<size_t>>> bmap_t;
#else
typedef vector<unordered_map<size_t, size_t>> bmap_t;
#endif
typedef vector<gt_hash_map<size_t, size_t>> bmap_t;
template <class BlockState>
size_t get_block_map(BlockState& state, typename bmap_t::value_type& bmap,
......
......@@ -25,17 +25,8 @@
#include "graph_blockmodel.hh"
#ifdef HAVE_SPARSEHASH
#include SPARSEHASH_INCLUDE(dense_hash_set)
#include SPARSEHASH_INCLUDE(dense_hash_map)
#endif
namespace graph_tool
{
#ifdef HAVE_SPARSEHASH
using google::dense_hash_set;
using google::dense_hash_map;
#endif
using namespace boost;
......@@ -359,12 +350,7 @@ public:
bool is_enabled() const { return _enabled; }
#ifdef HAVE_SPARSEHASH
typedef dense_hash_map<pair<size_t, size_t>, int,
std::hash<pair<size_t, size_t>>> phist_t;
#else
typedef unordered_map<pair<size_t, size_t>, int> phist_t;
#endif
typedef gt_hash_map<pair<size_t, size_t>, int> phist_t;
const vector<phist_t>& get_parallel_bundles() { return _parallel_bundles; }
const vector<int>& get_mi() { return _mi; }
......@@ -374,11 +360,7 @@ private:
vvmap_t _half_edges; // half-edges to each node
vimap_t _node_index; // node to each half edges
#ifdef HAVE_SPARSEHASH
typedef dense_hash_map<size_t, deg_t, std::hash<size_t>> node_map_t;
#else
typedef unordered_map<size_t, deg_t> node_map_t;
#endif
typedef gt_hash_map<size_t, deg_t> node_map_t;
vector<node_map_t> _block_nodes; // nodes (and degrees) in each block
......@@ -405,47 +387,24 @@ struct overlap_partition_stats_t
typedef vector<int> bv_t;
#ifdef HAVE_SPARSEHASH
typedef dense_hash_map<bv_t, size_t, std::hash<bv_t>> bhist_t;
typedef dense_hash_map<cdeg_t, size_t, std::hash<cdeg_t>> cdeg_hist_t;
typedef gt_hash_map<bv_t, size_t> bhist_t;
typedef gt_hash_map<cdeg_t, size_t, std::hash<cdeg_t>> cdeg_hist_t;
typedef dense_hash_map<bv_t, cdeg_hist_t, std::hash<bv_t>> deg_hist_t;
typedef gt_hash_map<bv_t, cdeg_hist_t> deg_hist_t;
typedef dense_hash_map<bv_t, vector<size_t>, std::hash<bv_t>> ebhist_t;
#else
typedef unordered_map<bv_t, size_t> bhist_t;
typedef unordered_map<cdeg_t, size_t, std::hash<cdeg_t>> cdeg_hist_t;
typedef gt_hash_map<bv_t, vector<size_t>> ebhist_t;
typedef unordered_map<bv_t, cdeg_hist_t> deg_hist_t;
typedef unordered_map<bv_t, vector<size_t>> ebhist_t;
#endif
typedef unordered_map<int, int> dmap_t;
typedef gt_hash_map<int, int> dmap_t;
overlap_partition_stats_t() : _enabled(false) {}
template <class Graph, class Vprop, class Eprop>
overlap_partition_stats_t(Graph& g, Vprop b, overlap_stats_t& overlap_stats,
Eprop eweight, size_t N, size_t B, bool edges_dl)
: _enabled(true), _N(N), _B(B), _D(0),
_dhist(B + 1), _r_count(B), _bhist(N), _emhist(B), _ephist(B),
_embhist(N), _epbhist(N), _deg_hist(N), _bvs(N), _nbvs(N), _degs(N),
_ndegs(N), _deg_delta(B), _edges_dl(edges_dl)
: _enabled(true), _N(N), _B(B), _D(0), _dhist(B + 1), _r_count(B),
_bvs(N), _nbvs(N), _degs(N), _ndegs(N), _deg_delta(B),
_edges_dl(edges_dl)
{
#ifdef HAVE_SPARSEHASH
bv_t empty = {numeric_limits<int>::max()};
bv_t deleted = {numeric_limits<int>::max() - 1};
_bhist.set_empty_key(empty);
_bhist.set_deleted_key(deleted);
_deg_hist.set_empty_key(empty);
_deg_hist.set_deleted_key(deleted);
_embhist.set_empty_key(empty);
_embhist.set_deleted_key(deleted);
_epbhist.set_empty_key(empty);
_epbhist.set_deleted_key(deleted);
#endif
dmap_t in_hist, out_hist;
for (size_t v = 0; v < N; ++v)
{
......@@ -473,18 +432,6 @@ struct overlap_partition_stats_t
_ndegs[v].reserve(cdeg.size() * 2);
auto & cdh = _deg_hist[bv];
#ifdef HAVE_SPARSEHASH
if (cdh.empty())
{
cdeg_t empty = {deg_t(numeric_limits<int>::max(),
numeric_limits<int>::max())};
cdeg_t deleted = {deg_t(numeric_limits<int>::max() - 1,
numeric_limits<int>::max() - 1)};
cdh.set_empty_key(empty);
cdh.set_deleted_key(deleted);
}
#endif
cdh[cdeg]++;
size_t d = bv.size();
......@@ -1017,15 +964,8 @@ struct overlap_partition_stats_t
S_b += lbinom_fast(_r_count[s] + _ephist[s] - 1, _ephist[s]);
}
#ifdef HAVE_SPARSEHASH
dense_hash_map<size_t, pair<int, int>, std::hash<size_t>> deg_delta;
dense_hash_map<size_t, int, std::hash<size_t>> r_count_delta;
deg_delta.set_empty_key(numeric_limits<size_t>::max());
r_count_delta.set_empty_key(numeric_limits<size_t>::max());
#else
unordered_map<size_t, pair<int, int>> deg_delta;
unordered_map<size_t, int> r_count_delta;
#endif
gt_hash_map<size_t, pair<int, int>> deg_delta;
gt_hash_map<size_t, int> r_count_delta;
if (bv != n_bv)
{
......@@ -1171,17 +1111,6 @@ struct overlap_partition_stats_t
_ephist[r] -= kout;
auto& hist = _deg_hist[n_bv];
#ifdef HAVE_SPARSEHASH
if (hist.empty())
{
cdeg_t empty = {deg_t(numeric_limits<int>::max(),
numeric_limits<int>::max())};
cdeg_t deleted = {deg_t(numeric_limits<int>::max() - 1,
numeric_limits<int>::max() - 1)};
hist.set_empty_key(empty);
hist.set_deleted_key(deleted);
}
#endif
hist[n_deg] += 1;
auto& n_bmh = _embhist[n_bv];
auto& n_bph = _epbhist[n_bv];
......
......@@ -18,11 +18,7 @@
#ifndef GRAPH_COMMUNITY_NETWORK_HH
#define GRAPH_COMMUNITY_NETWORK_HH
#include <unordered_map>
#ifdef HAVE_SPARSEHASH
#include SPARSEHASH_INCLUDE(dense_hash_map)
#endif
#include "hash_map_wrap.hh"
#include <iostream>
#include <iomanip>
......@@ -50,17 +46,12 @@ struct get_community_network_vertices
typedef typename boost::property_traits<CommunityMap>::value_type
s_type;
#ifdef HAVE_SPARSEHASH
google::dense_hash_map<s_type, vertex_t, std::hash<s_type> > comms;
comms.set_empty_key(numeric_limits<s_type>::max());
#else
std::unordered_map<s_type, vertex_t, std::hash<s_type> > comms;
#endif
gt_hash_map<s_type, vertex_t, std::hash<s_type> > comms;
// create vertices
typename graph_traits<Graph>::vertex_iterator vi, vi_end;
for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
for (auto vi : vertices_range(g))
{
s_type s = get(s_map, *vi);
s_type s = get(s_map, vi);
typeof(comms.begin()) iter = comms.find(s);
cvertex_t v;
if (iter == comms.end())
......@@ -75,7 +66,7 @@ struct get_community_network_vertices
{
v = iter->second;
}
put(vertex_count, v, get(vertex_count, v) + get(vweight, *vi));
put(vertex_count, v, get(vertex_count, v) + get(vweight, vi));
}
}
......@@ -116,37 +107,22 @@ struct get_community_network_edges
typedef typename boost::property_traits<CommunityMap>::value_type
s_type;
#ifdef HAVE_SPARSEHASH
typedef google::dense_hash_map<s_type, vertex_t, std::hash<s_type> > comms_t;
comms_t comms(num_vertices(cg));
comms.set_empty_key(numeric_limits<s_type>::max());
typedef google::dense_hash_map<cvertex_t, cedge_t, std::hash<cvertex_t>> ecomms_t;
#else
typedef std::unordered_map<s_type, vertex_t, std::hash<s_type> > comms_t;
comms_t comms(num_vertices(cg));
typedef std::unordered_map<cvertex_t, cedge_t> ecomms_t;
#endif
typedef gt_hash_map<s_type, vertex_t, std::hash<s_type> > comms_t;
comms_t comms;
typedef gt_hash_map<cvertex_t, cedge_t> ecomms_t;
auto index_map = get(vertex_index_t(), cg);
unchecked_vector_property_map<ecomms_t, decltype(index_map)>
comm_edges(index_map, num_vertices(cg));
typename graph_traits<CommunityGraph>::vertex_iterator v, v_end;
for (tie(v, v_end) = vertices(cg); v != v_end; ++v)
{
comms[cs_map[*v]] = *v;
#ifdef HAVE_SPARSEHASH
comm_edges[*v].set_empty_key(numeric_limits<cvertex_t>::max());
#endif
}
for (auto v : vertices_range(cg))
comms[cs_map[v]] = v;
// create edges
typename graph_traits<Graph>::edge_iterator e, e_end;
for (tie(e, e_end) = edges(g); e != e_end; ++e)
for (auto e : edges_range(g))
{
cvertex_t cs = comms[get(s_map, source(*e, g))];
cvertex_t ct = comms[get(s_map, target(*e, g))];
cvertex_t cs = comms[get(s_map, source(e, g))];
cvertex_t ct = comms[get(s_map, target(e, g))];
if (ct == cs && !self_loops)
continue;
cedge_t ce;
......@@ -157,7 +133,7 @@ struct get_community_network_edges
}
else
{
typeof(comm_edges[cs].begin()) iter = comm_edges[cs].find(ct);
auto iter = comm_edges[cs].find(ct);
if (iter != comm_edges[cs].end())
{
ce = iter->second;
......@@ -184,7 +160,7 @@ struct get_community_network_edges
}
}
}
put(edge_count, ce, get(edge_count, ce) + get(eweight, *e));
put(edge_count, ce, get(edge_count, ce) + get(eweight, e));
}
}
};
......@@ -241,9 +217,8 @@ struct get_weighted_vertex_property
void operator()(const Graph& g, VertexWeightMap vweight, Vprop vprop,
Vprop temp) const
{
typename graph_traits<Graph>::vertex_iterator vi, vi_end;
for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
temp[*vi] = vprop[*vi] * get(vweight, *vi);
for (auto vi : vertices_range(g))
temp[vi] = vprop[vi] * get(vweight, vi);
}
};
......@@ -259,21 +234,15 @@ struct get_vertex_community_property_sum
typedef typename boost::property_traits<CommunityMap>::value_type
s_type;
#ifdef HAVE_SPARSEHASH
google::dense_hash_map<s_type, vertex_t, std::hash<s_type> > comms(num_vertices(cg));
comms.set_empty_key(numeric_limits<s_type>::max());
#else
std::unordered_map<s_type, vertex_t, std::hash<s_type> > comms(num_vertices(cg));
#endif
typename graph_traits<CommunityGraph>::vertex_iterator v, v_end;
for (tie(v, v_end) = vertices(cg); v != v_end; ++v)
comms[cs_map[*v]] = *v;
typename graph_traits<Graph>::vertex_iterator vi, vi_end;
for (tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
gt_hash_map<s_type, vertex_t> comms;
for (auto v : vertices_range(cg))
comms[cs_map[v]] = v;
for (auto v : vertices_range(g))
{
s_type s = get(s_map, *vi);
cvprop[comms[s]] += vprop[*vi];
s_type s = get(s_map, v);
cvprop[comms[s]] += vprop[v];
}
}
};
......@@ -284,9 +253,8 @@ struct get_weighted_edge_property
void operator()(const Graph& g, EdgeWeightMap eweight, Eprop eprop,
Eprop temp) const
{
typename graph_traits<Graph>::edge_iterator ei, ei_end;
for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
temp[*ei] = eprop[*ei] * get(eweight, *ei);
for (auto e : edges_range(g))
temp[e] = eprop[e] * get(eweight, e);
}
};
......@@ -306,43 +274,27 @@ struct get_edge_community_property_sum
typedef typename boost::property_traits<CommunityMap>::value_type
s_type;
#ifdef HAVE_SPARSEHASH
google::dense_hash_map<s_type, vertex_t, std::hash<s_type> > comms(num_vertices(cg));
comms.set_empty_key(numeric_limits<s_type>::max());
#else
std::unordered_map<s_type, vertex_t, std::hash<s_type> > comms(num_vertices(cg));
#endif
typename graph_traits<CommunityGraph>::vertex_iterator v, v_end;
for (tie(v, v_end) = vertices(cg); v != v_end; ++v)
comms[cs_map[*v]] = *v;
#ifdef HAVE_SPARSEHASH
google::dense_hash_map<pair<size_t, size_t>, cedge_t,
std::hash<pair<size_t, size_t> > >
comm_edges(num_vertices(cg));
comm_edges.set_empty_key(make_pair(numeric_limits<size_t>::max(),
numeric_limits<size_t>::max()));
#else
std::unordered_map<pair<size_t, size_t>, cedge_t>
comm_edges(num_vertices(cg));
#endif
typename graph_traits<CommunityGraph>::edge_iterator e, e_end;
for (tie(e, e_end) = edges(cg); e != e_end; ++e)
gt_hash_map<s_type, vertex_t> comms;
for (auto v : vertices_range(cg))
comms[cs_map[v]] = v;
gt_hash_map<pair<size_t, size_t>, cedge_t> comm_edges;
for (auto e : edges_range(cg))
{
cvertex_t cs = comms[get(cs_map, source(*e, cg))];
cvertex_t ct = comms[get(cs_map, target(*e, cg))];
comm_edges[make_pair(cs, ct)] = *e;
cvertex_t cs = comms[get(cs_map, source(e, cg))];
cvertex_t ct = comms[get(cs_map, target(e, cg))];
comm_edges[make_pair(cs, ct)] = e;
}
typename graph_traits<Graph>::edge_iterator ei, ei_end;
for (tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
for (auto e : edges_range(g))