From e07c8505c01ca391a791eb207392d095d7468e23 Mon Sep 17 00:00:00 2001
From: NAUD Maxence <maxence.naud@cea.fr>
Date: Fri, 12 Jan 2024 13:43:57 +0000
Subject: [PATCH] small optimizations in some graphRegex classes

---
 .../graphRegex/matchFsm/FsmRunTimeContext.hpp | 308 +++++++++---------
 .../aidge/graphRegex/matchFsm/MatchResult.hpp |  15 +-
 src/graphRegex/matchFsm/FsmRunTimeContext.cpp |  27 +-
 src/graphRegex/matchFsm/MatchResult.cpp       |   4 -
 4 files changed, 172 insertions(+), 182 deletions(-)

diff --git a/include/aidge/graphRegex/matchFsm/FsmRunTimeContext.hpp b/include/aidge/graphRegex/matchFsm/FsmRunTimeContext.hpp
index 2f6066ba4..36d09db47 100644
--- a/include/aidge/graphRegex/matchFsm/FsmRunTimeContext.hpp
+++ b/include/aidge/graphRegex/matchFsm/FsmRunTimeContext.hpp
@@ -10,164 +10,162 @@
 #include "aidge/graph/Node.hpp"
 
 
-
 namespace Aidge{
 
-    class FsmNode;
+class FsmNode;
+
+/**
+ * @brief a class used to save the execution context of state machines, that is the actual state in the FSM, the actual node in the graph
+ * all node that have been Validate,Rejecte or Considered common
+*/
+class FsmRunTimeContext
+{
+private:
+    /**
+     * @brief the list of node rejected for all the context
+    */
+    static std::vector<std::set<NodePtr>> mRejectedNodes;
+    /**
+     * @brief the actual state of this Context (where it's in the FSM graph)
+    */
+    std::shared_ptr<FsmNode> mActState;
+    /**
+     * @brief the actual node of this Context (where it's in the graph)
+    */
+    NodePtr mActOpNode;
+    /**
+     * @brief the map of the node consider as common and the common ID
+     * @details we need to store what node it's consider as common because of the end
+     * resolution of the matching, all node consider as common need to be the same in all context
+    */
+    std::map<NodePtr,std::size_t> mCommonNodes;
+    /**
+     * @brief the map of the node that as been valid in this context , and the test that valide the node
+    */
+    std::map<std::shared_ptr<ConditionalInterpreter>,std::set<NodePtr>> mValidNodes;
+    /**
+     * @brief the index in the rejected node of this context
+    */
+    std::size_t mLocalIdxRejeced;
+public:
+    /**
+     * @brief constructor
+     * @param actState the actual state in the FSM
+     * @param actOpNode the actual node in the graph
+     * @param idxRejeced the idx in the global regected node vector init max() as sentinel value of undefind
+    */
+    FsmRunTimeContext(std::shared_ptr<FsmNode> actState ,NodePtr actOpNode ,std::size_t idxRejeced =std::numeric_limits<std::size_t>::max() );
+    FsmRunTimeContext(std::shared_ptr<FsmRunTimeContext> fsmRunTime);
+    FsmRunTimeContext(std::shared_ptr<FsmRunTimeContext> fsmRunTime,std::shared_ptr<FsmNode> actState ,NodePtr actOpNode );
+
+    virtual ~FsmRunTimeContext()=default;
+
+    /**
+     * @defgroup FsmRunTimeContextRejected Function for managing rejected nodes
+     */
+
+    /**
+     * @ingroup FsmRunTimeContextRejected
+     * @brief Add a node as rejected in this context
+     */
+    void addRejectedNode(NodePtr node);
+
+    /**
+     * @ingroup FsmRunTimeContextRejected
+     * @brief get the rejected nodes of this context
+     */
+    inline std::set<NodePtr> getRejectedNodes(void) const {
+        return mRejectedNodes[mLocalIdxRejeced];
+    }
+
+
+    /**
+     * @defgroup FsmRunTimeContextTest Function for test the context
+     */
 
-    class FsmNode;
+    /**
+     * @ingroup FsmRunTimeContextTest
+     * @brief test if the actual state is valide
+     * @return bool
+     */
+    bool isOnValidState(void);
+    /**
+     * @ingroup FsmRunTimeContextTest
+     * @brief test if the node is considered as common in this context
+     * @param node node to test
+     * @return bool
+     */
+    bool isCommonDefined(NodePtr node);
+    /**
+     * @ingroup FsmRunTimeContextTest
+     * @brief test if has already validated in this context
+     * @param node node to test
+     * @return bool
+     */
+    bool isAlreadyValid(NodePtr node);
+    /**
+     * @ingroup FsmRunTimeContextTest
+     * @brief test if this context is compatible with an others
+     * @details to say that two contexts are compatible is to check :
+     *  that the contexts do not validate the same nodes (other than the common ones)
+     *  and that the common ones have the same idx
+     * @param fsmContext the others context
+     * @return bool
+     */
+    bool areCompatible(std::shared_ptr<FsmRunTimeContext> fsmContext);
+    /**
+     * @ingroup FsmRunTimeContextTest
+     * @brief test if this context is strictly equal with an others
+     * @param fsmContext the others context
+     * @return bool
+     */
+    bool areEqual(std::shared_ptr<FsmRunTimeContext> fsmContext);
 
     /**
-     * @brief a class used to save the execution context of state machines, that is the actual state in the FSM, the actual node in the graph
-     * all node that have been Validate,Rejecte or Considered common
+     * @defgroup FsmRunTimeContextSet Function set context
     */
-    class FsmRunTimeContext
-    {
-    private:
-        /**
-         * @brief the list of node rejected for all the context
-        */
-        static std::vector<std::set<NodePtr>> mRejectedNodes;
-        /**
-         * @brief the actual state of this Context (where it's in the FSM graph)
-        */
-        std::shared_ptr<FsmNode> mActState;
-        /**
-         * @brief the actual node of this Context (where it's in the graph)
-        */
-        NodePtr mActOpNode;
-        /**
-         * @brief the map of the node consider as common and the common ID
-         * @details we need to store what node it's consider as common because of the end
-         * resolution of the matching, all node consider as common need to be the same in all context
-        */
-        std::map<NodePtr,std::size_t> mCommonNodes;
-        /**
-         * @brief the map of the node that as been valid in this context , and the test that valide the node
-        */
-        std::map<std::shared_ptr<ConditionalInterpreter>,std::set<NodePtr>> mValidNodes;
-        /**
-         * @brief the index in the rejected node of this context
-        */
-        std::size_t mLocalIdxRejeced;
-    public:
-        /**
-         * @brief constructor
-         * @param actState the actual state in the FSM
-         * @param actOpNode the actual node in the graph
-         * @param idxRejeced the idx in the global regected node vector init max() as sentinel value of undefind
-        */
-        FsmRunTimeContext(std::shared_ptr<FsmNode> actState ,NodePtr actOpNode ,std::size_t idxRejeced =std::numeric_limits<std::size_t>::max() );
-        FsmRunTimeContext(std::shared_ptr<FsmRunTimeContext> fsmRunTime);
-        FsmRunTimeContext(std::shared_ptr<FsmRunTimeContext> fsmRunTime,std::shared_ptr<FsmNode> actState ,NodePtr actOpNode );
-
-        virtual ~FsmRunTimeContext()=default;
-
-        /**
-         * @defgroup FsmRunTimeContextRejected Function for managing rejected nodes
-         */
-
-        /**
-         * @ingroup FsmRunTimeContextRejected
-         * @brief Add a node as rejected in this context
-         */
-        void addRejectedNode(NodePtr node);
-
-        /**
-         * @ingroup FsmRunTimeContextRejected
-         * @brief get the rejected nodes of this context
-         */
-        std::set<NodePtr> getRejectedNodes(void);
-
-
-        /**
-         * @defgroup FsmRunTimeContextTest Function for test the context
-         */
-
-        /**
-         * @ingroup FsmRunTimeContextTest
-         * @brief test if the actual state is valide
-         * @return bool
-         */
-        bool isOnValidState(void);
-        /**
-         * @ingroup FsmRunTimeContextTest
-         * @brief test if the node is considered as common in this context
-         * @param node node to test
-         * @return bool
-         */
-        bool isCommonDefined(NodePtr node);
-        /**
-         * @ingroup FsmRunTimeContextTest
-         * @brief test if has already validated in this context
-         * @param node node to test
-         * @return bool
-         */
-        bool isAlreadyValid(NodePtr node);
-        /**
-         * @ingroup FsmRunTimeContextTest
-         * @brief test if this context is compatible with an others
-         * @details to say that two contexts are compatible is to check :
-         *  that the contexts do not validate the same nodes (other than the common ones)
-         *  and that the common ones have the same idx
-         * @param fsmContext the others context
-         * @return bool
-         */
-        bool areCompatible(std::shared_ptr<FsmRunTimeContext> fsmContext);
-        /**
-         * @ingroup FsmRunTimeContextTest
-         * @brief test if this context is strictly equal with an others
-         * @param fsmContext the others context
-         * @return bool
-         */
-        bool areEqual(std::shared_ptr<FsmRunTimeContext> fsmContext);
-
-        /**
-         * @defgroup FsmRunTimeContextSet Function set context
-        */
-
-
-        void setCommon(NodePtr node,std::size_t commonIdx);
-
-
-        void setValid(NodePtr node,std::shared_ptr<ConditionalInterpreter> tag);
-
-        /**
-         * @defgroup FsmRunTimeContextGet Function get context
-         */
-
-
-        /**
-         * @ingroup FsmRunTimeContextGet
-         * @brief get the sub idx state
-         * @return bool
-         */
-        std::size_t getSubStmId(void);
-
-        NodePtr getCommonNodeFromIdx(std::size_t commonIdx);
-        std::size_t getCommonNodeIdx(NodePtr node);
-        std::set<NodePtr> getCommonNodes(void);
-
-        std::map<NodePtr,std::size_t> getCommon(void);
-        std::set<NodePtr> getValidNodes(void);
-
-        std::set<NodePtr> getValidNodesNoCommon(void);
-        std::map<std::shared_ptr<ConditionalInterpreter>,std::set<NodePtr>>& getValid(void);
-
-
-        NodePtr getActNode(void);
-        std::shared_ptr<FsmNode> getActState(void);
-
-
-        /**
-         * @defgroup FsmRunTimeContextMem
-         */
-
-        void rst(void);
-
-
-    };
-
-}
-
-#endif //AIDGE_CORE_FSM_RUN_TIME_CONTEXT_H_
+
+
+    void setCommon(NodePtr node,std::size_t commonIdx);
+
+
+    void setValid(NodePtr node,std::shared_ptr<ConditionalInterpreter> tag);
+
+    /**
+     * @defgroup FsmRunTimeContextGet Function get context
+     */
+
+
+    /**
+     * @ingroup FsmRunTimeContextGet
+     * @brief get the sub idx state
+     * @return bool
+     */
+    std::size_t getSubStmId(void);
+
+    NodePtr getCommonNodeFromIdx(std::size_t commonIdx);
+    std::size_t getCommonNodeIdx(NodePtr node);
+    std::set<NodePtr> getCommonNodes(void);
+
+    std::map<NodePtr,std::size_t> getCommon(void);
+    std::set<NodePtr> getValidNodes(void);
+
+    std::set<NodePtr> getValidNodesNoCommon(void);
+    std::map<std::shared_ptr<ConditionalInterpreter>,std::set<NodePtr>>& getValid(void);
+
+
+    NodePtr getActNode(void);
+    std::shared_ptr<FsmNode> getActState(void);
+
+
+    /**
+     * @defgroup FsmRunTimeContextMem
+     */
+
+    void rst(void);
+
+
+};
+} // namespace Aidge
+
+#endif // AIDGE_CORE_FSM_RUN_TIME_CONTEXT_H_
diff --git a/include/aidge/graphRegex/matchFsm/MatchResult.hpp b/include/aidge/graphRegex/matchFsm/MatchResult.hpp
index 4f7f9bf1d..7954e932a 100644
--- a/include/aidge/graphRegex/matchFsm/MatchResult.hpp
+++ b/include/aidge/graphRegex/matchFsm/MatchResult.hpp
@@ -24,8 +24,10 @@ private:
     const std::vector<NodePtr> mStartNode;
 
 public:
+    MatchSolution() = delete;
     MatchSolution(std::vector<std::shared_ptr<FsmRunTimeContext>>& precedence,const std::string query,const std::vector<NodePtr> startNode);
-    inline const std::set<NodePtr>& at(const std::string key) {
+
+    inline const std::set<NodePtr>& at(const std::string& key) {
         return mSolution[key];
     }
     const std::set<NodePtr> getAll();
@@ -33,7 +35,6 @@ public:
 
     inline const std::string& getQuery() const noexcept { return mQueryFrom; }
     inline const std::vector<NodePtr>& getStartNode() const noexcept { return mStartNode; }
-
 };
 
 
@@ -60,14 +61,18 @@ private:
 
 
 public:
-    MatchResult(std::vector<std::shared_ptr<FsmRunTimeContext>> allValid, std::size_t nbSubStm,
-     const std::string& query,const std::vector<NodePtr>& startNodes);
+    MatchResult() = delete;
+    MatchResult(std::vector<std::shared_ptr<FsmRunTimeContext>> allValid,
+                std::size_t nbSubStm,
+                const std::string& query,const std::vector<NodePtr>& startNodes);
 
     /**
      * @brief get the set of the node match for une expression
      * @return the set of node of the graph that corresponding to an expression
     */
-    std::shared_ptr<MatchSolution> getBiggerSolution(void);
+    inline std::shared_ptr<MatchSolution> getBiggerSolution(void) const noexcept {
+        return mSolve.empty() ? nullptr : mSolve[0];
+    }
 
     inline std::vector<std::shared_ptr<MatchSolution>> getSolutions(void) const noexcept {
         return mSolve;
diff --git a/src/graphRegex/matchFsm/FsmRunTimeContext.cpp b/src/graphRegex/matchFsm/FsmRunTimeContext.cpp
index ddf6a46cc..7a09908e5 100644
--- a/src/graphRegex/matchFsm/FsmRunTimeContext.cpp
+++ b/src/graphRegex/matchFsm/FsmRunTimeContext.cpp
@@ -1,7 +1,7 @@
 #include "aidge/graphRegex/matchFsm/FsmRunTimeContext.hpp"
 #include "aidge/graphRegex/matchFsm/FsmNode.hpp"
 
-using namespace Aidge; 
+using namespace Aidge;
 
 std::vector<std::set<NodePtr>> FsmRunTimeContext::mRejectedNodes;
 
@@ -42,10 +42,6 @@ void FsmRunTimeContext::addRejectedNode(NodePtr node){
     mRejectedNodes[mLocalIdxRejeced].insert(node);
 }
 
-std::set<NodePtr> FsmRunTimeContext::getRejectedNodes(void){
-    return mRejectedNodes[mLocalIdxRejeced];
-}
-
 bool FsmRunTimeContext::isOnValidState(void){
     return mActState->isValid();
 }
@@ -57,7 +53,7 @@ bool FsmRunTimeContext::isCommonDefined(NodePtr node){
     for(const auto& nodeC : nodes){
         if(nodeC.get() == node.get()){
             return true;
-        } 
+        }
     }
     return false;
 }
@@ -68,7 +64,7 @@ bool FsmRunTimeContext::isAlreadyValid(NodePtr node){
     for(const auto& nodeV : nodes){
         if(nodeV.get() == node.get()){
             return true;
-        } 
+        }
     }
     return false;
 
@@ -82,7 +78,7 @@ bool FsmRunTimeContext::areCompatible(std::shared_ptr<FsmRunTimeContext> fsmCont
     and the same idx for the common
     */
 
-   //common node 
+   //common node
 
    for (const auto& ref : getCommon()) {
         for (const auto& test : fsmContext->getCommon()) {
@@ -97,20 +93,15 @@ bool FsmRunTimeContext::areCompatible(std::shared_ptr<FsmRunTimeContext> fsmCont
 
    //valid nodes
     std::set<NodePtr> commonElements;
-   std::set<NodePtr> A = getValidNodesNoCommon();
-   std::set<NodePtr> B = fsmContext->getValidNodesNoCommon();
+    std::set<NodePtr> A = getValidNodesNoCommon();
+    std::set<NodePtr> B = fsmContext->getValidNodesNoCommon();
     std::set_intersection(
         A.begin(),A.end(),
         B.begin(),  B.end(),
         std::inserter(commonElements, commonElements.end())
        );
 
-
-    if (!commonElements.empty()) {
-        return false;
-    }
-
-    return true;
+    return (commonElements.empty()) ? true : false;
 }
 
 bool FsmRunTimeContext::areEqual(std::shared_ptr<FsmRunTimeContext> fsmContext){
@@ -142,7 +133,7 @@ void FsmRunTimeContext::setCommon(NodePtr node,std::size_t commonIdx){
 }
 
 void FsmRunTimeContext::setValid(NodePtr node,std::shared_ptr<ConditionalInterpreter> tag){
-    //we already find a node of this type 
+    //we already find a node of this type
     if(mValidNodes.find(tag) != mValidNodes.end()){
         if(isAlreadyValid(node) && !isCommonDefined(node) ){
             throw std::runtime_error("setValid you valid tow time");
@@ -151,7 +142,7 @@ void FsmRunTimeContext::setValid(NodePtr node,std::shared_ptr<ConditionalInterpr
     }else{
         mValidNodes[tag] = {node};
     }
-    
+
 }
 
 std::size_t FsmRunTimeContext::getSubStmId(void){
diff --git a/src/graphRegex/matchFsm/MatchResult.cpp b/src/graphRegex/matchFsm/MatchResult.cpp
index 08be00dea..99df00e19 100644
--- a/src/graphRegex/matchFsm/MatchResult.cpp
+++ b/src/graphRegex/matchFsm/MatchResult.cpp
@@ -132,8 +132,4 @@ void Aidge::MatchResult::_generateCombination( std::size_t idxSubStm,
     }
     return;
 
-}
-
-std::shared_ptr<Aidge::MatchSolution> Aidge::MatchResult::getBiggerSolution(void){
-    return mSolve.empty() ? nullptr : mSolve[0];
 }
\ No newline at end of file
-- 
GitLab