~srba/verifytapn/trunk

« back to all changes in this revision

Viewing changes to src/ReachabilityChecker/PassedWaitingList/PriorityQueueWaitingList.hpp

  • Committer: Morten Jacobsen
  • Date: 2011-06-14 13:52:39 UTC
  • Revision ID: morten.jacobsen.2k@gmail.com-20110614135239-3ht7mfn4qt3h5gim
Added Cover most and Random search strategies.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef PRIORITYQUEUEWAITINGLIST_HPP_
 
2
#define PRIORITYQUEUEWAITINGLIST_HPP_
 
3
 
 
4
#include "WaitingList.hpp"
 
5
#include <queue>
 
6
#include "Node.hpp"
 
7
 
 
8
namespace VerifyTAPN {
 
9
 
 
10
        struct WeightedNode{
 
11
                Node* node;
 
12
                int weight;
 
13
                virtual ~WeightedNode() { if(node->GetColor() == COVERED) delete node; };
 
14
        };
 
15
 
 
16
        struct less : public std::binary_function<WeightedNode*, WeightedNode*, bool>
 
17
        {
 
18
                bool operator()(const WeightedNode* x, const WeightedNode* y) const
 
19
                {
 
20
                        return x->weight < y->weight;
 
21
                }
 
22
        };
 
23
 
 
24
        template <typename CalcWeight>
 
25
        class PriorityQueueWaitingList : public VerifyTAPN::WaitingList {
 
26
        public:
 
27
                typedef std::priority_queue<WeightedNode*, std::vector<WeightedNode*>, less > priority_queue;
 
28
        public:
 
29
                PriorityQueueWaitingList(): calcWeight(), queue(), actualSize(0) { };
 
30
                virtual ~PriorityQueueWaitingList()
 
31
                {
 
32
                        while(!queue.empty())
 
33
                        {
 
34
                                WeightedNode* it = queue.top();
 
35
                                delete it;
 
36
                                queue.pop();
 
37
                        }
 
38
                }
 
39
        public:
 
40
                virtual void Add(Node* node);
 
41
                virtual Node* Next();
 
42
                virtual long long Size() const { return actualSize; };
 
43
                inline virtual long long SizeIncludingCovered() const { return queue.size(); };
 
44
                inline virtual void DecrementActualSize() { actualSize--; };
 
45
 
 
46
        private:
 
47
                CalcWeight calcWeight;
 
48
                priority_queue queue;
 
49
                long long actualSize;
 
50
        };
 
51
 
 
52
        template <typename CalcWeight>
 
53
                void PriorityQueueWaitingList<CalcWeight>::Add(Node* node)
 
54
                {
 
55
                        if(node){
 
56
                                assert(node->GetColor()==WAITING);
 
57
                                WeightedNode* wnode = new WeightedNode;
 
58
                                wnode->node = node;
 
59
                                wnode->weight = calcWeight(node);
 
60
                                queue.push(wnode);
 
61
                                actualSize++;
 
62
                        }
 
63
                }
 
64
 
 
65
                template <typename CalcWeight>
 
66
                Node* PriorityQueueWaitingList<CalcWeight>::Next()
 
67
                {
 
68
                        if(Size() == 0) return NULL;
 
69
                        WeightedNode* node = queue.top();
 
70
                        assert(node->node->GetColor() == WAITING || node->node->GetColor() == COVERED);
 
71
                        while(node->node->GetColor() == COVERED){
 
72
                                queue.pop();
 
73
                                delete node;
 
74
                                node = queue.top();
 
75
                                if(node == NULL || node->node == NULL) return NULL;
 
76
                        }
 
77
 
 
78
                        queue.pop(); actualSize--;
 
79
                        node->node->Recolor(PASSED);
 
80
        //              assert(AllElementsAreWatingOrCovered(stack.begin(), stack.end()));
 
81
                        return node->node;
 
82
                }
 
83
}
 
84
 
 
85
#endif /* PRIORITYQUEUEWAITINGLIST_HPP_ */