File: node.c

package info (click to toggle)
tcllib 2.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,560 kB
  • sloc: tcl: 306,798; ansic: 14,272; sh: 3,035; xml: 1,766; yacc: 1,157; pascal: 881; makefile: 124; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (137 lines) | stat: -rw-r--r-- 3,146 bytes parent folder | download | duplicates (2)
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
/* struct::graph - critcl - layer 1 declarations
 * (b) Node operations.
 */

#include <nacommon.h>
#include <arc.h>
#include <node.h>
#include <util.h>

/* .................................................. */

GN*
gn_new (G* g, const char* name)
{
    GN* n;
    int	new;

    if (Tcl_FindHashEntry (g->nodes.map, name) != NULL) {
	Tcl_Panic ("struct::graph(c) gn_new - tried to use duplicate name for new node");
    }

    n = ALLOC (GN);

    gc_setup ((GC*) n, &g->nodes, name, g);
    gc_add   ((GC*) n, &g->nodes);

    gn_shimmer_self (n);

    n->in.first    = NULL;    n->in.n   = 0;
    n->out.first   = NULL;    n->out.n  = 0;

    return n;
}

void
gn_delete (GN* n)
{
    /* We assume that the node may still have incoming and outgoing arcs. They
     * are deleted recursively.
     */

    gc_remove ((GC*) n, &n->base.graph->nodes);
    gc_delete ((GC*) n);

    while (n->in.first)  { ga_delete (n->in.first->a);  }
    while (n->out.first) { ga_delete (n->out.first->a); }

    n->in.first   = NULL;    n->in.n  = 0;
    n->out.first  = NULL;    n->out.n = 0;

    ckfree ((char*) n);
}

/* .................................................. */

void
gn_err_duplicate (Tcl_Interp* interp, Tcl_Obj* n, Tcl_Obj* g)
{
    Tcl_Obj* err = Tcl_NewObj ();

    Tcl_AppendToObj    (err, "node \"", TCL_AUTO_LENGTH); /* OK tcl9 */
    Tcl_AppendObjToObj (err, n);
    Tcl_AppendToObj    (err, "\" already exists in graph \"", TCL_AUTO_LENGTH); /* OK tcl9 */
    Tcl_AppendObjToObj (err, g);
    Tcl_AppendToObj    (err, "\"", TCL_AUTO_LENGTH); /* OK tcl9 */
	    
    Tcl_SetObjResult (interp, err);
}

void
gn_err_missing (Tcl_Interp* interp, Tcl_Obj* n, Tcl_Obj* g)
{
    Tcl_Obj* err = Tcl_NewObj ();

    /* Keep any prefix ... */
    Tcl_AppendObjToObj (err, Tcl_GetObjResult (interp));
    Tcl_AppendToObj    (err, "node \"", TCL_AUTO_LENGTH); /* OK tcl9 */
    Tcl_AppendObjToObj (err, n);
    Tcl_AppendToObj    (err, "\" does not exist in graph \"", TCL_AUTO_LENGTH); /* OK tcl9 */
    Tcl_AppendObjToObj (err, g);
    Tcl_AppendToObj    (err, "\"", TCL_AUTO_LENGTH); /* OK tcl9 */

    Tcl_SetObjResult (interp, err);
}

/* .................................................. */

Tcl_Obj*
gn_serial_arcs (GN* n, Tcl_Obj* empty, Tcl_HashTable* cn)
{
    Tcl_Size  lc, i;
    Tcl_Obj** lv;
    Tcl_Obj*  arcs;
    GL*       il;
    GA*       a;
    long int  id;
    Tcl_HashEntry* he;

    /* Quick return if node has no outgoing arcs */

    if (!n->out.n) return empty;

    lc = n->out.n;
    lv = NALLOC (lc, Tcl_Obj*);

    for (i=0, il = n->out.first;
	 il != NULL;
	 il = il->next) {
	a = il->a;
	he = Tcl_FindHashEntry (cn, (char*) a->end->n);

	/* Ignore arcs which lead out of the subgraph spanned up by the nodes
	 * in 'cn'.
	 */

	if (!he) continue;
	ASSERT_BOUNDS(i, lc);
	id = (long int) Tcl_GetHashValue (he);
	lv [i] = ga_serial (a, empty, id);
	i++;
    }
    lc = i;

    arcs = Tcl_NewListObj (lc, lv); /* OK tcl9 */
    ckfree ((char*) lv);
    return arcs;
}

/* .................................................. */

/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 78
 * End:
 */