graph_analysis
NWeighted.hpp
Go to the documentation of this file.
1 #ifndef GRAPH_ANALYSIS_NWEIGHTED_HPP
2 #define GRAPH_ANALYSIS_NWEIGHTED_HPP
3 
4 #include <sstream>
5 #include <stdexcept>
6 #include <vector>
7 #include <iomanip>
8 
9 #include <boost/serialization/vector.hpp>
10 #include <boost/archive/text_iarchive.hpp>
11 #include <boost/archive/text_oarchive.hpp>
12 
13 #include "SharedPtr.hpp"
14 
15 namespace graph_analysis {
16 
25 template<typename T, size_t Dim, typename GraphElementType>
26 class NWeighted : public GraphElementType
27 {
28 public:
29  typedef shared_ptr< NWeighted > Ptr;
30  typedef T value_t;
31 
36  NWeighted(const T& weight)
37  : GraphElementType()
38  , mWeights(Dim)
39  {
40  setWeight(weight, 0);
42  }
43 
47  NWeighted(const std::vector<T>& weights = std::vector<T>(Dim))
48  : GraphElementType()
49  , mWeights(weights)
50  {
52  }
53 
54  NWeighted(const NWeighted& other)
55  {
56  mWeights = other.mWeights;
57  }
58 
59  virtual ~NWeighted() {}
60 
61  // Get class name
62  // \return class name
63  virtual std::string getClassName() const { return "graph_analysis::NWeighted"; }
64 
68  virtual std::string toString() const
69  {
70  std::stringstream ss;
71  ss << GraphElementType::toString();
72  ss << ": " << "[";
73  typename std::vector<T>::const_iterator cit = mWeights.begin();
74  for(;; ++cit)
75  {
76  ss << *cit;
77  if(cit + 1 == mWeights.end())
78  {
79  break;
80  } else {
81  ss << ",";
82  }
83  }
84  ss << "]";
85  return ss.str();
86  }
87 
88  void setWeights(const std::vector<T>& weights)
89  {
90  validateDimensions(weights);
91  mWeights = weights;
92  }
93 
94  std::vector<T> getWeights() const
95  {
96  return mWeights;
97  }
98 
99  void setWeight(T value, size_t index = 0)
100  {
101  try {
102  mWeights.at(index) = value;
103  } catch(const std::out_of_range& e)
104  {
105  std::stringstream ss;
106  ss << "graph_analysis::NWeighted::setWeights: ";
107  ss << " dim: is " << Dim << ", tried access at: " << index;
108  ss << ": " << std::string(e.what());
109  throw std::out_of_range(ss.str());
110  }
111  }
112 
116  const T& getWeight(size_t index = 0) const
117  {
118  try {
119  return mWeights.at(index);
120  } catch(const std::out_of_range& e)
121  {
122  throw std::out_of_range("graph_analysis::NWeighted::getWeight: " + std::string(e.what()) );
123  }
124  }
125 
126  std::string serializeWeights()
127  {
128  std::stringstream ss;
129  boost::archive::text_oarchive oarch(ss);
130  oarch << mWeights;
131  return ss.str();
132  }
133 
134  void deserializeWeights(const std::string& s)
135  {
136  std::stringstream ss;
137  ss << s;
138  boost::archive::text_iarchive iarch(ss);
139  iarch >> mWeights;
140  }
141 
142  std::string stringifyWeights() const
143  {
144  std::stringstream ss;
145  ss << "[";
146  bool first = true;
147  for(const T& w : mWeights)
148  {
149  if(first)
150  {
151  first = false;
152  } else {
153  ss << ",";
154  }
155  ss << w;
156  }
157  ss << "]";
158  return ss.str();
159  }
160 
161 protected:
162 
163  void validateDimensions(const std::vector<T>& weights) const
164  {
165  if(weights.size() != Dim)
166  {
167  std::stringstream ss;
168  ss << "graph_analysis::NWeighted: invalid number of weights: expected " << Dim << " got " << weights.size();
169  throw std::invalid_argument(ss.str());
170  }
171  }
172 
173  virtual GraphElementType* getClone() const { return new NWeighted(*this); }
174 
175  std::vector<T> mWeights;
176 };
177 
178 } // end namespace graph_analysis
179 #endif // GRAPH_ANALYSIS_NWEIGHTED_VERTEX_HPP
graph_analysis::NWeighted::value_t
T value_t
Definition: NWeighted.hpp:30
graph_analysis::NWeighted::setWeights
void setWeights(const std::vector< T > &weights)
Definition: NWeighted.hpp:88
SharedPtr.hpp
graph_analysis::NWeighted::serializeWeights
std::string serializeWeights()
Definition: NWeighted.hpp:126
graph_analysis::NWeighted::getClone
virtual GraphElementType * getClone() const
Definition: NWeighted.hpp:173
graph_analysis::NWeighted::NWeighted
NWeighted(const NWeighted &other)
Definition: NWeighted.hpp:54
graph_analysis::NWeighted::deserializeWeights
void deserializeWeights(const std::string &s)
Definition: NWeighted.hpp:134
graph_analysis::NWeighted::toString
virtual std::string toString() const
Definition: NWeighted.hpp:68
graph_analysis::NWeighted::mWeights
std::vector< T > mWeights
Definition: NWeighted.hpp:175
graph_analysis
Main namespace of the graph analysis library.
Definition: BFS.cpp:3
graph_analysis::NWeighted::NWeighted
NWeighted(const std::vector< T > &weights=std::vector< T >(Dim))
Definition: NWeighted.hpp:47
graph_analysis::NWeighted::setWeight
void setWeight(T value, size_t index=0)
Definition: NWeighted.hpp:99
graph_analysis::NWeighted::NWeighted
NWeighted(const T &weight)
Definition: NWeighted.hpp:36
graph_analysis::NWeighted::validateDimensions
void validateDimensions(const std::vector< T > &weights) const
Definition: NWeighted.hpp:163
graph_analysis::NWeighted::Ptr
shared_ptr< NWeighted > Ptr
Definition: NWeighted.hpp:29
graph_analysis::NWeighted::getClassName
virtual std::string getClassName() const
Definition: NWeighted.hpp:63
graph_analysis::NWeighted::~NWeighted
virtual ~NWeighted()
Definition: NWeighted.hpp:59
graph_analysis::NWeighted::getWeight
const T & getWeight(size_t index=0) const
Definition: NWeighted.hpp:116
graph_analysis::NWeighted::stringifyWeights
std::string stringifyWeights() const
Definition: NWeighted.hpp:142
graph_analysis::NWeighted
NWeighted is a template to associate N-weights with either a vertex or an edge, i....
Definition: NWeighted.hpp:26
graph_analysis::NWeighted::getWeights
std::vector< T > getWeights() const
Definition: NWeighted.hpp:94