Created
August 19, 2013 09:12
-
-
Save anonymous/6267170 to your computer and use it in GitHub Desktop.
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
#include <boost/operators.hpp> | |
#include <iostream> | |
#include <cmath> | |
#include <array> | |
#include <limits> | |
#include <vector> | |
#include <boost/graph/adjacency_list.hpp> | |
#include <boost/graph/graph_traits.hpp> | |
#include <boost/graph/dijkstra_shortest_paths.hpp> | |
template< int dim > | |
struct MyWeight : | |
boost::addable<MyWeight<dim> >, boost::subtractable<MyWeight<dim> >, | |
boost::less_than_comparable<MyWeight<dim> >, boost::equality_comparable<MyWeight<dim> > | |
{ | |
typedef std::array<double, dim> values_t; | |
values_t values; | |
struct Inf : MyWeight< dim > | |
{ | |
Inf() | |
{ | |
// greater than most weights, TODO: something better here. | |
for(size_t i = 0; i < dim; ++i) | |
values[i] = 9999999.; | |
} | |
}; | |
struct Zero : MyWeight< dim > | |
{ | |
Zero() | |
{ | |
for(size_t i = 0; i < dim; ++i) | |
values[i] = 0.0; | |
} | |
}; | |
MyWeight(const values_t& val) : values(val) | |
{ | |
} | |
MyWeight() = default; | |
MyWeight operator+() const | |
{ | |
return *this; | |
} | |
MyWeight operator-() const | |
{ | |
values_t val; | |
for(int32_t i = 0; i < dim; ++i) | |
val = -this->values[i]; | |
return MyWeight(val); | |
} | |
MyWeight& operator+=(const MyWeight& rhs) | |
{ | |
for(int32_t i = 0; i < dim; ++i) | |
this->values[i] += rhs.values[i]; | |
return *this; | |
} | |
MyWeight& operator-=(const MyWeight& rhs) | |
{ | |
for(int32_t i = 0; i < dim; ++i) | |
this->values[i] -= rhs.values[i]; | |
return *this; | |
} | |
virtual double eval() const | |
{ | |
double sum = 0; | |
for(const auto& v: values) | |
sum += v*v; | |
return std::sqrt(sum); | |
} | |
}; | |
template< int dim > | |
bool operator==(const MyWeight<dim>& a, const MyWeight<dim>& b) | |
{ | |
return a.eval() == b.eval(); | |
} | |
template< int dim > | |
bool operator<(const MyWeight<dim>& a, const MyWeight<dim>& b) | |
{ | |
return a.eval() < b.eval(); | |
} | |
int main( int argc, char** argv ) | |
{ | |
typedef MyWeight<2> weight_t; | |
typedef boost::adjacency_list< boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, boost::property < boost::edge_weight_t, weight_t > > graph_t; | |
graph_t G(5); | |
weight_t w(weight_t::values_t({{1, 2}})); | |
boost::add_edge(0, 1, w, G); | |
boost::add_edge(1, 2, w, G); | |
boost::add_edge(2, 3, w, G); | |
std::vector< int32_t > predecessors( boost::num_vertices(G) ); | |
std::vector< weight_t > distances( boost::num_vertices(G) ); | |
weight_t weight_inf = weight_t::Inf(); | |
weight_t weight_zero = weight_t::Zero(); | |
std::cout << w.eval() << std::endl; | |
std::cout << weight_zero.eval() << std::endl; | |
std::cout << weight_inf.eval() << std::endl; | |
assert( w < weight_inf ); | |
assert( w > weight_zero ); | |
boost::dijkstra_shortest_paths( G, 1, boost::predecessor_map(&predecessors[0]) | |
.distance_map(&distances[0]) | |
.distance_inf(weight_inf) | |
.distance_zero(weight_zero) | |
); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment