oo-browser / tree-x / tree.c

  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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/* ----------------------------------------------------------------------------
 * File    : tree.c
 * Purpose : dynamic tree program based on Sven Moen's algorithm
 * ----------------------------------------------------------------------------
 */

#include "defs.h"
#include "tree.h"
#include "dbl.h"
#include "intf.h"
#include <string.h>
#include <stdlib.h>

/* ------------------------------------------------------------------------- */
/*				Global Variables                             */
/* ------------------------------------------------------------------------- */

static int NumLines = 0;
static int NumNodes = 0;


/* ----------------------------------------------------------------------------
 *
 *   MakeLine() allocates the memory required for a Polyline and
 *   initializes the fields of a Polyline to the arguments. The
 *   newly-allocated Polyline is returned by the function.
 *
 * ----------------------------------------------------------------------------
 */

Polyline*
MakeLine(short dx, short dy, Polyline *line)
{
   Polyline *new_line = (Polyline *) malloc(sizeof(Polyline));
   
   NASSERT(new_line, "could not allocate memory for polyline");
   NumLines++;

   new_line->dx = dx;
   new_line->dy = dy;
   new_line->link = line;

   return new_line;
}

/* ----------------------------------------------------------------------------
 *
 *   MakeNode() allocates the memory required for a tree node, and
 *   zeros out all the fields in the Node. It returns a pointer to the
 *   tree node upon success, and NULL upon failure.
 *
 * ----------------------------------------------------------------------------
 */

Tree*
MakeNode(void)
{
   Tree *node = (Tree *) malloc(sizeof(Tree));
   
   NASSERT(node, "could not allocate memory for node");
   NumNodes++;

   if (node == NULL)
      return NULL;
   else {
     memset((char *) node, 0, sizeof(Tree));
     return node;
   }
}

/* ----------------------------------------------------------------------------
 *
 *   MakeBridge()
 *
 * ----------------------------------------------------------------------------
 */

static Polyline*
MakeBridge(Polyline *line1, int x1, int y1, Polyline *line2, int x2, int y2)
{
   int dx, dy, s;
   Polyline *r;

   dx = x2 + line2->dx - x1;
   if (line2->dx == 0)
      dy = line2->dy;
   else {
      s = dx * line2->dy;
      dy = s / line2->dx;
   }
   r = MakeLine(dx, dy, line2->link);
   line1->link = MakeLine(0, y2 + line2->dy - dy - y1, r);

   return r;
}

/* ----------------------------------------------------------------------------
 *
 *   Offset() computes the necessary offset that prevents two line segments
 *   from intersecting each other. This is the "heart" of the merge step
 *   that computes how two subtree contours should be separated.
 *
 *   The code is taken directly from Sven Moen's paper, with changes in
 *   some variable names to give more meaning:
 *
 *   - px,py indicate the x- and y-coordinates of the point on the longer
 *     segment if the previous Offset() call had two unequal segments
 *
 *   - lx,ly indicate the dx and dy values of the "lower" line segment
 *
 *   - ux,uy indicate the dx and dy values of the "upper" line segment
 *
 * ----------------------------------------------------------------------------
 */

static int
Offset(int px, int py, int lx, int ly, int ux, int uy)
{
   int d, s, t;

   if (ux <= px || px+lx <= 0)
      return 0;

   t = ux*ly - lx*uy;

   if (t > 0) {
      if (px < 0) {
	 s = px*ly;
	 d = s/lx - py;
      }
      else if (px > 0) {
	 s = px*uy;
	 d = s/ux - py;
      }
      else {
	 d = -py;
      }
   }
   else {
      if (ux < px+lx) {
	 s = (ux-px) * ly;
	 d = uy - (py + s/lx);
      }
      else if (ux > px+lx) {
	 s = (lx+px) * uy;
	 d = s/ux - (py+ly);
      }
      else {
	 d = uy - (py+ly);
      }
   }

   return MAX(0, d);
}

/* ----------------------------------------------------------------------------
 *
 *   Merge()
 *
 * ----------------------------------------------------------------------------
 */

static int
Merge(Polygon *c1, Polygon *c2)
{
   int x, y, total, d;
   Polyline *lower, *upper, *bridge;

   x = y = total = 0;

   /*  compare lower part of upper child's contour
    *  with upper part of lower child's contour
    */
   upper = c1->lower.head;
   lower = c2->upper.head;

   while (lower && upper) {
      d = Offset(x, y, lower->dx, lower->dy, upper->dx, upper->dy);
      y += d;
      total += d;

      if (x + lower->dx <= upper->dx) {
	 x += lower->dx;
	 y += lower->dy;
	 lower = lower->link;
      }
      else {
	 x -= upper->dx;
	 y -= upper->dy;
	 upper = upper->link;
      }
   }

   if (lower) {
      bridge = MakeBridge(c1->upper.tail, 0, 0, lower, x, y);
      c1->upper.tail = (bridge->link) ? c2->upper.tail : bridge;
      c1->lower.tail = c2->lower.tail;
   }
   else {
      bridge = MakeBridge(c2->lower.tail, x, y, upper, 0, 0);
      if (!bridge->link)
	 c1->lower.tail = bridge;
   }
   c1->lower.head = c2->lower.head;

   return total;
}

/* ----------------------------------------------------------------------------
 *
 *   DetachParent() reverses the effects of AttachParent by removing
 *   the four line segments that connect the subtree contour to the
 *   node specified by 'tree'.
 *
 * ----------------------------------------------------------------------------
 */

static void
DetachParent(Tree *tree)
{
   free(tree->contour.upper.head->link);
   free(tree->contour.upper.head);
   tree->contour.upper.head = NULL;
   tree->contour.upper.tail = NULL;

   free(tree->contour.lower.head->link);
   free(tree->contour.lower.head);
   tree->contour.lower.head = NULL;
   tree->contour.lower.tail = NULL;

   NumLines -= 4;
}

/* ----------------------------------------------------------------------------
 *
 *   AttachParent()
 *   This function also establishes the position of the first child
 *   The code follows Sven Moen's version, with slight modification to
 *   support varying borders at different levels.
 *
 * ----------------------------------------------------------------------------
 */

static void
AttachParent(Tree *tree, int h)
{
   int x, y1, y2;

   if (TreeAlignNodes)
      x = tree->border + (TreeParentDistance * 2) +
	 (TreeParentDistance - tree->width);
   else
      x = tree->border + TreeParentDistance;
   y2 = (h - tree->height)/2 - tree->border;
   y1 = y2 + tree->height + (2 * tree->border) - h;
   tree->child->offset.x = x + tree->width;
   tree->child->offset.y = y1;
   tree->contour.upper.head = MakeLine(tree->width, 0,
				       MakeLine(x, y1,
						tree->contour.upper.head));
   tree->contour.lower.head = MakeLine(tree->width, 0,
				       MakeLine(x, y2,
						tree->contour.lower.head));
}

/* ----------------------------------------------------------------------------
 *
 *   Split()
 *   The tree passed to Split() must have at least 1 child, because
 *   it doesn't make sense to split a leaf (there are no bridges)
 *
 * ----------------------------------------------------------------------------
 */

static void
Split(Tree *tree)
{
   Tree *child;
   Polyline *link;

   FOREACH_CHILD(child, tree) {
      if ((link = child->contour.upper.tail->link)) {
	 free(link->link);
	 free(link);
	 child->contour.upper.tail->link = NULL;
	 NumLines -= 2;
      }
      if ((link = child->contour.lower.tail->link)) {
	 free(link->link);
	 free(link);
	 NumLines -= 2;
	 child->contour.lower.tail->link = NULL;
      }
   }
}

/* ----------------------------------------------------------------------------
 *
 *   Join() merges all subtree contours of the given tree and returns the
 *   height of the entire tree contour.
 *
 * ----------------------------------------------------------------------------
 */

static int
Join(Tree *tree)
{
   Tree *child;
   int d, h, sum;

   /*   to start, set the parent's contour and height
    *   to contour and height of first child
    */
   child = tree->child;
   tree->contour = child->contour;
   sum = h = child->height + (2 * child->border);

   /* extend contour to include contours of all children of parent */
   for (child = child->sibling ; child ; child = child->sibling) {
      d = Merge(&tree->contour, &child->contour);
      child->offset.y = d + h;
      child->offset.x = 0;
      h = child->height + (2 * child->border);
      /* keep cumulative heights of subtree contours */
      sum += d + h;
   }
   return sum;
}

/* ----------------------------------------------------------------------------
 *
 *   RuboutLeaf() accepts a single node (leaf) and removes its contour.
 *   The memory associated with the contour is deallocated.
 *
 * ----------------------------------------------------------------------------
 */

void
RuboutLeaf(Tree *tree)
{
   free(tree->contour.upper.head);
   free(tree->contour.lower.tail);
   free(tree->contour.lower.head);
   tree->contour.upper.head = NULL;
   tree->contour.upper.tail = NULL;
   tree->contour.lower.head = NULL;
   tree->contour.lower.tail = NULL;
   NumLines -= 3;
}

/* ----------------------------------------------------------------------------
 *
 *   LayoutLeaf() accepts a single node (leaf) and forms its contour. This
 *   function assumes that the width, height, and border fields of the
 *   node have been assigned meaningful values.
 *
 * ----------------------------------------------------------------------------
 */

void
LayoutLeaf(Tree *tree)
{
   tree->node_height = 0;
   tree->border = TreeBorderSize;

   tree->contour.upper.tail = MakeLine(tree->width + 2 * tree->border, 0,
				       NULL);
   tree->contour.upper.head = tree->contour.upper.tail;

   tree->contour.lower.tail = MakeLine(0, -tree->height - 2 * tree->border,
				       NULL);
   tree->contour.lower.head = MakeLine(tree->width + 2 * tree->border, 0,
				       tree->contour.lower.tail);

}

/* ----------------------------------------------------------------------------
 *
 *   LayoutTree() traverses the given tree (in depth-first order), and forms
 *   subtree or leaf contours at each node as needed. Each node's contour is
 *   stored in its "contour" field. Elision is also supported by generating
 *   the contour for both the expanded and collapsed node. This routine
 *   also computes the tree height of each node in the tree, so that variable
 *   density layout can be demonstrated.
 * 
 * ----------------------------------------------------------------------------
 */

void
LayoutTree(Tree *tree)
{
   Tree *child;
   int   height = 0;

   FOREACH_CHILD(child, tree) {
      LayoutTree(child);

      if (child->elision) {	/* support elision */
	 child->old_contour = child->contour;
	 LayoutLeaf(child);
      }

   }

   if (tree->child) {

      FOREACH_CHILD(child, tree)
	 height = MAX(child->node_height, height);
      tree->node_height = height + 1;

      if (TreeLayoutDensity == Fixed)
	 tree->border = TreeBorderSize;
      else
	 tree->border =
	    (int) (TreeBorderSize * (tree->node_height * DENSITY_FACTOR));

      AttachParent(tree, Join(tree));
   }
   else
      LayoutLeaf(tree);
}

/* ------------------------------------------------------------------------- */

void
Unzip(Tree *tree)
{
   Tree *child;

#ifdef INTF
   if (TreeShowSteps) {
      HiliteNode(tree, New);
      tree->on_path = TRUE;
      StatusMsg("Unzip: follow parent links up to root", 0);
      Pause();
   }
#endif

   if (tree->parent)
      Unzip(tree->parent);

   if (tree->child) {

#ifdef INTF
      /*   draw entire contour; do it only for root, because the last
       *   frame drawn in this function will have already drawn the
       *   contour for the most recently split subtree.
       */
      if (TreeShowSteps) {
	 if (tree->parent == NULL) {
	    BeginFrame();
	      DrawTreeContour(tree, New, CONTOUR_COLOR, FALSE, FALSE, FALSE);
	      DrawTree(TheTree, New);
	    EndFrame();
	    StatusMsg("Unzip: disassemble entire contour", 0);
	    Pause();
	 }
      }
#endif

#ifdef INTF
      /* draw contour as it would appear after DetachParent() */
      if (TreeShowSteps) {
	 BeginFrame();
#if 0 /* mrb */
	   DrawTreeContour(tree, New, CONTOUR_COLOR, TRUE,
			   FALSE, FALSE, FALSE);
#endif
	   DrawTreeContour(tree, New, CONTOUR_COLOR, TRUE,
			   FALSE, FALSE);
	   DrawTree(TheTree, New);
	 EndFrame();
	 StatusMsg("Unzip: detach parent", 0);
	 Pause();
      }
#endif

      DetachParent(tree);
      Split(tree);

#ifdef INTF
      if (TreeShowSteps) {
	 BeginFrame();
           /* mark other subtree contours as split, and */
	   /* draw only the contour on path in full     */
	   FOREACH_CHILD(child, tree) {
	      if (!child->on_path)
		 child->split = TRUE;
	      else
		 DrawTreeContour(child, New, CONTOUR_COLOR,
				 FALSE, FALSE, FALSE);
	   }
	   DrawTree(TheTree, New);
	 EndFrame();
	 StatusMsg("Unzip: split tree", 0);
	 Pause();
      }
#endif

   }
   else
      RuboutLeaf(tree);		/* leaf node */
}

/* ------------------------------------------------------------------------- */

void
Zip(Tree *tree)
{
   if (tree->child)
      AttachParent(tree, Join(tree));
   else
      LayoutLeaf(tree);

   if (tree->parent)
      Zip(tree->parent);
}

/* ----------------------------------------------------------------------------
 *
 *   Insert() adds the specified child to parent, just after the specified
 *   sibling. If 'sibling' is Null, the child is added as the first child.
 *
 * ----------------------------------------------------------------------------
 */

void
Insert(Tree *parent, Tree *child, Tree *sibling)
{
   Unzip(parent);
   child->parent = parent;
   if (sibling) {
      child->sibling = sibling->sibling;
      sibling->sibling = child;
   }
   else {
      child->sibling = parent->child;
      parent->child = child;
   }
   Zip(parent);
}




/* ----------------------------------------------------------------------------
 *
 *   Delete() traverses the specified tree and frees all storage
 *   allocated to the subtree, including contours and bridges.
 *   If the tree had a preceding sibling, the preceding sibling is
 *   modified to point to the tree's succeeding sibling, if any.
 *
 * ----------------------------------------------------------------------------
 */

void
Delete(Tree *tree)
{
   Tree *sibling = NULL;
   Tree *parent, *child;

   /* find sibling */
   parent = tree->parent;
   if (parent) {
      FOREACH_CHILD(child, parent)
	 if (child->sibling == tree) {
	    sibling = child;
	    break;
	 }
   }
   if (sibling)
      sibling->sibling = tree->sibling;
   else if (parent)
      parent->child = tree->sibling;

   DeleteTree(tree, FALSE);
}


/* ----------------------------------------------------------------------------
 *
 *   DeleteTree() is the recursive function that supports Delete().
 *   If 'contour' is True, then only the contours are recursively deleted.
 *   This flag should be True when you are regenerating a tree's layout
 *   and still want to preserve the nodes. Since contours would be deleted
 *   only due to a change in sibling or level distance, each node's border
 *   value is updated with the current value of TreeBorderSize;
 *
 * ----------------------------------------------------------------------------
 */

void
DeleteTree(Tree *tree, int contour)
{
   Tree *child;

   if (tree->elision) {
      RuboutLeaf(tree);
      tree->contour = tree->old_contour;
      tree->old_contour.upper.head = NULL;    /* flag to note 'NULL' contour */
   }

   if (!IS_LEAF(tree)) {
      DetachParent(tree);
      Split(tree);

#if 0
      /* This macro makes a child->sibling reference
         after the child has been deleted, so don't
         use it.  - kkm@kis.ru, 4/9/1998  */
      FOREACH_CHILD(child,tree)
	 DeleteTree(child, contour);
#else
      child = tree->child;
      while (child)
	{
	  Tree* next = child->sibling;
	  DeleteTree (child, contour);
	  child = next;
	}
#endif
   }
   else
      RuboutLeaf(tree);

   if (contour)
      tree->border = TreeBorderSize;
   else {
      free(tree->label.text);
      free(tree);
      NumNodes--;
   }
}


/* ----------------------------------------------------------------------------
 *
 *   ComputeTreeSize()
 *   This function should be called after tree layout.
 *
 * ----------------------------------------------------------------------------
 */

void
ComputeTreeSize(Tree *tree,
		int *width, int *height,
		int *x_offset, int *y_offset)
{
   Polyline *contour, *tail;
   int upper_min_y = 0, lower_max_y = 0;
   int upper_abs_y = 0, lower_abs_y = 0;
   int x = 0;

   /* do upper contour */
   contour = tree->contour.upper.head;
   tail    = tree->contour.upper.tail;
   while (contour) {
      if ((contour->dy + upper_abs_y) < upper_min_y)
	 upper_min_y = contour->dy + upper_abs_y;
      upper_abs_y += contour->dy;
      if (contour == tail)
	 contour = NULL;
      else
	 contour = contour->link;
   }

   /* do lower contour */
   contour = tree->contour.lower.head;
   tail    = tree->contour.lower.tail;
   while (contour) {
      if ((contour->dy + lower_abs_y) > lower_max_y)
	 lower_max_y = contour->dy + lower_abs_y;
      lower_abs_y += contour->dy;
      x += contour->dx;
      if (contour == tail)
	 contour = NULL;
      else
	 contour = contour->link;
   }

   *width = x + 1;
   *height = lower_max_y - upper_min_y +
             (tree->height + (2 * tree->border)) + 1;
   if (x_offset)
      *x_offset = tree->border;
   if (y_offset)
      *y_offset = - upper_min_y + tree->border;
}

/* ----------------------------------------------------------------------------
 *
 *   PetrifyTree()
 *
 * ----------------------------------------------------------------------------
 */

void
PetrifyTree(Tree *tree, int x, int y)
{
   tree->old_pos = tree->pos;	/* used by AnimateTree */

   /* fix position of each node */
   tree->pos.x = x + tree->offset.x;
   tree->pos.y = y + tree->offset.y;

   if (tree->child) {
      PetrifyTree(tree->child, tree->pos.x, tree->pos.y);
      ComputeSubTreeExtent(tree); /* for benefit of interface picking */
   }
   if (tree->sibling)
      PetrifyTree(tree->sibling, tree->pos.x, tree->pos.y);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.