-
Notifications
You must be signed in to change notification settings - Fork 33
Шайхуллин Евгений #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
5e3792e
4f1cfc1
be4a8cb
f42a998
d7911f1
28f780a
d2884ca
8f7f434
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,5 +6,5 @@ struct AlgTag {}; | |
|
|
||
|
|
||
| class Phasor { | ||
|
|
||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,194 @@ | ||
| #include <vector> | ||
|
|
||
|
|
||
|
|
||
| class RingBuffer { | ||
| public: | ||
|
|
||
| // ---- Конструкторы ----- | ||
| explicit RingBuffer(size_t capacity); // Конструктор от емкости | ||
| RingBuffer(size_t capacity, int initialValue); // Конструткор от емкасти и начального значения | ||
| RingBuffer(std::initializer_list<int> init); // конструктор по размеру контейнера | ||
|
|
||
| // ---- Методы ----- | ||
| void Push(int value); | ||
| bool TryPush(int value); | ||
|
|
||
| bool Pop(); | ||
| bool TryPop(int& value); | ||
|
|
||
| int& Front(); | ||
| const int& Front() const; | ||
| int& Back(); | ||
| const int& Back() const; | ||
|
|
||
| bool Empty() const {return countElem == 0;} | ||
| bool Full() const {return countElem == buf.capacity();} | ||
| size_t Size() const{return countElem;} | ||
| size_t Capacity() const{return buf.capacity();} | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. проблемы с пробелами, и лучше сделать единообразно и вынести все методы вне класса |
||
|
|
||
| void Clear() {prevElem = nextElem = countElem = 0;} | ||
| void Resize(size_t newCapacity); | ||
|
|
||
| std::vector<int> Vector() const; | ||
|
|
||
| bool operator==(const RingBuffer& other) const; | ||
| bool operator!=(const RingBuffer& other) const; | ||
| int& operator[](size_t index); | ||
| const int& operator[](size_t index) const; | ||
|
|
||
| private: | ||
| std::vector<int> buf; | ||
| size_t prevElem = 0; | ||
| size_t nextElem = 0; | ||
| size_t countElem = 0; // счетчик текущего элемента | ||
|
|
||
|
|
||
| size_t NextIndex(size_t i) const { | ||
| if((i + 1) >= buf.capacity()) return 0; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лишние пробелы перед return не пробела после if |
||
| return i+1; | ||
| } | ||
|
|
||
|
|
||
| }; | ||
|
|
||
| RingBuffer::RingBuffer(size_t capacity) { | ||
| if(capacity == 0) capacity = 1; // Емкость 0 не должна быть | ||
| buf.reserve(capacity); | ||
| buf.resize(capacity); | ||
| prevElem = 0; | ||
| nextElem = 0; | ||
| countElem = 0; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше сделать в списке инициализации. для буфера предварительный reserve излишний в данном случае |
||
| } | ||
|
|
||
| RingBuffer::RingBuffer(size_t capacity, int initialValue) { | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лишняя строка |
||
| if(capacity == 0) capacity = 1; // Емкость 0 не должна быть | ||
| buf.reserve(capacity); | ||
| buf.resize(capacity); | ||
| prevElem = 0; | ||
| nextElem = 0; | ||
| countElem = 0; | ||
| for (size_t i = 0; i < capacity; ++i) Push(initialValue); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. правильнее все действия выполнить в списке инициализации, для buf воспользоваться конструктором |
||
|
|
||
| } | ||
|
|
||
| RingBuffer::RingBuffer(std::initializer_list<int> init) { | ||
|
|
||
| size_t capacity = init.size(); | ||
| if(capacity == 0) capacity = 1; // Емкость 0 не должна быть | ||
|
|
||
| buf.reserve(capacity); | ||
| buf.resize(capacity); | ||
| prevElem = 0; | ||
| nextElem = 0; | ||
| countElem = 0; | ||
|
|
||
| for (int value : init) Push(value); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. аналогично, все можно сделать в списке инициализации |
||
| } | ||
|
|
||
| void RingBuffer::Push(int value) { | ||
| if (Full()) { | ||
| buf[nextElem] = value; | ||
| prevElem = NextIndex(prevElem); | ||
| nextElem = NextIndex(nextElem); | ||
| } else { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше убрать else сделать одну короткую ветвь с return |
||
| buf[nextElem] = value; | ||
| nextElem = NextIndex(nextElem); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. общие действия которые выполняются в любом случае, лучше вынести, чтобы не дублировать |
||
| ++countElem; | ||
| } | ||
| } | ||
|
|
||
| bool RingBuffer::TryPush(int value) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. код Push и TryPush достаточно похож, так что можно было переиспользовать один в другом |
||
| if (Full()) return false; | ||
| buf[nextElem] = value; | ||
| nextElem = NextIndex(nextElem); | ||
| ++countElem; | ||
| return true; | ||
| } | ||
|
|
||
| bool RingBuffer::Pop() { | ||
| if (Empty()) return false; | ||
| prevElem = NextIndex(prevElem); | ||
| --countElem; | ||
| return true; | ||
| } | ||
|
|
||
| bool RingBuffer::TryPop(int& value) { | ||
| if (Empty()) return false; | ||
| value = buf[prevElem]; | ||
| Pop(); | ||
| return true; | ||
| } | ||
|
|
||
| int& RingBuffer::operator[](size_t i) { | ||
| if(i >= countElem) return buf[countElem]; | ||
| return buf[((i + prevElem)%buf.capacity())]; | ||
|
|
||
| } | ||
|
|
||
| const int& RingBuffer::operator[](size_t i) const { | ||
| if(i >= countElem) return buf[countElem]; | ||
| return buf[((i + prevElem)%buf.capacity())]; | ||
| } | ||
|
|
||
| int& RingBuffer::Front() { | ||
| if(nextElem == 0) return buf[buf.capacity() - 1]; | ||
| return buf[nextElem - 1]; | ||
| } | ||
|
|
||
| const int& RingBuffer::Front() const { | ||
| if(nextElem == 0) return buf[buf.capacity() - 1]; | ||
| return buf[nextElem - 1]; | ||
| } | ||
|
|
||
| int& RingBuffer::Back() { | ||
| return buf[prevElem]; | ||
| } | ||
|
|
||
| const int& RingBuffer::Back() const { | ||
| return buf[prevElem]; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лишние пустые строки |
||
| void RingBuffer::Resize(size_t newCapacity) { | ||
| newCapacity = newCapacity ? newCapacity : 1; | ||
| if (newCapacity == buf.capacity()) return; | ||
|
|
||
| size_t newCount = countElem < newCapacity ? countElem : newCapacity; | ||
| std::vector<int> newBuffer = {}; | ||
| newBuffer.reserve(newCapacity); | ||
| newBuffer.resize(newCapacity); | ||
|
|
||
| for (size_t i = 0; i < newCount; ++i) { | ||
| newBuffer[i] = (*this)[countElem - newCount + i]; | ||
| } | ||
|
|
||
| buf = std::move(newBuffer); | ||
| prevElem = 0; | ||
| nextElem = newCount % newCapacity; | ||
| countElem = newCount; | ||
| } | ||
|
|
||
| std::vector<int> RingBuffer::Vector() const { | ||
| std::vector<int> result; | ||
| result.reserve(countElem); | ||
| for (size_t i = 0; i < countElem; ++i) result.push_back((*this)[i]); | ||
| return result; | ||
| } | ||
|
|
||
| bool RingBuffer::operator==(const RingBuffer& other) const { | ||
| if (countElem != other.countElem || buf.capacity() != other.buf.capacity()) | ||
| return false; | ||
| for (size_t i = 0; i < countElem; ++i) | ||
| if ((*this)[i] != other[i]) return false; | ||
| return true; | ||
| } | ||
|
|
||
| bool RingBuffer::operator!=(const RingBuffer& other) const { | ||
| return !(*this == other); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,5 +2,71 @@ | |
|
|
||
|
|
||
| class Stack { | ||
| public: | ||
| // методы: | ||
|
|
||
| void Push(int newData); // Добавление нэлемента в стек | ||
|
|
||
| bool Pop(void); // удаление элемента с верхушки стека | ||
|
|
||
| int& Top(); // Доступ к элементу на вершине стека | ||
|
|
||
| const int& Top() const; // Доступ к элементу на вершине стека | ||
|
|
||
| bool Empty() const; // Проверка отсутсвия элементов на вершине | ||
|
|
||
| size_t Size() const; // Размер стека | ||
|
|
||
| void Clear(); // Очистка стека | ||
|
|
||
| void Swap(Stack& other); // меняем местами элементы | ||
|
|
||
| // определение для == | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лишний комментарий |
||
| bool operator==(const Stack& x) const {return data == x.data;} | ||
|
|
||
| // определение для != | ||
| bool operator!=(const Stack& x) const {return !(data == x.data);} | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. обычно методы группирует без лишних пустых строк, например операторы сравнения можно объявить без пустой строки, также для методов доступа, например, методов изменения |
||
| private: | ||
| std::vector<int> data; // данные | ||
|
|
||
| }; | ||
|
|
||
|
|
||
|
|
||
| // в Методах для Stack используем методы для vector | ||
|
|
||
| void Stack::Push(int newData){ | ||
| data.push_back(newData); | ||
| } | ||
|
|
||
| bool Stack::Pop(void){ | ||
| if(data.empty()) return false; | ||
| data.pop_back(); | ||
| return true; | ||
| } | ||
|
|
||
| int& Stack::Top(){ | ||
| return data.back(); | ||
| } | ||
|
|
||
| const int& Stack::Top() const{ | ||
| return data.back(); | ||
| } | ||
|
|
||
| bool Stack::Empty() const{ | ||
| return data.empty(); | ||
| } | ||
|
|
||
| size_t Stack::Size() const{ | ||
| return data.size(); | ||
| } | ||
|
|
||
| void Stack::Clear() { | ||
| data.clear(); | ||
| } | ||
|
|
||
| void Stack::Swap(Stack& other){ | ||
| data.swap(other.data); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
емкОсти, вместимости