Update TaskGoalListManager::GetListByID to std algos

This commit is contained in:
Michael Cook (mackal) 2018-06-11 22:25:44 -04:00
parent e3dfb2f19d
commit 6399710c1d

View File

@ -3140,41 +3140,39 @@ bool TaskGoalListManager::LoadLists()
listIndex++; listIndex++;
} }
for(int listIndex = 0; listIndex < NumberOfLists; listIndex++) { for (int listIndex = 0; listIndex < NumberOfLists; listIndex++) {
int listID = TaskGoalLists[listIndex].ListID; int listID = TaskGoalLists[listIndex].ListID;
unsigned int size = TaskGoalLists[listIndex].Size; unsigned int size = TaskGoalLists[listIndex].Size;
query = StringFormat("SELECT `entry` from `goallists` " query = StringFormat("SELECT `entry` from `goallists` "
"WHERE `listid` = %i " "WHERE `listid` = %i "
"ORDER BY `entry` ASC LIMIT %i", "ORDER BY `entry` ASC LIMIT %i",
listID, size); listID, size);
results = database.QueryDatabase(query); results = database.QueryDatabase(query);
if (!results.Success()) { if (!results.Success()) {
TaskGoalLists[listIndex].Size = 0; TaskGoalLists[listIndex].Size = 0;
continue; continue;
} }
// This should only happen if a row is deleted in between us retrieving the counts // This should only happen if a row is deleted in between us retrieving the counts
// at the start of this method and getting to here. It should not be possible for // at the start of this method and getting to here. It should not be possible for
// an INSERT to cause a problem, as the SELECT is used with a LIMIT // an INSERT to cause a problem, as the SELECT is used with a LIMIT
if(results.RowCount() < size) if (results.RowCount() < size)
TaskGoalLists[listIndex].Size = results.RowCount(); TaskGoalLists[listIndex].Size = results.RowCount();
int entryIndex = 0; int entryIndex = 0;
for (auto row = results.begin(); row != results.end(); ++row, ++entryIndex) { for (auto row = results.begin(); row != results.end(); ++row, ++entryIndex) {
int entry = atoi(row[0]); int entry = atoi(row[0]);
if(entry < TaskGoalLists[listIndex].Min) if (entry < TaskGoalLists[listIndex].Min)
TaskGoalLists[listIndex].Min = entry; TaskGoalLists[listIndex].Min = entry;
if(entry > TaskGoalLists[listIndex].Max) if (entry > TaskGoalLists[listIndex].Max)
TaskGoalLists[listIndex].Max = entry; TaskGoalLists[listIndex].Max = entry;
TaskGoalLists[listIndex].GoalItemEntries[entryIndex] = entry;
}
TaskGoalLists[listIndex].GoalItemEntries[entryIndex] = entry;
}
} }
return true; return true;
@ -3184,24 +3182,13 @@ bool TaskGoalListManager::LoadLists()
int TaskGoalListManager::GetListByID(int ListID) { int TaskGoalListManager::GetListByID(int ListID) {
// Find the list with the specified ListID and return the index // Find the list with the specified ListID and return the index
auto it = std::find_if(TaskGoalLists.begin(), TaskGoalLists.end(),
[ListID](const TaskGoalList_Struct &t) { return t.ListID == ListID; });
int FirstEntry = 0; if (it == TaskGoalLists.end())
int LastEntry = NumberOfLists - 1; return -1;
while(FirstEntry <= LastEntry) {
int MiddleEntry = (FirstEntry + LastEntry) / 2;
if(ListID > TaskGoalLists[MiddleEntry].ListID)
FirstEntry = MiddleEntry + 1;
else if(ListID < TaskGoalLists[MiddleEntry].ListID)
LastEntry = MiddleEntry - 1;
else
return MiddleEntry;
}
return -1;
return std::distance(TaskGoalLists.begin(), it);
} }
int TaskGoalListManager::GetFirstEntry(int ListID) { int TaskGoalListManager::GetFirstEntry(int ListID) {