RepastHPC  2.3.1
Vertex.h
1 /*
2  * Repast for High Performance Computing (Repast HPC)
3  *
4  * Copyright (c) 2010 Argonne National Laboratory
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * Neither the name of the Argonne National Laboratory nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  * Vertex.h
36  *
37  * Created on: Oct 13, 2010
38  * Author: nick
39  */
40 
41 #ifndef VERTEX_H_
42 #define VERTEX_H_
43 
44 #include "AgentId.h"
45 
46 #include <boost/unordered_map.hpp>
47 #include <boost/smart_ptr.hpp>
48 
49 namespace repast {
50 
51 template<typename V, typename E>
52 class Vertex;
53 
58 template<typename V, typename E>
59 struct HashVertex {
60  std::size_t operator()(Vertex<V, E>* vertex) const {
61  return vertex->item()->getId().hashcode();
62  }
63 };
64 
69 template<typename V, typename E>
70 struct NodeGetter: public std::unary_function<
71  typename boost::unordered_map<AgentId, Vertex<V, E>*, HashId>::value_type, V*> {
72  V* operator()(const typename boost::unordered_map<AgentId, Vertex<V, E>*, HashId>::value_type& value) const {
73  return value.second->ptr.get();
74  }
75 };
76 
84 template<typename V, typename E>
85 class Vertex {
86 
87 public:
92  typedef boost::unordered_map<Vertex<V, E>*, boost::shared_ptr<E>, HashVertex<V, E> > AdjListMap;
93  typedef typename AdjListMap::iterator AdjListMapIterator;
94 
98  enum EdgeType {
99  INCOMING, OUTGOING
100  };
101 
107  Vertex(boost::shared_ptr<V> item);
108  virtual ~Vertex() {
109  }
110 
120  virtual boost::shared_ptr<E> removeEdge(Vertex* other, EdgeType type) = 0;
121 
131  virtual boost::shared_ptr<E> findEdge(Vertex* other, EdgeType type) = 0;
132 
141  virtual void addEdge(Vertex<V, E>* other, boost::shared_ptr<E> edge, EdgeType type) = 0;
142 
148  virtual void successors(std::vector<V*>& out) = 0;
149 
155  virtual void predecessors(std::vector<V*>& out) = 0;
156 
162  virtual void adjacent(std::vector<V*>& out) = 0;
163 
171  virtual void edges(EdgeType type, std::vector<boost::shared_ptr<E> >& out) = 0;
172 
178  virtual int inDegree() = 0;
179 
185  virtual int outDegree() = 0;
186 
192  boost::shared_ptr<V> item() const {
193  return ptr;
194  }
195 
196 protected:
197  friend struct NodeGetter<V, E> ;
198  boost::shared_ptr<V> ptr;
199  boost::shared_ptr<E> removeEdge(Vertex<V, E>* other, AdjListMap* adjMap);
200  void getItems(AdjListMap *adjMap, std::vector<V*>& out);
201  void edges(AdjListMap *adjMap, std::vector<boost::shared_ptr<E> >& out);
202 };
203 
204 template<typename V, typename E>
205 Vertex<V, E>::Vertex(boost::shared_ptr<V> item) :
206  ptr(item) {
207 }
208 
209 template<typename V, typename E>
210 boost::shared_ptr<E> Vertex<V, E>::removeEdge(Vertex<V, E>* other, AdjListMap* adjMap) {
211  boost::shared_ptr<E> ret;
212  AdjListMapIterator iter = adjMap->find(other);
213  if (iter != adjMap->end()) {
214  ret = iter->second;
215  adjMap->erase(iter);
216  }
217  return ret;
218 }
219 
220 template<typename V, typename E>
221 void Vertex<V, E>::getItems(AdjListMap *adjMap, std::vector<V*>& out) {
222  const AdjListMapIterator iterEnd = adjMap->end();
223  for (AdjListMapIterator iter = adjMap->begin(); iter != iterEnd; ++iter) {
224  out.push_back(iter->first->item().get());
225  }
226 }
227 
228 template<typename V, typename E>
229 void Vertex<V, E>::edges(AdjListMap *adjMap, std::vector<boost::shared_ptr<E> >& out){
230  const AdjListMapIterator mapEnd = adjMap->end();
231  for (AdjListMapIterator iter = adjMap->begin(); iter != mapEnd; ++iter) {
232  out.push_back(iter->second);
233  }
234 }
235 
236 }
237 
238 #endif /* VERTEX_H_ */
repast::Vertex::removeEdge
virtual boost::shared_ptr< E > removeEdge(Vertex *other, EdgeType type)=0
Removes the edge of the specified type between this Vertex and the specified Vertex.
repast::Vertex::edges
virtual void edges(EdgeType type, std::vector< boost::shared_ptr< E > > &out)=0
Gets all the edges of the specified type in which this Vertex participates and return them in out.
repast::Vertex::addEdge
virtual void addEdge(Vertex< V, E > *other, boost::shared_ptr< E > edge, EdgeType type)=0
Adds an edge of the specified type between this Vertex and the specified vertex.
repast::Vertex::Vertex
Vertex(boost::shared_ptr< V > item)
Creates a Vertex that contains the specified item.
Definition: Vertex.h:205
repast::AgentId
Agent identity information.
Definition: AgentId.h:60
repast::Vertex::predecessors
virtual void predecessors(std::vector< V * > &out)=0
Gets the predecessors of this Vertex.
repast::Vertex::adjacent
virtual void adjacent(std::vector< V * > &out)=0
Gets the Vertices adjacent to this Vertex.
repast::HashVertex
Hashes a Vertex using the hashcode of the AgentId that the vertex contains.
Definition: Vertex.h:59
repast::Vertex::outDegree
virtual int outDegree()=0
Gets the out degree of this Vertex.
repast::Vertex
Used internally by repast graphs / networks to encapsulate Vertices.
Definition: Vertex.h:52
repast::Vertex::successors
virtual void successors(std::vector< V * > &out)=0
Gets the successors of this Vertex.
repast::NodeGetter
Unary function used in the transform_iterator that allows an iterator over the vertex map to return t...
Definition: Vertex.h:70
repast::Vertex::AdjListMap
boost::unordered_map< Vertex< V, E > *, boost::shared_ptr< E >, HashVertex< V, E > > AdjListMap
Typedef for the adjacency list map that contains the other Vertices that this Vertex links to.
Definition: Vertex.h:92
repast::Vertex::findEdge
virtual boost::shared_ptr< E > findEdge(Vertex *other, EdgeType type)=0
Finds the edge of the specified type between this Vertex and the specified vertex.
repast::Vertex::item
boost::shared_ptr< V > item() const
Gets the item that this Vertex contains.
Definition: Vertex.h:192
repast::Vertex::EdgeType
EdgeType
Enum the identifies whether an edge is incoming or outgoing.
Definition: Vertex.h:98
repast::HashId
operator() implementation that returns the hashcode of an AgentId.
Definition: AgentId.h:185
repast::Vertex::inDegree
virtual int inDegree()=0
Gets the in degree of this Vertex.