/*-------------------------------------------------------------------------*\ * Metametho: __eq [boolean] * Compare two edges. * Example: * e1 == e2 or e1 ~= e2 with metamethods \*-------------------------------------------------------------------------*/ static int gr_equal(lua_State *L) { gr_edge_t *ud1 = toedge(L, 1, STRICT); gr_edge_t *ud2 = toedge(L, 2, STRICT); lua_pushboolean(L, (AGID(ud1->e) == AGID(ud2->e))); return 1; }
static int gr_tail(lua_State *L) { gr_edge_t *ud = toedge(L, 1, STRICT); Agnode_t *n = ud->e->tail; if (n == NULL){ lua_pushnil(L); return 1; } else return get_object(L, n); }
/*-------------------------------------------------------------------------*\ * Determines the graph to which of a node belongs. * Returns graph userdata. * Example: * rv = n.graph * Methods: n, err = e.head(self) and n, err = e.tail(self) \*-------------------------------------------------------------------------*/ static int gr_graphof(lua_State *L) { gr_edge_t *ud = toedge(L, 1, STRICT); gr_node_t *udh; Agnode_t *head, *tail; Agraph_t *g; int rv; head = ud->e->head; tail = ud->e->tail; rv = get_object(L, head); udh = tonode(L, -rv, STRICT); g = udh->subg; lua_pop(L, rv); return get_object(L, g); }
/*-------------------------------------------------------------------------*\ * Method: e = nextedge(self, prev) * Retrieves next edge of a node. * Returns the next edge of the given node. With nil as prev the first * edge is returned. * Example: * first = n:nextedge(nil) * second = n:nextedge(first) * third = n:nextedge(second) \*-------------------------------------------------------------------------*/ static int gr_nextedge(lua_State *L) { int rv; char sbuf[32]; Agraph_t *g; Agedge_t *e; gr_edge_t *ud_e; gr_node_t *ud_n = tonode(L, 1, STRICT); g = agroot(ud_n->n); if (lua_isnil(L, 2)){ e = agfstedge(g, ud_n->n); } else { ud_e = toedge(L, 2, STRICT); e = agnxtedge(g, ud_e->e, ud_n->n); } if (!e){ /* no more edges */ lua_pushnil(L); return 1; } else { /* Check whether userdata exists .. */ rv = get_object(L, e); if (rv == 1){ /* .. yes: return it */ return rv; } else { /* .. no: create it */ lua_pop(L, rv); ud_e = lua_newuserdata(L, sizeof(gr_edge_t)); ud_e->e = e; sprintf(sbuf, "edge@%lu", (unsigned long) AGID(e)); ud_e->name = strdup(sbuf); ud_e->type = AGEDGE; ud_e->status = ALIVE; set_object(L, e); return new_edge(L); } } }
/*-------------------------------------------------------------------------*\ * Method: e.delete(self) * Delete an edge. * Returns non-nil on success. * Example: * rv, err = n:delete() \*-------------------------------------------------------------------------*/ static int gr_delete(lua_State *L) { int rv = -1; gr_edge_t *ud = toedge(L, 1, NONSTRICT); Agraph_t *g; if (ud->e != NULL){ g = ud->e->tail->graph; TRACE("e.delete(): edge: ud=%p '%s' id=0x%x e=%p\n", (void *) ud, ud->name, AGID(ud->e), (void *)ud->e); del_object(L, ud->e); agdelete(g, ud->e); ud->e = NULL; if (ud->name){ free(ud->name); ud->name = NULL; } } else { TRACE("e:delete(): ud=%p already closed\n", (void *)ud); } lua_pushnumber(L, rv); return 1; }
static int gr_nextinout(lua_State *L, edge_first_iter_t *fst, edge_next_iter_t *nxt) { int rv; Agedge_t *e; char sbuf[32]; gr_edge_t *ud_e; gr_node_t *ud_n = tonode(L, 1, STRICT); Agraph_t *g = agroot(ud_n->n); if (lua_isnil(L, 2)) e = fst(g, ud_n->n); else { ud_e = toedge(L, 2, STRICT); e = nxt(g, ud_e->e); } if (!e){ /* no more nodes */ lua_pushnil(L); return 1; } else { /* Check whether userdata exists .. */ rv = get_object(L, e); if (rv == 1) /* .. yes: return it */ return rv; else { /* .. no: create it */ lua_pop(L, rv); ud_e = lua_newuserdata(L, sizeof(gr_edge_t)); ud_e->e = e; sprintf(sbuf, "edge@%lu", (unsigned long) AGID(e)); ud_e->name = strdup(sbuf); ud_e->type = AGEDGE; set_object(L, e); return new_edge(L); } } }
/*-------------------------------------------------------------------------*\ * Property: id [number] * Get an edge's id. * Example: * n = e.id \*-------------------------------------------------------------------------*/ static int gr_id(lua_State *L) { gr_edge_t *ud = toedge(L, 1, STRICT); lua_pushnumber(L, AGID(ud->e)); return 1; }
/*-------------------------------------------------------------------------*\ * Property: name [string] * Provides the name of an edge. * Example: * name = e.name \*-------------------------------------------------------------------------*/ static int gr_nameof(lua_State *L) { gr_edge_t *ud = toedge(L, 1, STRICT); lua_pushstring(L, ud->name); return 1; }