Graphviz 13.0.0~dev.20250607.1528
Loading...
Searching...
No Matches
index.c
Go to the documentation of this file.
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#include <stdlib.h>
12
13#include <label/index.h>
14#include <stddef.h>
15#include <stdio.h>
16#include <assert.h>
17#include <stdbool.h>
18
20{
21 LeafList_t *llp;
22
23 if ((llp = calloc(1, sizeof(LeafList_t)))) {
24 llp->leaf = lp;
25 llp->next = 0;
26 }
27 return llp;
28}
29
31{
32 if (!lp)
33 return llp;
34
36 nlp->next = llp;
37 return nlp;
38}
39
41{
42 while (llp->next) {
43 LeafList_t *tlp = llp->next;
44 free(llp);
45 llp = tlp;
46 }
47 free(llp);
48 return;
49}
50
52{
53 RTree_t *rtp;
54
55 if ((rtp = calloc(1, sizeof(RTree_t))))
56 rtp->root = RTreeNewIndex();
57 return rtp;
58}
59
60/* Make a new index, empty. Consists of a single node. */
62 Node_t *x = RTreeNewNode();
63 x->level = 0; /* leaf */
64 return x;
65}
66
67static int RTreeClose2(RTree_t * rtp, Node_t * n)
68{
69 if (n->level > 0) {
70 for (int i = 0; i < NODECARD; i++) {
71 if (!n->branch[i].child)
72 continue;
73 if (!RTreeClose2(rtp, n->branch[i].child)) {
74 free(n->branch[i].child);
75 DisconBranch(n, i);
76 }
77 }
78 } else {
79 for (int i = 0; i < NODECARD; i++) {
80 if (!n->branch[i].child)
81 continue;
82 DisconBranch(n, i);
83 }
84 }
85 return 0;
86}
87
88
90{
91 RTreeClose2(rtp, rtp->root);
92 free(rtp->root);
93 free(rtp);
94 return 0;
95}
96
97#ifdef RTDEBUG
98/* Print out all the nodes in an index.
99** Prints from root downward.
100*/
101void PrintIndex(Node_t * n)
102{
103 Node_t *nn;
104 assert(n);
105 assert(n->level >= 0);
106
107 if (n->level > 0) {
108 for (size_t i = 0; i < NODECARD; i++) {
109 if ((nn = n->branch[i].child) != NULL)
110 PrintIndex(nn);
111 }
112 }
113
114 PrintNode(n);
115}
116
117/* Print out all the data rectangles in an index.
118*/
119void PrintData(Node_t * n)
120{
121 Node_t *nn;
122 assert(n);
123 assert(n->level >= 0);
124
125 if (n->level == 0)
126 PrintNode(n);
127 else {
128 for (size_t i = 0; i < NODECARD; i++) {
129 if ((nn = n->branch[i].child) != NULL)
130 PrintData(nn);
131 }
132 }
133}
134#endif
135
136/* RTreeSearch in an index tree or subtree for all data retangles that
137** overlap the argument rectangle.
138** Returns the number of qualifying data rects.
139*/
141 LeafList_t *llp = 0;
142
143 assert(n);
144 assert(n->level >= 0);
145
146 if (n->level > 0) { /* this is an internal node in the tree */
147 for (size_t i = 0; i < NODECARD; i++)
148 if (n->branch[i].child && Overlap(r, n->branch[i].rect)) {
149 LeafList_t *tlp = RTreeSearch(rtp, n->branch[i].child, r);
150 if (llp) {
151 LeafList_t *xlp = llp;
152 while (xlp->next)
153 xlp = xlp->next;
154 xlp->next = tlp;
155 } else
156 llp = tlp;
157 }
158 } else { /* this is a leaf node */
159 for (size_t i = 0; i < NODECARD; i++) {
160 if (n->branch[i].child && Overlap(r, n->branch[i].rect)) {
161 llp = RTreeLeafListAdd(llp, (Leaf_t *) & n->branch[i]);
162# ifdef RTDEBUG
163 PrintRect(n->branch[i].rect);
164# endif
165 }
166 }
167 }
168 return llp;
169}
170
171/* Insert a data rectangle into an index structure.
172** RTreeInsert provides for splitting the root;
173** returns 1 if root was split, 0 if it was not.
174** The level argument specifies the number of steps up from the leaf
175** level to insert; e.g. a data rectangle goes in at level = 0.
176** RTreeInsert2 does the recursion.
177*/
178static int RTreeInsert2(RTree_t *, Rect_t, void *, Node_t *, Node_t **, int);
179
180int RTreeInsert(RTree_t *rtp, Rect_t r, void *data, Node_t **n) {
181 Node_t *newnode=0;
182 Branch_t b;
183 int result = 0;
184
185
186 assert(n);
187 for (size_t i = 0; i < NUMDIMS; i++)
188 assert(r.boundary[i] <= r.boundary[NUMDIMS + i]);
189
190 if (RTreeInsert2(rtp, r, data, *n, &newnode, 0)) { // root was split
191
192 Node_t *newroot = RTreeNewNode(); /* grow a new root, make tree taller */
193 newroot->level = (*n)->level + 1;
194 b.rect = NodeCover(*n);
195 b.child = *n;
196 AddBranch(rtp, &b, newroot, NULL);
198 b.child = newnode;
199 AddBranch(rtp, &b, newroot, NULL);
200 *n = newroot;
201 result = 1;
202 }
203
204 return result;
205}
206
207/* Inserts a new data rectangle into the index structure.
208** Recursively descends tree, propagates splits back up.
209** Returns 0 if node was not split. Old node updated.
210** If node was split, returns 1 and sets the pointer pointed to by
211** new to point to the new node. Old node updated to become one of two.
212** The level argument specifies the number of steps up from the leaf
213** level to insert; e.g. a data rectangle goes in at level = 0.
214*/
215static int RTreeInsert2(RTree_t *rtp, Rect_t r, void *data, Node_t *n,
216 Node_t **new, int level) {
217 Branch_t b;
218 Node_t *n2=0;
219
220 assert(n && new);
221 assert(level >= 0 && level <= n->level);
222
223 /* Still above level for insertion, go down tree recursively */
224 if (n->level > level) {
225 int i = PickBranch(r, n);
226 if (!RTreeInsert2(rtp, r, data, n->branch[i].child, &n2, level)) { /* recurse: child was not split */
227 n->branch[i].rect = CombineRect(r, n->branch[i].rect);
228 return 0;
229 } else { /* child was split */
230 n->branch[i].rect = NodeCover(n->branch[i].child);
231 b.child = n2;
232 b.rect = NodeCover(n2);
233 return AddBranch(rtp, &b, n, new);
234 }
235 } else if (n->level == level) { /* at level for insertion. */
236 /*Add rect, split if necessary */
237 b.rect = r;
238 b.child = data;
239 return AddBranch(rtp, &b, n, new);
240 } else { /* Not supposed to happen */
241 assert(false);
242 return 0;
243 }
244}
static Agnode_t * newnode(Agraph_t *g, IDTYPE id, uint64_t seq)
Definition node.c:73
void free(void *)
node NULL
Definition grammar.y:180
static int RTreeClose2(RTree_t *rtp, Node_t *n)
Definition index.c:67
static int RTreeInsert2(RTree_t *, Rect_t, void *, Node_t *, Node_t **, int)
Definition index.c:215
LeafList_t * RTreeLeafListAdd(LeafList_t *llp, Leaf_t *lp)
Definition index.c:30
void RTreeLeafListFree(LeafList_t *llp)
Definition index.c:40
RTree_t * RTreeOpen(void)
Definition index.c:51
int RTreeClose(RTree_t *rtp)
Definition index.c:89
Node_t * RTreeNewIndex(void)
Definition index.c:61
LeafList_t * RTreeNewLeafList(Leaf_t *lp)
Definition index.c:19
int RTreeInsert(RTree_t *rtp, Rect_t r, void *data, Node_t **n)
Definition index.c:180
LeafList_t * RTreeSearch(RTree_t *rtp, Node_t *n, Rect_t r)
Definition index.c:140
#define NUMDIMS
Definition index.h:37
#define NODECARD
Definition index.h:43
Node_t * RTreeNewNode(void)
Definition node.c:24
int AddBranch(RTree_t *rtp, Branch_t *b, Node_t *n, Node_t **new)
Definition node.c:146
Rect_t NodeCover(Node_t *n)
Definition node.c:81
void DisconBranch(Node_t *n, int i)
Definition node.c:171
int PickBranch(Rect_t r, Node_t *n)
Definition node.c:105
Rect_t CombineRect(const Rect_t r, const Rect_t rr)
Definition rectangle.c:84
bool Overlap(const Rect_t r, const Rect_t s)
Definition rectangle.c:103
Definition node.h:19
Rect_t rect
Definition node.h:20
struct Node * child
Definition node.h:21
Leaf_t * leaf
Definition index.h:62
struct LeafList * next
Definition index.h:61
Definition index.h:55
Definition node.h:24
int level
Definition node.h:26
struct Branch branch[NODECARD]
Definition node.h:27
Definition index.h:65
Node_t * root
Definition index.h:66
double boundary[NUMSIDES]
Definition rectangle.h:21
Definition legal.c:50