Skip to content

Commit

Permalink
pre-allocate unordered map and make getter const-correct
Browse files Browse the repository at this point in the history
  • Loading branch information
jadebenn committed Apr 24, 2024
1 parent e8a0eaa commit 52adaeb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
#include "CDTamingBuildPuzzleTable.h"

void CDTamingBuildPuzzleTable::LoadValuesFromDatabase() {
auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM TamingBuildPuzzles");
// First, get the size of the table
uint32_t size = 0;
auto tableSize = CDClientDatabase::ExecuteQuery("SELECT COUNT(*) FROM TamingBuildPuzzles");
while (!tableSize.eof()) {
size = tableSize.getIntField(0, 0);
tableSize.nextRow();
}
tableSize.finalize();

// Reserve the size
auto& entries = GetEntriesMutable();
entries.reserve(size);

// Now get the data
auto tableData = CDClientDatabase::ExecuteQuery("SELECT * FROM TamingBuildPuzzles");
while (!tableData.eof()) {
const auto lot = static_cast<LOT>(tableData.getIntField("NPCLot", LOT_NULL));
entries.emplace(lot, CDTamingBuildPuzzle{
Expand All @@ -17,7 +29,7 @@ void CDTamingBuildPuzzleTable::LoadValuesFromDatabase() {
}
}

const CDTamingBuildPuzzle* CDTamingBuildPuzzleTable::GetByLOT(const LOT lot) {
const CDTamingBuildPuzzle* CDTamingBuildPuzzleTable::GetByLOT(const LOT lot) const {
const auto& entries = GetEntries();
const auto itr = entries.find(lot);
return itr != entries.cend() ? &itr->second : nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ class CDTamingBuildPuzzleTable : public CDTable<CDTamingBuildPuzzleTable, std::u
* Gets the pet ability table corresponding to the pet LOT
* @returns A pointer to the corresponding table, or nullptr if one cannot be found
*/
const CDTamingBuildPuzzle* GetByLOT(const LOT lot);
[[nodiscard]]
const CDTamingBuildPuzzle* GetByLOT(const LOT lot) const;
};

0 comments on commit 52adaeb

Please sign in to comment.