mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-26 02:35:38 +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);
|
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>
|
template <class OBJ, class CONTAINER, typename KEYTYPE>
|
||||||
void IDatastoreObjectList<OBJ, CONTAINER, KEYTYPE>::sortByKey()
|
void IDatastoreObjectList<OBJ, CONTAINER, KEYTYPE>::sortByKey()
|
||||||
{
|
{
|
||||||
@@ -48,6 +65,20 @@ namespace BlackMisc
|
|||||||
return keys;
|
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>
|
template <class OBJ, class CONTAINER, typename KEYTYPE>
|
||||||
int IDatastoreObjectList<OBJ, CONTAINER, KEYTYPE>::removeObjectsWithKeys(const QList<KEYTYPE> &keys)
|
int IDatastoreObjectList<OBJ, CONTAINER, KEYTYPE>::removeObjectsWithKeys(const QList<KEYTYPE> &keys)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -28,12 +28,18 @@ namespace BlackMisc
|
|||||||
//! Object with key, notFound otherwise
|
//! Object with key, notFound otherwise
|
||||||
OBJ findByKey(KEYTYPE key, const OBJ ¬Found = OBJ()) const;
|
OBJ findByKey(KEYTYPE key, const OBJ ¬Found = OBJ()) const;
|
||||||
|
|
||||||
|
//! Object with max.key
|
||||||
|
OBJ maxKeyObject() const;
|
||||||
|
|
||||||
//! Sort by timestamp
|
//! Sort by timestamp
|
||||||
void sortByKey();
|
void sortByKey();
|
||||||
|
|
||||||
//! All keys as list
|
//! All keys as list
|
||||||
QList<KEYTYPE> toDbKeyList() const;
|
QList<KEYTYPE> toDbKeyList() const;
|
||||||
|
|
||||||
|
//! Max.key value (making sense with integer key)
|
||||||
|
KEYTYPE getMaxKey(bool *ok = nullptr) const;
|
||||||
|
|
||||||
//! Remove objects with key
|
//! Remove objects with key
|
||||||
int removeObjectsWithKeys(const QList<KEYTYPE> &keys);
|
int removeObjectsWithKeys(const QList<KEYTYPE> &keys);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user