From ee5ed32172637bf432382f483d453221809832f4 Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Fri, 24 May 2024 19:26:01 -0700 Subject: [PATCH] Array cleanup. --- common/lc_library.cpp | 71 ++++++++----------------------- common/lc_library.h | 8 ++-- common/lc_partselectionwidget.cpp | 14 +++--- qt/lc_qpreferencesdialog.cpp | 12 +++--- 4 files changed, 32 insertions(+), 73 deletions(-) diff --git a/common/lc_library.cpp b/common/lc_library.cpp index 3e3b67bd..cc6bcd7d 100644 --- a/common/lc_library.cpp +++ b/common/lc_library.cpp @@ -1723,16 +1723,16 @@ bool lcPiecesLibrary::PieceInCategory(PieceInfo* Info, const char* CategoryKeywo return lcMatchCategory(PieceName, CategoryKeywords); } -void lcPiecesLibrary::GetCategoryEntries(int CategoryIndex, bool GroupPieces, lcArray& SinglePieces, lcArray& GroupedPieces) +void lcPiecesLibrary::GetCategoryEntries(int CategoryIndex, bool GroupPieces, std::vector& SinglePieces, std::vector& GroupedPieces) { if (CategoryIndex >= 0 && CategoryIndex < static_cast(gCategories.size())) GetCategoryEntries(gCategories[CategoryIndex].Keywords.constData(), GroupPieces, SinglePieces, GroupedPieces); } -void lcPiecesLibrary::GetCategoryEntries(const char* CategoryKeywords, bool GroupPieces, lcArray& SinglePieces, lcArray& GroupedPieces) +void lcPiecesLibrary::GetCategoryEntries(const char* CategoryKeywords, bool GroupPieces, std::vector& SinglePieces, std::vector& GroupedPieces) { - SinglePieces.RemoveAll(); - GroupedPieces.RemoveAll(); + SinglePieces.clear(); + GroupedPieces.clear(); for (const auto& PieceIt : mPieces) { @@ -1743,7 +1743,7 @@ void lcPiecesLibrary::GetCategoryEntries(const char* CategoryKeywords, bool Grou if (!GroupPieces) { - SinglePieces.Add(Info); + SinglePieces.emplace_back(Info); continue; } @@ -1763,73 +1763,36 @@ void lcPiecesLibrary::GetCategoryEntries(const char* CategoryKeywords, bool Grou if (Parent) { // Check if the parent was added as a single piece. - int Index = SinglePieces.FindIndex(Parent); + auto ParentIt = std::find(SinglePieces.begin(), SinglePieces.end(), Parent); - if (Index != -1) - SinglePieces.RemoveIndex(Index); + if (ParentIt != SinglePieces.end()) + SinglePieces.erase(ParentIt); - Index = GroupedPieces.FindIndex(Parent); - - if (Index == -1) - GroupedPieces.Add(Parent); + if (std::find(GroupedPieces.begin(), GroupedPieces.end(), Parent) == GroupedPieces.end()) + GroupedPieces.emplace_back(Parent); } else { // Patterned pieces should have a parent but in case they don't just add them anyway. - SinglePieces.Add(Info); + SinglePieces.emplace_back(Info); } } else { // Check if this piece has already been added to this category by one of its children. - const int Index = GroupedPieces.FindIndex(Info); - - if (Index == -1) - SinglePieces.Add(Info); + if (std::find(GroupedPieces.begin(), GroupedPieces.end(), Info) == GroupedPieces.end()) + SinglePieces.emplace_back(Info); } } } -void lcPiecesLibrary::GetPatternedPieces(PieceInfo* Parent, lcArray& Pieces) const +void lcPiecesLibrary::GetParts(std::vector& Parts) const { - char Name[LC_PIECE_NAME_LEN]; - strcpy(Name, Parent->mFileName); - char* Ext = strchr(Name, '.'); - if (Ext) - *Ext = 0; - strcat(Name, "P"); - strupr(Name); - - Pieces.RemoveAll(); - - for (const auto& PieceIt : mPieces) - if (strncmp(Name, PieceIt.first.c_str(), strlen(Name)) == 0) - Pieces.Add(PieceIt.second); - - // Sometimes pieces with A and B versions don't follow the same convention (for example, 3040Pxx instead of 3040BPxx). - if (Pieces.GetSize() == 0) - { - strcpy(Name, Parent->mFileName); - Ext = strchr(Name, '.'); - if (Ext) - *Ext = 0; - size_t Len = strlen(Name); - if (Name[Len-1] < '0' || Name[Len-1] > '9') - Name[Len-1] = 'P'; - - for (const auto& PieceIt : mPieces) - if (strncmp(Name, PieceIt.first.c_str(), strlen(Name)) == 0) - Pieces.Add(PieceIt.second); - } -} - -void lcPiecesLibrary::GetParts(lcArray& Parts) const -{ - Parts.SetSize(0); - Parts.AllocGrow(mPieces.size()); + Parts.clear(); + Parts.reserve(mPieces.size()); for (const auto& PartIt : mPieces) - Parts.Add(PartIt.second); + Parts.emplace_back(PartIt.second); } std::vector lcPiecesLibrary::GetPartsFromSet(const std::vector& PartIds) const diff --git a/common/lc_library.h b/common/lc_library.h index 078bb2e7..b626fbc4 100644 --- a/common/lc_library.h +++ b/common/lc_library.h @@ -2,7 +2,6 @@ #include "lc_context.h" #include "lc_math.h" -#include "lc_array.h" #include "lc_meshloader.h" class PieceInfo; @@ -147,10 +146,9 @@ public: void ReleaseTexture(lcTexture* Texture); bool PieceInCategory(PieceInfo* Info, const char* CategoryKeywords) const; - void GetCategoryEntries(int CategoryIndex, bool GroupPieces, lcArray& SinglePieces, lcArray& GroupedPieces); - void GetCategoryEntries(const char* CategoryKeywords, bool GroupPieces, lcArray& SinglePieces, lcArray& GroupedPieces); - void GetPatternedPieces(PieceInfo* Parent, lcArray& Pieces) const; - void GetParts(lcArray& Parts) const; + void GetCategoryEntries(int CategoryIndex, bool GroupPieces, std::vector& SinglePieces, std::vector& GroupedPieces); + void GetCategoryEntries(const char* CategoryKeywords, bool GroupPieces, std::vector& SinglePieces, std::vector& GroupedPieces); + void GetParts(std::vector& Parts) const; std::vector GetPartsFromSet(const std::vector& PartIds) const; std::string GetPartId(const PieceInfo* Info) const; diff --git a/common/lc_partselectionwidget.cpp b/common/lc_partselectionwidget.cpp index 51570adc..e020b399 100644 --- a/common/lc_partselectionwidget.cpp +++ b/common/lc_partselectionwidget.cpp @@ -130,7 +130,7 @@ void lcPartSelectionListModel::SetCategory(int CategoryIndex) beginResetModel(); lcPiecesLibrary* Library = lcGetPiecesLibrary(); - lcArray SingleParts, GroupedParts; + std::vector SingleParts, GroupedParts; if (CategoryIndex != -1) Library->GetCategoryEntries(CategoryIndex, false, SingleParts, GroupedParts); @@ -140,14 +140,14 @@ void lcPartSelectionListModel::SetCategory(int CategoryIndex) lcModel* ActiveModel = gMainWindow->GetActiveModel(); - for (int PartIdx = 0; PartIdx < SingleParts.GetSize(); ) + for (size_t PartIndex = 0; PartIndex < SingleParts.size(); ) { - PieceInfo* Info = SingleParts[PartIdx]; + PieceInfo* Info = SingleParts[PartIndex]; if (!Info->IsModel() || !Info->GetModel()->IncludesModel(ActiveModel)) - PartIdx++; + PartIndex++; else - SingleParts.RemoveIndex(PartIdx); + SingleParts.erase(SingleParts.begin() + PartIndex); } } @@ -158,9 +158,9 @@ void lcPartSelectionListModel::SetCategory(int CategoryIndex) std::sort(SingleParts.begin(), SingleParts.end(), lcPartSortFunc); - mParts.resize(SingleParts.GetSize()); + mParts.resize(SingleParts.size()); - for (int PartIdx = 0; PartIdx < SingleParts.GetSize(); PartIdx++) + for (size_t PartIdx = 0; PartIdx < SingleParts.size(); PartIdx++) mParts[PartIdx] = std::pair(SingleParts[PartIdx], QPixmap()); endResetModel(); diff --git a/qt/lc_qpreferencesdialog.cpp b/qt/lc_qpreferencesdialog.cpp index be33cac2..0cd6f8a3 100644 --- a/qt/lc_qpreferencesdialog.cpp +++ b/qt/lc_qpreferencesdialog.cpp @@ -701,16 +701,14 @@ void lcQPreferencesDialog::updateParts() if (categoryIndex != -1) { - lcArray singleParts, groupedParts; + std::vector SingleParts, GroupedParts; - Library->GetCategoryEntries(mOptions->Categories[categoryIndex].Keywords.constData(), false, singleParts, groupedParts); + Library->GetCategoryEntries(mOptions->Categories[categoryIndex].Keywords.constData(), false, SingleParts, GroupedParts); - for (int partIndex = 0; partIndex < singleParts.GetSize(); partIndex++) + for (PieceInfo* Info : SingleParts) { - PieceInfo *info = singleParts[partIndex]; - - QStringList rowList(info->m_strDescription); - rowList.append(info->mFileName); + QStringList rowList(Info->m_strDescription); + rowList.append(Info->mFileName); new QTreeWidgetItem(tree, rowList); }