MultiAgentDecisionProcess  Release 0.2.1
FixedCapacityPriorityQueue.h
Go to the documentation of this file.
1 
28 /* Only include this header file once. */
29 #ifndef _FIXEDCAPACITYPRIORITYQUEUE_H_
30 #define _FIXEDCAPACITYPRIORITYQUEUE_H_ 1
31 
32 /* the include directives */
33 #include "Globals.h"
34 #include <list>
35 
42 template <class T>
44 {
45  private:
46  std::list <T> _m_l;
47  size_t _m_capacity;
48 
49  protected:
50 
51  public:
52 
53  // Constructor, destructor and copy assignment.
54  //
56  FixedCapacityPriorityQueue(size_t capacity)
57  : _m_capacity(capacity)
58  {};
59 /* let's trust the compiler to deal with this correctly...?
61  FixedCapacityPriorityQueue(const FixedCapacityPriorityQueue& a);
63  ~FixedCapacityPriorityQueue();
65  FixedCapacityPriorityQueue& operator= (const FixedCapacityPriorityQueue& o);
66 */
67  //operators:
68 
69  //data manipulation (set) functions:
76  bool insert( T& a, T& overflown_T );
77 
78  //get (data) functions:
79  bool empty() const {return _m_l.empty();}
80  T& top() { return _m_l.front(); } //highest priority (value) at the front
81  void pop() { _m_l.pop_front(); }
82 
83  T& back() {return _m_l.back(); }
84 
85  size_t size() const {return _m_l.size();}
86 };
87 
88 #define DEBUG_FCPQ 0
89 template <class T>
90 bool FixedCapacityPriorityQueue<T>::insert( T& a, T& overflown_T )
91 {
92  bool overflow = ( _m_l.size() == _m_capacity);
93  bool skip_insert = false;
94 
95  if(overflow) //check if we need to make space at the end
96  {
97  // overflown_Tp is the T* to which overflown_Tpp points
98  // we set the former to point to the overflown element.
99  //T* & overflown_Tp = *overflown_Tpp;
100 
101  T& last_in_queue = _m_l.back();
102  std::less< T > theLessOp;
103  if( theLessOp( last_in_queue, a) )
104  //if( last_in_queue < a ) //doesn't work automatically...
105  {
106  //This does not work with (or need) pointers,
107  //this simply points to last position in queue !!!
108  //overflown_Tp = &last_in_queue;
109 
110  //rather copy the value of the stuff we will throw out!
111  overflown_T = last_in_queue;
112  _m_l.pop_back();
113  }
114  else
115  {
116  overflown_T = a;
117  skip_insert = true;
118  }
119  }
120 
121  if(!skip_insert)
122  {
123  //insert a at the appropriate place
124  typename std::list<T>::iterator it = _m_l.begin();
125  typename std::list<T>::iterator last = _m_l.end();
126  bool not_positioned = true; //so long as it does not point to the correct pos.
127  while(it != last && not_positioned)
128  {
129  if( a > *it)
130  not_positioned = false;
131  else
132  it++;
133  }
134  _m_l.insert( it, a);
135  }
136 #if 0 && DEBUG_FCPQ
137  std::cout << "----AFTER POP AND INSERT\nthe overflown_Tp=" <<overflown_Tp << ", which means it points to..." <<std::endl ;
138  if(overflown_Tp != NULL)
139  {
140  T& the_overflown_T = (*overflown_Tp);
141  std::string typestr = " JPPVValPair* ";
142  std::cout << "the_overflown_T [T="<< typestr <<
143  "]=" << the_overflown_T << ", which points to..."<<std::endl;
144 
145  if(the_overflown_T != NULL)
146  std::cout<< (*the_overflown_T).SoftPrintBrief() << std::endl;
147  else
148  std::cout << "nothing" <<std::endl;
149  }
150  else
151  std::cout << " nothing." << std::endl;
152 
153  std::cout << "----" <<std::endl ;
154 #endif
155 
156  return(overflow);
157 }
158 
159 #endif /* !_FIXEDCAPACITYPRIORITYQUEUE_H_ */
160 
161 // Local Variables: ***
162 // mode:c++ ***
163 // End: ***