/
vladlevin790
/
cpp-lab-2
Обзор
Документация
Войти
/
vladlevin790
/
cpp-lab-2
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/app/StructureManager.hpp
363 строки
12 KB
vladlevin790
init
05 май 2026, 21:32
05 май 2026, 21:32
3310cbd
Код
Авторство
О чём код?
#ifndef StructureManager_hpp #define StructureManager_hpp #include <chrono> #include <sstream> #include <string> #include "OperationLog.hpp" #include "TypedStructureBucket.hpp" #include "ValueAdapters.hpp" class StructureManager { private: static constexpr int stateWindowLimit = 512; static constexpr int maxRequestedWindow = 1600; TypedStructureBucket<int, IntAdapter> intBucket; TypedStructureBucket<long long, LongAdapter> longBucket; TypedStructureBucket<double, DoubleAdapter> doubleBucket; TypedStructureBucket<char, CharAdapter> charBucket; TypedStructureBucket<std::string, StringAdapter> stringBucket; std::string selectedStructure = "Vector"; std::string selectedType = "int"; OperationLog lastLog; template <typename Action> std::string measureOperation(Action action) { const auto start = std::chrono::high_resolution_clock::now(); std::string result = action(); const auto finish = std::chrono::high_resolution_clock::now(); const long long elapsed = std::chrono::duration_cast<std::chrono::microseconds>( finish - start ).count(); lastLog.setElapsedMicroseconds(elapsed); return result; } template <typename Action> auto withCurrentBucket(Action action) -> decltype(action(intBucket)) { if (selectedType == "long") { return action(longBucket); } if (selectedType == "double") { return action(doubleBucket); } if (selectedType == "char") { return action(charBucket); } if (selectedType == "string") { return action(stringBucket); } return action(intBucket); } const char* selectedTypeLabel() const { if (selectedType == "long") { return LongAdapter::label(); } if (selectedType == "double") { return DoubleAdapter::label(); } if (selectedType == "char") { return CharAdapter::label(); } if (selectedType == "string") { return StringAdapter::label(); } return IntAdapter::label(); } bool supportsShift() const { return selectedType == "int" || selectedType == "long"; } bool supportsMean() const { return selectedType == "int" || selectedType == "long" || selectedType == "double"; } public: StructureManager() { intBucket.setStructure(selectedStructure); longBucket.setStructure(selectedStructure); doubleBucket.setStructure(selectedStructure); charBucket.setStructure(selectedStructure); stringBucket.setStructure(selectedStructure); lastLog.reset("init", "Приложение инициализировано."); } std::string setStructure(const std::string& structureName) { if ( structureName != "Array" && structureName != "Vector" && structureName != "Stack" && structureName != "Queue" ) { lastLog.reset("set-structure", "Неизвестная структура данных.", false); return getStateJson(); } selectedStructure = structureName; intBucket.setStructure(selectedStructure); longBucket.setStructure(selectedStructure); doubleBucket.setStructure(selectedStructure); charBucket.setStructure(selectedStructure); stringBucket.setStructure(selectedStructure); lastLog.reset("set-structure", "Выбрана структура: " + structureName); return getStateJson(); } std::string setDataType(const std::string& dataType) { if ( dataType != "int" && dataType != "long" && dataType != "double" && dataType != "char" && dataType != "string" ) { lastLog.reset("set-data-type", "Неизвестный тип данных.", false); return getStateJson(); } selectedType = dataType; withCurrentBucket([&](auto& bucket) { bucket.setStructure(selectedStructure); return 0; }); lastLog.reset("set-data-type", "Выбран тип данных: " + dataType); return getStateJson(); } std::string insertValue(const std::string& rawValue) { return measureOperation([&]() { return withCurrentBucket([&](auto& bucket) { std::string error; std::string insertedValue; int insertedIndex = 0; if (!bucket.insertRaw(rawValue, error, insertedIndex, insertedValue)) { lastLog.reset("insert", error, false); return getStateJson(); } lastLog.reset("insert", "Элемент добавлен в структуру."); lastLog.addStep(insertedIndex, insertedValue, insertedValue, "insert(" + insertedValue + ")"); return getStateJson(); }); }); } std::string removeLast() { return measureOperation([&]() { return withCurrentBucket([&](auto& bucket) { std::string error; std::string removedValue; int removedIndex = 0; if (!bucket.removeLast(error, removedIndex, removedValue)) { lastLog.reset("remove-last", error, false); return getStateJson(); } if (selectedStructure == "Queue") { lastLog.reset("remove-last", "Удалён первый элемент очереди (dequeue)."); } else if (selectedStructure == "Stack") { lastLog.reset("remove-last", "Удалён верхний элемент стека (pop)."); } else { lastLog.reset("remove-last", "Последний элемент удалён."); } lastLog.addStep(removedIndex, removedValue, "", "remove()"); return getStateJson(); }); }); } std::string replaceAt(int index, const std::string& rawValue) { return measureOperation([&]() { return withCurrentBucket([&](auto& bucket) { std::string error; std::string oldValue; std::string newValue; if (!bucket.replaceRaw(index, rawValue, error, oldValue, newValue)) { lastLog.reset("replace-at", error, false); return getStateJson(); } lastLog.reset("replace-at", "Элемент по индексу заменён."); lastLog.addStep(index, oldValue, newValue, oldValue + " -> " + newValue); return getStateJson(); }); }); } std::string clearCurrent() { return measureOperation([&]() { withCurrentBucket([&](auto& bucket) { bucket.clear(); return 0; }); lastLog.reset("clear", "Текущая структура очищена."); return getStateJson(); }); } std::string applyShiftLeftEvenPositions() { return measureOperation([&]() { withCurrentBucket([&](auto& bucket) { bucket.shiftLeftEvenPositions(lastLog); return 0; }); return getStateJson(); }); } std::string calculateMeanJson() { const auto start = std::chrono::high_resolution_clock::now(); const double result = withCurrentBucket([&](auto& bucket) { return bucket.calculateMean(lastLog); }); const auto finish = std::chrono::high_resolution_clock::now(); const long long elapsed = std::chrono::duration_cast<std::chrono::microseconds>( finish - start ).count(); lastLog.setElapsedMicroseconds(elapsed); std::ostringstream extra; extra << "\"result\":" << result; return lastLog.toJson(extra.str()); } std::string fillDemoData(int count) { return measureOperation([&]() { return withCurrentBucket([&](auto& bucket) { std::string error; if (!bucket.fillDemoData(count, error)) { lastLog.reset("fill-demo-data", error, false); return getStateJson(); } lastLog.reset("fill-demo-data", "Структура заполнена демонстрационными данными."); return getStateJson(); }); }); } std::string getAsciiPreviewJson() { const std::string itemsJson = withCurrentBucket([&](auto& bucket) { return bucket.asciiPreviewJson(); }); lastLog.reset("ascii-preview", "Создан ASCII-preview текущей структуры."); std::ostringstream json; json << "{"; json << "\"items\":" << itemsJson; json << "}"; return json.str(); } std::string getLastOperationJson() const { return lastLog.toJson(); } std::string getItemsWindowJson(int start, int count) const { if (start < 0) { start = 0; } if (count < 0) { count = 0; } if (count > maxRequestedWindow) { count = maxRequestedWindow; } const int size = const_cast<StructureManager*>(this)->withCurrentBucket([](auto& bucket) { return bucket.size(); }); if (start > size) { start = size; } const std::string items = const_cast<StructureManager*>(this)->withCurrentBucket([&](auto& bucket) { return bucket.stateItemsWindowJson(start, count); }); std::ostringstream json; json << "{"; json << "\"structure\":\"" << selectedStructure << "\","; json << "\"dataType\":\"" << selectedType << "\","; json << "\"size\":" << size << ","; json << "\"windowStart\":" << start << ","; json << "\"windowLimit\":" << count << ","; json << "\"items\":" << items; json << "}"; return json.str(); } std::string getStateJson() const { std::ostringstream json; const int size = const_cast<StructureManager*>(this)->withCurrentBucket([](auto& bucket) { return bucket.size(); }); const int capacity = const_cast<StructureManager*>(this)->withCurrentBucket([](auto& bucket) { return bucket.capacity(); }); const int windowLimit = size > stateWindowLimit ? stateWindowLimit : size; const std::string items = const_cast<StructureManager*>(this)->withCurrentBucket([&](auto& bucket) { return bucket.stateItemsWindowJson(0, windowLimit); }); json << "{"; json << "\"structure\":\"" << selectedStructure << "\","; json << "\"dataType\":\"" << selectedType << "\","; json << "\"dataTypeLabel\":\"" << selectedTypeLabel() << "\","; json << "\"supportsShift\":" << (supportsShift() ? "true" : "false") << ","; json << "\"supportsMean\":" << (supportsMean() ? "true" : "false") << ","; json << "\"size\":" << size << ","; json << "\"capacity\":" << capacity << ","; json << "\"windowStart\":0,"; json << "\"windowLimit\":" << windowLimit << ","; json << "\"virtualized\":" << (size > windowLimit ? "true" : "false") << ","; json << "\"items\":" << items; json << "}"; return json.str(); } }; #endif