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