From b4494bf276f50775f7a6193075c7fb11a1ab99b3 Mon Sep 17 00:00:00 2001 From: shewer Date: Thu, 26 Sep 2024 17:44:45 +0800 Subject: [PATCH] added new function of SpansReg Signed-off-by: shewer --- src/types.cc | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/types.cc b/src/types.cc index bd07922..246c47c 100644 --- a/src/types.cc +++ b/src/types.cc @@ -272,7 +272,9 @@ namespace CandidateReg { if (auto phrase = As(Candidate::GetGenuineCandidate(cand))) { return phrase->spans(); } - return Spans(); + Spans spans; + spans.AddSpan(cand->start(), cand->end()); + return spans; } template @@ -2039,38 +2041,34 @@ namespace SpansReg { return spans.Count(start, end); } - vector get_vertices(T &spans) { + vector get_vertices(const T &spans) { vector res; - if (spans.HasVertex(0)) { - size_t end = spans.end(); - for (size_t stop =0; stop <= end; stop = spans.NextStop(stop)) { + size_t end = spans.end(); + for (size_t stop =spans.start(); ; stop = spans.NextStop(stop)) { + if (spans.HasVertex(stop)) { res.push_back(stop); - if (stop == end) - break; + } + if (stop == end) { + break; } } return res; } int raw_set_vertices(lua_State *L) { + C_State C; + auto &spans = LuaType::todata(L, 1); if (lua_istable(L, 2)) { - Spans &spans = LuaType::todata(L, 1); spans.Clear(); - for (int i=1;;i++) { - lua_rawgeti(L, 2,i); - if (lua_isnil(L, -1)) { - break; - } - else if (!lua_isinteger(L, -1)) { - return 0; + for (auto vertex : LuaType>::todata(L, 2, &C)) { + if (vertex >=0) { + spans.AddVertex(vertex); } - int v = lua_tointeger(L, -1); - if (0 <= v) { - spans.AddVertex(v); - } - lua_pop(L, 1); } } + else { + luaL_error(L, "bad argument #2 to set_vertices (table expected, got %s)" , lua_typename(L, 2)); + } return 0; } @@ -2087,6 +2085,7 @@ namespace SpansReg { { "next_stop", WRAPMEM(T, NextStop) }, { "has_vertex", WRAPMEM(T, HasVertex) }, { "count_between", WRAP(count_between) }, + { "clear", WRAPMEM(T, Clear) }, { NULL, NULL }, };