libgexf  0.1.3
graph.h
Go to the documentation of this file.
1 
7 /*
8 # Copyright (c) <2009> <Sebastien Heymann>
9 #
10 # Permission is hereby granted, free of charge, to any person obtaining a copy
11 # of this software and associated documentation files (the "Software"), to deal
12 # in the Software without restriction, including without limitation the rights
13 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 # copies of the Software, and to permit persons to whom the Software is
15 # furnished to do so, subject to the following conditions:
16 #
17 # The above copyright notice and this permission notice shall be included in
18 # all copies or substantial portions of the Software.
19 #
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 # THE SOFTWARE.
27 */
28 
29 #ifndef _GRAPH_H
30 #define _GRAPH_H
31 
32 #include <set>
33 #include <vector>
34 #include <map>
35 #include <iostream>
36 #include "typedefs.h"
37 #include "exceptions.h"
38 #include "nodeiter.h"
39 #include "edgeiter.h"
40 
41 namespace libgexf {
42 
43 class NodeIter;
44 class EdgeIter;
45 
49 class Graph {
50 public:
51  Graph();
52 
56  Graph(const Graph& orig);
57 
58  virtual ~Graph();
59 
65  void addNode(const libgexf::t_id id);
66 
76  void addEdge(const libgexf::t_id id, const libgexf::t_id source_id, const libgexf::t_id target_id, const float weight=1.0, const libgexf::t_edge_type type=EDGE_UNDEF);
77 
83  void removeNode(const libgexf::t_id id);
84 
91  void removeEdge(const libgexf::t_id source_id, const libgexf::t_id target_id);
92 
93 
100  bool containsNode(const libgexf::t_id id) const;
101 
109  bool containsEdge(const libgexf::t_id source_id, const libgexf::t_id target_id) const;
110 
118  t_id getEdge(const libgexf::t_id source_id, const libgexf::t_id target_id) const;
119 
120 
126  libgexf::NodeIter* getNodes() const;
127 
133  libgexf::EdgeIter* getEdges() const;
134 
141  std::vector<libgexf::t_id> getNeighbors(const libgexf::t_id node_id) const;
142 
143 
149  unsigned int getNodeCount() const;
150 
156  unsigned int getEdgeCount() const;
157 
164  unsigned int getDegree(const libgexf::t_id node_id) const;
165 
166 
172  void clearEdges(const libgexf::t_id node_id);
173 
177  void clear();
178 
182  void clearEdges();
183 
184 
189  void readLock() throw(libgexf::ReadLockException);
190 
194  void readUnlock();
195 
201 
205  void writeUnlock();
206 
210  bool isReadLock();
211 
215  bool isWriteLock();
216 
220  bool isUnlock();
221 protected:
222  std::set<t_id> _nodes;
223  std::map<t_id,std::map<t_id,t_id> > _edges;
224  std::map<t_id,std::set<t_id> > _reverse_edges;
225  std::set<t_id> _bloom_edges;
226  std::map<t_id,std::map<t_edge_property,t_edge_value> > _edges_properties;
227  unsigned short int _rlock_count;
235 
236  friend std::ostream& operator<<(std::ostream& os, const Graph& o);
237  friend class NodeIter;
238  friend class EdgeIter;
239 private:
240  //Graph& operator=(const Graph& orig);
241 };
242 
243 } /* namespace libgexf */
244 
245 #endif /* _GRAPH_H */
246 
void readLock()
Set a lock on reading.
Definition: graph.cpp:412
std::string t_id
Definition: typedefs.h:35
unsigned short int _rlock_count
Definition: graph.h:227
t_id getEdge(const libgexf::t_id source_id, const libgexf::t_id target_id) const
Get the edge id.
Definition: graph.cpp:255
unsigned int getEdgeCount() const
Count the edges.
Definition: graph.cpp:280
Definition: abstractiter.h:32
unsigned int getDegree(const libgexf::t_id node_id) const
Get node degree.
Definition: graph.cpp:295
std::set< t_id > _nodes
Definition: graph.h:222
unsigned int getNodeCount() const
Count the nodes.
Definition: graph.cpp:271
void removeEdge(const libgexf::t_id source_id, const libgexf::t_id target_id)
Remove an edge.
Definition: graph.cpp:190
void writeUnlock()
Unset a lock on writing.
Definition: graph.cpp:438
std::map< t_id, std::map< t_edge_property, t_edge_value > > _edges_properties
Definition: graph.h:226
t_edge_type
Available edge types.
Definition: typedefs.h:50
bool isReadLock()
Test if a read lock exists.
Definition: graph.cpp:444
void writeLock()
Get a lock on writing.
Definition: graph.cpp:431
Exception occuring on a read-lock.
Definition: exceptions.h:43
bool isWriteLock()
Test if a write lock exists.
Definition: graph.cpp:450
std::set< t_id > _bloom_edges
Definition: graph.h:225
bool isUnlock()
Unset all locks.
Definition: graph.cpp:456
std::map< t_id, std::set< t_id > > _reverse_edges
Definition: graph.h:224
char _lock_flag
Flag used for determining the lock type:
Definition: graph.h:234
Topology structure of the graph.
Definition: graph.h:49
void readUnlock()
Unset a lock on reading.
Definition: graph.cpp:422
void addNode(const libgexf::t_id id)
Add a node.
Definition: graph.cpp:57
std::map< t_id, std::map< t_id, t_id > > _edges
Definition: graph.h:223
bool containsEdge(const libgexf::t_id source_id, const libgexf::t_id target_id) const
Test edge existence.
Definition: graph.cpp:242
libgexf::EdgeIter * getEdges() const
Get all edges.
Definition: graph.cpp:227
void addEdge(const libgexf::t_id id, const libgexf::t_id source_id, const libgexf::t_id target_id, const float weight=1.0, const libgexf::t_edge_type type=EDGE_UNDEF)
Add an edge.
Definition: graph.cpp:65
Iterator on edges.
Definition: edgeiter.h:45
libgexf::NodeIter * getNodes() const
Get all nodes.
Definition: graph.cpp:221
void clearEdges()
Delete all edges.
Definition: graph.cpp:402
Iterator on nodes.
Definition: nodeiter.h:45
bool containsNode(const libgexf::t_id id) const
Test node existence.
Definition: graph.cpp:233
Exception occuring on a write-lock.
Definition: exceptions.h:57
void removeNode(const libgexf::t_id id)
Remove a node.
Definition: graph.cpp:178
std::vector< libgexf::t_id > getNeighbors(const libgexf::t_id node_id) const
Get node neighbors.
Definition: graph.cpp:328
void clear()
Clear the graph.
Definition: graph.cpp:390