[2] | 1 | #ifndef SORT_FILE_DYNAMIC_H |
---|
| 2 | #define SORT_FILE_DYNAMIC_H |
---|
| 3 | |
---|
| 4 | #include <stdint.h> |
---|
| 5 | #include <iostream> |
---|
| 6 | #include "file.h" |
---|
| 7 | #include "slot.h" |
---|
| 8 | #include "param.h" |
---|
| 9 | |
---|
| 10 | using namespace std; |
---|
| 11 | |
---|
| 12 | namespace hierarchy_memory |
---|
| 13 | { |
---|
| 14 | namespace sort_file |
---|
| 15 | { |
---|
| 16 | /* |
---|
| 17 | * File, sort by delay with a dynamic size |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | template <class T> |
---|
| 21 | class Sort_File_Dynamic : public File<T> |
---|
| 22 | { |
---|
| 23 | // *****[ variables ]***** |
---|
| 24 | protected : uint32_t ptr_read ; // pointer of the next slot to read |
---|
| 25 | protected : uint32_t ptr_write; // pointer of the next slot to write |
---|
| 26 | private : uint32_t factor ; |
---|
| 27 | private : const uint32_t size_base; |
---|
| 28 | // *****[ constructor ]***** |
---|
| 29 | public : Sort_File_Dynamic (param_t param) : File <T> (param.name,param.size), size_base (param.size) |
---|
| 30 | { |
---|
| 31 | factor = 1; |
---|
| 32 | }; |
---|
| 33 | |
---|
| 34 | // *****[ destructor ]***** |
---|
| 35 | public : ~Sort_File_Dynamic () |
---|
| 36 | { |
---|
| 37 | }; |
---|
| 38 | |
---|
| 39 | // *****[ must_decrease ]***** |
---|
| 40 | // Test if the file must be decrease |
---|
| 41 | private : bool must_decrease (void) |
---|
| 42 | { |
---|
| 43 | return ( (factor > 1 ) && |
---|
| 44 | (((100*File <T>::nb_slot)/File <T>::size) <= 33)); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | // *****[ must_increase ]***** |
---|
| 48 | // Test if the file must be increase |
---|
| 49 | private : bool must_increase (void) |
---|
| 50 | { |
---|
| 51 | return (((100*File <T>::nb_slot)/File <T>::size) >= 90); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | // *****[ increase ]***** |
---|
| 55 | // increase the file |
---|
| 56 | private : void increase (void) |
---|
| 57 | { |
---|
| 58 | factor ++; |
---|
| 59 | change_size (factor*size_base); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | // *****[ decrease ]***** |
---|
| 63 | // decrease the file |
---|
| 64 | private : void decrease (void) |
---|
| 65 | { |
---|
| 66 | factor --; |
---|
| 67 | change_size (factor*size_base); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | // *****[ change_size ]***** |
---|
| 71 | // decrease the file |
---|
| 72 | private : void change_size (uint32_t new_size) |
---|
| 73 | { |
---|
| 74 | uint32_t old_size = File <T>::size; |
---|
| 75 | slot_t<T> * new_slot = new slot_t<T> [new_size]; |
---|
| 76 | |
---|
| 77 | // copy data |
---|
| 78 | for (uint32_t it = 0; it < File <T>::nb_slot; it ++) |
---|
| 79 | { |
---|
| 80 | uint32_t ptr = (ptr_read+it)%old_size; |
---|
| 81 | |
---|
| 82 | new_slot [it] = File <T>::slot [ptr]; |
---|
| 83 | } |
---|
| 84 | // Change information |
---|
| 85 | delete File <T>::slot; |
---|
| 86 | File <T>::slot = new_slot; |
---|
| 87 | File <T>::size = new_size; |
---|
| 88 | ptr_write = File <T>::nb_slot; |
---|
| 89 | ptr_read = 0; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | // *****[ reset ]***** |
---|
| 93 | // Reset the file in the empty state |
---|
| 94 | public : void reset (void) |
---|
| 95 | { |
---|
| 96 | ptr_read = 0; |
---|
| 97 | ptr_write = 0; |
---|
| 98 | File <T>::nb_slot = 0; |
---|
| 99 | //nb_slot = 0; |
---|
| 100 | }; |
---|
| 101 | |
---|
| 102 | // *****[ transition ]***** |
---|
| 103 | // Decrease the delay at all cycle |
---|
| 104 | public : void transition () |
---|
| 105 | { |
---|
| 106 | for (uint32_t it = 0; it < File <T>::nb_slot; it ++) |
---|
| 107 | { |
---|
| 108 | uint32_t ptr = (ptr_read + it)%File <T>::size; |
---|
| 109 | |
---|
| 110 | if (File <T>::slot[ptr].delay != 0) |
---|
| 111 | File <T>::slot[ptr] .delay --; |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | // *****[ read ]***** |
---|
| 116 | // return the n-eme slot. |
---|
| 117 | // (first = 0) |
---|
| 118 | public : slot_t<T> read (uint32_t num) |
---|
| 119 | { |
---|
| 120 | if (num >= File <T>::nb_slot) |
---|
| 121 | { |
---|
| 122 | cerr << "<Sort_File_Dynamic.read> {ERROR} can't read because : num (" << num << ") >= nb_slot (" << File <T>::nb_slot << ")" << endl; |
---|
| 123 | exit(1); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | return File <T>::slot[(ptr_read + num)%File <T>::size]; |
---|
| 127 | }; |
---|
| 128 | |
---|
| 129 | // *****[ pop ]***** |
---|
| 130 | // read the file, and update the pointer |
---|
| 131 | public : T pop () |
---|
| 132 | { |
---|
| 133 | return pop (0); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | // *****[ pop ]***** |
---|
| 137 | // read the file, and update the pointer |
---|
| 138 | public : T pop (uint32_t num) |
---|
| 139 | { |
---|
| 140 | if (num >= File <T>::nb_slot) |
---|
| 141 | { |
---|
| 142 | cerr << "<Sort_File_Dynamic.pop> {ERROR} can't read because : num (" << num << ") >= nb_slot (" << File <T>::nb_slot << ")" << endl; |
---|
| 143 | exit(1); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | // If full -> quit |
---|
| 147 | if (must_decrease() == true) |
---|
| 148 | decrease(); |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | T val = File <T>::slot [(ptr_read+num)%File <T>::size].data; |
---|
| 152 | |
---|
| 153 | // Reorganize the file |
---|
| 154 | |
---|
| 155 | for (uint32_t it = 0; it < num; it ++) |
---|
| 156 | { |
---|
| 157 | uint32_t ptr = (ptr_read + num - it )%File <T>::size; |
---|
| 158 | uint32_t ptr_next = (ptr-1)%File <T>::size; |
---|
| 159 | |
---|
| 160 | File <T>::slot [ptr] = File <T>::slot [ptr_next]; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | File <T>::nb_slot --; |
---|
| 164 | ptr_read = (ptr_read+1)%File <T>::size; |
---|
| 165 | |
---|
| 166 | return val; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | // *****[ push ]***** |
---|
| 170 | // Push a new value (they must have a slot free) |
---|
| 171 | // Is sort by delay |
---|
| 172 | public : bool push (uint32_t delay, T val) |
---|
| 173 | { |
---|
| 174 | // If full -> quit |
---|
| 175 | if (must_increase() == true) |
---|
| 176 | increase(); |
---|
| 177 | |
---|
| 178 | uint32_t ptr = ptr_write; |
---|
| 179 | |
---|
| 180 | // Scan the fill to find the good position (keep the sort) |
---|
| 181 | for (uint32_t it = 0; it < File <T>::nb_slot; it ++) |
---|
| 182 | { |
---|
| 183 | uint32_t ptr_scan = (ptr != 0)?ptr:(File <T>::size); |
---|
| 184 | ptr_scan --; |
---|
| 185 | |
---|
| 186 | if (File <T>::slot[ptr_scan].delay <= delay) |
---|
| 187 | break; //find |
---|
| 188 | |
---|
| 189 | // reformor the file |
---|
| 190 | File <T>::slot [ptr] = File <T>::slot [ptr_scan]; |
---|
| 191 | ptr = ptr_scan; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | File <T>::slot [ptr] = slot_t <T> (delay,val); |
---|
| 195 | ptr_write = (ptr_write+1)%File <T>::size; // update the pointer |
---|
| 196 | File <T>::nb_slot ++; |
---|
| 197 | |
---|
| 198 | return true; |
---|
| 199 | } |
---|
| 200 | // *****[ print ]***** |
---|
| 201 | public : friend ostream& operator<< (ostream& output_stream, const Sort_File_Dynamic & x) |
---|
| 202 | { |
---|
| 203 | output_stream << "<" << x.name << ">" << endl; |
---|
| 204 | output_stream << " * ptr_read : " << x.ptr_read << endl; |
---|
| 205 | output_stream << " * ptr_write : " << x.ptr_write << endl; |
---|
| 206 | output_stream << " * nb_slot : " << x.nb_slot << endl; |
---|
| 207 | output_stream << " * size : " << x.size << endl; |
---|
| 208 | output_stream << " * factor : " << x.factor << endl; |
---|
| 209 | |
---|
| 210 | for (uint32_t it = 0; it < x.nb_slot; it ++) |
---|
| 211 | { |
---|
| 212 | uint32_t ptr = (x.ptr_read+it)%x.size; |
---|
| 213 | output_stream << x.slot [ptr] << endl; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | return output_stream; |
---|
| 217 | }; |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | }; |
---|
| 221 | }; //sort_file |
---|
| 222 | }; //hierarchy_memory |
---|
| 223 | #endif //!SORT_FILE_DYNAMIC_H |
---|
| 224 | |
---|