21 #ifndef GRAPHSTORAGE_H 22 #define GRAPHSTORAGE_H 27 #include <tulip/Node.h> 28 #include <tulip/Edge.h> 29 #include <tulip/IdManager.h> 41 class GraphStorageIdsMemento {
43 virtual ~GraphStorageIdsMemento() {}
59 inline bool isElement(
const node n)
const {
60 return nodeIds.isElement(n);
66 inline unsigned int numberOfNodes()
const {
67 return nodeIds.size();
73 inline bool isElement(
const edge e)
const {
74 return edgeIds.isElement(e);
80 inline unsigned int numberOfEdges()
const {
81 return edgeIds.size();
89 void reserveNodes(
const size_t nb);
96 void reserveEdges(
const size_t nb);
103 void reserveAdj(
const node n,
const size_t nb);
110 void reserveAdj(
const size_t nb);
115 void restoreAdj(
const node n,
const std::vector<edge> &edges);
120 inline const std::vector<edge> &adj(
const node n)
const {
121 assert(isElement(n));
122 return nodeData[n.id].edges;
128 inline node getOneNode()
const {
129 return numberOfNodes() ? nodeIds[0] : node();
137 inline Iterator<node> *getNodes()
const {
138 return nodeIds.getElts();
146 const GraphStorageIdsMemento *getIdsMemento()
const;
151 void restoreIdsMemento(
const GraphStorageIdsMemento *);
157 inline Iterator<edge> *getEdges()
const {
158 return edgeIds.getElts();
166 Iterator<edge> *getInOutEdges(
const node n)
const;
172 Iterator<edge> *getOutEdges(
const node n)
const;
178 Iterator<edge> *getInEdges(
const node n)
const;
190 bool getEdges(
const node src,
const node tgt,
bool directed, std::vector<edge> &edges,
191 const Graph *sg =
nullptr,
bool onlyFirst =
false)
const;
198 Iterator<node> *getInOutNodes(
const node n)
const;
204 Iterator<node> *getInNodes(
const node n)
const;
210 Iterator<node> *getOutNodes(
const node n)
const;
215 inline unsigned int deg(
const node n)
const {
216 assert(isElement(n));
217 return nodeData[n.id].edges.size();
223 inline unsigned int outdeg(
const node n)
const {
224 assert(isElement(n));
225 return nodeData[n.id].outDegree;
231 inline unsigned int indeg(
const node n)
const {
232 assert(isElement(n));
233 const NodeData &ctnr = nodeData[n.id];
234 return ctnr.edges.size() - ctnr.outDegree;
240 inline const std::vector<edge> &edges()
const {
247 inline unsigned int edgePos(
const edge e)
const {
248 return edgeIds.getPos(e);
254 inline const std::vector<node> &nodes()
const {
261 inline unsigned int nodePos(
const node n)
const {
262 return nodeIds.getPos(n);
268 inline const std::pair<node, node> &ends(
const edge e)
const {
269 assert(isElement(e));
270 return edgeEnds[e.id];
276 inline node source(
const edge e)
const {
277 assert(isElement(e));
278 return edgeEnds[e.id].first;
284 inline node target(
const edge e)
const {
285 assert(isElement(e));
286 return edgeEnds[e.id].second;
292 inline node opposite(
const edge e,
const node n)
const {
293 assert(isElement(e));
294 const std::pair<node, node> &eEnds = edgeEnds[e.id];
295 assert((eEnds.first == n) || (eEnds.second == n));
296 return (eEnds.first == n) ? eEnds.second : eEnds.first;
306 void setEnds(
const edge e,
const node newSrc,
const node newTgt);
315 inline void setSource(
const edge e,
const node n) {
316 setEnds(e, n, node());
326 inline void setTarget(
const edge e,
const node n) {
327 setEnds(e, node(), n);
333 void reverse(
const edge e);
338 void setEdgeOrder(
const node n,
const std::vector<edge> &v);
344 void swapEdgeOrder(
const node n,
const edge e1,
const edge e2);
352 void restoreNode(
const node n);
370 void addNodes(
unsigned int nb, std::vector<node> *addedNodes =
nullptr);
378 void removeFromNodes(
const node n);
389 void delNode(
const node n);
399 void restoreEdge(
const node src,
const node tgt,
const edge e);
407 edge addEdge(
const node src,
const node tgt);
416 void addEdges(
const std::vector<std::pair<node, node>> &edges,
417 std::vector<edge> *addedEdges =
nullptr);
427 void delEdge(
const edge e);
448 inline void sortElts() {
456 std::vector<edge> edges;
457 unsigned int outDegree;
459 NodeData() : outDegree(0) {}
463 mutable std::vector<std::pair<node, node>> edgeEnds;
464 mutable std::vector<NodeData> nodeData;
465 IdContainer<node> nodeIds;
466 IdContainer<edge> edgeIds;
476 static void removeFromNodeData(NodeData &c,
const edge e);
485 void removeFromEdges(
const edge e, node end = node());
488 #endif // GRAPHSTORAGE_H