refs #568, find max key value

(useful when key is integer to find latest values)
This commit is contained in:
Klaus Basan
2016-01-15 02:15:46 +01:00
parent 6802880e52
commit 4c92ab0444
2 changed files with 37 additions and 0 deletions

View File

@@ -30,6 +30,23 @@ namespace BlackMisc
return this->container().findFirstByOrDefault(&OBJ::getDbKey, key, notFound);
}
template <class OBJ, class CONTAINER, typename KEYTYPE>
OBJ IDatastoreObjectList<OBJ, CONTAINER, KEYTYPE>::maxKeyObject() const
{
if (this->container().isEmpty()) { return OBJ(); }
const OBJ max = *std::max_element(this->container().begin(), this->container().end(), [](const OBJ & obj1, const OBJ & obj2)
{
bool v1 = obj1.hasValidDbKey();
bool v2 = obj2.hasValidDbKey();
if (v1 && v2)
{
return obj1.getDbKey() < obj2.getDbKey();
}
return v2;
});
return max;
}
template <class OBJ, class CONTAINER, typename KEYTYPE>
void IDatastoreObjectList<OBJ, CONTAINER, KEYTYPE>::sortByKey()
{
@@ -48,6 +65,20 @@ namespace BlackMisc
return keys;
}
template <class OBJ, class CONTAINER, typename KEYTYPE>
KEYTYPE IDatastoreObjectList<OBJ, CONTAINER, KEYTYPE>::getMaxKey(bool *ok) const
{
QList<KEYTYPE> keys(this->toDbKeyList());
if (keys.isEmpty())
{
if (ok) { *ok = false; }
return KEYTYPE();
}
KEYTYPE max = *std::max_element(keys.begin(), keys.end());
if (ok) { *ok = true; }
return max;
}
template <class OBJ, class CONTAINER, typename KEYTYPE>
int IDatastoreObjectList<OBJ, CONTAINER, KEYTYPE>::removeObjectsWithKeys(const QList<KEYTYPE> &keys)
{