[80] | 1 | #ifndef ENVIRONMENT_QUEUE_SORT_QUEUE_H |
---|
| 2 | #define ENVIRONMENT_QUEUE_SORT_QUEUE_H |
---|
[78] | 3 | |
---|
| 4 | #include <stdint.h> |
---|
| 5 | #include <iostream> |
---|
| 6 | #include "Parameters.h" |
---|
| 7 | #include "Queue.h" |
---|
| 8 | |
---|
[80] | 9 | namespace environment { |
---|
[78] | 10 | namespace queue { |
---|
| 11 | |
---|
| 12 | /* |
---|
| 13 | * Cicurlar queue, sort by delay |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | template <class T> |
---|
| 17 | class Sort_Queue : public Queue<T> |
---|
| 18 | { |
---|
| 19 | // *****[ variables ]***** |
---|
| 20 | protected : uint32_t _ptr_read; // pointer of the next slot to read |
---|
| 21 | protected : uint32_t _ptr_write; // pointer of the next slot to write |
---|
| 22 | |
---|
| 23 | // *****[ constructor ]***** |
---|
| 24 | public : Sort_Queue (std::string name, |
---|
| 25 | Parameters * param) : Queue <T> (name,param->_size) |
---|
| 26 | { |
---|
[81] | 27 | _ptr_read = 0; |
---|
| 28 | _ptr_write = 0; |
---|
[78] | 29 | }; |
---|
| 30 | |
---|
| 31 | // *****[ destructor ]***** |
---|
| 32 | public : ~Sort_Queue () |
---|
| 33 | { |
---|
| 34 | }; |
---|
| 35 | |
---|
| 36 | // *****[ reset ]***** |
---|
| 37 | // Reset the queue in the empty state |
---|
| 38 | public : void reset () |
---|
| 39 | { |
---|
| 40 | _ptr_read = 0; |
---|
| 41 | _ptr_write = 0; |
---|
| 42 | Queue <T>::_nb_slot = 0; |
---|
| 43 | }; |
---|
| 44 | |
---|
| 45 | // *****[ transition ]***** |
---|
| 46 | // Decrease the delay at all cycle |
---|
| 47 | public : void transition () |
---|
| 48 | { |
---|
| 49 | for (uint32_t it = 0; it < Queue <T>::_nb_slot; it ++) |
---|
| 50 | { |
---|
| 51 | uint32_t ptr = (_ptr_read + it)%Queue <T>::_size; |
---|
| 52 | |
---|
| 53 | if (Queue <T>::_slot[ptr]._delay != 0) |
---|
| 54 | Queue <T>::_slot[ptr] ._delay --; |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | // *****[ read ]***** |
---|
| 59 | // return the n-eme slot. |
---|
| 60 | // (first = 0) |
---|
| 61 | public : slot_t<T> read (uint32_t num) |
---|
| 62 | { |
---|
| 63 | if (num >= Queue <T>::_nb_slot) |
---|
| 64 | { |
---|
| 65 | std::cerr << "<Sort_Queue.read> {ERROR} can't read because : num (" << num << ") >= nb_slot (" << Queue <T>::_nb_slot << ")" << std::endl; |
---|
| 66 | exit(1); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | return Queue <T>::_slot[(_ptr_read + num)%Queue <T>::_size]; |
---|
| 70 | }; |
---|
| 71 | |
---|
| 72 | // *****[ pop ]***** |
---|
| 73 | // read the queue, and update the pointer |
---|
| 74 | public : T pop () |
---|
| 75 | { |
---|
| 76 | return pop (0); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | // *****[ pop ]***** |
---|
| 80 | // read the queue, and update the pointer |
---|
| 81 | public : T pop (uint32_t num) |
---|
| 82 | { |
---|
| 83 | if (num >= Queue <T>::_nb_slot) |
---|
| 84 | { |
---|
| 85 | std::cerr << "<Sort_Queue.pop> {ERROR} can't read because : num (" << num << ") >= nb_slot (" << Queue <T>::_nb_slot << ")" << std::endl; |
---|
| 86 | exit(1); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | T val = Queue <T>::_slot [(_ptr_read+num)%Queue <T>::_size]._data; |
---|
| 90 | |
---|
| 91 | // Reorganize the queue |
---|
| 92 | |
---|
| 93 | for (uint32_t it = 0; it < num; it ++) |
---|
| 94 | { |
---|
| 95 | uint32_t ptr = (_ptr_read + num - it )%Queue <T>::_size; |
---|
| 96 | uint32_t _ptr_next = (ptr-1)%Queue <T>::_size; |
---|
| 97 | |
---|
| 98 | Queue <T>::_slot [ptr] = Queue <T>::_slot [_ptr_next]; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | Queue <T>::_nb_slot --; |
---|
| 102 | _ptr_read = (_ptr_read+1)%Queue <T>::_size; |
---|
| 103 | |
---|
| 104 | return val; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // *****[ push ]***** |
---|
[81] | 108 | public : bool push (T val) |
---|
| 109 | { |
---|
| 110 | return push (0, val); |
---|
| 111 | } |
---|
| 112 | |
---|
[78] | 113 | // Push a new value (they must have a slot free) |
---|
| 114 | // Is sort by delay |
---|
| 115 | public : bool push (uint32_t delay, T val) |
---|
| 116 | { |
---|
| 117 | // If full -> quit |
---|
| 118 | if (Queue <T>::_nb_slot == Queue <T>::_size) |
---|
| 119 | return false; |
---|
| 120 | |
---|
| 121 | uint32_t ptr = _ptr_write; |
---|
| 122 | |
---|
| 123 | // Scan the fill to find the good position (keep the sort) |
---|
| 124 | for (uint32_t it = 0; it < Queue <T>::_nb_slot; it ++) |
---|
| 125 | { |
---|
| 126 | uint32_t _ptr_scan = (ptr-1)%Queue <T>::_size; |
---|
| 127 | |
---|
| 128 | if (Queue <T>::_slot[_ptr_scan]._delay <= delay) |
---|
| 129 | break; //find |
---|
| 130 | |
---|
| 131 | // reformor the queue |
---|
| 132 | Queue <T>::_slot [ptr] = Queue <T>::_slot [_ptr_scan]; |
---|
| 133 | ptr = _ptr_scan; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | Queue <T>::_slot [ptr] = slot_t <T> (delay,val); |
---|
| 137 | _ptr_write = (_ptr_write+1)%Queue <T>::_size; // update the pointer |
---|
| 138 | Queue <T>::_nb_slot ++; |
---|
| 139 | |
---|
| 140 | return true; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | // *****[ print ]***** |
---|
| 144 | public : friend std::ostream& operator<< (std::ostream& output, const Sort_Queue & x) |
---|
| 145 | { |
---|
| 146 | output << "<" << x._name << ">" << std::endl; |
---|
| 147 | output << " * ptr_read : " << x._ptr_read << std::endl; |
---|
| 148 | output << " * ptr_write : " << x._ptr_write << std::endl; |
---|
| 149 | output << " * nb_slot : " << x._nb_slot << std::endl; |
---|
| 150 | output << " * size : " << x._size << std::endl; |
---|
| 151 | |
---|
| 152 | for (uint32_t it = 0; it < x._nb_slot; it ++) |
---|
| 153 | { |
---|
| 154 | uint32_t ptr = (x._ptr_read+it)%x._size; |
---|
| 155 | output << x._slot [ptr] << std::endl; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | return output; |
---|
| 159 | }; |
---|
| 160 | }; |
---|
| 161 | |
---|
| 162 | }; |
---|
| 163 | }; |
---|
| 164 | #endif |
---|