source: trunk/IPs/systemC/Environment/Queue/include/Sort_Queue.h @ 80

Last change on this file since 80 was 80, checked in by rosiere, 16 years ago

Oups, Environnement is french :P

File size: 3.9 KB
Line 
1#ifndef ENVIRONMENT_QUEUE_SORT_QUEUE_H
2#define ENVIRONMENT_QUEUE_SORT_QUEUE_H
3
4#include <stdint.h>
5#include <iostream>
6#include "Parameters.h"
7#include "Queue.h"
8
9namespace environment {
10namespace 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      {
27      };
28     
29      // *****[ destructor ]*****
30    public : ~Sort_Queue ()
31      {
32      };
33     
34      // *****[ reset ]*****
35      // Reset the queue in the empty state
36    public : void reset ()
37      {
38        _ptr_read  = 0;
39        _ptr_write = 0;
40        Queue <T>::_nb_slot   = 0;
41      };
42     
43      // *****[ transition ]*****
44      // Decrease the delay at all cycle
45    public : void transition ()
46      {
47        for (uint32_t it = 0; it < Queue <T>::_nb_slot; it ++)
48          {
49            uint32_t ptr = (_ptr_read + it)%Queue <T>::_size;
50           
51            if (Queue <T>::_slot[ptr]._delay != 0)
52              Queue <T>::_slot[ptr] ._delay --;
53          }
54      }
55     
56      // *****[ read ]*****
57      // return the n-eme slot.
58      // (first = 0)
59    public : slot_t<T> read (uint32_t num)
60      {
61        if (num >= Queue <T>::_nb_slot)
62          {
63            std::cerr << "<Sort_Queue.read> {ERROR} can't read because : num (" << num << ") >= nb_slot (" << Queue <T>::_nb_slot << ")" << std::endl;
64            exit(1);
65          }
66
67        return Queue <T>::_slot[(_ptr_read + num)%Queue <T>::_size];
68      };
69     
70      // *****[ pop ]*****
71      // read the queue, and update the pointer
72    public : T pop  ()
73      {
74        return pop (0);
75      }
76
77      // *****[ pop ]*****
78      // read the queue, and update the pointer
79    public : T pop  (uint32_t num)
80      {
81        if (num >= Queue <T>::_nb_slot)
82          {
83            std::cerr << "<Sort_Queue.pop> {ERROR} can't read because : num (" << num << ") >= nb_slot (" << Queue <T>::_nb_slot << ")" << std::endl;
84            exit(1);
85          }
86
87        T val = Queue <T>::_slot [(_ptr_read+num)%Queue <T>::_size]._data;
88       
89        // Reorganize the queue
90
91        for (uint32_t it = 0; it < num; it ++)
92          {
93            uint32_t ptr      = (_ptr_read + num - it   )%Queue <T>::_size;
94            uint32_t _ptr_next = (ptr-1)%Queue <T>::_size;
95
96            Queue <T>::_slot [ptr] = Queue <T>::_slot [_ptr_next];
97          }
98
99        Queue <T>::_nb_slot --;
100        _ptr_read = (_ptr_read+1)%Queue <T>::_size;
101       
102        return val;
103      }
104     
105      // *****[ push ]*****
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
Note: See TracBrowser for help on using the repository browser.