From 2ab6e7f41e46c87ede7864a8dbdd671d8fe9a7c5 Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Sun, 5 Mar 2017 14:03:22 +0100 Subject: [PATCH] CIdentifierList utility function getMachinesUnique() returns a list of identifiers reduced to maximum one per machine. If there is more than one per machine, it is undefined which one will be returned. refs #881 --- src/blackmisc/identifierlist.cpp | 14 ++++++++++++++ src/blackmisc/identifierlist.h | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/src/blackmisc/identifierlist.cpp b/src/blackmisc/identifierlist.cpp index bc21f0ef8..b587ab230 100644 --- a/src/blackmisc/identifierlist.cpp +++ b/src/blackmisc/identifierlist.cpp @@ -26,6 +26,20 @@ namespace BlackMisc return containsBy([&other](const CIdentifier & id) { return ! other.contains(id); }); } + CIdentifierList CIdentifierList::getMachinesUnique() const + { + CIdentifierList il; + for (const CIdentifier &identifier : *this) + { + bool contained = il.containsBy([ = ] (const CIdentifier &ident) + { + return identifier.isFromSameMachine(ident); + }); + if (!contained) { il.push_back(identifier); } + } + return il; + } + QStringList CIdentifierList::getMachineNames(bool unique, bool sort) const { QStringList codes = this->transform(Predicates::MemberTransform(&CIdentifier::getMachineName)); diff --git a/src/blackmisc/identifierlist.h b/src/blackmisc/identifierlist.h index b04c270f8..6344cd07b 100644 --- a/src/blackmisc/identifierlist.h +++ b/src/blackmisc/identifierlist.h @@ -43,6 +43,10 @@ namespace BlackMisc //! This list contains an identifier which is not contained in other. bool containsAnyNotIn(const CIdentifierList &other) const; + //! Get a list of identifiers reduced to maximum one per machine. + //! If there is more than one per machine, it is undefined which one will be added. + CIdentifierList getMachinesUnique() const; + //! Get machine names QStringList getMachineNames(bool unique = true, bool sort = true) const;