fixing sorting script and list sorting

This commit is contained in:
noffie 2024-11-10 16:16:34 +01:00
parent 2fba0b1d53
commit 3afe24c9a8
5 changed files with 34 additions and 18 deletions

View file

@ -3,6 +3,8 @@
SortingTester::SortingTester() : list(10)
{
std::cout << "Loading lua" << std::endl;
lua.lua.open_libraries(sol::lib::coroutine, sol::lib::table);
lua.lua.new_usertype<SortingTester>("List",
"size", &SortingTester::list_size,
@ -37,6 +39,16 @@ void SortingTester::load_internal()
void SortingTester::populate(const populate_function& f)
{
std::cout << "[List(" << list.size() << ")] [ ";
for (size_t i = 0; i < list.size(); i++)
{
f(i, list[i]);
if(i == list.size()-1)
{
std::cout << list[i] << " ]\n";
break;
}
std::cout << list[i] << ", ";
}
std::flush(std::cout);
}

View file

@ -69,7 +69,7 @@ class SortingTester
{
if (list.size() <= 1)
return true;
for (size_t i = 1; i < list.size(); ++i)
for (size_t i = 1; i < list.size(); i++)
{
if (list[i - 1] > list[i])
return false;
@ -140,6 +140,18 @@ public:
std::terminate();
}
std::cout << "[sorted: List(" << list.size() << ")] [ ";
for (size_t i = 0; i < list.size(); i++)
{
if(i == list.size()-1)
{
std::cout << list[i] << " ]\n";
break;
}
std::cout << list[i] << ", ";
}
std::flush(std::cout);
result.count_comparisons = result.count_equal + result.count_greater + result.count_less;
active_result = nullptr;

View file

@ -3,10 +3,10 @@
inline const char* std_intern_sorting_script = R"(
-- bubble sort
function bubble_sort()
isSorted = false
while isSorted == false do
movedElements = 0
for x=1, list:size(), 1 do
local isSorted = false
while not isSorted do
local movedElements = 0
for x = 0, list:size() - 2 do
if list:greater(x, x+1) then
movedElements = movedElements + 1
list:swap(x, x+1)

View file

@ -37,12 +37,16 @@ public:
template<typename ...Args, typename Caller = std::function<sol::protected_function_result(const sol::protected_function&, Args&&...)>>
LuaResultType run_on_caller(const std::string& function_name, const Caller& caller, Args&&... args)
{
std::cout << "Looking for function: " << function_name << std::endl;
auto function = lua[function_name];
if (!function.valid()) {
std::cout << "Error: function " << function_name << " not found" << std::endl;
return LuaResultType::RUN_ERROR;
}
std::cout << "Found function, attempting to run" << std::endl;
auto result = caller(function, std::forward<Args>(args)...);
if (!result.valid()) {
sol::error err = result;

View file

@ -81,18 +81,6 @@ int main(int argc, char** argv)
std::cout << "\n\n";
result = tester.run("do_nothing");
std::cout << "Time: " << result.timer.seconds << " seconds" << std::endl;
std::cout << "Difftime: " << result.timer.difftime << std::endl;
std::cout << "Count swaps: " << result.count_swaps << std::endl;
std::cout << "Count comparisons: " << result.count_comparisons << std::endl;
std::cout << "\tCount greater: " << result.count_greater << std::endl;
std::cout << "\tCount less: " << result.count_less << std::endl;
std::cout << "\tCount equal: " << result.count_equal << "\n\n";
std::cout << "\nPress any key to exit..." << std::endl;
getchar();
return 0;