fixing sorting script and list sorting
This commit is contained in:
parent
2fba0b1d53
commit
3afe24c9a8
5 changed files with 34 additions and 18 deletions
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
SortingTester::SortingTester() : list(10)
|
SortingTester::SortingTester() : list(10)
|
||||||
{
|
{
|
||||||
|
std::cout << "Loading lua" << std::endl;
|
||||||
|
|
||||||
lua.lua.open_libraries(sol::lib::coroutine, sol::lib::table);
|
lua.lua.open_libraries(sol::lib::coroutine, sol::lib::table);
|
||||||
lua.lua.new_usertype<SortingTester>("List",
|
lua.lua.new_usertype<SortingTester>("List",
|
||||||
"size", &SortingTester::list_size,
|
"size", &SortingTester::list_size,
|
||||||
|
@ -37,6 +39,16 @@ void SortingTester::load_internal()
|
||||||
|
|
||||||
void SortingTester::populate(const populate_function& f)
|
void SortingTester::populate(const populate_function& f)
|
||||||
{
|
{
|
||||||
|
std::cout << "[List(" << list.size() << ")] [ ";
|
||||||
for (size_t i = 0; i < list.size(); i++)
|
for (size_t i = 0; i < list.size(); i++)
|
||||||
|
{
|
||||||
f(i, list[i]);
|
f(i, list[i]);
|
||||||
|
if(i == list.size()-1)
|
||||||
|
{
|
||||||
|
std::cout << list[i] << " ]\n";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::cout << list[i] << ", ";
|
||||||
|
}
|
||||||
|
std::flush(std::cout);
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,7 +69,7 @@ class SortingTester
|
||||||
{
|
{
|
||||||
if (list.size() <= 1)
|
if (list.size() <= 1)
|
||||||
return true;
|
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])
|
if (list[i - 1] > list[i])
|
||||||
return false;
|
return false;
|
||||||
|
@ -140,6 +140,18 @@ public:
|
||||||
std::terminate();
|
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;
|
result.count_comparisons = result.count_equal + result.count_greater + result.count_less;
|
||||||
|
|
||||||
active_result = nullptr;
|
active_result = nullptr;
|
||||||
|
|
|
@ -3,10 +3,10 @@
|
||||||
inline const char* std_intern_sorting_script = R"(
|
inline const char* std_intern_sorting_script = R"(
|
||||||
-- bubble sort
|
-- bubble sort
|
||||||
function bubble_sort()
|
function bubble_sort()
|
||||||
isSorted = false
|
local isSorted = false
|
||||||
while isSorted == false do
|
while not isSorted do
|
||||||
movedElements = 0
|
local movedElements = 0
|
||||||
for x=1, list:size(), 1 do
|
for x = 0, list:size() - 2 do
|
||||||
if list:greater(x, x+1) then
|
if list:greater(x, x+1) then
|
||||||
movedElements = movedElements + 1
|
movedElements = movedElements + 1
|
||||||
list:swap(x, x+1)
|
list:swap(x, x+1)
|
||||||
|
|
|
@ -37,12 +37,16 @@ public:
|
||||||
template<typename ...Args, typename Caller = std::function<sol::protected_function_result(const sol::protected_function&, Args&&...)>>
|
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)
|
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];
|
auto function = lua[function_name];
|
||||||
if (!function.valid()) {
|
if (!function.valid()) {
|
||||||
std::cout << "Error: function " << function_name << " not found" << std::endl;
|
std::cout << "Error: function " << function_name << " not found" << std::endl;
|
||||||
return LuaResultType::RUN_ERROR;
|
return LuaResultType::RUN_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cout << "Found function, attempting to run" << std::endl;
|
||||||
|
|
||||||
auto result = caller(function, std::forward<Args>(args)...);
|
auto result = caller(function, std::forward<Args>(args)...);
|
||||||
if (!result.valid()) {
|
if (!result.valid()) {
|
||||||
sol::error err = result;
|
sol::error err = result;
|
||||||
|
|
12
src/main.cpp
12
src/main.cpp
|
@ -81,18 +81,6 @@ int main(int argc, char** argv)
|
||||||
std::cout << "\n\n";
|
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;
|
std::cout << "\nPress any key to exit..." << std::endl;
|
||||||
getchar();
|
getchar();
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue