3 #ifndef GCP_UTIL_CIRCBUF_H
4 #define GCP_UTIL_CIRCBUF_H
19 #include "gcp/util/common/Exception.h"
35 std::ostream& operator<<(std::ostream& os, const CircBuf<type>& obj);
38 std::ostream& operator<<(std::ostream& os, const Node<type>& obj);
84 CircBuf(
unsigned n=1);
88 CircBuf(
const CircBuf<type>& objToBeCopied);
92 CircBuf(CircBuf<type>& objToBeCopied);
96 void operator=(
const CircBuf<type>& objToBeAssigned);
100 void operator=(CircBuf<type>& objToBeAssigned);
123 unsigned nInBuffer();
127 void resize(
unsigned n);
131 void initialize(
unsigned n);
135 void copy(type* ptr);
139 std::valarray<type> copy();
149 std::valarray<Node<type> > nodes_;
159 template <
class type>
160 CircBuf<type>::CircBuf(
unsigned n)
167 template <
class type>
168 CircBuf<type>::CircBuf(
const CircBuf<type>& objToBeCopied)
170 *
this = (CircBuf<type>&)objToBeCopied;
175 template <
class type>
176 CircBuf<type>::CircBuf(CircBuf<type>& objToBeCopied)
178 *
this = objToBeCopied;
184 template <
class type>
185 void CircBuf<type>::operator=(
const CircBuf<type>& objToBeAssigned)
187 *
this = (CircBuf<type>&)objToBeAssigned;
192 template <
class type>
193 void CircBuf<type>::operator=(CircBuf<type>& objToBeAssigned)
195 std::cout <<
"Calling default assignment operator for class: CircBuf" << std::endl;
200 template <
class type>
201 std::ostream& operator<<(std::ostream& os, CircBuf<type>& obj)
203 COUT(
"Default output operator called");
208 template <
class type>
209 CircBuf<type>::~CircBuf() {};
214 template <
class type>
215 void CircBuf<type>::push(type obj)
218 tail_ = tail_->next_;
221 head_ = head_->next_;
229 template <
class type>
230 type CircBuf<type>::oldest()
235 template <
class type>
236 type CircBuf<type>::newest()
238 return tail_->prev_->node_;
244 unsigned CircBuf<type>::size()
253 unsigned CircBuf<type>::nInBuffer()
261 void CircBuf<type>::resize(
unsigned n)
267 void CircBuf<type>::initialize(
unsigned n)
276 if(nodes_.size() != n+1)
283 for(
unsigned iNode=0; iNode <= n; iNode++) {
284 nodes_[iNode].id_ = iNode;
285 nodes_[iNode].node_ = 0;
286 nodes_[iNode].prev_ = &nodes_[iNode==0 ? n : iNode-1];
287 nodes_[iNode].next_ = &nodes_[iNode==n ? 0 : iNode+1];
306 void CircBuf<type>::copy(type* ptr)
310 for(
Node<type>* iter = head_; iter != tail_; iter = iter->next_, i++)
311 *(ptr+i) = iter->node_;
317 std::valarray<type> CircBuf<type>::copy()
319 std::valarray<type> arr(nInBuf_);
326 template <
class type>
327 std::ostream& operator<<(std::ostream& os,
328 const CircBuf<type>& buf)
330 COUT(
"Calling default output operator for CircBuf");
335 template <
class type>
336 std::ostream& operator<<(std::ostream& os,
339 COUT(node.node_ <<
" " << node.id_);
348 #endif // End #ifndef GCP_UTIL_CIRCBUF_H
Definition: eval_defs.h:51