RepastHPC  2.3.1
DirectedVertex.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  * DirectedVertex.h
36  *
37  * Created on: Oct 13, 2010
38  * Author: nick
39  */
40 
41 #ifndef DIRECTEDVERTEX_H_
42 #define DIRECTEDVERTEX_H_
43 
44 #include "Vertex.h"
45 
46 namespace repast {
47 
55 template<typename V, typename E>
56 class DirectedVertex : public Vertex<V, E> {
57 
58 private:
59  typedef typename Vertex<V,E>::AdjListMap AdjListMap;
60  typedef typename Vertex<V,E>::AdjListMap::iterator AdjListMapIterator;
61  typedef typename Vertex<V,E>::EdgeType EdgeType;
62 
63  AdjListMap* incoming, *outgoing;
64 
65 public:
69  DirectedVertex(boost::shared_ptr<V> item);
70  virtual ~DirectedVertex();
71 
72  // doc inherited from Vertex
73  virtual boost::shared_ptr<E> removeEdge(Vertex<V,E>* other, EdgeType type);
74 
75  // doc inherited from Vertex
76  virtual boost::shared_ptr<E> findEdge(Vertex<V,E>* other, EdgeType type);
77 
78  // doc inherited from Vertex
79  virtual void addEdge(Vertex<V,E>* other, boost::shared_ptr<E> edge, EdgeType type);
80 
81  // doc inherited from Vertex
82  virtual void successors(std::vector<V*>& out);
83 
84  // doc inherited from Vertex
85  virtual void predecessors(std::vector<V*>& out);
86 
87  // doc inherited from Vertex
88  virtual void adjacent(std::vector<V*>& out);
89 
90  // doc inherited from Vertex
91  virtual void edges(EdgeType type , std::vector<boost::shared_ptr<E> >& out);
92 
93  // doc inherited from Vertex
94  int inDegree();
95 
96  // doc inherited from Vertex
97  int outDegree();
98 };
99 
100 template<typename V, typename E>
101 DirectedVertex<V,E>::DirectedVertex(boost::shared_ptr<V> item) : Vertex<V,E>(item) {
102  incoming = new AdjListMap();
103  outgoing = new AdjListMap();
104 }
105 
106 template<typename V, typename E>
108  delete incoming;
109  delete outgoing;
110 }
111 
112 template<typename V, typename E>
113 boost::shared_ptr<E> DirectedVertex<V,E>::removeEdge(Vertex<V,E>* other, EdgeType type) {
114  return Vertex<V,E>::removeEdge(other, (type == Vertex<V,E>::INCOMING ? incoming : outgoing));
115 }
116 
117 template<typename V, typename E>
118 boost::shared_ptr<E> DirectedVertex<V,E>::findEdge(Vertex<V,E>* other, EdgeType type) {
119  boost::shared_ptr<E> ret;
120  AdjListMap* adjMap = (type == Vertex<V,E>::INCOMING ? incoming : outgoing);
121  AdjListMapIterator iter = adjMap->find(other);
122  return (iter != adjMap->end() ? iter->second : ret);
123 }
124 
125 template<typename V, typename E>
126 void DirectedVertex<V,E>::addEdge(Vertex<V,E>* other, boost::shared_ptr<E> edge, EdgeType type) {
127  if (type == Vertex<V,E>::INCOMING) (*incoming)[other] = edge;
128  else (*outgoing)[other] = edge;
129 }
130 
131 template<typename V, typename E>
132 void DirectedVertex<V,E>::successors(std::vector<V*>& out) {
133  this->getItems(outgoing, out);
134 }
135 
136 template<typename V, typename E>
137 void DirectedVertex<V,E>::predecessors(std::vector<V*>& out) {
138  this->getItems(incoming, out);
139 }
140 
141 template<typename V, typename E>
142 void DirectedVertex<V,E>::adjacent(std::vector<V*>& out) {
143  this->getItems(incoming, out);
144  this->getItems(outgoing, out);
145 }
146 
147 template<typename V, typename E>
149  return incoming->size();
150 }
151 
152 template<typename V, typename E>
154  return outgoing->size();
155 }
156 
157 template<typename V, typename E>
158 void DirectedVertex<V,E>::edges(EdgeType type, std::vector<boost::shared_ptr<E> >& out) {
159  Vertex<V, E>::edges((type == Vertex<V,E>::INCOMING ? incoming : outgoing), out);
160 }
161 
162 }
163 
164 
165 
166 #endif /* DIRECTEDVERTEX_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::DirectedVertex::inDegree
int inDegree()
Gets the in degree of this Vertex.
Definition: DirectedVertex.h:148
repast::DirectedVertex::adjacent
virtual void adjacent(std::vector< V * > &out)
Gets the Vertices adjacent to this Vertex.
Definition: DirectedVertex.h:142
repast::DirectedVertex::predecessors
virtual void predecessors(std::vector< V * > &out)
Gets the predecessors of this Vertex.
Definition: DirectedVertex.h:137
repast::DirectedVertex::edges
virtual void edges(EdgeType type, std::vector< boost::shared_ptr< E > > &out)
Gets all the edges of the specified type in which this Vertex participates and return them in out.
Definition: DirectedVertex.h:158
repast::Vertex
Used internally by repast graphs / networks to encapsulate Vertices.
Definition: Vertex.h:52
repast::DirectedVertex::outDegree
int outDegree()
Gets the out degree of this Vertex.
Definition: DirectedVertex.h:153
repast::DirectedVertex
Used internally by repast graphs / networks to encapsulate the vertices of a directed graph.
Definition: DirectedVertex.h:56
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::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::DirectedVertex::addEdge
virtual void addEdge(Vertex< V, E > *other, boost::shared_ptr< E > edge, EdgeType type)
Adds an edge of the specified type between this Vertex and the specified vertex.
Definition: DirectedVertex.h:126
repast::DirectedVertex::removeEdge
virtual boost::shared_ptr< E > removeEdge(Vertex< V, E > *other, EdgeType type)
Removes the edge of the specified type between this Vertex and the specified Vertex.
Definition: DirectedVertex.h:113
repast::DirectedVertex::successors
virtual void successors(std::vector< V * > &out)
Gets the successors of this Vertex.
Definition: DirectedVertex.h:132
repast::DirectedVertex::findEdge
virtual boost::shared_ptr< E > findEdge(Vertex< V, E > *other, EdgeType type)
Finds the edge of the specified type between this Vertex and the specified vertex.
Definition: DirectedVertex.h:118
repast::DirectedVertex::DirectedVertex
DirectedVertex(boost::shared_ptr< V > item)
Creates a DirectedVertex that will contain the specified item.
Definition: DirectedVertex.h:101