Browse Source

Add graph unique terms.

scossu 2 weeks ago
parent
commit
11007fef86
4 changed files with 35 additions and 14 deletions
  1. 2 2
      lua/Makefile
  2. 10 0
      lua/scratch.lua
  3. 20 2
      lua/src/lua_graph.c
  4. 3 10
      lua/src/lua_term.c

+ 2 - 2
lua/Makefile

@@ -25,5 +25,5 @@ clean:
 .PHONY: memcheck
 memcheck:
 	valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes \
-	--suppressions=valgrind-lua.supp \
-	lua -i scratch.lua
+	--suppressions=valgrind-lua.supp --log-fd=9 \
+	lua -i scratch.lua 9>|/tmp/lua_lsup_valgrind.log

+ 10 - 0
lua/scratch.lua

@@ -32,4 +32,14 @@ print("Connections")
 for t1, ts in pairs(lm) do
     for t2 in pairs(ts) do print(t1, t2) end
 end
+
+unique_t = gr1:unique_terms(triple.POS_S)
+print("Unique subjects")
+for t in pairs(unique_t) do print(t) end
+print("Unique predicates")
+unique_t = gr1:unique_terms(triple.POS_P)
+for t in pairs(unique_t) do print(t) end
+print("Unique objects")
+unique_t = gr1:unique_terms(triple.POS_O)
+for t in pairs(unique_t) do print(t) end
 --]]

+ 20 - 2
lua/src/lua_graph.c

@@ -238,8 +238,7 @@ static int l_graph_connections (lua_State *L)
     const LSUP_LinkType type = luaL_checkinteger (L, 3);
     LOG_DEBUG ("Adding term for connections: @%p", *t);
 
-    LSUP_LinkMap **lm_p =
-        (LSUP_LinkMap **)lua_newuserdata (L, sizeof *lm_p);
+    LSUP_LinkMap **lm_p = lua_newuserdata (L, sizeof *lm_p);
     *lm_p = LSUP_graph_connections (gr, t, type);
     luaL_getmetatable (L, "LSUP.LinkMap");
     lua_setmetatable (L, -2);
@@ -251,6 +250,17 @@ static int l_graph_connections (lua_State *L)
 static int l_graph_term_set (lua_State *L)
 {
     const LSUP_Graph *gr = check_graph (L);
+    const LSUP_Term *t1 = *(LSUP_Term **)luaL_checkudata (L, 2, "LSUP.Term");
+    const LSUP_TriplePos t1_pos = luaL_checkinteger (L, 3);
+    const LSUP_Term *t2 = *(LSUP_Term **)luaL_checkudata (L, 3, "LSUP.Term");
+    const LSUP_TriplePos t2_pos = luaL_checkinteger (L, 4);
+
+    LSUP_TermSet **ts_p = lua_newuserdata (L, sizeof *ts_p);
+    luaL_getmetatable (L, "LSUP.TermSet");
+    lua_setmetatable (L, -2);
+
+    *ts_p = LSUP_graph_term_set (gr, t1, t1_pos, t2, t2_pos);
+    LUA_NLCHECK (*ts_p, "Error creating term set.");
 
     return 1;
 }
@@ -259,6 +269,14 @@ static int l_graph_term_set (lua_State *L)
 static int l_graph_unique_terms (lua_State *L)
 {
     const LSUP_Graph *gr = check_graph (L);
+    const LSUP_TriplePos pos = luaL_checkinteger (L, 2);
+
+    LSUP_TermSet **ts_p = lua_newuserdata (L, sizeof *ts_p);
+    luaL_getmetatable (L, "LSUP.TermSet");
+    lua_setmetatable (L, -2);
+
+    *ts_p = LSUP_graph_unique_terms (gr, pos);
+    LUA_NLCHECK (*ts_p, "Error creating term set.");
 
     return 1;
 }

+ 3 - 10
lua/src/lua_term.c

@@ -148,10 +148,7 @@ static int l_term_equals (lua_State *L)
 static int l_term_gc (lua_State *L)
 {
     LSUP_Term **tp = luaL_checkudata(L, 1, "LSUP.Term");
-    // FIXME This is to prevent a double-free on shutdown.
-    // Must find the culprit instead.
-    if (UNLIKELY (!tp || !*tp)) return 0;
-    LOG_DEBUG ("Garbage collecting term @%p", *tp);
+    LOG_TRACE ("Garbage collecting term @%p", *tp);
 
     LSUP_term_free (*tp);
     *tp = NULL;
@@ -333,8 +330,7 @@ static int term_set_iter_next (lua_State *L)
         *(LSUP_TermSet **)lua_touserdata (L, lua_upvalueindex (1));
     size_t *ip = lua_touserdata (L, lua_upvalueindex (2));
 
-    LSUP_Term **tp = allocate_term (L);
-    LSUP_Term *tmp = NULL;
+    LSUP_Term *tmp;
     LSUP_rc rc = LSUP_term_set_next (ts, ip, &tmp);
 
     LUA_PCHECK (rc, "Error iterating over term set");
@@ -344,6 +340,7 @@ static int term_set_iter_next (lua_State *L)
         return 0;
     }
 
+    LSUP_Term **tp = allocate_term (L);
     *tp = LSUP_term_copy (tmp);
     if (!*tp) {
         free (ip);
@@ -571,8 +568,6 @@ int luaopen_lsup_term (lua_State *L)
      */
     // Term set.
     luaL_newmetatable (L, "LSUP.TermSet");
-    lua_pushvalue (L, -1);
-    lua_setfield (L, -2, "__index");
     lua_pushcfunction (L, term_set_gc);
     lua_setfield (L, -2, "__gc");
     lua_pushcfunction (L, l_term_set_iter_init);
@@ -580,8 +575,6 @@ int luaopen_lsup_term (lua_State *L)
 
     // Link map.
     luaL_newmetatable (L, "LSUP.LinkMap");
-    lua_pushvalue (L, -1);
-    lua_setfield (L, -2, "__index");
     lua_pushcfunction (L, link_map_gc);
     lua_setfield (L, -2, "__gc");
     lua_pushcfunction (L, l_lmap_iter_init);