Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 01_week/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
endif()
4 changes: 2 additions & 2 deletions 02_week/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
endif()
4 changes: 2 additions & 2 deletions 03_week/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
endif()
2 changes: 1 addition & 1 deletion 04_week/tasks/phasor/phasor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ struct AlgTag {};


class Phasor {

};
188 changes: 188 additions & 0 deletions 04_week/tasks/ring_buffer/ring_buffer.cpp
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();}

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;
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<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);
}

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<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);
}
66 changes: 66 additions & 0 deletions 04_week/tasks/stack/stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<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);
}