diff --git a/src/blackmisc/identifierlist.cpp b/src/blackmisc/identifierlist.cpp index 0f99ef7ad..bc21f0ef8 100644 --- a/src/blackmisc/identifierlist.cpp +++ b/src/blackmisc/identifierlist.cpp @@ -23,6 +23,29 @@ namespace BlackMisc bool CIdentifierList::containsAnyNotIn(const CIdentifierList &other) const { - return containsBy([&other](const CIdentifier &id) { return ! other.contains(id); }); + return containsBy([&other](const CIdentifier & id) { return ! other.contains(id); }); + } + + QStringList CIdentifierList::getMachineNames(bool unique, bool sort) const + { + QStringList codes = this->transform(Predicates::MemberTransform(&CIdentifier::getMachineName)); + if (sort) { codes.sort(); } + if (unique) { codes.removeDuplicates(); } + return codes; + } + + int CIdentifierList::removeDuplicates() + { + if (this->size() < 2) { return 0; } + CIdentifierList il; + for (const CIdentifier &identifier : *this) + { + if (il.contains(identifier)) continue; + il.push_back(identifier); + } + const int delta = this->size() - il.size(); + if (delta == 0) { return 0; } + *this = il; + return delta; } } // namespace diff --git a/src/blackmisc/identifierlist.h b/src/blackmisc/identifierlist.h index f3c9a3955..b04c270f8 100644 --- a/src/blackmisc/identifierlist.h +++ b/src/blackmisc/identifierlist.h @@ -42,6 +42,12 @@ namespace BlackMisc //! This list contains an identifier which is not contained in other. bool containsAnyNotIn(const CIdentifierList &other) const; + + //! Get machine names + QStringList getMachineNames(bool unique = true, bool sort = true) const; + + //! Remove duplicates + int removeDuplicates(); }; } //namespace