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 void push_back(kind object
);
20 void pop_front(kind
&object
);
21 void get( int index
, kind
&object
);
22 kind
* get_ref( int index
);
31 template<class kind
, int length
> RingBuffer
<kind
, length
>::RingBuffer(){
32 this->head
= this->tail
= 0;
35 template<class kind
, int length
> int RingBuffer
<kind
, length
>::capacity(){
39 template<class kind
, int length
> int RingBuffer
<kind
, length
>::size(){
40 return((this->head
>this->tail
)?length
:0)+this->tail
-head
;
43 template<class kind
, int length
> void RingBuffer
<kind
, length
>::push_back(kind object
){
44 this->buffer
[this->tail
] = object
;
45 this->tail
= (tail
+1)&(length
-1);
48 template<class kind
, int length
> void RingBuffer
<kind
, length
>::get(int index
, kind
&object
){
51 while (k
!= this->tail
){
52 if (j
== index
) break;
54 k
= (k
+ 1) & (length
- 1);
59 object
= this->buffer
[k
];
63 template<class kind
, int length
> kind
* RingBuffer
<kind
, length
>::get_ref(int index
){
66 while (k
!= this->tail
){
67 if (j
== index
) break;
69 k
= (k
+ 1) & (length
- 1);
74 return &(this->buffer
[k
]);
77 template<class kind
, int length
> void RingBuffer
<kind
, length
>::pop_front(kind
&object
){
78 object
= this->buffer
[this->head
];
79 this->head
= (this->head
+1)&(length
-1);
82 template<class kind
, int length
> void RingBuffer
<kind
, length
>::delete_first(){
84 this->pop_front(dummy
);