From 43cd2bd879d529fcbd0a0f64eccd4ce1eb872ab4 Mon Sep 17 00:00:00 2001 From: rtk0c Date: Mon, 21 Jun 2021 18:30:38 -0700 Subject: Implement array group conversion logic --- core/src/Model/Template/TableTemplate.cpp | 65 +++++++++++++++++++++++++++---- core/src/Model/Template/TableTemplate.hpp | 3 +- 2 files changed, 59 insertions(+), 9 deletions(-) (limited to 'core/src/Model/Template') diff --git a/core/src/Model/Template/TableTemplate.cpp b/core/src/Model/Template/TableTemplate.cpp index 88980ae..e01af02 100644 --- a/core/src/Model/Template/TableTemplate.cpp +++ b/core/src/Model/Template/TableTemplate.cpp @@ -1,6 +1,7 @@ #include "TableTemplate.hpp" #include +#include #include #include @@ -153,18 +154,50 @@ void TableTemplate::SetCellType(Vec2i pos, TableCell::CellType type) return; } - switch (type) { - case TableCell::ConstantCell: { - // TODO + switch (cell.Type) { + // Nothing to change + case TableCell::ConstantCell: break; + + case TableCell::SingularParametricCell: + mName2Parameters.erase(cell.Content); + break; + + case TableCell::ArrayParametricCell: { + auto& ag = mArrayGroups[cell.DataId]; + if (cell.Location.x == ag.LeftCell) { + + } else if (cell.Location.x == ag.RightCell) { + + } else { + } } break; + } + + switch (type) { + // Nothing to do + case TableCell::ConstantCell: break; case TableCell::SingularParametricCell: { - cell.Type = type; + int idx = pos.y * GetTableWidth() + pos.x; + auto [DISCARD, inserted] = mName2Parameters.emplace(cell.Content, idx); + + // Duplicate name + if (!inserted) { + return; + } } break; - // Setting the cell to be a part of a group requires calling the overload that supplies the group - case TableCell::ArrayParametricCell: break; + case TableCell::ArrayParametricCell: { + auto ptr = AddArrayGroup(pos.y, pos.x, pos.x); + + // Duplicate name + if (ptr == nullptr) { + return; + } + } break; } + + cell.Type = type; } int TableTemplate::GetArrayGroupCount() const @@ -182,7 +215,18 @@ TableCellArrayGroup& TableTemplate::GetArrayGroup(int id) return mArrayGroups[id]; } -TableCellArrayGroup& TableTemplate::AddArrayGroup(int row, int left, int right) +TableCellArrayGroup* TableTemplate::AddArrayGroup(int row, int left, int right) +{ + // size_t max value: 18446744073709551615 + // ^~~~~~~~~~~~~~~~~~~~ 20 chars + char name[20]; + auto res = std::to_chars(std::begin(name), std::end(name), mArrayGroups.size()); + std::string_view nameStr(name, res.ptr - name); + + return AddArrayGroup(nameStr, row, left, right); +} + +TableCellArrayGroup* TableTemplate::AddArrayGroup(std::string_view name, int row, int left, int right) { assert(row >= 0 && row < GetTableHeight()); assert(left >= 0 && left < GetTableWidth()); @@ -194,12 +238,17 @@ TableCellArrayGroup& TableTemplate::AddArrayGroup(int row, int left, int right) std::swap(left, right); } + auto [DISCARD, inserted] = mName2ArrayGroups.emplace(name, (int)mArrayGroups.size()); + if (!inserted) { + return nullptr; + } + mArrayGroups.push_back(TableCellArrayGroup{ .Row = row, .LeftCell = left, .RightCell = right, }); - return mArrayGroups.back(); + return &mArrayGroups.back(); } bool TableTemplate::ExtendArrayGroupLeft(int id, int n) diff --git a/core/src/Model/Template/TableTemplate.hpp b/core/src/Model/Template/TableTemplate.hpp index 754ebe1..7f331fa 100644 --- a/core/src/Model/Template/TableTemplate.hpp +++ b/core/src/Model/Template/TableTemplate.hpp @@ -167,7 +167,8 @@ public: int GetArrayGroupCount() const; const TableCellArrayGroup& GetArrayGroup(int id) const; TableCellArrayGroup& GetArrayGroup(int id); - TableCellArrayGroup& AddArrayGroup(int row, int left, int right); + TableCellArrayGroup* AddArrayGroup(int row, int left, int right); + TableCellArrayGroup* AddArrayGroup(std::string_view name, int row, int left, int right); bool ExtendArrayGroupLeft(int id, int n); bool ExtendArrayGroupRight(int id, int n); -- cgit v1.2.3-70-g09d2