[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) |
---|
[85] | 61 | public : slot_t<T> read (uint32_t num=0) |
---|
[78] | 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 |
---|
[85] | 74 | public : T pop (uint32_t num=0) |
---|
[78] | 75 | { |
---|
| 76 | if (num >= Queue <T>::_nb_slot) |
---|
| 77 | { |
---|
| 78 | std::cerr << "<Sort_Queue.pop> {ERROR} can't read because : num (" << num << ") >= nb_slot (" << Queue <T>::_nb_slot << ")" << std::endl; |
---|
| 79 | exit(1); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | T val = Queue <T>::_slot [(_ptr_read+num)%Queue <T>::_size]._data; |
---|
| 83 | |
---|
| 84 | // Reorganize the queue |
---|
| 85 | |
---|
| 86 | for (uint32_t it = 0; it < num; it ++) |
---|
| 87 | { |
---|
| 88 | uint32_t ptr = (_ptr_read + num - it )%Queue <T>::_size; |
---|
| 89 | uint32_t _ptr_next = (ptr-1)%Queue <T>::_size; |
---|
| 90 | |
---|
| 91 | Queue <T>::_slot [ptr] = Queue <T>::_slot [_ptr_next]; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | Queue <T>::_nb_slot --; |
---|
| 95 | _ptr_read = (_ptr_read+1)%Queue <T>::_size; |
---|
| 96 | |
---|
| 97 | return val; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | // *****[ push ]***** |
---|
[81] | 101 | public : bool push (T val) |
---|
| 102 | { |
---|
| 103 | return push (0, val); |
---|
| 104 | } |
---|
| 105 | |
---|
[78] | 106 | // Push a new value (they must have a slot free) |
---|
| 107 | // Is sort by delay |
---|
| 108 | public : bool push (uint32_t delay, T val) |
---|
| 109 | { |
---|
| 110 | // If full -> quit |
---|
| 111 | if (Queue <T>::_nb_slot == Queue <T>::_size) |
---|
| 112 | return false; |
---|
| 113 | |
---|
| 114 | uint32_t ptr = _ptr_write; |
---|
| 115 | |
---|
| 116 | // Scan the fill to find the good position (keep the sort) |
---|
| 117 | for (uint32_t it = 0; it < Queue <T>::_nb_slot; it ++) |
---|
| 118 | { |
---|
| 119 | uint32_t _ptr_scan = (ptr-1)%Queue <T>::_size; |
---|
| 120 | |
---|
| 121 | if (Queue <T>::_slot[_ptr_scan]._delay <= delay) |
---|
| 122 | break; //find |
---|
| 123 | |
---|
| 124 | // reformor the queue |
---|
| 125 | Queue <T>::_slot [ptr] = Queue <T>::_slot [_ptr_scan]; |
---|
| 126 | ptr = _ptr_scan; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | Queue <T>::_slot [ptr] = slot_t <T> (delay,val); |
---|
| 130 | _ptr_write = (_ptr_write+1)%Queue <T>::_size; // update the pointer |
---|
| 131 | Queue <T>::_nb_slot ++; |
---|
| 132 | |
---|
| 133 | return true; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | // *****[ print ]***** |
---|
| 137 | public : friend std::ostream& operator<< (std::ostream& output, const Sort_Queue & x) |
---|
| 138 | { |
---|
| 139 | output << "<" << x._name << ">" << std::endl; |
---|
| 140 | output << " * ptr_read : " << x._ptr_read << std::endl; |
---|
| 141 | output << " * ptr_write : " << x._ptr_write << std::endl; |
---|
| 142 | output << " * nb_slot : " << x._nb_slot << std::endl; |
---|
| 143 | output << " * size : " << x._size << std::endl; |
---|
| 144 | |
---|
| 145 | for (uint32_t it = 0; it < x._nb_slot; it ++) |
---|
| 146 | { |
---|
| 147 | uint32_t ptr = (x._ptr_read+it)%x._size; |
---|
| 148 | output << x._slot [ptr] << std::endl; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | return output; |
---|
| 152 | }; |
---|
| 153 | }; |
---|
| 154 | |
---|
| 155 | }; |
---|
| 156 | }; |
---|
| 157 | #endif |
---|