/
savushkin
/
CubeTaskManager
Обзор
Документация
Войти
/
savushkin
/
CubeTaskManager
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
cube_tm/cube_tm_1.cpp
262 строки
7 KB
Alexander Savushkin
feat: cube_tm_2 - 2025-10-23 17:58:06
23 окт 2025, 17:58
23 окт 2025, 17:58
02548f3
Код
Авторство
О чём код?
#include <iostream> #include <memory> template <typename T> class ThreeDNode { public: T data; int x, y, z; // координаты в кубе // Ссылки на соседние узлы в трех направлениях std::shared_ptr<ThreeDNode<T>> next_x; // по оси X std::shared_ptr<ThreeDNode<T>> next_y; // по оси Y std::shared_ptr<ThreeDNode<T>> next_z; // по оси Z ThreeDNode(T value, int x_coord, int y_coord, int z_coord) : data(value), x(x_coord), y(y_coord), z(z_coord), next_x(nullptr), next_y(nullptr), next_z(nullptr) {} }; template <typename T> class ThreeDList { private: std::shared_ptr<ThreeDNode<T>> origin; // начальный узел (0,0,0) int size_x, size_y, size_z; // размеры куба public: ThreeDList(int x_size, int y_size, int z_size) : size_x(x_size), size_y(y_size), size_z(z_size), origin(nullptr) { initializeCube(); } private: // Инициализация кубической структуры void initializeCube() { if (size_x <= 0 || size_y <= 0 || size_z <= 0) return; // Создаем все узлы и организуем связи auto ***nodes = new std::shared_ptr<ThreeDNode<T>> **[size_x]; // Создание всех узлов for (int x = 0; x < size_x; x++) { nodes[x] = new std::shared_ptr<ThreeDNode<T>> *[size_y]; for (int y = 0; y < size_y; y++) { nodes[x][y] = new std::shared_ptr<ThreeDNode<T>>[size_z]; for (int z = 0; z < size_z; z++) { nodes[x][y][z] = std::make_shared<ThreeDNode<T>>( T(), x, y, z); } } } // Установка связей между узлами for (int x = 0; x < size_x; x++) { for (int y = 0; y < size_y; y++) { for (int z = 0; z < size_z; z++) { // Связь по X (если есть следующий узел справа) if (x < size_x - 1) { nodes[x][y][z]->next_x = nodes[x + 1][y][z]; } // Связь по Y (если есть следующий узел вглубь) if (y < size_y - 1) { nodes[x][y][z]->next_y = nodes[x][y + 1][z]; } // Связь по Z (если есть следующий узел выше) if (z < size_z - 1) { nodes[x][y][z]->next_z = nodes[x][y][z + 1]; } } } } origin = nodes[0][0][0]; // Очистка временного массива for (int x = 0; x < size_x; x++) { for (int y = 0; y < size_y; y++) { delete[] nodes[x][y]; } delete[] nodes[x]; } delete[] nodes; } public: // Установка значения в конкретной позиции void setValue(const T &value, int x, int y, int z) { if (!isValidPosition(x, y, z)) { std::cout << "Неверная позиция!" << std::endl; return; } auto node = getNodeAt(x, y, z); if (node) { node->data = value; } } // Получение значения из конкретной позиции T getValue(int x, int y, int z) { if (!isValidPosition(x, y, z)) { std::cout << "Неверная позиция!" << std::endl; return T(); } auto node = getNodeAt(x, y, z); return node ? node->data : T(); } // Получение узла по координатам std::shared_ptr<ThreeDNode<T>> getNodeAt(int x, int y, int z) { if (!origin || !isValidPosition(x, y, z)) return nullptr; auto current = origin; // Двигаемся по оси X for (int i = 0; i < x && current; i++) { current = current->next_x; } if (!current) return nullptr; // Сохраняем начало строки по Y auto row_start = current; // Двигаемся по оси Y for (int i = 0; i < y && current; i++) { current = current->next_y; } if (!current) return nullptr; // Сохраняем начало столбца по Z auto column_start = current; // Двигаемся по оси Z for (int i = 0; i < z && current; i++) { current = current->next_z; } return current; } // Проверка валидности позиции bool isValidPosition(int x, int y, int z) const { return x >= 0 && x < size_x && y >= 0 && y < size_y && z >= 0 && z < size_z; } // Визуализация среза куба (по постоянному Z) void printSlice(int fixed_z) { if (fixed_z < 0 || fixed_z >= size_z) { std::cout << "Неверный срез Z!" << std::endl; return; } std::cout << "Срез Z = " << fixed_z << ":" << std::endl; for (int y = 0; y < size_y; y++) { for (int x = 0; x < size_x; x++) { auto node = getNodeAt(x, y, fixed_z); if (node) { std::cout << node->data << "\t"; } else { std::cout << "X\t"; } } std::cout << std::endl; } std::cout << std::endl; } // Получение размеров void getDimensions(int &x, int &y, int &z) const { x = size_x; y = size_y; z = size_z; } }; // Пример использования int main() { // Создаем куб 3x3x3 ThreeDList<int> cube(3, 3, 3); // Заполняем данными int counter = 1; for (int z = 0; z < 3; z++) { for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) { cube.setValue(counter++, x, y, z); } } } // Выводим срезы cube.printSlice(0); // Нижний слой cube.printSlice(1); // Средний слой cube.printSlice(2); // Верхний слой // Демонстрация навигации auto node = cube.getNodeAt(1, 1, 1); if (node) { std::cout << "Центральный узел: " << node->data << std::endl; if (node->next_x) { std::cout << "Справа: " << node->next_x->data << std::endl; } if (node->next_y) { std::cout << "Вглубь: " << node->next_y->data << std::endl; } if (node->next_z) { std::cout << "Выше: " << node->next_z->data << std::endl; } } return 0; }