mirror of
https://github.com/koloideal/SortLab.git
synced 2026-06-10 10:25:30 +03:00
update
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
#pragma once
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Audio.hpp>
|
||||
#include <memory>
|
||||
#include "Array.hpp"
|
||||
#include "Sorter.hpp"
|
||||
#include "UI.hpp"
|
||||
|
||||
class App {
|
||||
public:
|
||||
@@ -11,7 +15,26 @@ private:
|
||||
void handleEvents();
|
||||
void update(float dt);
|
||||
void render();
|
||||
void switchSorter(std::unique_ptr<Sorter> newSorter);
|
||||
void generateBeepSound();
|
||||
void playBeep(float pitch);
|
||||
|
||||
sf::RenderWindow window_;
|
||||
Array array_;
|
||||
std::unique_ptr<Sorter> currentSorter_;
|
||||
UI ui_;
|
||||
bool isPlaying_;
|
||||
float timeSinceLastStep_;
|
||||
float stepDelay_;
|
||||
|
||||
sf::SoundBuffer beepBuffer_;
|
||||
sf::Sound beepSound_;
|
||||
|
||||
bool isSweeping_;
|
||||
int sweepIndex_;
|
||||
float sweepTimer_;
|
||||
float sweepDelay_;
|
||||
|
||||
size_t lastComparisons_;
|
||||
size_t lastSwaps_;
|
||||
};
|
||||
|
||||
@@ -15,8 +15,16 @@ public:
|
||||
void setValue(int index, float value);
|
||||
void setState(int index, State state);
|
||||
void resetStates();
|
||||
void resetCounters();
|
||||
|
||||
size_t getComparisons() const;
|
||||
size_t getSwaps() const;
|
||||
void incrementComparisons();
|
||||
void incrementSwaps();
|
||||
|
||||
private:
|
||||
std::vector<float> data_;
|
||||
std::vector<State> states_;
|
||||
size_t comparisons_;
|
||||
size_t swaps_;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
#include "Array.hpp"
|
||||
#include <string>
|
||||
|
||||
class Sorter {
|
||||
public:
|
||||
virtual ~Sorter() = default;
|
||||
|
||||
virtual void step(Array& array) = 0;
|
||||
virtual bool isFinished() const = 0;
|
||||
virtual std::string getName() const = 0;
|
||||
virtual void reset() = 0;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "Sorter.hpp"
|
||||
#include <string>
|
||||
|
||||
class UI {
|
||||
public:
|
||||
UI();
|
||||
|
||||
void update(const Sorter& sorter, bool isPlaying, bool isFinished, float stepDelay, const Array& array);
|
||||
void draw(sf::RenderWindow& window);
|
||||
|
||||
private:
|
||||
sf::Font font_;
|
||||
sf::Text algorithmText_;
|
||||
sf::Text stateText_;
|
||||
sf::Text comparisonsText_;
|
||||
sf::Text swapsText_;
|
||||
sf::Text speedText_;
|
||||
sf::Text controlsText_;
|
||||
bool fontLoaded_;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#include "Sorter.hpp"
|
||||
|
||||
class BubbleSorter : public Sorter {
|
||||
public:
|
||||
BubbleSorter();
|
||||
|
||||
void step(Array& array) override;
|
||||
bool isFinished() const override;
|
||||
std::string getName() const override;
|
||||
void reset() override;
|
||||
|
||||
private:
|
||||
enum class Phase { COMPARING, SWAPPING, NEXT };
|
||||
|
||||
int i_;
|
||||
int j_;
|
||||
int n_;
|
||||
bool finished_;
|
||||
Phase phase_;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include "Sorter.hpp"
|
||||
|
||||
class InsertionSorter : public Sorter {
|
||||
public:
|
||||
InsertionSorter();
|
||||
|
||||
void step(Array& array) override;
|
||||
bool isFinished() const override;
|
||||
std::string getName() const override;
|
||||
void reset() override;
|
||||
|
||||
private:
|
||||
enum class Phase { COMPARING, SHIFTING, INSERTING, NEXT };
|
||||
|
||||
int i_;
|
||||
int j_;
|
||||
int n_;
|
||||
float key_;
|
||||
bool finished_;
|
||||
Phase phase_;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
#include "Sorter.hpp"
|
||||
#include <vector>
|
||||
|
||||
class MergeSorter : public Sorter {
|
||||
public:
|
||||
MergeSorter();
|
||||
|
||||
void step(Array& array) override;
|
||||
bool isFinished() const override;
|
||||
std::string getName() const override;
|
||||
void reset() override;
|
||||
|
||||
private:
|
||||
enum class Phase { MERGING, COPYING_LEFT, COPYING_RIGHT, COPYING_BACK, NEXT_MERGE };
|
||||
|
||||
int currentSize_;
|
||||
int leftStart_;
|
||||
int mid_;
|
||||
int rightEnd_;
|
||||
int leftIndex_;
|
||||
int rightIndex_;
|
||||
int mergeIndex_;
|
||||
std::vector<float> temp_;
|
||||
bool finished_;
|
||||
Phase phase_;
|
||||
int n_;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include "Sorter.hpp"
|
||||
#include <vector>
|
||||
|
||||
class QuickSorter : public Sorter {
|
||||
public:
|
||||
QuickSorter();
|
||||
|
||||
void step(Array& array) override;
|
||||
bool isFinished() const override;
|
||||
std::string getName() const override;
|
||||
void reset() override;
|
||||
|
||||
private:
|
||||
enum class Phase { PARTITIONING, SWAPPING_PIVOT, PUSHING_RANGES };
|
||||
|
||||
struct Range {
|
||||
int low;
|
||||
int high;
|
||||
};
|
||||
|
||||
std::vector<Range> stack_;
|
||||
int currentLow_;
|
||||
int currentHigh_;
|
||||
int pivotIndex_;
|
||||
int i_;
|
||||
int j_;
|
||||
float pivotValue_;
|
||||
bool finished_;
|
||||
Phase phase_;
|
||||
int n_;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include "Sorter.hpp"
|
||||
|
||||
class SelectionSorter : public Sorter {
|
||||
public:
|
||||
SelectionSorter();
|
||||
|
||||
void step(Array& array) override;
|
||||
bool isFinished() const override;
|
||||
std::string getName() const override;
|
||||
void reset() override;
|
||||
|
||||
private:
|
||||
enum class Phase { FINDING_MIN, SWAPPING, NEXT };
|
||||
|
||||
int i_;
|
||||
int j_;
|
||||
int minIndex_;
|
||||
int n_;
|
||||
bool finished_;
|
||||
Phase phase_;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user