v1 cleanup

This commit is contained in:
noffie 2025-01-11 04:30:15 +01:00
parent a89b27d456
commit 38793a28e3
12 changed files with 32 additions and 220 deletions

10
include/Future.hpp Normal file
View file

@ -0,0 +1,10 @@
#pragma once
template<typename T>
class Future
{
public:
virtual ~Future() = default;
virtual bool poll() = 0;
virtual T& get() = 0;
};

View file

@ -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

View file

@ -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

View file

@ -1,7 +1,7 @@
#pragma once
#include <vector>
class LuaSortList
class SortingList
{
public:
std::vector<uint16_t> list;

View file

@ -1,7 +1,7 @@
#define RAYGUI_IMPLEMENTATION
#include <raylibs/raygui.h>
#undef RAYGUI_IMPLEMENTATION
#define GUI_WINDOW_FILE_DIALOG_IMPLEMENTATION
#define RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT 24
#include <raylibs/modules/gui_window_file_dialog.hpp>
//#undef RAYGUI_IMPLEMENTATION
//#define GUI_WINDOW_FILE_DIALOG_IMPLEMENTATION
//#define RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT 24
//#include <raylibs/modules/gui_window_file_dialog.hpp>

View file

@ -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

View file

@ -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<typename T>
class Future
{
public:
virtual ~Future() = default;
virtual bool poll() = 0;
virtual T& get() = 0;
};

View file

@ -1,45 +0,0 @@
#include "GuiFileDialog.hpp"
#include <raylibs/raygui.h>
#include <raylibs/modules/gui_window_file_dialog.hpp>
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);
//--------------------------------------------------------------------------------
}

View file

@ -1,19 +0,0 @@
#pragma once
#include <raylibs/modules/gui_window_file_dialog.hpp>
#include <string>
class Gui_WindowFileDialog
{
GuiWindowFileDialogState* fileDialogState;
char fileNameToLoad[512] = { 0 };
public:
Gui_WindowFileDialog();
std::string openedFile;
bool update();
void draw();
};

View file

@ -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<float>(GetScreenHeight()) - 15 - 40, 100, 40 }, "Reset"))
if (GuiButton({ 140, 15, 100, 40 }, "Reset"))
{
m_Steps->clear();
m_SortingFinished = true;
}
}

View file

@ -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);

View file

@ -2,20 +2,20 @@
#include <cstdint>
#include <memory>
#include <collections.hpp>
#include "../lua/FutureLua.hpp"
#include <Future.hpp>
#include <TickSystem.hpp>
#include "../LuaSortList.hpp"
#include "../SortingList.hpp"
class Bubble_Sorter : Future<LuaSortList>
class Bubble_Sorter : Future<SortingList>
{
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