Tulip  5.0.0
Large graphs analysis and drawing
GraphView.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef Tulip_SUPERGRAPHVIEW_H
22 #define Tulip_SUPERGRAPHVIEW_H
23 
24 #include <vector>
25 #include <tulip/GraphAbstract.h>
26 #include <tulip/GraphImpl.h>
27 #include <tulip/MutableContainer.h>
28 #include <tulip/IdManager.h>
29 
30 namespace tlp {
31 // used for node management
32 struct SGraphNodeData {
33  unsigned int outDegree;
34  unsigned int inDegree;
35  SGraphNodeData() :outDegree(0), inDegree(0) {}
36  inline void outDegreeAdd(int i) {
37  outDegree += i;
38  }
39  inline void inDegreeAdd(int i) {
40  inDegree += i;
41  }
42 };
43 
44 typedef SGraphNodeData* SGraphNodeDataPtr;
45 DECL_STORED_PTR(SGraphNodeDataPtr);
46 
47 /**
48  * This class is one of the implementation of the Graph Interface
49  * It only filters the elements of its parents.
50  */
51 class GraphView:public GraphAbstract {
52 
53  inline GraphImpl* getRootImpl() const {
54  return (GraphImpl *) getRoot();
55  }
56 
57  friend class GraphImpl;
58 public:
59  GraphView(Graph *supergraph, BooleanProperty *filter, unsigned int id);
60  ~GraphView();
61  //========================================================================
62  node addNode();
63  void addNodes(unsigned int nb);
64  void addNodes(unsigned int nb, std::vector<node>& addedNodes);
65  void addNode(const node);
66  void addNodes(Iterator<node>* nodes);
67  edge addEdge(const node n1,const node n2);
68  void addEdges(const std::vector<std::pair<node, node> >& edges,
69  std::vector<edge>& addedEdges);
70  void addEdges(const std::vector<std::pair<node, node> >& edges);
71  void addEdge(const edge);
72  void addEdges(Iterator<edge>* edges);
73  void delNode(const tlp::node n, bool deleteInAllGraphs = false);
74  void delEdge(const tlp::edge e, bool deleteInAllGraphs = false);
75  void setEdgeOrder(const node n, const std::vector<edge> &v) {
76  getRootImpl()->setEdgeOrder(n,v);
77  }
78  void swapEdgeOrder(const node n, const edge e1, const edge e2) {
79  getRootImpl()->swapEdgeOrder(n, e1, e2);
80  }
81  //=========================================================================
82  inline bool isElement(const node n) const {
83  return _nodeData.get(n.id) != NULL;
84  }
85  inline bool isElement(const edge e) const {
86  return _edges.isElement(e);
87  }
88  edge existEdge(const node source, const node target,
89  bool directed) const;
90  inline unsigned int numberOfNodes() const {
91  return _nodes.size();
92  }
93  inline unsigned int numberOfEdges() const {
94  return _edges.size();
95  }
96  //=========================================================================
97  inline unsigned int deg(const node n) const {
98  assert(isElement(n));
99  SGraphNodeData* nData = _nodeData.get(n.id);
100  return nData->inDegree + nData->outDegree;
101  }
102  inline unsigned int indeg(const node n) const {
103  assert(isElement(n));
104  return _nodeData.get(n.id)->inDegree;
105  }
106  inline unsigned int outdeg(const node n) const {
107  assert(isElement(n));
108  return _nodeData.get(n.id)->outDegree;
109  }
110  //=========================================================================
111  inline node source(const edge e) const {
112  return getRootImpl()->source(e);
113  }
114  inline void setSource(const edge e, const node newSrc) {
115  assert(isElement(e));
116  getRootImpl()->setEnds(e, newSrc, node());
117  }
118  inline node target(const edge e) const {
119  return getRootImpl()->target(e);
120  }
121  inline void setTarget(const edge e, const node newTgt) {
122  assert(isElement(e));
123  getRootImpl()->setEnds(e, node(), newTgt);
124  }
125  inline const std::pair<node, node>& ends(const edge e) const {
126  return getRootImpl()->ends(e);
127  }
128  inline void setEnds(const edge e, const node newSrc, const node newTgt) {
129  assert(isElement(e));
130  getRootImpl()->setEnds(e, newSrc, newTgt);
131  }
132  inline node opposite(const edge e, const node n)const {
133  return getRootImpl()->opposite(e, n);
134  }
135  inline void reverse(const edge e) {
136  assert(isElement(e));
137  getRootImpl()->reverse(e);
138  }
139  //=========================================================================
140  inline const std::vector<node>& nodes() const {
141  return _nodes;
142  }
143  inline unsigned int nodePos(const node n) const {
144  return _nodes.getPos(n);
145  }
146  Iterator<node>* getNodes() const;
147  Iterator<node>* getInNodes(const node) const;
148  Iterator<node>* getOutNodes(const node) const;
149  Iterator<node>* getInOutNodes(const node) const;
150  inline const std::vector<edge>& edges() const {
151  return _edges;
152  }
153  inline unsigned int edgePos(const edge e) const {
154  return _edges.getPos(e);
155  }
156  Iterator<edge>* getEdges() const;
157  Iterator<edge>* getInEdges(const node) const;
158  Iterator<edge>* getOutEdges(const node) const;
159  Iterator<edge>* getInOutEdges(const node) const;
160  std::vector<edge> getEdges(const node source, const node target,
161  bool directed = true) const;
162  inline const std::vector<edge>& allEdges(const node n) const {
163  return getRootImpl()->allEdges(n);
164  }
165  inline void sortElts() {
166  _nodes.sort();
167  _edges.sort();
168  }
169  //=========================================================================
170  // only implemented on a root graph
171  virtual void reserveNodes(unsigned int nbNodes);
172  virtual void reserveEdges(unsigned int nbEdges);
173  //=========================================================================
174  // updates management
175  virtual void push(bool unpopAllowed = true,
176  std::vector<PropertyInterface*>* propertiesToPreserveOnPop= NULL);
177  virtual void pop(bool unpopAllowed = true);
178  virtual void popIfNoUpdates();
179  virtual void unpop();
180  virtual bool canPop();
181  virtual bool canUnpop();
182  virtual bool canPopThenUnpop();
183 
184 protected:
185  // designed to reassign an id to a previously deleted elt
186  // used by GraphUpdatesRecorder
187  virtual void restoreNode(node);
188  virtual void restoreEdge(edge, node source, node target);
189  // designed to only update own structures
190  // used by GraphUpdatesRecorder
191  virtual void removeNode(const node);
192  virtual void removeEdge(const edge);
193  void removeNode(const node n, const std::vector<edge>& edges);
194  void removeEdges(const std::vector<edge>& edges);
195 
196 private:
197  MutableContainer<SGraphNodeDataPtr> _nodeData;
198  SGraphIdContainer<node> _nodes;
199  SGraphIdContainer<edge> _edges;
200  void outDegreeAdd(node n, int i);
201  void inDegreeAdd(node n, int i);
202  edge addEdgeInternal(edge);
203  void reverseInternal(const edge, const node src, const node tgt);
204  void setEndsInternal(const edge, node src, node tgt,
205  const node newSrc, const node newTgt);
206  void addNodesInternal(unsigned int nbAdded, const std::vector<node>* nodes);
207  void addEdgesInternal(unsigned int nbAdded, const std::vector<edge>* edges,
208  const std::vector<std::pair<node, node> >& ends);
209 };
210 
211 }
212 #endif
213 
214 ///@endcond
Interface for Tulip iterators. Allows basic iteration operations only.
Definition: Iterator.h:39
The edge struct represents an edge in a Graph object.
Definition: Edge.h:39
The node struct represents a node in a Graph object.
Definition: Node.h:39