mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-06 18:25:37 +08:00
refs #568, find max key value
(useful when key is integer to find latest values)
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user