1 /*=========================================================================*\
3 * Graph support for Lua.
7 * Edge related functionality.
9 \*=========================================================================*/
11 /*=========================================================================*\
13 \*=========================================================================*/
22 /*=========================================================================*\
24 \*=========================================================================*/
26 /*=========================================================================*\
28 \*=========================================================================*/
29 static int gr_equal(lua_State
*L
);
30 static int gr_nameof(lua_State
*L
);
31 static int gr_id(lua_State
*L
);
32 static int gr_delete(lua_State
*L
);
33 static int gr_head(lua_State
*L
);
34 static int gr_tail(lua_State
*L
);
35 static int gr_info(lua_State
*L
);
36 static int gr_graphof(lua_State
*L
);
37 static int gr_tostring(lua_State
*L
);
39 /*=========================================================================*\
41 \*=========================================================================*/
46 static const luaL_Reg reg_methods
[] = {
47 {"delete", gr_delete
},
48 {"type", get_object_type
},
57 static const luaL_Reg reg_rmembers
[] = {
62 {"graph", gr_graphof
},
63 {"status", gr_status
},
68 * Standard metamethods
70 static const luaL_Reg reg_metamethods
[] = {
73 {"__newindex", object_newindex_handler
},
74 {"__tostring", gr_tostring
},
78 /*=========================================================================*\
80 \*=========================================================================*/
81 /*-------------------------------------------------------------------------*\
83 \*-------------------------------------------------------------------------*/
84 int new_edge(lua_State
*L
){
85 return new_object(L
, "edge", reg_rmembers
, reg_methods
, reg_metamethods
,
86 object_index_handler
);
89 /*-------------------------------------------------------------------------*\
90 * Metametho: __eq [boolean]
93 * e1 == e2 or e1 ~= e2 with metamethods
94 \*-------------------------------------------------------------------------*/
95 static int gr_equal(lua_State
*L
)
97 gr_edge_t
*ud1
= toedge(L
, 1, STRICT
);
98 gr_edge_t
*ud2
= toedge(L
, 2, STRICT
);
99 lua_pushboolean(L
, ageqedge(ud1
->e
, ud2
->e
));
103 /*-------------------------------------------------------------------------*\
104 * Property: name [string]
105 * Provides the name of an edge.
108 \*-------------------------------------------------------------------------*/
109 static int gr_nameof(lua_State
*L
)
111 gr_edge_t
*ud
= toedge(L
, 1, STRICT
);
112 if (ud
->status
!= ALIVE
)
113 luaL_error(L
, "deleted");
114 lua_pushstring(L
, ud
->name
);
118 /*-------------------------------------------------------------------------*\
119 * Property: id [number]
123 \*-------------------------------------------------------------------------*/
124 static int gr_id(lua_State
*L
)
126 gr_edge_t
*ud
= toedge(L
, 1, STRICT
);
127 lua_pushnumber(L
, AGID(ud
->e
));
131 /*-------------------------------------------------------------------------*\
132 * Method: e.delete(self)
134 * Returns non-nil on success.
136 * rv, err = n:delete()
137 \*-------------------------------------------------------------------------*/
138 static int gr_delete(lua_State
*L
)
141 gr_edge_t
*ud
= toedge(L
, 1, NONSTRICT
);
145 if (ud
->status
== ALIVE
){
146 TRACE(" e.delete(): deleting edge '%s' ud=%p id=0x%lx e=%p (%s %d)\n",
147 agnameof(ud
->e
), (void *) ud
, (unsigned long) AGID(ud
->e
), (void *)ud
->e
, __FILE__
, __LINE__
);
151 TRACE(" e.delete(): ud=%p already closed (%s %d)\n", (void *)ud
, __FILE__
, __LINE__
);
153 lua_pushnumber(L
, rv
);
158 * Provided for external access to gr_delete function.
160 int gr_delete_edge(lua_State
*L
)
165 /*-------------------------------------------------------------------------*\
166 * Print info about a node to stdout.
169 \*-------------------------------------------------------------------------*/
170 static int gr_info(lua_State
*L
)
172 gr_edge_t
*ud
= toedge(L
, 1, STRICT
);
173 Agnode_t
*head
= aghead(ud
->e
);
174 Agnode_t
*tail
= agtail(ud
->e
);
175 Agedge_t
*cedge
= agopp(ud
->e
);
176 Agnode_t
*chead
= aghead(cedge
);
177 Agnode_t
*ctail
= agtail(cedge
);
178 printf("INFO EDGE:\n");
179 printf(" label: '%s' '%s'\n", agnameof(ud
->e
), ud
->name
);
180 printf(" ptr : %p\n", (void *)ud
->e
);
181 printf(" name: %s\n", agnameof(ud
->e
));
182 printf(" head: %s\n", agnameof(head
));
183 printf(" tail: %s\n", agnameof(tail
));
184 printf(" cptr: %p\n", (void*)cedge
);
185 printf(" cname: %s\n", agnameof(cedge
));
186 printf(" chead: %s\n", agnameof(chead
));
187 printf(" ctail: %s\n", agnameof(ctail
));
191 /*-------------------------------------------------------------------------* \
192 * Methods: n, err = e.head(self) and n, err = e.tail(self)
193 * Retrieves head and tail node of an edge.
194 * Returns node userdata on success.
196 * head, err = e:head()
197 * tail ,err = e.tail()
198 \*-------------------------------------------------------------------------*/
199 static int gr_head(lua_State
*L
)
201 gr_edge_t
*ud
= toedge(L
, 1, STRICT
);
202 Agnode_t
*n
= aghead(ud
->e
);
207 return get_object(L
, n
);
210 static int gr_tail(lua_State
*L
)
212 gr_edge_t
*ud
= toedge(L
, 1, STRICT
);
213 Agnode_t
*n
= agtail(ud
->e
);
218 return get_object(L
, n
);
221 /*-------------------------------------------------------------------------*\
222 * Determines the graph to which of a edge belongs.
223 * Returns graph userdata.
226 * Methods: n, err = e.head(self) and n, err = e.tail(self)
227 \*-------------------------------------------------------------------------*/
228 static int gr_graphof(lua_State
*L
)
230 gr_edge_t
*ud
= toedge(L
, 1, STRICT
);
231 Agraph_t
*g
= agraphof(ud
->e
);
234 lua_pushstring(L
, "no graph");
237 return get_object(L
, g
);
240 static int gr_tostring(lua_State
*L
)
242 gr_object_t
*ud
= toobject(L
, 1, NULL
, NONSTRICT
);
243 lua_pushfstring(L
, "edge: %p (%s)", ud
, ud
->p
.status
== ALIVE
? "alive" : "dead");