| 1 |
/** |
| 2 |
* @file |
| 3 |
* @brief implementation of subgraphs |
| 4 |
* @ingroup cgraph_subgraph |
| 5 |
* @ingroup cgraph_core |
| 6 |
*/ |
| 7 |
/************************************************************************* |
| 8 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 9 |
* All rights reserved. This program and the accompanying materials |
| 10 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 11 |
* which accompanies this distribution, and is available at |
| 12 |
* https://www.eclipse.org/legal/epl-v10.html |
| 13 |
* |
| 14 |
* Contributors: Details at https://graphviz.org |
| 15 |
*************************************************************************/ |
| 16 |
|
| 17 |
#include "config.h" |
| 18 |
|
| 19 |
#include <cgraph/cghdr.h> |
| 20 |
#include <stdbool.h> |
| 21 |
#include <stddef.h> |
| 22 |
#include <util/alloc.h> |
| 23 |
|
| 24 |
static Agraph_t *agfindsubg_by_id(Agraph_t * g, IDTYPE id) |
| 25 |
{ |
| 26 |
Agraph_t template; |
| 27 |
|
| 28 |
agdtdisc(g, g->g_id, &Ag_subgraph_id_disc); |
| 29 |
AGID(&template) = id; |
| 30 |
return dtsearch(g->g_id, &template); |
| 31 |
} |
| 32 |
|
| 33 |
static Agraph_t *localsubg(Agraph_t * g, IDTYPE id) |
| 34 |
{ |
| 35 |
Agraph_t *subg; |
| 36 |
|
| 37 |
subg = agfindsubg_by_id(g, id); |
| 38 |
if (subg) |
| 39 |
return subg; |
| 40 |
|
| 41 |
subg = gv_alloc(sizeof(Agraph_t)); |
| 42 |
subg->clos = g->clos; |
| 43 |
subg->desc = g->desc; |
| 44 |
subg->desc.maingraph = false; |
| 45 |
subg->parent = g; |
| 46 |
subg->root = g->root; |
| 47 |
AGID(subg) = id; |
| 48 |
return agopen1(subg); |
| 49 |
} |
| 50 |
|
| 51 |
Agraph_t *agidsubg(Agraph_t *g, IDTYPE id) { |
| 52 |
return agfindsubg_by_id(g, id); |
| 53 |
} |
| 54 |
|
| 55 |
Agraph_t *agsubg(Agraph_t * g, char *name, int cflag) |
| 56 |
{ |
| 57 |
IDTYPE id; |
| 58 |
Agraph_t *subg; |
| 59 |
|
| 60 |
if (name && agmapnametoid(g, AGRAPH, name, &id, false)) { |
| 61 |
/* might already exist */ |
| 62 |
if ((subg = agfindsubg_by_id(g, id))) |
| 63 |
return subg; |
| 64 |
} |
| 65 |
|
| 66 |
if (cflag && agmapnametoid(g, AGRAPH, name, &id, true)) { /* reserve id */ |
| 67 |
subg = localsubg(g, id); |
| 68 |
agregister(g, AGRAPH, subg); |
| 69 |
return subg; |
| 70 |
} |
| 71 |
|
| 72 |
return NULL; |
| 73 |
} |
| 74 |
|
| 75 |
Agraph_t *agfstsubg(Agraph_t * g) |
| 76 |
{ |
| 77 |
return dtfirst(g->g_seq); |
| 78 |
} |
| 79 |
|
| 80 |
Agraph_t *agnxtsubg(Agraph_t * subg) |
| 81 |
{ |
| 82 |
Agraph_t *g; |
| 83 |
|
| 84 |
g = agparent(subg); |
| 85 |
return g? dtnext(g->g_seq, subg) : 0; |
| 86 |
} |
| 87 |
|
| 88 |
Agraph_t *agparent(Agraph_t * g) |
| 89 |
{ |
| 90 |
return g->parent; |
| 91 |
} |
| 92 |
|
| 93 |
/* this function is only responsible for deleting the entry |
| 94 |
* in the parent's subg dict. the rest is done in agclose(). |
| 95 |
*/ |
| 96 |
int agdelsubg(Agraph_t * g, Agraph_t * subg) |
| 97 |
{ |
| 98 |
return dtdelete(g->g_seq, subg) != NULL && dtdelete(g->g_id, subg) != NULL; |
| 99 |
} |