ronantakizawa/codereview-bench huggingface dataset:
Repository: TheAlgorithms/C-Plus-Plus
File: data_structures/stack_using_array.cpp (C++)

Code before:
#include <iostream>   /// For IO operations
#include <stdexcept>  /// For std::out_of_range

/*
 * @namespace
 * @brief Data structures
 */
namespace data_structures {
/**
 * @brief Class representation of a stack
 * @tparam T The type of the elements in the stack
 */
template <typename T>
class Stack {
 private:
    T* stack;        ///< Pointer to the stack array
    int stackSize;   ///< Maximum size of the stack
    int stackIndex;  ///< Index pointing to the top element of the stack

 public:
    /**
     * @brief Constructs a new Stack object
     *
     * @param size Maximum size of the stack
     */
    Stack(int size) : stackSize(size), stackIndex(0) { stack = new T[size]; }

    /**
     * @brief Destroys the Stack object
     */
    ~Stack() { delete[] stack; }

    /**
     * @brief Checks if the stack is full
     *
     * @return true if the stack is full, false otherwise
     */
    bool full() const { return stackIndex == stackSize; }

    /**
     * @brief Checks if the stack is empty
     *
     * @return true if the stack is empty, false otherwise
     */
    bool empty() const { return stackIndex == 0; }

    /**
     * @brief Pushes an element onto the stack
     *
     * @param element Element to push onto the stack
     * @return true if the element was successfully pushed onto the stack, false

Review comment (suggestion):
```suggestion
    Stack(int size) : stackSize(size), stackIndex(-1) { stack = new T[size]; }
```
TOS should be -1 at the start

Code after:
/**
 * @brief Class representation of a stack
 * @tparam T The type of the elements in the stack
 */
template <typename T>
class Stack {
 private:
    std::unique_ptr<T[]> stack;  ///< Smart pointer to the stack array
    int stackSize;               ///< Maximum size of the stack
    int stackIndex;  ///< Index pointing to the top element of the stack

 public:
    /**
     * @brief Constructs a new Stack object
     *
     * @param size Maximum size of the stack
     */
    Stack(int size) : stackSize(size), stackIndex(-1), stack(new T[size]) {}

    /**
     * @brief Checks if the stack is full
     *
     * @return true if the stack is full, false otherwise
     */
    bool full() const { return stackIndex == stackSize - 1; }

    /**
     * @brief Checks if the stack is empty
     * @return true if the stack is empty, false otherwise
     */
    bool empty() const { return stackIndex == -1; }

    /**
     * @brief Pushes an element onto the stack
     *
     * @param element Element to push onto the stack
     */
    void push(T element) {
        if (full()) {
            throw std::out_of_range("Stack overflow");
        } else {
            stack[++stackIndex] = element;
        }
    }

    /**
     * @brief Pops an element from the stack
     *
     * @return The popped element
     * @throws std::out_of_range if the stack is empty
     */
Repository: TheAlgorithms/C-Plus-Plus
File: data_structures/stack_using_array.cpp (C++)

Code before:
 public:
    /**
     * @brief Constructs a new Stack object
     *
     * @param size Maximum size of the stack
     */
    Stack(int size) : stackSize(size), stackIndex(0) { stack = new T[size]; }

    /**
     * @brief Destroys the Stack object
     */
    ~Stack() { delete[] stack; }

    /**
     * @brief Checks if the stack is full
     *
     * @return true if the stack is full, false otherwise
     */
    bool full() const { return stackIndex == stackSize; }

    /**
     * @brief Checks if the stack is empty
     *
     * @return true if the stack is empty, false otherwise
     */
    bool empty() const { return stackIndex == 0; }

    /**
     * @brief Pushes an element onto the stack
     *
     * @param element Element to push onto the stack
     * @return true if the element was successfully pushed onto the stack, false
     * otherwise
     */
    bool push(T element) {
        if (full()) {
            return false;
        } else {
            stack[stackIndex++] = element;
            return true;
        }
    }

    /**
     * @brief Pops an element from the stack
     *
     * @return The popped element
     * @throws std::out_of_range if the stack is empty
     */
    T pop() {
        if (empty()) {

Review comment (suggestion):
```suggestion
    bool empty() const { return stackIndex == -1; }
```


Code after:
    /**
     * @brief Checks if the stack is full
     *
     * @return true if the stack is full, false otherwise
     */
    bool full() const { return stackIndex == stackSize - 1; }

    /**
     * @brief Checks if the stack is empty
     * @return true if the stack is empty, false otherwise
     */
    bool empty() const { return stackIndex == -1; }

    /**
     * @brief Pushes an element onto the stack
     *
     * @param element Element to push onto the stack
     */
    void push(T element) {
        if (full()) {
            throw std::out_of_range("Stack overflow");
        } else {
            stack[++stackIndex] = element;
        }
    }

    /**
     * @brief Pops an element from the stack
     *
     * @return The popped element
     * @throws std::out_of_range if the stack is empty
     */
    T pop() {
        if (empty()) {
            throw std::out_of_range("Stack underflow");
        }
        return stack[stackIndex--];
    }

    /**
     * @brief Displays all elements in the stack
     */
    void show() const {
        for (int i = 0; i <= stackIndex; i++) {
            std::cout << stack[i] << "\n";
        }
    }

    /**
     * @brief Displays the topmost element of the stack
     *
Repository: TheAlgorithms/C-Plus-Plus
File: data_structures/stack_using_array.cpp (C++)

Code before:
     *
     * @return The bottom element of the stack
     * @throws std::out_of_range if the stack is empty
     */
    T bottom() const {
        if (empty()) {
            throw std::out_of_range("Stack is empty");
        } else {
            return stack[0];
        }
    }
};
}  // namespace data_structures

/**
 * @brief Self-test implementations
 * @returns void
 */
static void test() {
    data_structures::Stack<int> stack(5);

    // Test push, pop, topmost, bottom, full, and empty operations
    assert(stack.empty() == true);
    assert(stack.full() == false);

    assert(stack.push(10) == true);
    assert(stack.push(20) == true);
    assert(stack.push(30) == true);
    assert(stack.push(40) == true);
    assert(stack.push(50) == true);
    assert(stack.push(60) == false);

    assert(stack.pop() == 50);
    assert(stack.pop() == 40);
    assert(stack.pop() == 30);

    assert(stack.topmost() == 20);
    assert(stack.bottom() == 10);

    assert(stack.pop() == 20);
    assert(stack.pop() == 10);

    assert(stack.empty() == true);
    assert(stack.full() == false);

    // Test for exceptions when stack is empty
    try {
        stack.topmost();
    } catch (const std::out_of_range& e) {
        assert(std::string(e.what()) == "Stack is empty");
    }

Review comment (suggestion):
Since we are not returning true any use this as a guide

```suggestion
    stack.push(10);
    assert(stack.topmost() == 10);
```


Code after:

    // Test empty and full operations
    assert(stack.empty());
    assert(!stack.full());

    // Test pushing elements and checking topmost
    stack.push(10);
    assert(stack.topmost() == 10);

    stack.push(20);
    assert(stack.topmost() == 20);

    stack.push(30);
    stack.push(40);
    stack.push(50);
    assert(stack.full());

    // Test stack overflow
    try {
        stack.push(60);
    } catch (const std::out_of_range& e) {
        assert(std::string(e.what()) == "Stack overflow");
    }

    // Test popping elements
    assert(stack.pop() == 50);
    assert(stack.pop() == 40);
    assert(stack.pop() == 30);

    // Check topmost and bottom elements
    assert(stack.topmost() == 20);
    assert(stack.bottom() == 10);

    assert(stack.pop() == 20);
    assert(stack.pop() == 10);

    assert(stack.empty());
    assert(!stack.full());

    // Test stack underflow
    try {
        stack.pop();
    } catch (const std::out_of_range& e) {
        assert(std::string(e.what()) == "Stack underflow");
    }

    try {
        stack.topmost();
    } catch (const std::out_of_range& e) {
        assert(std::string(e.what()) == "Stack underflow");
    }
Repository: TheAlgorithms/C-Plus-Plus
File: data_structures/stack_using_array.cpp (C++)

Code before:
    }

    /**
     * @brief Displays the bottom element of the stack
     *
     * @return The bottom element of the stack
     * @throws std::out_of_range if the stack is empty
     */
    T bottom() const {
        if (empty()) {
            throw std::out_of_range("Stack underflow");
        }
        return stack[0];
    }
};
}  // namespace data_structures

/**
 * @brief Self-test implementations
 * @returns void
 */
static void test() {
    data_structures::Stack<int> stack(5);

    // Test empty and full operations
    assert(stack.empty() == true);
    assert(stack.full() == false);

    // Test pushing elements and checking topmost
    stack.push(10);
    assert(stack.topmost() == 10);

    stack.push(20);
    assert(stack.topmost() == 20);

    stack.push(30);
    stack.push(40);
    stack.push(50);
    assert(stack.full() == true);

    // Test stack overflow
    try {
        stack.push(60);
    } catch (const std::out_of_range& e) {
        assert(std::string(e.what()) == "Stack overflow");
    }

    // Test popping elements
    assert(stack.pop() == 50);
    assert(stack.pop() == 40);
    assert(stack.pop() == 30);

Review comment (suggestion):
```suggestion
    assert(stack.empty());
```
assert checks for booleans so this should work by default

Code after:
            throw std::out_of_range("Stack underflow");
        }
        return stack[stackIndex];
    }

    /**
     * @brief Displays the bottom element of the stack
     *
     * @return The bottom element of the stack
     * @throws std::out_of_range if the stack is empty
     */
    T bottom() const {
        if (empty()) {
            throw std::out_of_range("Stack underflow");
        }
        return stack[0];
    }
};
}  // namespace data_structures

/**
 * @brief Self-test implementations
 * @returns void
 */
static void test() {
    data_structures::Stack<int> stack(5);

    // Test empty and full operations
    assert(stack.empty());
    assert(!stack.full());

    // Test pushing elements and checking topmost
    stack.push(10);
    assert(stack.topmost() == 10);

    stack.push(20);
    assert(stack.topmost() == 20);

    stack.push(30);
    stack.push(40);
    stack.push(50);
    assert(stack.full());

    // Test stack overflow
    try {
        stack.push(60);
    } catch (const std::out_of_range& e) {
        assert(std::string(e.what()) == "Stack overflow");
    }

    // Test popping elements
Repository: TheAlgorithms/C-Plus-Plus
File: data_structures/stack_using_array.cpp (C++)

Code before:
#include <cassert>
#include <iostream>
#include <memory>
#include <stdexcept>

namespace data_structures {
/**
 * @brief Class representation of a stack
 * @tparam T The type of the elements in the stack
 */
template <typename T>
class Stack {
 private:
    std::unique_ptr<T[]> stack;  ///< Smart pointer to the stack array
    int stackSize;               ///< Maximum size of the stack
    int stackIndex;  ///< Index pointing to the top element of the stack

 public:
    /**
     * @brief Constructs a new Stack object
     *
     * @param size Maximum size of the stack
     */
    Stack(int size) : stackSize(size), stackIndex(-1), stack(new T[size]) {}

    /**
     * @brief Checks if the stack is full
     *
     * @return true if the stack is full, false otherwise

Review comment (suggestion):
```suggestion
#include <cassert> /// For std::assert
#include <iostream> /// For std::cout
#include <memory> /// For std::unique_ptr
#include <stdexcept> /// For std::out_of_range
```

Code after:
#include <cassert>    /// For std::assert
#include <iostream>   /// For std::cout
#include <memory>     /// For std::unique_ptr
#include <stdexcept>  /// For std::out_of_range

/**
 * @namespace
 * @brief data_structures
 */
namespace data_structures {
/**
 * @brief Class representation of a stack
 * @tparam T The type of the elements in the stack
 */
template <typename T>
class Stack {
 private:
    std::unique_ptr<T[]> stack;  ///< Smart pointer to the stack array
    int stackSize;               ///< Maximum size of the stack
    int stackIndex;  ///< Index pointing to the top element of the stack

 public:
    /**
     * @brief Constructs a new Stack object
     *
     * @param size Maximum size of the stack
     */
    Stack(int size) : stackSize(size), stackIndex(-1), stack(new T[size]) {}
Repository: TheAlgorithms/C-Plus-Plus
File: data_structures/stack_using_array.cpp (C++)

Code before:
#include <cassert> /// For std::assert
#include <iostream> /// For std::cout
#include <memory> /// For std::unique_ptr
#include <stdexcept> /// For std::out_of_range

namespace data_structures {
/**
 * @brief Class representation of a stack
 * @tparam T The type of the elements in the stack
 */
template <typename T>
class Stack {
 private:
    std::unique_ptr<T[]> stack;  ///< Smart pointer to the stack array
    int stackSize;               ///< Maximum size of the stack
    int stackIndex;  ///< Index pointing to the top element of the stack

 public:
    /**
     * @brief Constructs a new Stack object
     *
     * @param size Maximum size of the stack
     */
    Stack(int size) : stackSize(size), stackIndex(-1), stack(new T[size]) {}

    /**
     * @brief Checks if the stack is full
     *
     * @return true if the stack is full, false otherwise
     */
    bool full() const { return stackIndex == stackSize - 1; }

    /**
     * @brief Checks if the stack is empty
     *

Review comment (suggestion):
```suggestion
/**
 * @namespace
 * @brief data_structures
 */
namespace data_structures {
```

Code after:
#include <cassert>    /// For std::assert
#include <iostream>   /// For std::cout
#include <memory>     /// For std::unique_ptr
#include <stdexcept>  /// For std::out_of_range

/**
 * @namespace
 * @brief data_structures
 */
namespace data_structures {
/**
 * @brief Class representation of a stack
 * @tparam T The type of the elements in the stack
 */
template <typename T>
class Stack {
 private:
    std::unique_ptr<T[]> stack;  ///< Smart pointer to the stack array
    int stackSize;               ///< Maximum size of the stack
    int stackIndex;  ///< Index pointing to the top element of the stack

 public:
    /**
     * @brief Constructs a new Stack object
     *
     * @param size Maximum size of the stack
     */
    Stack(int size) : stackSize(size), stackIndex(-1), stack(new T[size]) {}

    /**
     * @brief Checks if the stack is full
     *
     * @return true if the stack is full, false otherwise
     */
    bool full() const { return stackIndex == stackSize - 1; }
Repository: TheAlgorithms/C-Plus-Plus
File: sorting/quick_sort_iterative.cpp (C++)

Code before:
/**
 * @file
 * @brief Quick Sort without recursion. This method uses the stack instead.
 * Both recursive and iterative implementations have O(n log n) best case
 * and O(n^2) worst case.
 * @details
 * https://stackoverflow.com/questions/12553238/quicksort-iterative-or-recursive
 * https://en.wikipedia.org/wiki/Quicksort
 * https://www.geeksforgeeks.org/iterative-quick-sort/
 * @author Sebe324 (https://github.com/sebe324)
 */

#include <iostream> //for std::cout
#include <vector> //for std::vector
#include <stack> //for std::stack
#include <algorithm> //for std::is_sorted
#include <cassert> //for assert

/**
 * @brief The partition function sorts the array from
 * start to end and uses the last element as the pivot.
 * @param arr the array to be sorted
 * @param start starting index
 * @param end ending index
 * @return int next index of the pivot
 */
int partition(std::vector<int> &arr, int start, int end)
{
    int pivot = arr[end];
    int index = start - 1;

    for (int j = start; j < end; j++) {
        if (arr[j] <= pivot) {
            std::swap(arr[++index], arr[j]);
        }

Review comment (security):
```suggestion
 * @author [Sebe324](https://github.com/sebe324)
```
For hyperlinking purposes

Code after:
/**
 * @file
 * @brief Quick Sort without recursion. This method uses the stack instead.
 * Both recursive and iterative implementations have O(n log n) best case
 * and O(n^2) worst case.
 * @details
 * https://stackoverflow.com/questions/12553238/quicksort-iterative-or-recursive
 * https://en.wikipedia.org/wiki/Quicksort
 * https://www.geeksforgeeks.org/iterative-quick-sort/
 * @author [Sebe324](https://github.com/sebe324)
 */

#include <iostream> /// for std::cout
#include <vector> /// for std::vector
#include <stack> /// for std::stack
#include <algorithm> /// for std::is_sorted
#include <cassert> /// for assert


/**
 * @namespace sorting
 * @brief Sorting algorithms
 */
namespace sorting {
/**
 * @brief The partition function sorts the array from
 * start to end and uses the last element as the pivot.
 * @param arr the array to be sorted
 * @param start starting index
 * @param end ending index
 * @return int next index of the pivot
 */
int partition(std::vector<int> &arr, int start, int end)
{
    int pivot = arr[end];
Repository: TheAlgorithms/C-Plus-Plus
File: sorting/quick_sort_iterative.cpp (C++)

Code before:
/**
 * @file
 * @brief Quick Sort without recursion. This method uses the stack instead.
 * Both recursive and iterative implementations have O(n log n) best case
 * and O(n^2) worst case.
 * @details
 * https://stackoverflow.com/questions/12553238/quicksort-iterative-or-recursive
 * https://en.wikipedia.org/wiki/Quicksort
 * https://www.geeksforgeeks.org/iterative-quick-sort/
 * @author Sebe324 (https://github.com/sebe324)
 */

#include <iostream> //for std::cout
#include <vector> //for std::vector
#include <stack> //for std::stack
#include <algorithm> //for std::is_sorted
#include <cassert> //for assert

/**
 * @brief The partition function sorts the array from
 * start to end and uses the last element as the pivot.
 * @param arr the array to be sorted
 * @param start starting index
 * @param end ending index
 * @return int next index of the pivot
 */
int partition(std::vector<int> &arr, int start, int end)
{
    int pivot = arr[end];
    int index = start - 1;

    for (int j = start; j < end; j++) {
        if (arr[j] <= pivot) {
            std::swap(arr[++index], arr[j]);
        }
    }

    std::swap(arr[index + 1], arr[end]);
    return index + 1;
}

/**

Review comment (suggestion):
```suggestion
#include <iostream> /// for std::cout
#include <vector> /// for std::vector
#include <stack> /// for std::stack
#include <algorithm> ///for std::is_sorted
#include <cassert> /// for assert
```


Code after:
/**
 * @file
 * @brief Quick Sort without recursion. This method uses the stack instead.
 * Both recursive and iterative implementations have O(n log n) best case
 * and O(n^2) worst case.
 * @details
 * https://stackoverflow.com/questions/12553238/quicksort-iterative-or-recursive
 * https://en.wikipedia.org/wiki/Quicksort
 * https://www.geeksforgeeks.org/iterative-quick-sort/
 * @author [Sebe324](https://github.com/sebe324)
 */

#include <iostream> /// for std::cout
#include <vector> /// for std::vector
#include <stack> /// for std::stack
#include <algorithm> /// for std::is_sorted
#include <cassert> /// for assert


/**
 * @namespace sorting
 * @brief Sorting algorithms
 */
namespace sorting {
/**
 * @brief The partition function sorts the array from
 * start to end and uses the last element as the pivot.
 * @param arr the array to be sorted
 * @param start starting index
 * @param end ending index
 * @return int next index of the pivot
 */
int partition(std::vector<int> &arr, int start, int end)
{
    int pivot = arr[end];
    int index = start - 1;

    for (int j = start; j < end; j++) {
        if (arr[j] <= pivot) {
            std::swap(arr[++index], arr[j]);
        }
    }
Repository: TheAlgorithms/C-Plus-Plus
File: sorting/quick_sort_iterative.cpp (C++)

Code before:
    std::cout<<"After: \n";
    for(auto x : case1) std::cout<<x<<",";
    std::cout<<"\n";

    //TEST 2 - Negative numbers
    std::vector<int> case2={-10,-2,-5,-2,-3746,-785,-123, -452, -32456};
    std::cout<<"TEST 2\n";
    std::cout<<"Before: \n";
    for(auto x : case2) std::cout<<x<<",";
    std::cout<<"\n";
    iterativeQuickSort(case2);
    assert(std::is_sorted(std::begin(case2),std::end(case2)));
    std::cout<<"Test 2 succesful!\n";
    std::cout<<"After: \n";
    for(auto x : case2) std::cout<<x<<",";
    std::cout<<"\n";
}


/**
 * @brief Main function
 * @returns 0 on exit
 */
int main()
{
    tests();
    return 0;
}


Review comment (suggestion):
```suggestion
    tests(); // run self test implementation
```


Code after:
    std::cout<<"TEST 1\n";
    std::cout<<"Before: \n";
    for(auto x : case1) std::cout<<x<<",";
    std::cout<<"\n";
    sorting::iterativeQuickSort(case1);
    assert(std::is_sorted(std::begin(case1),std::end(case1)));
    std::cout<<"Test 1 succesful!\n";
    std::cout<<"After: \n";
    for(auto x : case1) std::cout<<x<<",";
    std::cout<<"\n";

    //TEST 2 - Negative numbers
    std::vector<int> case2={-10,-2,-5,-2,-3746,-785,-123, -452, -32456};
    std::cout<<"TEST 2\n";
    std::cout<<"Before: \n";
    for(auto x : case2) std::cout<<x<<",";
    std::cout<<"\n";
    sorting::iterativeQuickSort(case2);
    assert(std::is_sorted(std::begin(case2),std::end(case2)));
    std::cout<<"Test 2 succesful!\n";
    std::cout<<"After: \n";
    for(auto x : case2) std::cout<<x<<",";
    std::cout<<"\n";
}


/**
 * @brief Main function
 * @returns 0 on exit
 */
int main()
{
    tests(); // run self test implementation
    return 0;
}