2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
4 Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
7 With chucks taken from http://en.wikipedia.org/wiki/Circular_buffer, see licence there also
14 template<class kind
, int length
> class RingBuffer
{
19 int next_block_index(int index
);
20 int prev_block_index(int index
);
21 void push_back(kind object
);
22 void pop_front(kind
&object
);
23 void get( int index
, kind
&object
);
24 kind
* get_ref( int index
);
33 template<class kind
, int length
> RingBuffer
<kind
, length
>::RingBuffer(){
34 this->head
= this->tail
= 0;
37 template<class kind
, int length
> int RingBuffer
<kind
, length
>::capacity(){
41 template<class kind
, int length
> int RingBuffer
<kind
, length
>::size(){
42 return((this->head
>this->tail
)?length
:0)+this->tail
-head
;
45 template<class kind
, int length
> int RingBuffer
<kind
, length
>::next_block_index(int index
){
47 if (index
== length
) { index
= 0; }
51 template<class kind
, int length
> int RingBuffer
<kind
, length
>::prev_block_index(int index
){
52 if (index
== 0) { index
= length
; }
57 template<class kind
, int length
> void RingBuffer
<kind
, length
>::push_back(kind object
){
58 this->buffer
[this->tail
] = object
;
59 this->tail
= (tail
+1)&(length
-1);
62 template<class kind
, int length
> void RingBuffer
<kind
, length
>::get(int index
, kind
&object
){
65 while (k
!= this->tail
){
66 if (j
== index
) break;
68 k
= (k
+ 1) & (length
- 1);
70 // TODO : this checks wether we are asked a value out of range
71 //if (k == this->tail){
74 object
= this->buffer
[k
];
78 template<class kind
, int length
> kind
* RingBuffer
<kind
, length
>::get_ref(int index
){
81 while (k
!= this->tail
){
82 if (j
== index
) break;
84 k
= (k
+ 1) & (length
- 1);
86 // TODO : this checks wether we are asked a value out of range
90 return &(this->buffer
[k
]);
93 template<class kind
, int length
> void RingBuffer
<kind
, length
>::pop_front(kind
&object
){
94 object
= this->buffer
[this->head
];
95 this->head
= (this->head
+1)&(length
-1);
98 template<class kind
, int length
> void RingBuffer
<kind
, length
>::delete_first(){
100 //this->pop_front(dummy);
101 this->head
= (this->head
+1)&(length
-1);