From 38793a28e3ad528eaad36cba6a8e7a38edd2dae2 Mon Sep 17 00:00:00 2001 From: noffie Date: Sat, 11 Jan 2025 04:30:15 +0100 Subject: [PATCH] v1 cleanup --- include/Future.hpp | 10 ++++ res/lua/bubble_sortiva.lua | 25 ---------- res/lua/quick_sortiva.lua | 59 ------------------------ src/{LuaSortList.hpp => SortingList.hpp} | 2 +- src/libimpl.cpp | 8 ++-- src/lua/Future.lua | 16 ------- src/lua/FutureLua.hpp | 35 -------------- src/sortiva/GuiFileDialog.cpp | 45 ------------------ src/sortiva/GuiFileDialog.hpp | 19 -------- src/sortiva/sortiva-draw_overlay.cpp | 15 +++--- src/sortiva/sortiva-update.cpp | 2 +- src/sortiva/sortiva.hpp | 16 +++---- 12 files changed, 32 insertions(+), 220 deletions(-) create mode 100644 include/Future.hpp delete mode 100644 res/lua/bubble_sortiva.lua delete mode 100644 res/lua/quick_sortiva.lua rename src/{LuaSortList.hpp => SortingList.hpp} (95%) delete mode 100644 src/lua/Future.lua delete mode 100644 src/lua/FutureLua.hpp delete mode 100644 src/sortiva/GuiFileDialog.cpp delete mode 100644 src/sortiva/GuiFileDialog.hpp diff --git a/include/Future.hpp b/include/Future.hpp new file mode 100644 index 0000000..d0e0599 --- /dev/null +++ b/include/Future.hpp @@ -0,0 +1,10 @@ +#pragma once + +template +class Future +{ +public: + virtual ~Future() = default; + virtual bool poll() = 0; + virtual T& get() = 0; +}; \ No newline at end of file diff --git a/res/lua/bubble_sortiva.lua b/res/lua/bubble_sortiva.lua deleted file mode 100644 index 1ecb004..0000000 --- a/res/lua/bubble_sortiva.lua +++ /dev/null @@ -1,25 +0,0 @@ - -local bubble_sort = Future:new() - -function bubble_sort:poll(array) - local n = array:size() - self.state[n] = self.state[n] or n - if n == 0 then - return false - end - - local swapped = false - for i = 1, n - 1 do - if array.at(i-1) > array.at(i) then - array.swap(i-1, i) - swapped = true - end - end - self.state.n = self.state.n - 1 - - return not swapped -end - -function make_available(sorter_list) - table.insert(sorter_list, "bubble_sort") -end \ No newline at end of file diff --git a/res/lua/quick_sortiva.lua b/res/lua/quick_sortiva.lua deleted file mode 100644 index 654cf6f..0000000 --- a/res/lua/quick_sortiva.lua +++ /dev/null @@ -1,59 +0,0 @@ -local quick_sort = Future:new() - -function quick_sort:sort(A, lo, hi, i) - if lo >= 0 and lo < hi then - lt, gt = partition(A, lo, hi) -- Multiple return values - - if #self.state["lt"] >= i and #self.state["gt"] >= i then - self:sort(A, lo, self.state["lt"][i] - 1, i+1) - self:sort(A, self.state["gt"][i] + 1, hi, i+1) - else - table.insert(self.state["lt"], lt) - table.insert(self.state["gt"], gt) - return false - end - end - return true -end - -function quick_sort:poll(array) - return self:sort(array, 0, array:size(),0) -end - - --- Divides array into three partitions -function partition(A, lo, hi) - -- Pivot value - local pivot = A.at((lo + hi) / 2) -- Choose the middle element as the pivot (integer division) - - -- Lesser, equal and greater index - local lt = lo - local eq = lo - local gt = hi - - -- Iterate and compare all elements with the pivot - while eq <= gt do - if A[eq] < pivot then - -- Swap the elements at the equal and lesser indices - A:swap(eq, lt) - -- Increase lesser index - lt = lt + 1 - -- Increase equal index - eq = eq + 1 - elseif A[eq] > pivot then - -- Swap the elements at the equal and greater indices - A:swap(eq, gt) - -- Decrease greater index - gt = gt - 1 - else -- if A[eq] = pivot then - -- Increase equal index - eq = eq + 1 - end - end - -- Return lesser and greater indices - return lt, gt -end - -function make_available(sorter_list) - table.insert(sorter_list, "quick_sort") -end \ No newline at end of file diff --git a/src/LuaSortList.hpp b/src/SortingList.hpp similarity index 95% rename from src/LuaSortList.hpp rename to src/SortingList.hpp index 5fc6255..05dad9d 100644 --- a/src/LuaSortList.hpp +++ b/src/SortingList.hpp @@ -1,7 +1,7 @@ #pragma once #include -class LuaSortList +class SortingList { public: std::vector list; diff --git a/src/libimpl.cpp b/src/libimpl.cpp index 565a7a9..8b36e02 100644 --- a/src/libimpl.cpp +++ b/src/libimpl.cpp @@ -1,7 +1,7 @@ #define RAYGUI_IMPLEMENTATION #include -#undef RAYGUI_IMPLEMENTATION -#define GUI_WINDOW_FILE_DIALOG_IMPLEMENTATION -#define RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT 24 -#include \ No newline at end of file +//#undef RAYGUI_IMPLEMENTATION +//#define GUI_WINDOW_FILE_DIALOG_IMPLEMENTATION +//#define RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT 24 +//#include \ No newline at end of file diff --git a/src/lua/Future.lua b/src/lua/Future.lua deleted file mode 100644 index 61e9f81..0000000 --- a/src/lua/Future.lua +++ /dev/null @@ -1,16 +0,0 @@ -Future = { - state = {} -} - -function Future:new(state) - local f = {} - setmetatable(f, self) - self.__index = self - f["state"] = state or {} - - return f -end - -function Future:pull() - return true -end \ No newline at end of file diff --git a/src/lua/FutureLua.hpp b/src/lua/FutureLua.hpp deleted file mode 100644 index 1d2b902..0000000 --- a/src/lua/FutureLua.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -/* -static inline const char lua_future_class[] = R"( -Future = { - state = {} -} - -function Future:new(state) - local f = {} - setmetatable(f, self) - self.__index = self - f["state"] = state or {} - - return f -end - -function Future:poll() - return true -end - -function Future.get(t, ...) - return t:poll(unpack(arg)) -end -)"; -*/ - -template -class Future -{ -public: - virtual ~Future() = default; - virtual bool poll() = 0; - virtual T& get() = 0; -}; \ No newline at end of file diff --git a/src/sortiva/GuiFileDialog.cpp b/src/sortiva/GuiFileDialog.cpp deleted file mode 100644 index 5dc3ce5..0000000 --- a/src/sortiva/GuiFileDialog.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "GuiFileDialog.hpp" -#include -#include - - -Gui_WindowFileDialog::Gui_WindowFileDialog() -{ - fileDialogState = new GuiWindowFileDialogState(InitGuiWindowFileDialog(GetWorkingDirectory())); - fileDialogState->windowBounds = { 10, 10, fileDialogState->windowBounds.height, fileDialogState->windowBounds.height }; -} - -bool Gui_WindowFileDialog::update() -{ - bool file_opened = false; - if (fileDialogState->SelectFilePressed) - { - // Load image file (if supported extension) - if (IsFileExtension(fileDialogState->fileNameText, ".lua")) - { - strcpy(fileNameToLoad, TextFormat("%s/%s", fileDialogState->dirPathText, fileDialogState->fileNameText)); - openedFile = fileNameToLoad; - file_opened = true; - } - - fileDialogState->SelectFilePressed = false; - } - return file_opened; -} - -void Gui_WindowFileDialog::draw() -{ - // raygui: controls drawing - //---------------------------------------------------------------------------------- - if (fileDialogState->windowActive) GuiLock(); - - if (GuiButton({ 20, 20, 140, 30 }, GuiIconText(GuiIconName::ICON_FILE_OPEN, "Open Image"))) fileDialogState->windowActive = true; - - GuiUnlock(); - - // GUI: Dialog Window - //-------------------------------------------------------------------------------- - GuiWindowFileDialog(fileDialogState); - //-------------------------------------------------------------------------------- - -} \ No newline at end of file diff --git a/src/sortiva/GuiFileDialog.hpp b/src/sortiva/GuiFileDialog.hpp deleted file mode 100644 index 6f2162a..0000000 --- a/src/sortiva/GuiFileDialog.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include -#include - - -class Gui_WindowFileDialog -{ - GuiWindowFileDialogState* fileDialogState; - char fileNameToLoad[512] = { 0 }; -public: - Gui_WindowFileDialog(); - - std::string openedFile; - - bool update(); - - void draw(); -}; diff --git a/src/sortiva/sortiva-draw_overlay.cpp b/src/sortiva/sortiva-draw_overlay.cpp index 345a529..97b4706 100644 --- a/src/sortiva/sortiva-draw_overlay.cpp +++ b/src/sortiva/sortiva-draw_overlay.cpp @@ -14,20 +14,21 @@ void Sortiva::draw_overlay() // //if (edit_mode) GuiLock(); - if (GuiButton({ 20, 15, 100, 40 }, "Run")) - { - setup(); - m_SortingFinished = false; - } - if (GuiButton({ 140, 15, 100, 40 }, m_SortingFinished ? "Continue" : "Pause")) + if (GuiButton({ 20, 15, 100, 40 }, m_SortingFinished ? "Run" : "Pause")) { if (!m_SortingFinished || !m_Steps->empty()) { m_SortingFinished = !m_SortingFinished; } + else + { + setup(); + m_SortingFinished = false; + } } - if (GuiButton({ 20, static_cast(GetScreenHeight()) - 15 - 40, 100, 40 }, "Reset")) + if (GuiButton({ 140, 15, 100, 40 }, "Reset")) { m_Steps->clear(); + m_SortingFinished = true; } } diff --git a/src/sortiva/sortiva-update.cpp b/src/sortiva/sortiva-update.cpp index f4c93c2..a82e8d6 100644 --- a/src/sortiva/sortiva-update.cpp +++ b/src/sortiva/sortiva-update.cpp @@ -11,7 +11,7 @@ void Sortiva::update(double dt) { } else { - LuaSortList& list = m_Sorter.get(); + SortingList& list = m_Sorter.get(); for (uint16_t i = 0; i < list.size(); ++i) { m_Steps->at(list.at(i) - 1).put(i + 1); diff --git a/src/sortiva/sortiva.hpp b/src/sortiva/sortiva.hpp index 55e00dd..4edd67f 100644 --- a/src/sortiva/sortiva.hpp +++ b/src/sortiva/sortiva.hpp @@ -2,20 +2,20 @@ #include #include #include -#include "../lua/FutureLua.hpp" +#include #include -#include "../LuaSortList.hpp" +#include "../SortingList.hpp" -class Bubble_Sorter : Future +class Bubble_Sorter : Future { - LuaSortList* list = nullptr; + SortingList* list = nullptr; struct STATE { size_t n = 1; } state; public: Bubble_Sorter() = default; - Bubble_Sorter(LuaSortList& l) : list(&l) {} + Bubble_Sorter(SortingList& l) : list(&l) {} bool poll() override { @@ -38,12 +38,12 @@ public: return list->sorted(); } - LuaSortList& get() override + SortingList& get() override { return *list; } - void set(LuaSortList& l) + void set(SortingList& l) { list = &l; } @@ -70,7 +70,7 @@ private: bool m_SortingFinished = true; TickSystem m_Ticker; Bubble_Sorter m_Sorter; - LuaSortList m_List; + SortingList m_List; // std::unique_ptr