diff --git a/01_week/CMakeLists.txt b/01_week/CMakeLists.txt index 5ecaacbc..166189d5 100644 --- a/01_week/CMakeLists.txt +++ b/01_week/CMakeLists.txt @@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # Гарантирует использов set(EXAMPLES_DIR examples) # Определим переменную с именем директории set(TASKS_DIR tasks) -add_subdirectory(tasks) +#add_subdirectory(tasks) # Создать исполняемый файл для каждого примера if (BUILD_EXAMPLES_01_WEEK) @@ -32,4 +32,4 @@ if (BUILD_EXAMPLES_01_WEEK) add_example(functions_recursive ${EXAMPLES_DIR}/functions_recursive.cpp) add_example(io_streams ${EXAMPLES_DIR}/io_streams.cpp) add_example(main_with_args ${EXAMPLES_DIR}/main_with_args.cpp) -endif() \ No newline at end of file +endif() diff --git a/02_week/CMakeLists.txt b/02_week/CMakeLists.txt index 625e6fbe..856d2248 100644 --- a/02_week/CMakeLists.txt +++ b/02_week/CMakeLists.txt @@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # Гарантирует использов set(EXAMPLES_DIR examples) # Определим переменную с именем директории set(TASKS_DIR tasks) -add_subdirectory(tasks) +#add_subdirectory(tasks) # Создать исполняемый файл для каждого примера if (BUILD_EXAMPLES_02_WEEK) @@ -17,4 +17,4 @@ if (BUILD_EXAMPLES_02_WEEK) add_example(ptr_access ${EXAMPLES_DIR}/ptr_access.cpp) add_example(ptr_arithmetic ${EXAMPLES_DIR}/ptr_arithmetic.cpp) add_example(reinterpret ${EXAMPLES_DIR}/reinterpret.cpp) -endif() \ No newline at end of file +endif() diff --git a/03_week/CMakeLists.txt b/03_week/CMakeLists.txt index e5df5bac..fb663b00 100644 --- a/03_week/CMakeLists.txt +++ b/03_week/CMakeLists.txt @@ -7,11 +7,11 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # Гарантирует использов set(EXAMPLES_DIR examples) # Определим переменную с именем директории set(TASKS_DIR tasks) -add_subdirectory(tasks) + #add_subdirectory(tasks) # Создать исполняемый файл для каждого примера if (BUILD_EXAMPLES_03_WEEK) add_example(struct_examples ${EXAMPLES_DIR}/struct_examples.cpp) add_example(union_examples ${EXAMPLES_DIR}/union_examples.cpp) add_example(vector_examples ${EXAMPLES_DIR}/vector_examples.cpp) -endif() \ No newline at end of file +endif() diff --git a/04_week/tasks/phasor/phasor.cpp b/04_week/tasks/phasor/phasor.cpp index 3ec1b9ad..fa99de69 100644 --- a/04_week/tasks/phasor/phasor.cpp +++ b/04_week/tasks/phasor/phasor.cpp @@ -6,5 +6,5 @@ struct AlgTag {}; class Phasor { - + }; diff --git a/04_week/tasks/ring_buffer/ring_buffer.cpp b/04_week/tasks/ring_buffer/ring_buffer.cpp index e2b57ba2..ae43a220 100644 --- a/04_week/tasks/ring_buffer/ring_buffer.cpp +++ b/04_week/tasks/ring_buffer/ring_buffer.cpp @@ -1,6 +1,194 @@ #include + class RingBuffer { +public: + + // ---- Конструкторы ----- + explicit RingBuffer(size_t capacity); // Конструктор от емкости + RingBuffer(size_t capacity, int initialValue); // Конструткор от емкасти и начального значения + RingBuffer(std::initializer_list 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();} + + void Clear() {prevElem = nextElem = countElem = 0;} + void Resize(size_t newCapacity); + + std::vector 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 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; + 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; +} + +RingBuffer::RingBuffer(size_t capacity, int initialValue) { + + 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); + +} + +RingBuffer::RingBuffer(std::initializer_list 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); +} + +void RingBuffer::Push(int value) { + if (Full()) { + buf[nextElem] = value; + prevElem = NextIndex(prevElem); + nextElem = NextIndex(nextElem); + } else { + buf[nextElem] = value; + nextElem = NextIndex(nextElem); + ++countElem; + } +} + +bool RingBuffer::TryPush(int value) { + 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]; +} + + + + + + +void RingBuffer::Resize(size_t newCapacity) { + newCapacity = newCapacity ? newCapacity : 1; + if (newCapacity == buf.capacity()) return; + + size_t newCount = countElem < newCapacity ? countElem : newCapacity; + std::vector 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 RingBuffer::Vector() const { + std::vector 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); +} diff --git a/04_week/tasks/stack/stack.cpp b/04_week/tasks/stack/stack.cpp index 222e4ffc..04841d83 100644 --- a/04_week/tasks/stack/stack.cpp +++ b/04_week/tasks/stack/stack.cpp @@ -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); // меняем местами элементы + + // определение для == + bool operator==(const Stack& x) const {return data == x.data;} + + // определение для != + bool operator!=(const Stack& x) const {return !(data == x.data);} + + private: + std::vector 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); +} +