* Contributors: Details at https://graphviz.org
*************************************************************************/
+#include <assert.h>
+#include <cgraph/list.h>
#include <patchwork/patchwork.h>
+#include <limits.h>
#include <neatogen/adjust.h>
#include <pack/pack.h>
#include <neatogen/neatoprocs.h>
/* the following code shamelessly copied from lib/fdpgen/layout.c
and should be extracted and made into a common function */
-#define CL_CHUNK 10
-
-typedef struct {
- graph_t **cl;
- int sz;
- int cnt;
-} clist_t;
-
-/* addCluster:
- * Append a new cluster to the list.
- * NOTE: cl[0] is empty. The clusters are in cl[1..cnt].
- * Normally, we increase the array when cnt == sz.
- * The test for cnt > sz is necessary for the first time.
- */
-static void addCluster(clist_t * clist, graph_t * subg)
-{
- clist->cnt++;
- if (clist->cnt >= clist->sz) {
- clist->sz += CL_CHUNK;
- clist->cl = RALLOC(clist->sz, clist->cl, graph_t *);
- }
- clist->cl[clist->cnt] = subg;
-}
+DEFINE_LIST(clist, graph_t*)
/* mkClusters:
* Attach list of immediate child clusters.
clist_t* clist;
if (pclist == NULL) {
+ // [0] is empty. The clusters are in [1..cnt].
+ clist_append(&list, NULL);
clist = &list;
}
else
LEVEL(subg) = LEVEL(parent) + 1;
GPARENT(subg) = parent;
#endif
- addCluster(clist, subg);
+ clist_append(clist, subg);
mkClusters(subg, NULL, subg);
}
else {
}
}
if (pclist == NULL) {
- GD_n_cluster(g) = list.cnt;
- if (list.cnt)
- GD_clust(g) = RALLOC(list.cnt + 1, list.cl, graph_t*);
+ assert(clist_size(&list) - 1 <= INT_MAX);
+ GD_n_cluster(g) = (int)(clist_size(&list) - 1);
+ if (clist_size(&list) > 1) {
+ clist_shrink_to_fit(&list);
+ GD_clust(g) = clist_detach(&list);
+ } else {
+ clist_free(&list);
+ }
}
}