From fae80597757be0820136ba72c12abf8acf3d5aaa Mon Sep 17 00:00:00 2001 From: explover Date: Sat, 23 May 2015 19:37:18 +0000 Subject: [PATCH 1/2] The solution of c-gaps problem. --- c-gaps/credit.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/c-gaps/credit.c b/c-gaps/credit.c index 2a60e39..72fa56f 100644 --- a/c-gaps/credit.c +++ b/c-gaps/credit.c @@ -1,9 +1,76 @@ #include #include +#include +#if LUA_VERSION_NUM == 501 +#define my_len lua_objlen +#else +#define my_len lua_rawlen +#endif -// implement it here +int removeGaps(char** table, char** new_table, int len_str, int len_table, int* new_sym_ind) { + int sym_ind, str_ind; + char gap_column; + for (sym_ind = 0; sym_ind < len_str; sym_ind++) { + gap_column = 't'; + for (str_ind = 0; str_ind < len_table; str_ind++) { + if (table[str_ind][sym_ind] != '-') { + gap_column = 'f'; + } + } + if (gap_column == 'f') { + *new_sym_ind = *new_sym_ind + 1; + for (str_ind = 0; str_ind < len_table; str_ind++) { + new_table[str_ind][*new_sym_ind] = table[str_ind][sym_ind]; + } + } + } + if (*new_sym_ind == -1) { + return 0; + } else { + return 1; + } +} +int wrapRemoveGaps(lua_State* L) { + luaL_checktype(L, -1, LUA_TTABLE); + char** table, **new_table; + size_t len, len1; + int len_table = my_len(L, -1); + int str_ind, i; + int new_sym_ind = -1; + table = malloc(len_table * sizeof(char*)); + new_table = malloc(len_table * sizeof(char*)); + for (str_ind = 1; str_ind <= len_table; str_ind++) { + lua_rawgeti(L, 1, str_ind); + table[str_ind - 1] = luaL_checklstring(L, -1, &len1); + if (str_ind == 1) { + len = len1; + } + if (len != len1) { + luaL_error(L, "fuck"); + } + } + for (i = 0; i < len_table; i++) { + new_table[i] = malloc(len * sizeof(char)); + } + int error = removeGaps(table, new_table, len, len_table, &new_sym_ind); + //if (error != 1) { + // luaL_error(L, "fuck"); + //} + lua_newtable(L); + for (str_ind = 0; str_ind < len_table; str_ind++) { + lua_pushlstring(L, new_table[str_ind], new_sym_ind + 1); + lua_rawseti(L, -2, str_ind + 1); + } + free(table); + for (i = 0; i < len_table; i++) { + free(new_table[i]); + } + free(new_table); + return 1; +} + int luaopen_gaps(lua_State* L) { - // return it here - return 0; + lua_pushcfunction(L, wrapRemoveGaps); + return 1; } From 909972d25e0da0d95379ee04ef8c0c9547979c96 Mon Sep 17 00:00:00 2001 From: explover Date: Sat, 23 May 2015 19:39:08 +0000 Subject: [PATCH 2/2] Replaced all "pending" with "it". --- c-gaps/spec.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/c-gaps/spec.lua b/c-gaps/spec.lua index 2129000..498c005 100644 --- a/c-gaps/spec.lua +++ b/c-gaps/spec.lua @@ -1,9 +1,9 @@ describe("gaps (implementation)", function() - pending("loads module", function() + it("loads module", function() local f = require 'gaps' end) - pending("doesn't change good alignment", function() + it("doesn't change good alignment", function() local f = require 'gaps' assert.same({}, f({})) assert.same({''}, f({''})) @@ -14,7 +14,7 @@ describe("gaps (implementation)", function() assert.same({'A'}, f({'A-'})) end) - pending("removes gap columns", function() + it("removes gap columns", function() local f = require 'gaps' assert.same({''}, f({'-'})) assert.same({'', ''}, f({'-', '-'})) @@ -30,7 +30,7 @@ describe("gaps (implementation)", function() })) end) - pending("throws error if input is not table", + it("throws error if input is not table", function() local f = require 'gaps' assert.has_error(function() @@ -41,7 +41,7 @@ describe("gaps (implementation)", function() end) end) - pending("throws error if table members are not strings", + it("throws error if table members are not strings", function() local f = require 'gaps' assert.has_error(function() @@ -49,7 +49,7 @@ describe("gaps (implementation)", function() end) end) - pending("throws error if strings' lengths differ", + it("throws error if strings' lengths differ", function() local f = require 'gaps' assert.has_error(function()