From 83ee753bc22b093ccd8493add47dd3bc4ec95c5f Mon Sep 17 00:00:00 2001 From: krozzzis Date: Tue, 19 Nov 2024 21:21:36 +0500 Subject: [PATCH 01/27] Some work --- task1/matrix.cpp | 83 +++++++++++++++++++++++++++++------------------ task1/matrix.hpp | 43 +++++++++++++++++++----- task2/figures.cpp | 69 ++++++++++++++++++++++++++++++++++++++- task2/figures.hpp | 34 ++++++++++++++++++- 4 files changed, 187 insertions(+), 42 deletions(-) diff --git a/task1/matrix.cpp b/task1/matrix.cpp index fb3598f..6f602e3 100644 --- a/task1/matrix.cpp +++ b/task1/matrix.cpp @@ -1,48 +1,69 @@ #include "matrix.hpp" - #include -Matrix::Matrix(int numRows, int numCols) -{ - // your implementation here +Matrix::Matrix(int num_rows, int num_cols) { + if (num_rows < 0 || num_cols < 0) { + throw std::out_of_range(""); + } + if (rows == 0 || cols == 0) { + rows = 0; + cols = 0; + data.clear(); + } else { + rows = num_rows; + cols = num_cols; + data.assign(rows, std::vector(cols, 0)); + } } -void Matrix::Reset(int numRows, int numCols) -{ - // your implementation here +void Matrix::Reset(int num_rows, int num_cols) { + if (num_rows < 0 || num_cols < 0) { + throw std::out_of_range(""); + } + if (num_rows == 0 || num_cols == 0) { + rows = 0; + cols = 0; + data.clear(); + } else { + rows = num_rows; + cols = num_cols; + data.assign(rows, std::vector(cols, 0)); + } } -int& Matrix::At(int row, int col) -{ - // your implementation here +int Matrix::At(int row, int col) const { + if (row < 0 || row >= rows || col < 0 || col >= cols) { + throw std::out_of_range(""); + } + return data[row][col]; } -const int& Matrix::At(int row, int col) const -{ - // your implementation here +int &Matrix::At(int row, int col) { + if (row < 0 || row >= rows || col < 0 || col >= cols) { + throw std::out_of_range(""); + } + return data[row][col]; } -int Matrix::GetRows() const -{ - // your implementation here -} +int Matrix::GetRows() const { return rows; } -int Matrix::GetCols() const -{ - // your implementation here -} +int Matrix::GetCols() const { return cols; } -bool Matrix::operator==(const Matrix& m2) -{ - // your implementation here +bool operator==(const Matrix &lhs, const Matrix &rhs) { + return lhs.rows == rhs.rows && lhs.cols == rhs.cols && lhs.data == rhs.data; } -bool Matrix::operator!=(const Matrix& m2) -{ - // your implementation here -} +bool operator!=(const Matrix &lhs, const Matrix &rhs) { return !(lhs == rhs); } -Matrix Matrix::operator+(const Matrix& m2) -{ - // your implementation here +Matrix operator+(const Matrix &lhs, const Matrix &rhs) { + if (lhs.GetRows() != rhs.GetRows() || lhs.GetCols() != rhs.GetCols()) { + throw std::invalid_argument(""); + } + Matrix result(lhs.GetRows(), lhs.GetCols()); + for (int i = 0; i < lhs.GetRows(); ++i) { + for (int j = 0; j < lhs.GetCols(); ++j) { + result.At(i, j) = lhs.At(i, j) + rhs.At(i, j); + } + } + return result; } diff --git a/task1/matrix.hpp b/task1/matrix.hpp index d314915..fe8705a 100644 --- a/task1/matrix.hpp +++ b/task1/matrix.hpp @@ -1,18 +1,43 @@ #pragma once +#include +#include +#include -class Matrix -{ +class Matrix { public: - Matrix() = default; - Matrix(int numRows, int numCols); + Matrix() : rows(0), cols(0) {} + explicit Matrix(int rows, int cols); + + Matrix(const Matrix& other) = default; + Matrix& operator=(const Matrix& other) = default; + + Matrix(Matrix&& other) noexcept : num_rows(other.num_rows), num_cols(other.num_cols), data(std::move(other.data)) { + other.rows = 0; + other.cols = 0; + } + Matrix& operator=(Matrix&& other) noexcept { + if (this != &other) { + rows = other.rows; + cols = other.cols; + data = std::move(other.data); + other.rows = 0; + other.cols = 0; + } + return *this; + } - void Reset(int numRows, int numCols); + void Reset(int rows, int cols); + int At(int row, int col) const; int& At(int row, int col); - const int& At(int row, int col) const; int GetRows() const; int GetCols() const; - bool operator==(const Matrix& m2); - bool operator!=(const Matrix& m2); - Matrix operator+(const Matrix& m2); + friend bool operator==(const Matrix& lhs, const Matrix& rhs); + friend bool operator!=(const Matrix& lhs, const Matrix& rhs); + friend Matrix operator+(const Matrix& lhs, const Matrix& rhs); + +private: + int rows; + int cols; + std::vector> data; }; diff --git a/task2/figures.cpp b/task2/figures.cpp index 3b33880..c450839 100644 --- a/task2/figures.cpp +++ b/task2/figures.cpp @@ -1,3 +1,70 @@ #include "figures.hpp" +#include -static constexpr double PI = 3.14; +Rect::Rect(double a, double b) : width(a), height(b) {} + +FigureType Rect::Type() const { + return FigureType::RECTANGLE; +} + +double Rect::Perimeter() const { + return 2 * (width + height); +} + +double Rect::Area() const { + return width * height; +} + +Triangle::Triangle(double side_a, double side_b, double side_c) + : a(side_a), b(side_b), c(side_c) {} + +FigureType Triangle::Type() const { + return FigureType::TRIANGLE; +} + +double Triangle::Perimeter() const { + return a + b + c; +} + +double Triangle::Area() const { + double s = Perimeter() / 2; + return std::sqrt(s * (s - a) * (s - b) * (s - c)); +} + +Circle::Circle(double r) : radius(r) {} + +FigureType Circle::Type() const { + return FigureType::CIRCLE; +} + +double Circle::Perimeter() const { + return 2 * PI * radius; +} + +double Circle::Area() const { + return PI * radius * radius; +} + + +std::unique_ptr
make_figure(FigureType type, double a, double b, double c) { + if (a < 0 || b < 0 || c < 0) { + throw LessThanZeroParam(""); + } + + switch (type) { + case FigureType::RECTANGLE: + return std::make_unique(a, b); + + case FigureType::TRIANGLE: + if (a + b <= c || b + c <= a || a + c <= b) { + throw WrongTriangle(""); + } + return std::make_unique(a, b, c); + + case FigureType::CIRCLE: + return std::make_unique(a); + + default: + return nullptr; + } +} diff --git a/task2/figures.hpp b/task2/figures.hpp index 65a6110..d27e188 100644 --- a/task2/figures.hpp +++ b/task2/figures.hpp @@ -3,6 +3,9 @@ #include #include +// Make PI accessible to tests and other code +constexpr double PI = 3.14; + enum class FigureType { TRIANGLE, @@ -12,6 +15,7 @@ enum class FigureType class Figure { public: + virtual ~Figure() = default; // Add virtual destructor for proper cleanup virtual FigureType Type() const = 0; virtual double Perimeter() const = 0; virtual double Area() const = 0; @@ -19,23 +23,51 @@ class Figure { class Rect : public Figure { +private: + double width; + double height; + +public: + Rect(double a, double b); + FigureType Type() const override; + double Perimeter() const override; + double Area() const override; }; class Triangle : public Figure { +private: + double a, b, c; + +public: + Triangle(double side_a, double side_b, double side_c); + FigureType Type() const override; + double Perimeter() const override; + double Area() const override; }; class Circle : public Figure { +private: + double radius; + +public: + Circle(double r); + FigureType Type() const override; + double Perimeter() const override; + double Area() const override; }; std::unique_ptr
make_figure(FigureType type, double a, double b = 0, double c = 0); class WrongTriangle : public std::invalid_argument { +public: + using std::invalid_argument::invalid_argument; }; class LessThanZeroParam : public std::invalid_argument { +public: + using std::invalid_argument::invalid_argument; }; - From 2d677647c8fae77fe024f7831e4960d2ad1f2b05 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Tue, 19 Nov 2024 21:25:07 +0500 Subject: [PATCH 02/27] Update matrix.hpp --- task1/matrix.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task1/matrix.hpp b/task1/matrix.hpp index fe8705a..669a2d5 100644 --- a/task1/matrix.hpp +++ b/task1/matrix.hpp @@ -11,7 +11,7 @@ class Matrix { Matrix(const Matrix& other) = default; Matrix& operator=(const Matrix& other) = default; - Matrix(Matrix&& other) noexcept : num_rows(other.num_rows), num_cols(other.num_cols), data(std::move(other.data)) { + Matrix(Matrix&& other) noexcept : rows(other.rows), cols(other.cols), data(std::move(other.data)) { other.rows = 0; other.cols = 0; } From d2c28019cfc27c0a03297e1bc2b7205a12dd4dbd Mon Sep 17 00:00:00 2001 From: krozzzis Date: Tue, 19 Nov 2024 21:26:54 +0500 Subject: [PATCH 03/27] Some work --- task1/matrix.cpp | 108 +++++++++++++++++++++++++---------------------- task1/matrix.hpp | 20 ++++----- 2 files changed, 68 insertions(+), 60 deletions(-) diff --git a/task1/matrix.cpp b/task1/matrix.cpp index 6f602e3..3964e86 100644 --- a/task1/matrix.cpp +++ b/task1/matrix.cpp @@ -1,69 +1,77 @@ #include "matrix.hpp" #include -Matrix::Matrix(int num_rows, int num_cols) { - if (num_rows < 0 || num_cols < 0) { - throw std::out_of_range(""); - } - if (rows == 0 || cols == 0) { - rows = 0; - cols = 0; - data.clear(); - } else { - rows = num_rows; - cols = num_cols; - data.assign(rows, std::vector(cols, 0)); - } +using namespace std; + +Matrix::Matrix(int rows, int cols) { + if (rows < 0 || cols < 0) { + throw out_of_range(""); + } + if (rows == 0 || cols == 0) { + num_rows = 0; + num_cols = 0; + data.clear(); + } else { + num_rows = rows; + num_cols = cols; + data.assign(rows, vector(cols, 0)); + } } -void Matrix::Reset(int num_rows, int num_cols) { - if (num_rows < 0 || num_cols < 0) { - throw std::out_of_range(""); - } - if (num_rows == 0 || num_cols == 0) { - rows = 0; - cols = 0; - data.clear(); - } else { - rows = num_rows; - cols = num_cols; - data.assign(rows, std::vector(cols, 0)); - } +void Matrix::Reset(int rows, int cols) { + if (rows < 0 || cols < 0) { + throw out_of_range(""); + } + if (rows == 0 || cols == 0) { + num_rows = 0; + num_cols = 0; + data.clear(); + } else { + num_rows = rows; + num_cols = cols; + data.assign(rows, vector(cols, 0)); + } } int Matrix::At(int row, int col) const { - if (row < 0 || row >= rows || col < 0 || col >= cols) { - throw std::out_of_range(""); - } - return data[row][col]; + if (row < 0 || row >= num_rows || col < 0 || col >= num_cols) { + throw out_of_range(""); + } + return data[row][col]; } -int &Matrix::At(int row, int col) { - if (row < 0 || row >= rows || col < 0 || col >= cols) { - throw std::out_of_range(""); - } - return data[row][col]; +int& Matrix::At(int row, int col) { + if (row < 0 || row >= num_rows || col < 0 || col >= num_cols) { + throw out_of_range(""); + } + return data[row][col]; } -int Matrix::GetRows() const { return rows; } +int Matrix::GetRows() const { + return num_rows; +} -int Matrix::GetCols() const { return cols; } +int Matrix::GetCols() const { + return num_cols; +} -bool operator==(const Matrix &lhs, const Matrix &rhs) { - return lhs.rows == rhs.rows && lhs.cols == rhs.cols && lhs.data == rhs.data; +bool operator==(const Matrix& lhs, const Matrix& rhs) { + return lhs.num_rows == rhs.num_rows && lhs.num_cols == rhs.num_cols && lhs.data == rhs.data; } -bool operator!=(const Matrix &lhs, const Matrix &rhs) { return !(lhs == rhs); } +bool operator!=(const Matrix& lhs, const Matrix& rhs) { + return !(lhs == rhs); +} -Matrix operator+(const Matrix &lhs, const Matrix &rhs) { - if (lhs.GetRows() != rhs.GetRows() || lhs.GetCols() != rhs.GetCols()) { - throw std::invalid_argument(""); - } - Matrix result(lhs.GetRows(), lhs.GetCols()); - for (int i = 0; i < lhs.GetRows(); ++i) { - for (int j = 0; j < lhs.GetCols(); ++j) { - result.At(i, j) = lhs.At(i, j) + rhs.At(i, j); +Matrix operator+(const Matrix& lhs, const Matrix& rhs) { + if (lhs.GetRows() != rhs.GetRows() || lhs.GetCols() != rhs.GetCols()) { + throw invalid_argument(""); + } + Matrix result(lhs.GetRows(), lhs.GetCols()); + for (int i = 0; i < lhs.GetRows(); ++i) { + for (int j = 0; j < lhs.GetCols(); ++j) { + result.At(i, j) = lhs.At(i, j) + rhs.At(i, j); + } } - } - return result; + return result; } diff --git a/task1/matrix.hpp b/task1/matrix.hpp index 669a2d5..7ca795a 100644 --- a/task1/matrix.hpp +++ b/task1/matrix.hpp @@ -5,23 +5,23 @@ class Matrix { public: - Matrix() : rows(0), cols(0) {} + Matrix() : num_rows(0), num_cols(0) {} explicit Matrix(int rows, int cols); Matrix(const Matrix& other) = default; Matrix& operator=(const Matrix& other) = default; - Matrix(Matrix&& other) noexcept : rows(other.rows), cols(other.cols), data(std::move(other.data)) { - other.rows = 0; - other.cols = 0; + Matrix(Matrix&& other) noexcept : num_rows(other.num_rows), num_cols(other.num_cols), data(std::move(other.data)) { + other.num_rows = 0; + other.num_cols = 0; } Matrix& operator=(Matrix&& other) noexcept { if (this != &other) { - rows = other.rows; - cols = other.cols; + num_rows = other.num_rows; + num_cols = other.num_cols; data = std::move(other.data); - other.rows = 0; - other.cols = 0; + other.num_rows = 0; + other.num_cols = 0; } return *this; } @@ -37,7 +37,7 @@ class Matrix { friend Matrix operator+(const Matrix& lhs, const Matrix& rhs); private: - int rows; - int cols; + int num_rows; + int num_cols; std::vector> data; }; From 2118de7efa51cacd3666621e6febc35a0a0f6c04 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Tue, 19 Nov 2024 21:35:58 +0500 Subject: [PATCH 04/27] Update circular_queue.hpp --- task3/circular_queue.hpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/task3/circular_queue.hpp b/task3/circular_queue.hpp index 2871091..0bc9b62 100644 --- a/task3/circular_queue.hpp +++ b/task3/circular_queue.hpp @@ -3,12 +3,24 @@ #include class CircularQueue { +private: + int* buffer; // Dynamic array to store elements + size_t capacity; // Maximum size of the queue + size_t front; // Index of the front element + size_t rear; // Index of the rear element + bool is_empty; // Flag to track empty state + public: - CircularQueue(size_t size); // создать очередь с определенным размером буффера - bool Push(int value); // добавить значение в конец очереди (false, если очередь заполнена) - bool Pop(); // удалить значение из начала очереди (false, если очередь пустая) - int Front() const; // получить значение из начала очереди (-1, если очередь пустая) - int Back() const; // получить значение из конца очереди (-1, если очередь пустая) - bool Empty() const; // проверить пустая ли очередь - bool Full() const; // проверить заполнена ли очередь + CircularQueue(size_t size); // Constructor + ~CircularQueue(); // Destructor + + // Core operations + bool Push(int value); // Add value to the end + bool Pop(); // Remove value from the front + int Front() const; // Get front value + int Back() const; // Get back value + + // State checks + bool Empty() const; // Check if empty + bool Full() const; // Check if full }; From 81211c377131e4378674f16c4d7ec189a4b3b42b Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Tue, 19 Nov 2024 21:36:29 +0500 Subject: [PATCH 05/27] Update circular_queue.cpp --- task3/circular_queue.cpp | 63 +++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/task3/circular_queue.cpp b/task3/circular_queue.cpp index fa74734..46a6568 100644 --- a/task3/circular_queue.cpp +++ b/task3/circular_queue.cpp @@ -1,36 +1,59 @@ #include "circular_queue.hpp" -CircularQueue::CircularQueue(size_t size) +CircularQueue::CircularQueue(size_t size) : + capacity(size), + front(0), + rear(0), + is_empty(true) { - // your implementation here + buffer = new int[size]; } -bool CircularQueue::Push(int value) -{ - // your implementation here +CircularQueue::~CircularQueue() { + delete[] buffer; } -bool CircularQueue::Pop() -{ - // your implementation here +bool CircularQueue::Push(int value) { + if (Full()) { + return false; + } + + buffer[rear] = value; + rear = (rear + 1) % capacity; + is_empty = false; + + return true; } -int CircularQueue::Front() const -{ - // your implementation here +bool CircularQueue::Pop() { + if (Empty()) { + return false; + } + + front = (front + 1) % capacity; + is_empty = (front == rear); + + return true; } -int CircularQueue::Back() const -{ - // your implementation here +int CircularQueue::Front() const { + if (Empty()) { + return -1; + } + return buffer[front]; } -bool CircularQueue::Empty() const -{ - // your implementation here +int CircularQueue::Back() const { + if (Empty()) { + return -1; + } + return buffer[(rear == 0 ? capacity : rear) - 1]; } -bool CircularQueue::Full() const -{ - // your implementation here +bool CircularQueue::Empty() const { + return is_empty; +} + +bool CircularQueue::Full() const { + return !is_empty && (front == rear); } From d302f1ce9b711bca695f04581ef61007e7c58aa1 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Wed, 20 Nov 2024 15:42:46 +0500 Subject: [PATCH 06/27] Update circular_queue.cpp --- task3/circular_queue.cpp | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/task3/circular_queue.cpp b/task3/circular_queue.cpp index 46a6568..7e03a2e 100644 --- a/task3/circular_queue.cpp +++ b/task3/circular_queue.cpp @@ -1,27 +1,14 @@ -#include "circular_queue.hpp" - -CircularQueue::CircularQueue(size_t size) : - capacity(size), - front(0), - rear(0), - is_empty(true) -{ +CircularQueue::CircularQueue(size_t size) : capacity(size), front(0), rear(0), size(0) { buffer = new int[size]; } -CircularQueue::~CircularQueue() { - delete[] buffer; -} - bool CircularQueue::Push(int value) { if (Full()) { return false; } - buffer[rear] = value; rear = (rear + 1) % capacity; - is_empty = false; - + size++; return true; } @@ -29,10 +16,8 @@ bool CircularQueue::Pop() { if (Empty()) { return false; } - front = (front + 1) % capacity; - is_empty = (front == rear); - + size--; return true; } @@ -47,13 +32,13 @@ int CircularQueue::Back() const { if (Empty()) { return -1; } - return buffer[(rear == 0 ? capacity : rear) - 1]; + return buffer[(rear - 1 + capacity) % capacity]; } bool CircularQueue::Empty() const { - return is_empty; + return size == 0; } bool CircularQueue::Full() const { - return !is_empty && (front == rear); + return size == capacity; } From d12ebae5d679c2e1072919517e232d3d31dda7bd Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Wed, 20 Nov 2024 15:43:00 +0500 Subject: [PATCH 07/27] Update circular_queue.hpp --- task3/circular_queue.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/task3/circular_queue.hpp b/task3/circular_queue.hpp index 0bc9b62..f78137e 100644 --- a/task3/circular_queue.hpp +++ b/task3/circular_queue.hpp @@ -8,7 +8,6 @@ class CircularQueue { size_t capacity; // Maximum size of the queue size_t front; // Index of the front element size_t rear; // Index of the rear element - bool is_empty; // Flag to track empty state public: CircularQueue(size_t size); // Constructor From 481a4eb06b08639b0c536b3c491b0a06df728552 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Wed, 20 Nov 2024 15:45:48 +0500 Subject: [PATCH 08/27] Update circular_queue.hpp --- task3/circular_queue.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/task3/circular_queue.hpp b/task3/circular_queue.hpp index f78137e..b0e271d 100644 --- a/task3/circular_queue.hpp +++ b/task3/circular_queue.hpp @@ -6,6 +6,7 @@ class CircularQueue { private: int* buffer; // Dynamic array to store elements size_t capacity; // Maximum size of the queue + size_t size; // Current count of elements size_t front; // Index of the front element size_t rear; // Index of the rear element From 3c253f1325bb2e2706e9a027313ea372061eb9db Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Wed, 20 Nov 2024 15:48:56 +0500 Subject: [PATCH 09/27] Update circular_queue.cpp --- task3/circular_queue.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/task3/circular_queue.cpp b/task3/circular_queue.cpp index 7e03a2e..bc7ea07 100644 --- a/task3/circular_queue.cpp +++ b/task3/circular_queue.cpp @@ -1,3 +1,6 @@ +#include "circular_queue.hpp" +#include + CircularQueue::CircularQueue(size_t size) : capacity(size), front(0), rear(0), size(0) { buffer = new int[size]; } From 7fa1635b8d1393c3d253002a096d028b1fe98de4 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Wed, 20 Nov 2024 15:52:51 +0500 Subject: [PATCH 10/27] Update circular_queue.cpp --- task3/circular_queue.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task3/circular_queue.cpp b/task3/circular_queue.cpp index bc7ea07..b4325b6 100644 --- a/task3/circular_queue.cpp +++ b/task3/circular_queue.cpp @@ -1,7 +1,7 @@ #include "circular_queue.hpp" #include -CircularQueue::CircularQueue(size_t size) : capacity(size), front(0), rear(0), size(0) { +CircularQueue::CircularQueue(size_t size) : capacity(size), size(0), front(0), rear(0) { buffer = new int[size]; } From 5ba759cf36d337fd2ec96b3951c85305eb707fd9 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Wed, 20 Nov 2024 15:55:25 +0500 Subject: [PATCH 11/27] Update circular_queue.cpp --- task3/circular_queue.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/task3/circular_queue.cpp b/task3/circular_queue.cpp index b4325b6..b329ade 100644 --- a/task3/circular_queue.cpp +++ b/task3/circular_queue.cpp @@ -5,6 +5,10 @@ CircularQueue::CircularQueue(size_t size) : capacity(size), size(0), front(0), r buffer = new int[size]; } +CircularQueue::~CircularQueue() { + delete[] buffer; +} + bool CircularQueue::Push(int value) { if (Full()) { return false; From 58a9af0926c1e315916f8d9bf394da1231cc779d Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Wed, 20 Nov 2024 15:57:37 +0500 Subject: [PATCH 12/27] Update circular_queue.hpp --- task3/circular_queue.hpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/task3/circular_queue.hpp b/task3/circular_queue.hpp index b0e271d..52919b3 100644 --- a/task3/circular_queue.hpp +++ b/task3/circular_queue.hpp @@ -12,15 +12,12 @@ class CircularQueue { public: CircularQueue(size_t size); // Constructor - ~CircularQueue(); // Destructor - - // Core operations + bool Push(int value); // Add value to the end bool Pop(); // Remove value from the front int Front() const; // Get front value int Back() const; // Get back value - // State checks bool Empty() const; // Check if empty bool Full() const; // Check if full }; From 0e108618f4abdfe46064c0e857c1c629d971a12c Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Wed, 20 Nov 2024 15:57:47 +0500 Subject: [PATCH 13/27] Update circular_queue.cpp --- task3/circular_queue.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/task3/circular_queue.cpp b/task3/circular_queue.cpp index b329ade..b4325b6 100644 --- a/task3/circular_queue.cpp +++ b/task3/circular_queue.cpp @@ -5,10 +5,6 @@ CircularQueue::CircularQueue(size_t size) : capacity(size), size(0), front(0), r buffer = new int[size]; } -CircularQueue::~CircularQueue() { - delete[] buffer; -} - bool CircularQueue::Push(int value) { if (Full()) { return false; From fce891d39a3760fb095073225db1c61d24cbbe68 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Thu, 21 Nov 2024 16:17:39 +0500 Subject: [PATCH 14/27] Update circular_queue.hpp --- task3/circular_queue.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/task3/circular_queue.hpp b/task3/circular_queue.hpp index 52919b3..a6005c3 100644 --- a/task3/circular_queue.hpp +++ b/task3/circular_queue.hpp @@ -1,12 +1,11 @@ #pragma once +#include #include class CircularQueue { private: - int* buffer; // Dynamic array to store elements - size_t capacity; // Maximum size of the queue - size_t size; // Current count of elements + std::vector data; // Elements size_t front; // Index of the front element size_t rear; // Index of the rear element From 5024b9ad24e13dfd77064966fc6027489868ce79 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Thu, 21 Nov 2024 16:21:51 +0500 Subject: [PATCH 15/27] Update circular_queue.cpp --- task3/circular_queue.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/task3/circular_queue.cpp b/task3/circular_queue.cpp index b4325b6..695e439 100644 --- a/task3/circular_queue.cpp +++ b/task3/circular_queue.cpp @@ -1,17 +1,17 @@ #include "circular_queue.hpp" #include +#include -CircularQueue::CircularQueue(size_t size) : capacity(size), size(0), front(0), rear(0) { - buffer = new int[size]; +CircularQueue::CircularQueue(size_t size) : capacity(size), front(0), rear(0) { + data = std::vector(size); } bool CircularQueue::Push(int value) { if (Full()) { return false; } - buffer[rear] = value; + data[rear] = value; rear = (rear + 1) % capacity; - size++; return true; } @@ -20,7 +20,6 @@ bool CircularQueue::Pop() { return false; } front = (front + 1) % capacity; - size--; return true; } @@ -28,20 +27,20 @@ int CircularQueue::Front() const { if (Empty()) { return -1; } - return buffer[front]; + return data[front]; } int CircularQueue::Back() const { if (Empty()) { return -1; } - return buffer[(rear - 1 + capacity) % capacity]; + return data[(rear - 1 + capacity) % capacity]; } bool CircularQueue::Empty() const { - return size == 0; + return data.size() == 0; } bool CircularQueue::Full() const { - return size == capacity; + return data.size() == capacity; } From 14edbc6b5b9d225a7a3637ee25975e81ac7a05d0 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Thu, 21 Nov 2024 16:22:19 +0500 Subject: [PATCH 16/27] Update circular_queue.hpp --- task3/circular_queue.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/task3/circular_queue.hpp b/task3/circular_queue.hpp index a6005c3..0ad9001 100644 --- a/task3/circular_queue.hpp +++ b/task3/circular_queue.hpp @@ -6,6 +6,7 @@ class CircularQueue { private: std::vector data; // Elements + size_t capatict; // Maximum count of elements size_t front; // Index of the front element size_t rear; // Index of the rear element From 644faa7f06245e03f9850d94e59add08920aa4a5 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Thu, 21 Nov 2024 16:27:12 +0500 Subject: [PATCH 17/27] Update circular_queue.hpp --- task3/circular_queue.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task3/circular_queue.hpp b/task3/circular_queue.hpp index 0ad9001..b79a115 100644 --- a/task3/circular_queue.hpp +++ b/task3/circular_queue.hpp @@ -6,7 +6,7 @@ class CircularQueue { private: std::vector data; // Elements - size_t capatict; // Maximum count of elements + size_t capacity; // Maximum count of elements size_t front; // Index of the front element size_t rear; // Index of the rear element From e760e90812524444e7c5bc90e8a99c19bf9e0b13 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Thu, 21 Nov 2024 16:30:26 +0500 Subject: [PATCH 18/27] Update circular_queue.cpp --- task3/circular_queue.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/task3/circular_queue.cpp b/task3/circular_queue.cpp index 695e439..e277011 100644 --- a/task3/circular_queue.cpp +++ b/task3/circular_queue.cpp @@ -3,7 +3,8 @@ #include CircularQueue::CircularQueue(size_t size) : capacity(size), front(0), rear(0) { - data = std::vector(size); + data = std::vector; + data.reserve(size); } bool CircularQueue::Push(int value) { From 4ed07fdf50df45555d6a8771db188cd32d0e0a4e Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Thu, 21 Nov 2024 16:32:26 +0500 Subject: [PATCH 19/27] Update circular_queue.cpp --- task3/circular_queue.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/task3/circular_queue.cpp b/task3/circular_queue.cpp index e277011..3192dcc 100644 --- a/task3/circular_queue.cpp +++ b/task3/circular_queue.cpp @@ -3,7 +3,7 @@ #include CircularQueue::CircularQueue(size_t size) : capacity(size), front(0), rear(0) { - data = std::vector; + data = std::vector(); data.reserve(size); } From c31ef7136545ca4b8f2dbcba9e5198e67092326c Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Thu, 21 Nov 2024 16:44:20 +0500 Subject: [PATCH 20/27] Update circular_queue.hpp --- task3/circular_queue.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/task3/circular_queue.hpp b/task3/circular_queue.hpp index b79a115..913c5b2 100644 --- a/task3/circular_queue.hpp +++ b/task3/circular_queue.hpp @@ -7,6 +7,7 @@ class CircularQueue { private: std::vector data; // Elements size_t capacity; // Maximum count of elements + size_t count; // Count of elements size_t front; // Index of the front element size_t rear; // Index of the rear element From b6e6b789e9874adf6a8c8045d138cfaf482b45da Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Thu, 21 Nov 2024 16:45:01 +0500 Subject: [PATCH 21/27] Update circular_queue.cpp --- task3/circular_queue.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/task3/circular_queue.cpp b/task3/circular_queue.cpp index 3192dcc..4cdd5b5 100644 --- a/task3/circular_queue.cpp +++ b/task3/circular_queue.cpp @@ -2,7 +2,7 @@ #include #include -CircularQueue::CircularQueue(size_t size) : capacity(size), front(0), rear(0) { +CircularQueue::CircularQueue(size_t size) : capacity(size), count(0), front(0), rear(0) { data = std::vector(); data.reserve(size); } @@ -13,6 +13,7 @@ bool CircularQueue::Push(int value) { } data[rear] = value; rear = (rear + 1) % capacity; + count++; return true; } @@ -21,6 +22,7 @@ bool CircularQueue::Pop() { return false; } front = (front + 1) % capacity; + count--; return true; } @@ -39,9 +41,9 @@ int CircularQueue::Back() const { } bool CircularQueue::Empty() const { - return data.size() == 0; + return count == 0; } bool CircularQueue::Full() const { - return data.size() == capacity; + return count == capacity; } From 757351574720d1ceabef1b99f7573593e06c4660 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Thu, 21 Nov 2024 16:48:59 +0500 Subject: [PATCH 22/27] Update circular_queue.cpp --- task3/circular_queue.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/task3/circular_queue.cpp b/task3/circular_queue.cpp index 4cdd5b5..b329ade 100644 --- a/task3/circular_queue.cpp +++ b/task3/circular_queue.cpp @@ -1,19 +1,21 @@ #include "circular_queue.hpp" #include -#include -CircularQueue::CircularQueue(size_t size) : capacity(size), count(0), front(0), rear(0) { - data = std::vector(); - data.reserve(size); +CircularQueue::CircularQueue(size_t size) : capacity(size), size(0), front(0), rear(0) { + buffer = new int[size]; +} + +CircularQueue::~CircularQueue() { + delete[] buffer; } bool CircularQueue::Push(int value) { if (Full()) { return false; } - data[rear] = value; + buffer[rear] = value; rear = (rear + 1) % capacity; - count++; + size++; return true; } @@ -22,7 +24,7 @@ bool CircularQueue::Pop() { return false; } front = (front + 1) % capacity; - count--; + size--; return true; } @@ -30,20 +32,20 @@ int CircularQueue::Front() const { if (Empty()) { return -1; } - return data[front]; + return buffer[front]; } int CircularQueue::Back() const { if (Empty()) { return -1; } - return data[(rear - 1 + capacity) % capacity]; + return buffer[(rear - 1 + capacity) % capacity]; } bool CircularQueue::Empty() const { - return count == 0; + return size == 0; } bool CircularQueue::Full() const { - return count == capacity; + return size == capacity; } From 42065b0503d5613ad3f318a75c2379364a0f6a31 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Thu, 21 Nov 2024 16:49:44 +0500 Subject: [PATCH 23/27] Update circular_queue.hpp --- task3/circular_queue.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/task3/circular_queue.hpp b/task3/circular_queue.hpp index 913c5b2..44366cd 100644 --- a/task3/circular_queue.hpp +++ b/task3/circular_queue.hpp @@ -1,18 +1,18 @@ #pragma once -#include #include class CircularQueue { private: - std::vector data; // Elements - size_t capacity; // Maximum count of elements - size_t count; // Count of elements + int* buffer; // Dynamic array to store elements + size_t capacity; // Maximum size of the queue + size_t size; // Current count of elements size_t front; // Index of the front element size_t rear; // Index of the rear element public: CircularQueue(size_t size); // Constructor + ~CircularQueue(); // Destructor bool Push(int value); // Add value to the end bool Pop(); // Remove value from the front From 92a8a4dcad7e48e6db2dc2fd69f3c47886483955 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Thu, 21 Nov 2024 17:00:46 +0500 Subject: [PATCH 24/27] Update circular_queue.hpp --- task3/circular_queue.hpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/task3/circular_queue.hpp b/task3/circular_queue.hpp index 44366cd..2422f6d 100644 --- a/task3/circular_queue.hpp +++ b/task3/circular_queue.hpp @@ -14,6 +14,11 @@ class CircularQueue { CircularQueue(size_t size); // Constructor ~CircularQueue(); // Destructor + CircularQueue(const CircularQueue& other); // Copy constructor + CircularQueue& operator=(const CircularQueue& other); // Copy assignment + CircularQueue(CircularQueue&& other) noexcept; // Move constructor + CircularQueue& operator=(CircularQueue&& other) noexcept; // Move assignment + bool Push(int value); // Add value to the end bool Pop(); // Remove value from the front int Front() const; // Get front value From 1fc3f72d0aabd3e04bf4923fc9166542df92a1b7 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Thu, 21 Nov 2024 17:05:47 +0500 Subject: [PATCH 25/27] Update circular_queue.cpp --- task3/circular_queue.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/task3/circular_queue.cpp b/task3/circular_queue.cpp index b329ade..c23c9b1 100644 --- a/task3/circular_queue.cpp +++ b/task3/circular_queue.cpp @@ -1,12 +1,8 @@ #include "circular_queue.hpp" -#include +#include -CircularQueue::CircularQueue(size_t size) : capacity(size), size(0), front(0), rear(0) { - buffer = new int[size]; -} - -CircularQueue::~CircularQueue() { - delete[] buffer; +CircularQueue::CircularQueue(size_t size) : capacity(size), size(0), front(0), rear(0) { + buffer.resize(size); // Pre-allocate exact size } bool CircularQueue::Push(int value) { From fa709e78a169f5e0f1f5edb6ce27cda9c0293402 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Thu, 21 Nov 2024 17:06:30 +0500 Subject: [PATCH 26/27] Update circular_queue.hpp --- task3/circular_queue.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/task3/circular_queue.hpp b/task3/circular_queue.hpp index 2422f6d..91a14f9 100644 --- a/task3/circular_queue.hpp +++ b/task3/circular_queue.hpp @@ -1,10 +1,10 @@ #pragma once -#include +#include class CircularQueue { private: - int* buffer; // Dynamic array to store elements + std::vector buffer; // Dynamic array to store elements size_t capacity; // Maximum size of the queue size_t size; // Current count of elements size_t front; // Index of the front element From 6edb7225f9be86c547237b99873b557a8cd9faf9 Mon Sep 17 00:00:00 2001 From: Nikita Shumov Date: Thu, 21 Nov 2024 17:08:44 +0500 Subject: [PATCH 27/27] Update circular_queue.hpp --- task3/circular_queue.hpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/task3/circular_queue.hpp b/task3/circular_queue.hpp index 91a14f9..32606b9 100644 --- a/task3/circular_queue.hpp +++ b/task3/circular_queue.hpp @@ -12,12 +12,6 @@ class CircularQueue { public: CircularQueue(size_t size); // Constructor - ~CircularQueue(); // Destructor - - CircularQueue(const CircularQueue& other); // Copy constructor - CircularQueue& operator=(const CircularQueue& other); // Copy assignment - CircularQueue(CircularQueue&& other) noexcept; // Move constructor - CircularQueue& operator=(CircularQueue&& other) noexcept; // Move assignment bool Push(int value); // Add value to the end bool Pop(); // Remove value from the front