Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #pragma once
  2. #include <set>
  3. #include <unordered_set>
  4. #include "texas/engine/MatcherNode.h"
  5. #include "texas/engine/RankMatcherNodes.h"
  6. #include "texas/engine/IMatcherNodeValidator.h"
  7.  
  8. using namespace std;
  9.  
  10. namespace texas {
  11.     namespace engine {
  12.  
  13.         template<class _Node = MatcherNode, class _Rank = RankMatcherNodes>
  14.         class MatcherNodeRepo {
  15.         public:
  16.  
  17.             typedef shared_ptr<_Node> _NodePtr;
  18.  
  19.             MatcherNodeRepo(const string& id) : m_id(id) { }
  20.             virtual ~MatcherNodeRepo() { }
  21.  
  22.             // Get repository ID.
  23.             virtual string GetID() throw() { return m_id; }
  24.  
  25.             // Register new node in the working set.
  26.             virtual void Register(_NodePtr node) {
  27.                 m_inactive.insert(node);
  28.             }
  29.  
  30.             // Exclude node from the working set.
  31.             virtual void Remove(_NodePtr node) {
  32.                 m_active.erase(node);
  33.                 m_inactive.erase(node);
  34.             }
  35.  
  36.             // The function actualize the current working set.
  37.             // Inactive nodes will be checked for activation.
  38.             // Active nodes will be checked for deactivation.
  39.             // Return number of currently active orders.
  40.             virtual size_t UpdateActiveNodes() {
  41.                 unordered_set<_NodePtr> activate, deactivate;
  42.                 for (auto node : m_inactive) {
  43.                     if (node->ApplyActivationConditions()) {
  44.                         activate.insert(node);
  45.                     }
  46.                 }
  47.                 for (auto node : m_active) {
  48.                     if (!node->ApplyActivationConditions()) {
  49.                         deactivate.insert(node);
  50.                     }
  51.                 }
  52.                 for (auto node : activate) {
  53.                     m_active.insert(node);
  54.                     m_inactive.erase(node);
  55.                 }
  56.                 for (auto node : deactivate) {
  57.                     m_inactive.insert(node);
  58.                     m_active.erase(node);
  59.                 }
  60.                 return m_active.size();
  61.             }
  62.  
  63.             // Check the node is currently in active set.
  64.             virtual bool IsActiveNode(_NodePtr node) {
  65.                 return m_active.find(node) != m_active.end();
  66.             }
  67.  
  68.             // Check the node is currently in inactive set.
  69.             virtual bool IsInactiveNode(_NodePtr node) {
  70.                 return m_inactive.find(node) != m_inactive.end();
  71.             }
  72.  
  73.             // Find a first appropriate node from active set.
  74.             // Returns first valid node or empty pointer if node not found.
  75.             virtual _NodePtr FindFirstActiveNode(IMatcherNodeValidator& validator) throw() {
  76.                 for (auto node : m_active) {
  77.                     if (validator.IsValid(node)) {
  78.                         return node;
  79.                     }
  80.                 }
  81.                 return _NodePtr();
  82.             }
  83.  
  84.             virtual bool HasNodes() throw() {
  85.                 return m_inactive.size() > 0 || m_active.size() > 0;
  86.             }
  87.  
  88.         protected:
  89.             // Currently inactive nodes. Waiting for activation.
  90.             set<_NodePtr> m_inactive;
  91.             // Currently active nodes. Available for matching.
  92.             set<_NodePtr, _Rank> m_active;
  93.             // Repo ID.
  94.             string m_id;
  95.         };
  96.  
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement