1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
|
/* struct::tree - critcl - layer 1 definitions
* (c) Tree functions
*/
#include <string.h>
#include <t.h>
#include <tn.h>
#include <util.h>
/* .................................................. */
T*
t_new (void)
{
T* t = ALLOC (T);
Tcl_InitHashTable (&t->node, TCL_STRING_KEYS);
t->cmd = NULL;
t->counter = 0;
t->nodes = NULL;
t->nnodes = 0;
t->leaves = NULL;
t->nleaves = 0;
t->root = tn_new (t, "root");
t->structure = 0;
return t;
}
void
t_delete (T* t)
{
/* Delete a tree in toto. Recursively deletes all nodes first,
* starting at root. This also handles the nodes/leaves lists.
* Then the name -> node mapping, and the object name. The
*/
tn_delete (t->root);
Tcl_DeleteHashTable(&t->node);
t->cmd = NULL;
ckfree ((char*) t);
}
/* .................................................. */
void
t_structure (T* t)
{
/* Computes all structural data,
* then declares it valid.
*/
tn_structure (t->root, 0);
t->structure = 1;
}
/* .................................................. */
int
t_deserialize (T* dst, Tcl_Interp* interp, Tcl_Obj* src)
{
Tcl_Size listc, nodes;
Tcl_Obj** listv;
int root = -1;
int* parent = NULL;
/* Basic checks:
* - Is the input a list ?
* - Is its length a multiple of three ?
*
* structure: node-name parent-index attr-dict
* i+0 i+1 i+2
*/
#define NAME(i) (i)
#define PARENT(i) ((i)+1)
#define ATTR(i) ((i)+2)
if (Tcl_ListObjGetElements (interp, src, &listc, &listv) != TCL_OK) { /* OK tcl9 */
return TCL_ERROR;
}
if ((listc % 3) != 0) {
Tcl_AppendResult (interp,
"error in serialization: list length not a multiple of 3.",
NULL);
return TCL_ERROR;
}
nodes = listc/3;
/* Iterate and check the attribute dictionaries for listness and
* size (even length).
*/
{
Tcl_Size ac, i, j;
Tcl_Obj** av;
for (i = 0, j = 0;
i < listc;
i += 3, j++) {
ASSERT_BOUNDS (ATTR(i), listc);
ASSERT_BOUNDS (j, nodes);
if (Tcl_ListObjGetElements (interp, listv [ATTR(i)], /* OK tcl9 */
&ac, &av) != TCL_OK) {
return TCL_ERROR;
}
if ((ac % 2) != 0) {
Tcl_AppendResult (interp,
"error in serialization: malformed attribute dictionary.",
NULL);
return TCL_ERROR;
}
}
}
/* Iterate to locate the definition of root. Fails if there is none,
* or more than one.
*/
{
Tcl_Size i, j;
CONST char* parent;
for (i = 0, j = 0, root = -1;
i < listc;
i += 3, j++) {
/* j == i/3 */
ASSERT_BOUNDS (PARENT(i), listc);
ASSERT_BOUNDS (j, nodes);
parent = Tcl_GetString (listv [PARENT(i)]);
if (0 == strcmp ("", parent)) {
if (root >= 0) {
Tcl_AppendResult (interp,
"error in serialization: multiple root nodes.",
NULL);
return TCL_ERROR;
}
root = j;
}
}
if (root < 0) {
Tcl_AppendResult (interp,
"error in serialization: no root specified.",
NULL);
return TCL_ERROR;
}
}
/* Iterate again, check that the non-empty parent references
* are ok. We use the information we have about root to skip
* over the empty reference. We save the extracted and parsed
* references in a temp. allocated array.
*/
{
int res;
Tcl_Size i, j, index;
Tcl_Obj* p;
parent = NALLOC (nodes, int);
ASSERT_BOUNDS (root, nodes);
parent [root] = -1; /* Sensible, unused */
for (i = 0, j = 0;
i < listc;
i += 3, j++) {
/* j == i/3 */
ASSERT_BOUNDS (PARENT(i), listc);
ASSERT_BOUNDS (j, nodes);
if (j == root)
continue;
p = listv [PARENT(i)];
res = Tcl_GetSizeIntFromObj (interp, p, &index); /* OK tcl9 */
if (
(res != TCL_OK) ||
(index < 0) ||
(index >= listc) ||
((index % 3) != 0)
) {
Tcl_ResetResult (interp);
Tcl_AppendResult (interp,
"error in serialization: bad parent reference \"",
Tcl_GetString (p),
"\".", NULL);
ckfree ((char*) parent);
return TCL_ERROR;
}
if (index == i) {
/* Found a cyclic reference (direct cycle, node defines
* itself as its parent)
*/
Tcl_AppendResult (interp,
"error in serialization: cycle detected.",
NULL);
ckfree ((char*) parent);
return TCL_ERROR;
}
parent [j] = index/3;
}
}
/* Iteration over the parent information from the last phase. We
* are looking for indirect cycles. We detect them indirectly. If
* there are cycles we are unable to tag all nodes starting from the
* root. A tag means that the depth of the node can be computed, and
* for nodes in a cycle this is not possible.
*/
{
int* tag = NALLOC (nodes, int);
Tcl_Size i;
int changed = 1; /* Flag that last iteration tagged new nodes */
int done = 0; /* #nodes tagged */
for (i = 0; i < nodes; i++) {
ASSERT_BOUNDS (i, nodes);
tag [i] = 0;
}
ASSERT_BOUNDS (root, nodes);
tag [root] = 1;
done ++;
while (changed) {
changed = 0;
for (i = 0; i < nodes; i++) {
ASSERT_BOUNDS (i, nodes);
if (tag [i])
continue;
/* Assert: parent [i] in 0 .. nodes-1 */
ASSERT_BOUNDS (parent[i], nodes);
if (!tag [parent [i]])
continue;
tag [i] = 1;
changed = 1;
done ++;
}
}
ckfree ((char*) tag);
if (done < nodes) {
Tcl_AppendResult (interp,
"error in serialization: cycle detected.",
NULL);
ckfree ((char*) parent);
return TCL_ERROR;
}
}
/* Last iteration. Check that the node names are unique.
*/
{
Tcl_Size i, j;
int new;
Tcl_HashTable nx;
Tcl_InitHashTable (&nx, TCL_STRING_KEYS);
for (i = 0, j = 0;
i < listc;
i += 3, j++) {
ASSERT_BOUNDS (NAME(i), listc);
ASSERT_BOUNDS (j, nodes);
Tcl_CreateHashEntry (&nx, Tcl_GetString (listv [NAME(i)]),
&new);
if (!new) {
Tcl_AppendResult (interp,
"error in serialization: duplicate node names.",
NULL);
Tcl_DeleteHashTable(&nx);
ckfree ((char*) parent);
return TCL_ERROR;
}
}
Tcl_DeleteHashTable(&nx);
}
/* The serialization has been successfully validated now.
* We create the nodes, their attributes, and link them
* into the proper structure per the root and parent
* information provided to us by the validation.
*/
{
Tcl_Size i, j;
TN** nv = NALLOC (nodes, TN*);
TN* n;
TN* p;
tn_delete (dst->root);
for (i = 0, j = 0;
i < listc;
i += 3, j++) {
/* j == i/3 */
ASSERT_BOUNDS (NAME(i), listc);
ASSERT_BOUNDS (j, nodes);
nv [j] = tn_new (dst, Tcl_GetString (listv [NAME(i)]));
}
dst->root = nv [root];
for (i = 0, j = 0;
i < listc;
i += 3, j++) {
/* j == i/3 */
ASSERT_BOUNDS (ATTR(i), listc);
ASSERT_BOUNDS (j, nodes);
if (j == root) {
/* We don't append the node, this has already been covered,
* but we have to process the attributes.
*/
tn_set_attr (nv [j], interp, listv [ATTR(i)]);
continue;
}
ASSERT_BOUNDS (parent[j], nodes);
n = nv [j];
p = nv [parent [j]];
tn_append (p, n);
tn_set_attr (n, interp, listv [ATTR(i)]);
}
ckfree ((char*) nv);
}
ckfree ((char*) parent);
return TCL_OK;
}
/* .................................................. */
int
t_assign (T* dst, T* src)
{
tn_delete (dst->root);
dst->root = tn_dup (dst, src->root);
return TCL_OK;
}
/* .................................................. */
CONST char*
t_newnodename (T* t)
{
int ok;
Tcl_HashEntry* he;
do {
t->counter ++;
sprintf (t->handle, "node%d", t->counter);
/* Check that there is no node using that name already */
he = Tcl_FindHashEntry (&t->node, t->handle);
ok = (he == NULL);
} while (!ok);
return t->handle;
}
/* .................................................. */
void
t_dump (TPtr t, FILE* f)
{
/* Write the structural data of the
* tree (i.e. internal pointers) to
* the file, as aid in debugging
*/
Tcl_HashSearch hs;
Tcl_HashEntry* he;
TNPtr n;
fprintf (f, "T (%p) {\n",t);fflush(f);
fprintf (f, ". Lstart %p '%s'\n", t->leaves, t->leaves?Tcl_GetString(t->leaves->name):"");fflush(f);
fprintf (f, ". Nstart %p '%s'\n", t->nodes, t->nodes ?Tcl_GetString(t->nodes ->name):"");fflush(f);
for (he = Tcl_FirstHashEntry (&t->node, &hs);
he != NULL;
he = Tcl_NextHashEntry (&hs)) {
n = (TNPtr) Tcl_GetHashValue(he);
fprintf (f, ". N [%p '%s']",n,Tcl_GetString(n->name)) ;fflush(f);
fprintf (f, " %p",n->tree);fflush(f);
fprintf (f, " %p '%s'",n->prevleaf,n->prevleaf?Tcl_GetString(n->prevleaf->name):"");fflush(f);
fprintf (f, " %p '%s'",n->nextleaf,n->nextleaf?Tcl_GetString(n->nextleaf->name):"");fflush(f);
fprintf (f, " %p '%s'",n->prevnode,n->prevnode?Tcl_GetString(n->prevnode->name):"");fflush(f);
fprintf (f, " %p '%s'",n->nextnode,n->nextnode?Tcl_GetString(n->nextnode->name):"");fflush(f);
fprintf (f, " %p '%s'",n->parent ,n->parent ?Tcl_GetString(n->parent->name) :"");fflush(f);
fprintf (f, "\n");fflush(f);
}
fprintf (f, "}\n");fflush(f);
}
/* .................................................. */
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|