Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <set>
- #include <unordered_set>
- #include "texas/engine/MatcherNode.h"
- #include "texas/engine/RankMatcherNodes.h"
- #include "texas/engine/IMatcherNodeValidator.h"
- using namespace std;
- namespace texas {
- namespace engine {
- template<class _Node = MatcherNode, class _Rank = RankMatcherNodes>
- class MatcherNodeRepo {
- public:
- typedef shared_ptr<_Node> _NodePtr;
- MatcherNodeRepo(const string& id) : m_id(id) { }
- virtual ~MatcherNodeRepo() { }
- // Get repository ID.
- virtual string GetID() throw() { return m_id; }
- // Register new node in the working set.
- virtual void Register(_NodePtr node) {
- m_inactive.insert(node);
- }
- // Exclude node from the working set.
- virtual void Remove(_NodePtr node) {
- m_active.erase(node);
- m_inactive.erase(node);
- }
- // The function actualize the current working set.
- // Inactive nodes will be checked for activation.
- // Active nodes will be checked for deactivation.
- // Return number of currently active orders.
- virtual size_t UpdateActiveNodes() {
- unordered_set<_NodePtr> activate, deactivate;
- for (auto node : m_inactive) {
- if (node->ApplyActivationConditions()) {
- activate.insert(node);
- }
- }
- for (auto node : m_active) {
- if (!node->ApplyActivationConditions()) {
- deactivate.insert(node);
- }
- }
- for (auto node : activate) {
- m_active.insert(node);
- m_inactive.erase(node);
- }
- for (auto node : deactivate) {
- m_inactive.insert(node);
- m_active.erase(node);
- }
- return m_active.size();
- }
- // Check the node is currently in active set.
- virtual bool IsActiveNode(_NodePtr node) {
- return m_active.find(node) != m_active.end();
- }
- // Check the node is currently in inactive set.
- virtual bool IsInactiveNode(_NodePtr node) {
- return m_inactive.find(node) != m_inactive.end();
- }
- // Find a first appropriate node from active set.
- // Returns first valid node or empty pointer if node not found.
- virtual _NodePtr FindFirstActiveNode(IMatcherNodeValidator& validator) throw() {
- for (auto node : m_active) {
- if (validator.IsValid(node)) {
- return node;
- }
- }
- return _NodePtr();
- }
- virtual bool HasNodes() throw() {
- return m_inactive.size() > 0 || m_active.size() > 0;
- }
- protected:
- // Currently inactive nodes. Waiting for activation.
- set<_NodePtr> m_inactive;
- // Currently active nodes. Available for matching.
- set<_NodePtr, _Rank> m_active;
- // Repo ID.
- string m_id;
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement