From 3a3c0533ebe4386b67b316bb859a60686f6b5f05 Mon Sep 17 00:00:00 2001
From: Olli Lupton <oliver.lupton@cern.ch>
Date: Fri, 10 Jan 2020 14:03:20 +0100
Subject: [PATCH] Prefer LHCb::span to std::vector const& as an argument type.

---
 Hlt/HLTScheduler/src/ControlFlowNode.cpp      | 18 +++++++--------
 Hlt/HLTScheduler/src/ControlFlowNode.h        | 23 +++++++++++--------
 .../src/ExecutionReportsWriter.cpp            |  2 +-
 Hlt/HLTScheduler/src/HLTControlFlowMgr.cpp    | 10 ++++----
 Hlt/HLTScheduler/src/HLTControlFlowMgr.h      |  7 +++---
 5 files changed, 32 insertions(+), 28 deletions(-)

diff --git a/Hlt/HLTScheduler/src/ControlFlowNode.cpp b/Hlt/HLTScheduler/src/ControlFlowNode.cpp
index ac7faca1a78..df948f682ed 100644
--- a/Hlt/HLTScheduler/src/ControlFlowNode.cpp
+++ b/Hlt/HLTScheduler/src/ControlFlowNode.cpp
@@ -24,8 +24,8 @@
 // more children. If an executed child returns TRUE, a counter is decremented,
 // to be sure to finish execution after every child is executed.
 template <>
-void CompositeNode<nodeType::LAZY_AND>::updateStateAndNotify( int                     senderNodeID,
-                                                              std::vector<NodeState>& NodeStates ) const {
+void CompositeNode<nodeType::LAZY_AND>::updateStateAndNotify( int                   senderNodeID,
+                                                              LHCb::span<NodeState> NodeStates ) const {
   if ( !NodeStates[senderNodeID].passed ) {
     NodeStates[m_NodeID].executionCtr = 0;
     NodeStates[m_NodeID].passed       = false;
@@ -40,8 +40,8 @@ void CompositeNode<nodeType::LAZY_AND>::updateStateAndNotify( int
 }
 
 template <>
-void CompositeNode<nodeType::LAZY_OR>::updateStateAndNotify( int                     senderNodeID,
-                                                             std::vector<NodeState>& NodeStates ) const {
+void CompositeNode<nodeType::LAZY_OR>::updateStateAndNotify( int                   senderNodeID,
+                                                             LHCb::span<NodeState> NodeStates ) const {
   if ( NodeStates[senderNodeID].passed ) {
     NodeStates[m_NodeID].executionCtr = 0;
     NodeStates[m_NodeID].passed       = true;
@@ -59,7 +59,7 @@ void CompositeNode<nodeType::LAZY_OR>::updateStateAndNotify( int
 // all children, sets executed state as soon as every child ran and if one child
 // passed, this passes as well
 template <>
-void CompositeNode<nodeType::NONLAZY_OR>::updateStateAndNotify( int, std::vector<NodeState>& NodeStates ) const {
+void CompositeNode<nodeType::NONLAZY_OR>::updateStateAndNotify( int, LHCb::span<NodeState> NodeStates ) const {
   NodeStates[m_NodeID].executionCtr--;
   if ( NodeStates[m_NodeID].executionCtr == 0 ) {
     NodeStates[m_NodeID].passed = std::any_of( begin( m_children ), end( m_children ), [&]( VNode const* vchild ) {
@@ -70,7 +70,7 @@ void CompositeNode<nodeType::NONLAZY_OR>::updateStateAndNotify( int, std::vector
 }
 
 template <>
-void CompositeNode<nodeType::NONLAZY_AND>::updateStateAndNotify( int, std::vector<NodeState>& NodeStates ) const {
+void CompositeNode<nodeType::NONLAZY_AND>::updateStateAndNotify( int, LHCb::span<NodeState> NodeStates ) const {
   NodeStates[m_NodeID].executionCtr--;
   if ( NodeStates[m_NodeID].executionCtr == 0 ) {
     NodeStates[m_NodeID].passed = std::all_of( begin( m_children ), end( m_children ), [&]( VNode const* vchild ) {
@@ -81,7 +81,7 @@ void CompositeNode<nodeType::NONLAZY_AND>::updateStateAndNotify( int, std::vecto
 }
 
 template <>
-void CompositeNode<nodeType::NOT>::updateStateAndNotify( int, std::vector<NodeState>& NodeStates ) const {
+void CompositeNode<nodeType::NOT>::updateStateAndNotify( int, LHCb::span<NodeState> NodeStates ) const {
   NodeStates[m_NodeID].executionCtr--;
   NodeStates[m_NodeID].passed =
       !std::visit( [&]( auto const& child ) { return NodeStates[child.m_NodeID].passed; }, *m_children.front() );
@@ -89,7 +89,7 @@ void CompositeNode<nodeType::NOT>::updateStateAndNotify( int, std::vector<NodeSt
 }
 
 // just the same as CompositeNode::requested()
-bool BasicNode::requested( std::vector<NodeState> const& NodeStates ) const {
+bool BasicNode::requested( LHCb::span<NodeState const> NodeStates ) const {
   return m_parents.empty() || std::any_of( begin( m_parents ), end( m_parents ), [&]( VNode const* Vparent ) {
            return std::visit( overload{[&]( auto const& parent ) { return parent.isActive( NodeStates ); },
                                        []( BasicNode const& ) { return false; }},
@@ -98,7 +98,7 @@ bool BasicNode::requested( std::vector<NodeState> const& NodeStates ) const {
 } // end of requested
 
 // just the same as CompositeNode::notifyParents
-void BasicNode::notifyParents( std::vector<NodeState>& NodeStates ) const {
+void BasicNode::notifyParents( LHCb::span<NodeState> NodeStates ) const {
   for ( VNode* Vparent : m_parents ) {
     std::visit( overload{[&]( auto& parent ) {
                            if ( NodeStates[parent.m_NodeID].executionCtr != 0 )
diff --git a/Hlt/HLTScheduler/src/ControlFlowNode.h b/Hlt/HLTScheduler/src/ControlFlowNode.h
index da9c12dabda..411470a7892 100644
--- a/Hlt/HLTScheduler/src/ControlFlowNode.h
+++ b/Hlt/HLTScheduler/src/ControlFlowNode.h
@@ -38,6 +38,8 @@
 #include "GaudiKernel/FunctionalFilterDecision.h"
 #include "GaudiKernel/MsgStream.h"
 
+#include "Kernel/STLExtensions.h"
+
 struct NodeState {
   uint16_t executionCtr;
   bool     passed;
@@ -59,14 +61,15 @@ struct AlgWrapper {
     assert( m_alg != nullptr );
   }
 
-  bool isExecuted( std::vector<AlgState> const& AlgoStates ) const { return AlgoStates[m_executedIndex].isExecuted; }
-  bool getFilterPassed( std::vector<AlgState> const& AlgoStates ) const {
+  bool isExecuted( LHCb::span<AlgState const> AlgoStates ) const { return AlgoStates[m_executedIndex].isExecuted; }
+
+  bool getFilterPassed( LHCb::span<AlgState const> AlgoStates ) const {
     return AlgoStates[m_executedIndex].filterPassed;
   }
 
   void setIndex( uint16_t i ) { m_executedIndex = i; }
 
-  void execute( EventContext& evtCtx, std::vector<AlgState>& AlgoStates ) const {
+  void execute( EventContext& evtCtx, LHCb::span<AlgState> AlgoStates ) const {
     m_alg->whiteboard()->selectStore( evtCtx.valid() ? evtCtx.slot() : 0 ).ignore();
 
     auto ret = m_callSysExecute ? m_alg->sysExecute( evtCtx ) : m_alg->execute( evtCtx );
@@ -121,7 +124,7 @@ public:
 
   BasicNode( std::string const& name, MsgStream& msg ) : m_name( name ), m_msg( msg ){};
 
-  void execute( std::vector<NodeState>& NodeStates, std::vector<AlgState>& AlgStates,
+  void execute( LHCb::span<NodeState> NodeStates, LHCb::span<AlgState> AlgStates,
                 std::vector<Gaudi::Accumulators::AveragingCounter<uint64_t>>& TimingCounters,
                 bool const createTimingTable, EventContext& evtCtx, IAlgExecStateSvc* aess,
                 SmartIF<IProperty>& appmgr ) const {
@@ -158,9 +161,9 @@ public:
 
   } // end of execute
 
-  void notifyParents( std::vector<NodeState>& NodeStates ) const;
+  void notifyParents( LHCb::span<NodeState> NodeStates ) const;
 
-  bool requested( std::vector<NodeState> const& NodeStates ) const;
+  bool requested( LHCb::span<NodeState const> NodeStates ) const;
 
 }; // end of BasicNode
 
@@ -186,7 +189,7 @@ public:
 
   // this calls this->updateStateAndNotify on all parents of the ControlFlowNode that
   // calls this->notifyParents() and recursively notifyParents again
-  void notifyParents( std::vector<NodeState>& NodeStates ) const {
+  void notifyParents( LHCb::span<NodeState> NodeStates ) const {
     for ( gsl::not_null<VNode*> Vparent : m_parents ) {
       std::visit( overload{[&]( auto& parent ) {
                              if ( NodeStates[parent.m_NodeID].executionCtr != 0 )
@@ -209,7 +212,7 @@ public:
   // and we continue to resolve the recursion, asking each composite
   // ControlFlowNode if it is active. If any of the parents of the basic
   // ControlFlowNode, it will be executed.
-  bool requested( std::vector<NodeState> const& NodeStates ) const {
+  bool requested( LHCb::span<NodeState const> NodeStates ) const {
     return m_parents.empty() || std::any_of( begin( m_parents ), end( m_parents ), [&]( VNode const* Vparent ) {
              return std::visit( overload{[&]( auto const& parent ) { return parent.isActive( NodeStates ); },
                                          []( BasicNode const& ) { return false; }},
@@ -217,7 +220,7 @@ public:
            } );
   } // end of requested
 
-  bool isActive( std::vector<NodeState> const& NodeStates ) const {
+  bool isActive( LHCb::span<NodeState const> NodeStates ) const {
     return NodeStates[m_NodeID].executionCtr != 0 && requested( NodeStates );
   }
 
@@ -239,7 +242,7 @@ public:
 
   // this should update the passed and executionCtr flags after each
   // loop
-  void updateStateAndNotify( int senderNodeID, std::vector<NodeState>& NodeStates ) const;
+  void updateStateAndNotify( int senderNodeID, LHCb::span<NodeState> NodeStates ) const;
 
   std::string getType() const { return nodeTypeNames.at( nType ); }
 
diff --git a/Hlt/HLTScheduler/src/ExecutionReportsWriter.cpp b/Hlt/HLTScheduler/src/ExecutionReportsWriter.cpp
index 2c89644beaf..d5110dc7053 100755
--- a/Hlt/HLTScheduler/src/ExecutionReportsWriter.cpp
+++ b/Hlt/HLTScheduler/src/ExecutionReportsWriter.cpp
@@ -95,7 +95,7 @@ LHCb::HltDecReports ExecutionReportsWriter::operator()( EventContext const& evtC
 
   if ( UNLIKELY( evtCtx.evt() % m_printFreq == 0 ) && msgLevel( MSG::DEBUG ) ) {
     debug() << m_schedulerPtr->buildAlgsWithStates( AlgStates ).str() << endmsg;
-    debug() << m_schedulerPtr->buildPrintableStateTree( NodeStates ).str() << endmsg;
+    debug() << m_schedulerPtr->buildPrintableStateTree( LHCb::make_span( NodeStates ) ).str() << endmsg;
   }
   LHCb::HltDecReports reports{};
   reports.reserve( m_name_indices.size() );
diff --git a/Hlt/HLTScheduler/src/HLTControlFlowMgr.cpp b/Hlt/HLTScheduler/src/HLTControlFlowMgr.cpp
index 55508211ad4..ddf102c1a3f 100644
--- a/Hlt/HLTScheduler/src/HLTControlFlowMgr.cpp
+++ b/Hlt/HLTScheduler/src/HLTControlFlowMgr.cpp
@@ -267,7 +267,7 @@ StatusCode HLTControlFlowMgr::finalize() {
   info() << endmsg;
 
   // print the counters
-  info() << buildPrintableStateTree( m_NodeStateCounters ).str() << endmsg;
+  info() << buildPrintableStateTree( LHCb::make_span( std::as_const( m_NodeStateCounters ) ) ).str() << endmsg;
 
   // Save Histograms Now
   if ( m_histoPersSvc ) {
@@ -422,7 +422,7 @@ void HLTControlFlowMgr::push( EventContext&& evtContext ) {
 
     // printing
     if ( UNLIKELY( msgLevel( MSG::VERBOSE ) && m_nextevt % m_printFreq == 0 ) ) {
-      verbose() << buildPrintableStateTree( NodeStates ).str() << endmsg;
+      verbose() << buildPrintableStateTree( LHCb::span<NodeState const>{NodeStates} ).str() << endmsg;
       verbose() << buildAlgsWithStates( AlgStates ).str() << endmsg;
     }
 
@@ -889,7 +889,7 @@ void HLTControlFlowMgr::registerTreePrintWidth() {
 
 // build the full tree
 template <typename printable>
-std::stringstream HLTControlFlowMgr::buildPrintableStateTree( std::vector<printable> const& states ) const {
+std::stringstream HLTControlFlowMgr::buildPrintableStateTree( LHCb::span<printable const> states ) const {
   assert( !m_printableDependencyTree.empty() );
   std::stringstream ss;
   ss << '\n';
@@ -901,10 +901,10 @@ std::stringstream HLTControlFlowMgr::buildPrintableStateTree( std::vector<printa
 }
 
 template std::stringstream
-HLTControlFlowMgr::buildPrintableStateTree<NodeState>( std::vector<NodeState> const& states ) const;
+HLTControlFlowMgr::buildPrintableStateTree<NodeState>( LHCb::span<NodeState const> states ) const;
 
 // build the AlgState printout
-std::stringstream HLTControlFlowMgr::buildAlgsWithStates( std::vector<AlgState> const& states ) const {
+std::stringstream HLTControlFlowMgr::buildAlgsWithStates( LHCb::span<AlgState const> states ) const {
   std::stringstream ss;
   ss << '\n';
   for ( auto const& [name, state] : Gaudi::Functional::details::zip::range( m_AlgNames, states ) ) {
diff --git a/Hlt/HLTScheduler/src/HLTControlFlowMgr.h b/Hlt/HLTScheduler/src/HLTControlFlowMgr.h
index 6535bf54f1c..a91d725bd8d 100644
--- a/Hlt/HLTScheduler/src/HLTControlFlowMgr.h
+++ b/Hlt/HLTScheduler/src/HLTControlFlowMgr.h
@@ -227,9 +227,10 @@ private:
 
   // runtime adding of states to print tree and states
 public:
-  template <typename printable>
-  std::stringstream buildPrintableStateTree( std::vector<printable> const& states ) const;
-  std::stringstream buildAlgsWithStates( std::vector<AlgState> const& states ) const;
+  template <typename Printable>
+  std::stringstream buildPrintableStateTree( LHCb::span<Printable const> states ) const;
+
+  std::stringstream buildAlgsWithStates( LHCb::span<AlgState const> states ) const;
 
   // to be able to check which states belong to which node (from the outside)
   auto getNodeNamesWithIndices() {
-- 
GitLab