fixed version in rockspec
[luagraph.git] / src / gr_edge.c
blob93ca25da340370a075470de708b11ef8c8e77131
1 /*=========================================================================*\
2 * LuaGRAPH toolkit
3 * Graph support for Lua.
4 * Herbert Leuwer
5 * 30-7-2006, 01/2017
7 * Edge related functionality.
9 \*=========================================================================*/
11 /*=========================================================================*\
12 * Includes
13 \*=========================================================================*/
14 #include <string.h>
15 #include <stdlib.h>
17 #include "lua.h"
18 #include "lauxlib.h"
20 #include "gr_graph.h"
22 /*=========================================================================*\
23 * Defines
24 \*=========================================================================*/
26 /*=========================================================================*\
27 * Prototypes
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 /*=========================================================================*\
40 * Data
41 \*=========================================================================*/
44 * Edge object methods
46 static const luaL_Reg reg_methods[] = {
47 {"delete", gr_delete},
48 {"type", get_object_type},
49 {"rawget", getval},
50 {"info", gr_info},
51 {NULL, NULL}
55 * Read-only members
57 static const luaL_Reg reg_rmembers[] = {
58 {"head", gr_head},
59 {"tail", gr_tail},
60 {"name", gr_nameof},
61 {"id", gr_id},
62 {"graph", gr_graphof},
63 {"status", gr_status},
64 {NULL, NULL}
68 * Standard metamethods
70 static const luaL_Reg reg_metamethods[] = {
71 {"__gc", gr_collect},
72 {"__eq", gr_equal},
73 {"__newindex", object_newindex_handler},
74 {"__tostring", gr_tostring},
75 {NULL, NULL}
78 /*=========================================================================*\
79 * Functions
80 \*=========================================================================*/
81 /*-------------------------------------------------------------------------*\
82 * Utility Functions
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]
91 * Compare two edges.
92 * Example:
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));
100 return 1;
103 /*-------------------------------------------------------------------------*\
104 * Property: name [string]
105 * Provides the name of an edge.
106 * Example:
107 * name = e.name
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);
115 return 1;
118 /*-------------------------------------------------------------------------*\
119 * Property: id [number]
120 * Get an edge's id.
121 * Example:
122 * n = e.id
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));
128 return 1;
131 /*-------------------------------------------------------------------------*\
132 * Method: e.delete(self)
133 * Delete an edge.
134 * Returns non-nil on success.
135 * Example:
136 * rv, err = n:delete()
137 \*-------------------------------------------------------------------------*/
138 static int gr_delete(lua_State *L)
140 int rv = -1;
141 gr_edge_t *ud = toedge(L, 1, NONSTRICT);
142 Agraph_t *g;
143 if (ud->e != NULL){
144 g = agroot(ud->e);
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__);
148 agdeledge(g, ud->e);
150 } else {
151 TRACE(" e.delete(): ud=%p already closed (%s %d)\n", (void *)ud, __FILE__, __LINE__);
153 lua_pushnumber(L, rv);
154 return 1;
158 * Provided for external access to gr_delete function.
160 int gr_delete_edge(lua_State *L)
162 return gr_delete(L);
165 /*-------------------------------------------------------------------------*\
166 * Print info about a node to stdout.
167 * Example:
168 * e:info()
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));
188 return 0;
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.
195 * Example:
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);
203 if (n == NULL){
204 lua_pushnil(L);
205 return 1;
206 } else
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);
214 if (n == NULL){
215 lua_pushnil(L);
216 return 1;
217 } else
218 return get_object(L, n);
221 /*-------------------------------------------------------------------------*\
222 * Determines the graph to which of a edge belongs.
223 * Returns graph userdata.
224 * Example:
225 * rv = n.graph
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);
232 if (g == NULL){
233 lua_pushnil(L);
234 lua_pushstring(L, "no graph");
235 return 2;
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");
244 return 1;