#ifndef SORT_FILE_H #define SORT_FILE_H #include #include #include "file.h" #include "slot.h" #include "param.h" using namespace std; using namespace hierarchy_memory::sort_file; namespace hierarchy_memory { namespace sort_file { /* * Cicurlar file, sort by delay */ template class Sort_File : public File { // *****[ variables ]***** protected : uint32_t ptr_read; // pointer of the next slot to read protected : uint32_t ptr_write; // pointer of the next slot to write // *****[ constructor ]***** public : Sort_File (param_t param) : File (param.name,param.size) { }; // *****[ destructor ]***** public : ~Sort_File () { }; // *****[ reset ]***** // Reset the file in the empty state public : void reset () { ptr_read = 0; ptr_write = 0; File ::nb_slot = 0; //nb_slot = 0; }; // *****[ transition ]***** // Decrease the delay at all cycle public : void transition () { for (uint32_t it = 0; it < File ::nb_slot; it ++) { uint32_t ptr = (ptr_read + it)%File ::size; if (File ::slot[ptr].delay != 0) File ::slot[ptr] .delay --; } } // *****[ read ]***** // return the n-eme slot. // (first = 0) public : slot_t read (uint32_t num) { if (num >= File ::nb_slot) { cerr << " {ERROR} can't read because : num (" << num << ") >= nb_slot (" << File ::nb_slot << ")" << endl; exit(1); } return File ::slot[(ptr_read + num)%File ::size]; }; // *****[ pop ]***** // read the file, and update the pointer public : T pop () { return pop (0); } // *****[ pop ]***** // read the file, and update the pointer public : T pop (uint32_t num) { if (num >= File ::nb_slot) { cerr << " {ERROR} can't read because : num (" << num << ") >= nb_slot (" << File ::nb_slot << ")" << endl; exit(1); } T val = File ::slot [(ptr_read+num)%File ::size].data; // Reorganize the file for (uint32_t it = 0; it < num; it ++) { uint32_t ptr = (ptr_read + num - it )%File ::size; uint32_t ptr_next = (ptr-1)%File ::size; File ::slot [ptr] = File ::slot [ptr_next]; } File ::nb_slot --; ptr_read = (ptr_read+1)%File ::size; return val; } // *****[ push ]***** // Push a new value (they must have a slot free) // Is sort by delay public : bool push (uint32_t delay, T val) { // If full -> quit if (File ::nb_slot == File ::size) return false; uint32_t ptr = ptr_write; // Scan the fill to find the good position (keep the sort) for (uint32_t it = 0; it < File ::nb_slot; it ++) { uint32_t ptr_scan = (ptr-1)%File ::size; if (File ::slot[ptr_scan].delay <= delay) break; //find // reformor the file File ::slot [ptr] = File ::slot [ptr_scan]; ptr = ptr_scan; } File ::slot [ptr] = slot_t (delay,val); ptr_write = (ptr_write+1)%File ::size; // update the pointer File ::nb_slot ++; return true; } // *****[ print ]***** public : friend ostream& operator<< (ostream& output_stream, const Sort_File & x) { output_stream << "<" << x.name << ">" << endl; output_stream << " * ptr_read : " << x.ptr_read << endl; output_stream << " * ptr_write : " << x.ptr_write << endl; output_stream << " * nb_slot : " << x.nb_slot << endl; output_stream << " * size : " << x.size << endl; for (uint32_t it = 0; it < x.nb_slot; it ++) { uint32_t ptr = (x.ptr_read+it)%x.size; output_stream << x.slot [ptr] << endl; } return output_stream; }; }; }; //sort_file }; //hierarchy_memory #endif //!SORT_FILE_H