grid.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. /*
  11. * grid.c
  12. * Written by Emden R. Gansner
  13. *
  14. * Support for grid to speed up layout. On each pass, nodes are
  15. * put into grid cells. Given a node, repulsion is only computed
  16. * for nodes in one of that nodes 9 adjacent grids.
  17. */
  18. /* uses PRIVATE interface for NOTUSED */
  19. #define FDP_PRIVATE 1
  20. #include <fdpgen/fdp.h>
  21. #include <fdpgen/grid.h>
  22. #include <common/macros.h>
  23. #include <stddef.h>
  24. #include <string.h>
  25. #include <util/alloc.h>
  26. /* structure for maintaining a free list of cells */
  27. typedef struct _block {
  28. cell *mem; /* block of cells */
  29. cell *cur; /* next available cell */
  30. cell *endp; /* after last cell */
  31. struct _block *next; /* next memory block */
  32. } block_t;
  33. /* newBlock:
  34. * Create new block of size cells
  35. */
  36. static block_t *newBlock(int size)
  37. {
  38. block_t *newb = gv_alloc(sizeof(block_t));
  39. newb->next = 0;
  40. newb->mem = gv_calloc(size, sizeof(cell));
  41. newb->endp = newb->mem + size;
  42. newb->cur = newb->mem;
  43. return newb;
  44. }
  45. /* freeBlock:
  46. * Free malloc'ed memory and block.
  47. * Recurse to next block
  48. */
  49. static void freeBlock(block_t * b)
  50. {
  51. if (b) {
  52. block_t *next = b->next;
  53. free(b->mem);
  54. free(b);
  55. freeBlock(next);
  56. }
  57. }
  58. struct _grid {
  59. Dt_t *data; /* cells indexed by (i,j) */
  60. block_t *cellMem; /* list of memory blocks for cells */
  61. block_t *cellCur; /* current block */
  62. int listSize; /* memory of nodes */
  63. node_list *listMem; /* list of memory for node items */
  64. node_list *listCur; /* next node item */
  65. };
  66. /* getCell:
  67. * Create a new cell using memory blocks.
  68. */
  69. static cell *getCell(Grid * g)
  70. {
  71. cell *cp;
  72. block_t *bp = g->cellCur; /* current block */
  73. if (bp->cur == bp->endp) { /* current block is full */
  74. if (bp->next == 0) {
  75. bp->next = newBlock(2 * (bp->endp - bp->mem));
  76. }
  77. bp = g->cellCur = bp->next;
  78. bp->cur = bp->mem;
  79. }
  80. cp = bp->cur++;
  81. return cp;
  82. }
  83. static int ijcmpf(void *point1, void *point2) {
  84. const gridpt *p1 = point1;
  85. const gridpt *p2 = point2;
  86. if (p1->i < p2->i) {
  87. return -1;
  88. }
  89. if (p1->i > p2->i) {
  90. return 1;
  91. }
  92. if (p1->j < p2->j) {
  93. return -1;
  94. }
  95. if (p1->j > p2->j) {
  96. return 1;
  97. }
  98. return 0;
  99. }
  100. static Grid _grid; // hack because can't attach info. to Dt_t
  101. /* newCell:
  102. * Allocate a new cell from free store and initialize its indices
  103. * This is used by the grid discipline to create cells.
  104. */
  105. static void *newCell(void *obj, Dtdisc_t *disc) {
  106. cell *cellp = obj;
  107. cell *newp;
  108. (void)disc;
  109. newp = getCell(&_grid);
  110. newp->p.i = cellp->p.i;
  111. newp->p.j = cellp->p.j;
  112. newp->nodes = 0;
  113. return newp;
  114. }
  115. /* newNode:
  116. * Allocate a new node item from free store.
  117. * Set node value and hook into list.
  118. * A grid assumes the memory allocated in adjustGrid
  119. * will be enough more all nodes added.
  120. */
  121. static node_list *newNode(Grid * g, Agnode_t * n, node_list * nxt)
  122. {
  123. node_list *newp;
  124. newp = g->listCur++;
  125. newp->node = n;
  126. newp->next = nxt;
  127. return newp;
  128. }
  129. static Dtdisc_t gridDisc = {
  130. offsetof(cell, p),
  131. sizeof(gridpt),
  132. offsetof(cell, link),
  133. newCell,
  134. NULL,
  135. ijcmpf,
  136. };
  137. /* mkGrid:
  138. * Create grid data structure.
  139. * cellHint provides rough idea of how many cells
  140. * may be needed.
  141. */
  142. Grid *mkGrid(int cellHint)
  143. {
  144. Grid *g = &_grid;
  145. memset(g, 0, sizeof(*g)); // see comment above
  146. g->data = dtopen(&gridDisc, Dtoset);
  147. g->cellMem = newBlock(cellHint);
  148. return g;
  149. }
  150. /* adjustGrid:
  151. * Set up node list for grid. Make sure the list
  152. * can handle nnodes nodes.
  153. * It is assumed no more than nnodes will be added
  154. * to the grid.
  155. */
  156. void adjustGrid(Grid * g, int nnodes)
  157. {
  158. int nsize;
  159. if (nnodes > g->listSize) {
  160. nsize = MAX(nnodes, 2 * g->listSize);
  161. if (g->listMem)
  162. free(g->listMem);
  163. g->listMem = gv_calloc(nsize, sizeof(node_list));
  164. g->listSize = nsize;
  165. }
  166. }
  167. /* clearGrid:
  168. * Reset grid. This clears the dictionary,
  169. * and reuses available memory.
  170. */
  171. void clearGrid(Grid * g)
  172. {
  173. dtclear(g->data);
  174. g->listCur = g->listMem;
  175. g->cellCur = g->cellMem;
  176. g->cellCur->cur = g->cellCur->mem;
  177. }
  178. /* delGrid:
  179. * Close and free all grid resources.
  180. */
  181. void delGrid(Grid * g)
  182. {
  183. dtclose(g->data);
  184. freeBlock(g->cellMem);
  185. free(g->listMem);
  186. }
  187. /* addGrid:
  188. * Add node n to cell (i,j) in grid g.
  189. */
  190. void addGrid(Grid * g, int i, int j, Agnode_t * n)
  191. {
  192. cell *cellp;
  193. cell key;
  194. key.p.i = i;
  195. key.p.j = j;
  196. cellp = dtinsert(g->data, &key);
  197. cellp->nodes = newNode(g, n, cellp->nodes);
  198. if (Verbose >= 3) {
  199. fprintf(stderr, "grid(%d,%d): %s\n", i, j, agnameof(n));
  200. }
  201. }
  202. typedef int (*walkfn_t)(void*, void*);
  203. /* walkGrid:
  204. * Apply function walkf to each cell in the grid.
  205. * The second argument to walkf is the cell; the
  206. * third argument is the grid. (The first argument
  207. * is the dictionary.) walkf must return 0.
  208. */
  209. void walkGrid(Grid *g, int (*walkf)(cell*, Grid*))
  210. {
  211. dtwalk(g->data, (walkfn_t) walkf, g);
  212. }
  213. /* findGrid;
  214. * Return the cell, if any, corresponding to
  215. * indices i,j
  216. */
  217. cell *findGrid(Grid * g, int i, int j)
  218. {
  219. cell key;
  220. key.p.i = i;
  221. key.p.j = j;
  222. return dtsearch(g->data, &key);
  223. }
  224. /* gLength:
  225. * Return the number of nodes in a cell.
  226. */
  227. int gLength(cell * p)
  228. {
  229. int len = 0;
  230. node_list *nodes = p->nodes;
  231. while (nodes) {
  232. len++;
  233. nodes = nodes->next;
  234. }
  235. return len;
  236. }