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 protected:
143 
144  void validateDimensions(const std::vector<T>& weights) const
145  {
146  if(weights.size() != Dim)
147  {
148  std::stringstream ss;
149  ss << "graph_analysis::NWeighted: invalid number of weights: expected " << Dim << " got " << weights.size();
150  throw std::invalid_argument(ss.str());
151  }
152  }
153 
154  virtual GraphElementType* getClone() const { return new NWeighted(*this); }
155 
156  std::vector<T> mWeights;
157 };
158 
159 } // end namespace graph_analysis
160 #endif // GRAPH_ANALYSIS_NWEIGHTED_VERTEX_HPP
NWeighted(const std::vector< T > &weights=std::vector< T >(Dim))
Definition: NWeighted.hpp:47
virtual ~NWeighted()
Definition: NWeighted.hpp:59
NWeighted(const T &weight)
Definition: NWeighted.hpp:36
std::vector< T > getWeights() const
Definition: NWeighted.hpp:94
std::string serializeWeights()
Definition: NWeighted.hpp:126
void setWeights(const std::vector< T > &weights)
Definition: NWeighted.hpp:88
const T & getWeight(size_t index=0) const
Definition: NWeighted.hpp:116
shared_ptr< NWeighted > Ptr
Definition: NWeighted.hpp:29
T value_t
Definition: NWeighted.hpp:30
Main namespace of the graph analysis library.
Definition: BFS.cpp:3
NWeighted is a template to associate N-weights with either a vertex or an edge, i.e., adds a vector of weights.
Definition: NWeighted.hpp:26
void setWeight(T value, size_t index=0)
Definition: NWeighted.hpp:99
std::vector< T > mWeights
Definition: NWeighted.hpp:156
virtual GraphElementType * getClone() const
Definition: NWeighted.hpp:154
void deserializeWeights(const std::string &s)
Definition: NWeighted.hpp:134
virtual std::string getClassName() const
Definition: NWeighted.hpp:63
void validateDimensions(const std::vector< T > &weights) const
Definition: NWeighted.hpp:144
virtual std::string toString() const
Definition: NWeighted.hpp:68
NWeighted(const NWeighted &other)
Definition: NWeighted.hpp:54