/
arseny213
/
PatternRecognitionMethods
Обзор
Документация
Войти
/
arseny213
/
PatternRecognitionMethods
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
cpp_classes/container.h
137 строк
4 KB
wannabeanything
renamed_classes
30 сен 2025, 21:50
30 сен 2025, 21:50
f67bb51
Код
Авторство
О чём код?
#include "image.h" #include <stdexcept> #include <set> #include <iterator> #include <fstream> #include <vector> // Этот класс отвечает за работу с выделенной памятью // в классе Image template <typename T> class Container { public: Container() = default; explicit Container(size_t capacity) : buf_for_img_(capacity) {} // Добавляет элемент в первую неинициализированную ячейку void Add(T val) { const size_t cap = buf_for_img_.GetCapacity(); if (size_ == cap) { throw std::out_of_range("Container is full"); } size_t pos = 0; for (; pos < cap; ++pos) { if (!IsInitialized(pos)) break; } init_idx_.insert(pos); *(buf_for_img_.GetAddress() + pos) = val; ++size_; } // Удалить последний элемент void PopBack() { if (size_ == 0) { throw std::out_of_range("Container is empty"); } const size_t idx = *init_idx_.rbegin(); init_idx_.erase(idx); *(buf_for_img_.GetAddress() + idx) = T{}; --size_; } // Добавить элемент по индексу // Индекс должен быть в интервале от 0 до n-1 void AddByIndex(size_t ind, T val) { const size_t cap = buf_for_img_.GetCapacity(); if (ind >= cap) { throw std::out_of_range("Index out of range"); } const bool was_init = IsInitialized(ind); if (!was_init && size_ == cap) { throw std::out_of_range("Container is full"); } *(buf_for_img_.GetAddress() + ind) = val; if (!was_init) { init_idx_.insert(ind); ++size_; } } // Удалить элемент по индексу // Индекс должен быть в интервале от 0 до n-1 void PopByIndex(size_t ind) { const size_t cap = buf_for_img_.GetCapacity(); if (ind >= cap) { throw std::out_of_range("Index out of range"); } if (!IsInitialized(ind)) { throw std::out_of_range("Index is not initialized"); } init_idx_.erase(ind); *(buf_for_img_.GetAddress() + ind) = T{}; --size_; } const std::vector<T> GetInitializedValues() const { std::vector<T> result; result.reserve(size_); for (auto it = init_idx_.begin(); it != init_idx_.end(); it++) { result.push_back(*(buf_for_img_.GetAddress() + *it)); } return result; } // Получить кусок выделенной памяти //(с инициализированными и нет значениями) const T *GetWholeChunk() const { return buf_for_img_.GetAddress(); } const size_t GetCapacity() const { return buf_for_img_.GetCapacity(); } void WriteWholeContent(const std::string &path) const { std::ofstream fout(path, std::ios::out | std::ios::trunc); const size_t cap = GetCapacity(); for (size_t i = 0; i < cap; i++) { if (IsInitialized(i)) { fout << i << ": " << *(buf_for_img_.GetAddress() + i) << '\n'; } else { fout << i << ": NULL\n"; } } } void WriteInitializedContent(const std::string &path) const { std::ofstream fout(path, std::ios::out | std::ios::trunc); for (int i : init_idx_) { fout << i << ": " << *(buf_for_img_.GetAddress() + i) << '\n'; } } private: BufferForImage<T> buf_for_img_; size_t size_ = 0; std::set<int> init_idx_; bool IsInitialized(size_t i)const { return init_idx_.find(i) != init_idx_.end(); } };