Skip to content

Commit 986b2ee

Browse files
committed
Improved SortController comments and documentation
1 parent 72e13ce commit 986b2ee

File tree

1 file changed

+56
-7
lines changed

1 file changed

+56
-7
lines changed

SortController.cpp

+56-7
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,74 @@
11
#include "SortController.h"
22
#include "Utils.h"
33

4+
/**
5+
* @brief Sort controller constructor
6+
*
7+
* @param windowSize Window size vector (x, y)
8+
* @param timeSleep Time to wait between iterations while sorting in miliseconds
9+
*/
410
SortController::SortController(sf::Vector2u windowSize, int timeSleep) {
511
this->winWidth = windowSize.x;
612
this->winHeight = windowSize.y;
713

814
this->timeSleep = timeSleep;
915
}
1016

11-
///////////////////////////////
1217
//
13-
// Vector control methods
18+
// ──────────────────────────────────────────────────────────────────────────────────── I ──────────
19+
// :::::: V E C T O R C O N T R O L M E T H O D S : : : : : : : :
20+
// ──────────────────────────────────────────────────────────────────────────────────────────────
1421
//
15-
///////////////////////////////
1622

23+
/**
24+
* @brief Empty the array
25+
*
26+
*/
1727
void SortController::clear() {
1828
sortElements.clear();
1929
}
2030

31+
/**
32+
* @brief Populate the array with Sorable elements
33+
*
34+
* @param numOfElements Number of elements to be included in the array (array size)
35+
*/
2136
void SortController::populate(int numOfElements) {
2237
for (int n = 0; n < numOfElements; n++) {
2338
Sortable sortable(((float)winWidth / numOfElements), ((float)winHeight / numOfElements) * (n+1), n); // Width defined for max space in window, height defined by element value
2439
sortElements.push_back(sortable);
2540
}
2641
}
2742

43+
/**
44+
* @brief Randomize element positions inside the array
45+
*
46+
*/
2847
void SortController::randomize() {
2948
auto rd = std::random_device{};
3049
auto rng = std::default_random_engine{ rd() };
3150
std::shuffle(std::begin(sortElements), std::end(sortElements), rng);
3251
};
3352

53+
/**
54+
* @brief Update time sleep
55+
*
56+
* @param t Time to wait between iterations while sorting in miliseconds
57+
*/
3458
void SortController::setTimeSleep(int t) {
3559
timeSleep = t;
3660
}
3761

62+
/**
63+
* @brief Display sort info on terminal after the algorithm finished
64+
*
65+
* @param sortType Number associated to each sort type
66+
* @param isSorting true = display only sort type and sorting message, false = display all info
67+
* @param numOfComparisons Number of comparisons that the algorithm running made
68+
* @param sortTime Time that the algorithm took to sort all elements
69+
*
70+
* TODO: Add number of comparisons and time while the algorithm is sorting
71+
*/
3872
void SortController::displaySortInfo(int sortType, bool isSorting, int numOfComparisons, int sortTime) {
3973
system(CLEAR);
4074

@@ -51,12 +85,17 @@ void SortController::displaySortInfo(int sortType, bool isSorting, int numOfComp
5185
}
5286
}
5387

54-
///////////////////////////////
5588
//
56-
// Sorting methods
89+
// ────────────────────────────────────────────────────────────────────── II ──────────
90+
// :::::: S O R T I N G M E T H O D S : : : : : : : :
91+
// ────────────────────────────────────────────────────────────────────────────────
5792
//
58-
///////////////////////////////
5993

94+
/**
95+
* @brief Start sort timer, run the algorithm and increment the number of comparisons while the array isn't sorted, and display sort info at the end.
96+
*
97+
* @param sortType Number associated to the algorithm
98+
*/
6099
void SortController::startSort(int sortType) {
61100
isSorting = true;
62101
int numOfComparisons = 0;
@@ -90,6 +129,12 @@ void SortController::startSort(int sortType) {
90129
checkSortAnim();
91130
}
92131

132+
/**
133+
* @brief Iterate one time through the array to check if is sorted
134+
*
135+
* @return true The array is sorted
136+
* @return false The array isn't sorted
137+
*/
93138
bool SortController::isSorted() {
94139
for (int n = 0; n < sortElements.size()-1; n++) {
95140
if (sortElements[n].value > sortElements[n+1].value)
@@ -101,7 +146,11 @@ bool SortController::isSorted() {
101146
return true;
102147
};
103148

104-
// This function is only for the "checking animation", the verification is made by isSorted()
149+
/**
150+
* @brief Do a checking animation from the start to the end of the array changing color to green to show that the algorithm finished. (Does not check the array, only do
151+
* the color animation)
152+
*
153+
*/
105154
void SortController::checkSortAnim() {
106155
for (int n = 0; n < sortElements.size(); n++) {
107156
sortElements[n].color = sf::Color::Green;

0 commit comments

Comments
 (0)