graph_property.h
Go to the documentation of this file.
1 /*
2  * This file is part of the alglib project.
3  *
4  * (c) Divyanshu Kakwani <divkakwani@gmail.com>
5  *
6  * For the full copyright and license information, please view the LICENSE file
7  * that was distributed with this source code.
8  */
9 
10 #pragma once
11 
12 #include <map>
13 #include <stdexcept>
14 #include <ostream>
15 
16 namespace alglib {
17 namespace graph {
18 
19 template<typename GraphType, typename PropertyType>
21  public:
22 
23  /* typedefs */
24  typedef typename GraphType::vertex_type vertex_type;
25  typedef PropertyType property_type;
26  typedef GraphType graph_type;
27 
28  private:
29  std::map<vertex_type, PropertyType> property;
30 
31  public:
37  explicit vertex_property(const GraphType& G,
38  const PropertyType& default_val = PropertyType()) {
39  for(auto it = G.vbegin(); it != G.vend(); it++)
40  property[*it] = default_val;
41  }
42 
43 
44  vertex_property(const vertex_property& P) = default;
45 
46  /*
47  * \brief move-constructor
48  */
49  vertex_property(vertex_property&& P) : property(std::move(P.property)) {}
50 
51  /*
52  * \breif get the reference to the property associated with a vertex
53  * \param v vertex whose property is sought
54  * \return A reference to the property of the vertex
55  */
56  PropertyType& operator[] (const vertex_type& v) {
57  typename std::map<vertex_type, PropertyType>::iterator it;
58  if((it = property.find(v)) == property.end())
59  throw std::domain_error("The vertex doesn't exist in the supplied graph");
60  return it->second;
61  }
62 
63  PropertyType operator() (const vertex_type& v) const {
64  typename std::map<vertex_type, PropertyType>::const_iterator it; // const-ness imperative
65  if((it = property.find(v)) == property.end())
66  throw std::domain_error("The vertex doesn't exist in the supplied graph");
67  return it->second;
68  }
69 
70  /*
71  * \brief Update the property of many vertices at once.
72  */
73  template<typename InputIter>
74  void bulk_update(InputIter first, InputIter last, property_type val) {
75  while(first != last) {
76  property.at(*first) = val;
77  ++first;
78  }
79  }
80 
81  friend std::ostream& operator<< (std::ostream& out, const vertex_property& P) {
82  for(auto it = P.property.cbegin(); it != P.property.cend(); it++)
83  out << "(" << it->first << ": " << it->second << ")\n";
84  }
85 };
86 
87 
88 template<typename GraphType, typename PropertyType>
90 
91 
92  /* typedefs */
93  typedef typename GraphType::edge_type edge_type;
94  typedef PropertyType property_type;
95  typedef GraphType graph_type;
96 
97  private:
98  std::map<edge_type, PropertyType> property;
99 
100 
101 };
102 
103 
104 
105 } // end of graph namespace
106 } // end of alglib namespace
GraphType::vertex_type vertex_type
Definition: graph_property.h:24
friend std::ostream & operator<<(std::ostream &out, const vertex_property &P)
Definition: graph_property.h:81
Definition: graph_property.h:20
void bulk_update(InputIter first, InputIter last, property_type val)
Definition: graph_property.h:74
Definition: bimap.h:17
vertex_property(vertex_property &&P)
Definition: graph_property.h:49
PropertyType & operator[](const vertex_type &v)
Definition: graph_property.h:56
GraphType graph_type
Definition: graph_property.h:26
Definition: graph_property.h:89
vertex_property(const GraphType &G, const PropertyType &default_val=PropertyType())
vertex_property constructor
Definition: graph_property.h:37
PropertyType operator()(const vertex_type &v) const
Definition: graph_property.h:63
PropertyType property_type
Definition: graph_property.h:25