Assembla home | Assembla project page
 

root/trunk/cbear.berlios.de/range/circular_buffer.hpp

Revision 167, 1.2 kB (checked in by sergey_shandar, 1 year ago)

range: circular buffer.

Line 
1 #ifndef CBEAR_BERLIOS_DE_RANGE_CIRCULAR_BUFFER_HPP_INCLUDED
2 #define CBEAR_BERLIOS_DE_RANGE_CIRCULAR_BUFFER_HPP_INCLUDED
3
4 #include <cbear.berlios.de/range/size_type.hpp>
5
6 namespace cbear_berlios_de
7 {
8 namespace range
9 {
10
11 template<class T, ::std::size_t L>
12 class circular_buffer
13 {
14 public:
15         typedef T value_type;
16         typedef value_type &reference;
17         typedef value_type const &const_reference;
18         typedef ::std::size_t size_type;
19         typedef ::std::ptrdiff_t difference_type;       
20
21         circular_buffer():
22                 b(0),
23                 s(0)
24         {
25         }
26
27         reference at(size_type i)
28         {
29                 return this->x[this->index(i)];
30         }
31
32         const_reference at(size_type i) const
33         {
34                 return this->x[this->index(i)];
35         }
36
37         reference operator[](size_type i)
38         {
39                 return this->at(i);
40         }
41
42         const_reference operator[](size_type i) const
43         {
44                 return this->at(i);
45         }
46
47         void push_back(const_reference v)
48         {
49                 this->at(this->s) = v;
50                 if(this->s < L)
51                 {
52                         ++this->s;
53                 }
54                 else
55                 {
56                         this->b = (this->b + 1) % L;
57                 }
58         }
59
60         size_type size() const
61         {
62                 return this->s;
63         }
64
65         void clear()
66         {
67                 this->b = 0;
68                 this->s = 0;
69         }
70
71 private:
72         value_type x[L];
73         size_type b;
74         size_type s;   
75         size_type index(size_type i) const
76         {
77                 return (this->b + i) % L;
78         }
79 };
80
81 }
82 }
83
84 #endif
85
Note: See TracBrowser for help on using the browser.