📄 circularbuffer.h
字号:
if ((offset_a > 0 && offset_a > getNumForward()) || (offset_a < 0 && offset_a < (-1 * getNumBackward()))) { Error::handle(name(), L"operator()", Error::ARG, __FILE__, __LINE__); } // get the element // long index = curr_d + offset_a; long capacity = getCapacity(); while (index > (capacity - 1)) { index -= capacity; } while (index < 0) { index += capacity; } // return the value // return v_d(index);}//---------------------------------------------------------------------------//// class-specific public methods:// get methods////--------------------------------------------------------------------------- // method: getElement// // arguments:// TObject& out: (output) the output data// long offset: (input) the offset with respect to curr_d in circular buffer//// return: a boolean value indicating status//// this method gets data at certain position from the circular buffer//template<class TObject>boolean CircularBuffer<TObject>::getElement(TObject& out_a, long offset_a) const { if ((offset_a > 0 && offset_a > getNumForward()) || (offset_a < 0 && offset_a < (-1 * getNumBackward()))) { return Error::handle(name(), L"getElement", Error::ARG, __FILE__, __LINE__); } // get the element // long index = curr_d + offset_a; long capacity = getCapacity(); while (index > capacity -1) { index -= capacity; } while (index < 0) { index += capacity; } // assign the value // out_a.assign(v_d(index)); // exit gracefully // return true;}// method: getElement// // arguments:// Vector<TObject>& out: (output) the output data// long num_elem: (input) the number of elements to get// long offset: (input) the offset with respect to curr_d in circular buffer//// return: a boolean value indicating status//// this method gets data at certain position from the circular buffer//template<class TObject>boolean CircularBuffer<TObject>::getElement(Vector<TObject>& out_a, long num_elem_a, long offset_a) const { // local variables // TObject obj; // set the length of vector // out_a.setLength(num_elem_a); // get each element and assign it to the output vector // for (long i = 0; i < num_elem_a; i++) { getElement(out_a(i), offset_a); offset_a++; } // exit gracefully // return true;}// method: getNumElements// // arguments: none// // return: a long number //// this method gets the number of valid elements of the circular buffer//template<class TObject>long CircularBuffer<TObject>::getNumElements() const { // local variables // long capacity = getCapacity(); // check the position of the read_d and write_d // if (isEmpty()) { return 0; } long dist = (write_d - read_d + capacity + 1) % capacity; if (dist == 0) { return capacity; } // return the number // return dist;}// method: getNumForward// // arguments: none// // return: a long number //// this method gets the number of valid elements in front of curr_d (not// including itself) //template<class TObject>long CircularBuffer<TObject>::getNumForward() const { // local variables // long num = 0; if (isEmpty()) { num = 0; } else if (curr_d == read_d) { num = getNumElements() - 1; } else { long capacity = getCapacity(); num = (write_d - curr_d + capacity) % capacity; } // return the number // return num;}// method: getNumBackward// // arguments: none// // return: a long number //// this method gets the number of valid elements lag of curr_d (not including// itself) //template<class TObject>long CircularBuffer<TObject>::getNumBackward() const { // local variables // long num = 0; if (isEmpty()) { num = 0; } else if (curr_d == write_d) { num = getNumElements() - 1; } else { long capacity = getCapacity(); num = (curr_d - read_d + capacity) % capacity; } // return the number // return num;}//---------------------------------------------------------------------------//// class-specific public methods:// class property methods////---------------------------------------------------------------------------// method: isFull// // arguments: none//// return: a boolean value indicating status//// this method check if the circular buffer is full//template<class TObject>boolean CircularBuffer<TObject>::isFull() const { // check if it's empty // if (isEmpty()) { return false; } // check the position of the read_d and write_d // long capacity = getCapacity(); return (((write_d - read_d + capacity + 1) % capacity) == 0);}// method: isEmpty// // arguments: none//// return: a boolean value indicating status//// this method check if the circular buffer is empty//template<class TObject>boolean CircularBuffer<TObject>::isEmpty() const { // check if it's empty // boolean ret = (write_d == DEF_WRITE_IDX); ret &= (read_d == DEF_READ_IDX); return ret;}//---------------------------------------------------------------------------//// class-specific public methods:// data manipulation methods////---------------------------------------------------------------------------// method: append// // arguments:// TObject& in: (input) the data to add//// return: a boolean value indicating status//// this method appends new data to the circular buffer//template<class TObject>boolean CircularBuffer<TObject>::append(const TObject& in_a) { // check now if the buffer is writable // if (isFull()) { return Error::handle(name(), L"append", ERR_FULL, __FILE__, __LINE__); } // advance the write_d and add the data // write_d = ((long)write_d + 1) % getCapacity(); v_d(write_d).assign(in_a); // exit gracefully // return true;}// method: prepend// // arguments:// TObject& in: (input) the data to add//// return: a boolean value indicating status//// this method appends new data to the circular buffer//template<class TObject>boolean CircularBuffer<TObject>::prepend(const TObject& in_a) { // check if number of elements to be appended are equal to the capacity // long cap = getCapacity(); long numele = getNumElements(); if (numele == (cap - 1)) { return Error::handle(name(), L"prepend-> no. of elements to be prepend equals to the capacity", ERR_FULL, __FILE__, __LINE__); } // check now if the buffer is writable // if (isFull()) { return Error::handle(name(), L"prepend", ERR_FULL, __FILE__, __LINE__); } // decrement read_d and add the data // read_d = (long)read_d - 1; if ((long)read_d < 0) { read_d += getCapacity(); } v_d(read_d).assign(in_a); // exit gracefully // return true;}// method: append// // arguments:// Vector<TObject> in: (input) the data to add//// return: a boolean value indicating status//// this method appends new data to the circular buffer//template<class TObject>boolean CircularBuffer<TObject>::append(const Vector<TObject>& in_a) { // get the number of elements of the input vector // long num_elem = in_a.length(); // append each element // append(in_a, num_elem, (long)0); // exit gracefully // return true;}// method: prepend// // arguments:// Vector<TObject> in: (input) the data to add//// return: a boolean value indicating status//// this method prepends new data to the circular buffer//template<class TObject>boolean CircularBuffer<TObject>::prepend(const Vector<TObject>& in_a) { // get the number of elements of the input vector // long num_elem = in_a.length(); // append each element // prepend(in_a, num_elem, (long)0); // exit gracefully // return true;}// method: append// // arguments:// Vector<TObject> in: (input) the data to add// long num: (input) the number of element to add// long invec_offset: (input) the start index in the input vector//// return: a boolean value indicating status//// this method appends new data to the circular buffer//template<class TObject>boolean CircularBuffer<TObject>::append(const Vector<TObject>& in_a, long num_a, long invec_offset_a) { // get the number of elements of the input vector // long num_elem = in_a.length(); if (num_a + invec_offset_a > num_elem) { num_a = num_elem - invec_offset_a; } // append each element // for (long i = 0; i < num_a; i++) { append(in_a(invec_offset_a + i)); } // exit gracefully // return true;}// method: prepend// // arguments:// Vector<TObject> in: (input) the data to add// long num: (input) the number of element to add// long invec_offset: (input) the start index in the input vector//// return: a boolean value indicating status//// this method prepends new data to the circular buffer//template<class TObject>boolean CircularBuffer<TObject>::prepend(const Vector<TObject>& in_a, long num_a, long invec_offset_a) { // get the number of elements of the input vector // long num_elem = in_a.length(); if (num_a + invec_offset_a > num_elem) { num_a = num_elem - invec_offset_a; } // append each element // for (long i = 0; i < num_a; i++) { prepend(in_a(invec_offset_a + i)); } // exit gracefully // return true;}//---------------------------------------------------------------------------//// class-specific public methods:// positioning methods////---------------------------------------------------------------------------// method: seekCurr// // arguments:// long offset: (input) the offset with respect to curr_d in circular buffer//// return: a boolean value indicating status//// this method moves the curr_d //template<class TObject>boolean CircularBuffer<TObject>::seekCurr(long offset_a) { long capacity = getCapacity(); // curr_d should reside between read_d and write_d // if ((offset_a > 0 && offset_a > getNumForward()) || (offset_a < 0 && offset_a < (-1* getNumBackward()))) { return Error::handle(name(), L"seekCurr", Error::ARG, __FILE__, __LINE__); } // compute // curr_d += offset_a; if (curr_d > capacity - 1) { curr_d -= capacity; } else if ((long)curr_d < 0) { curr_d += capacity; } // exit gracefully // return true;}// method: setRead// // arguments:// long num_read: (input) the numbers to move the read_d forward//// return: a boolean value indicating status//// this method moves the read_d//template<class TObject>boolean CircularBuffer<TObject>::setRead(long num_read_a) { // read_d can't move backward // if (num_read_a < 0) { return Error::handle(name(), L"setRead", Error::ARG, __FILE__, __LINE__); } if (getNumElements() == num_read_a) { // reset the circular buffer to be empty // clear(Integral::RESET); } else { // get capacity // long capacity = getCapacity(); // compute // long tmp_read_idx = read_d + num_read_a; if (tmp_read_idx > capacity - 1) { tmp_read_idx -= capacity; } // move curr_d if necessary to make sure it is still in the proper range // if (getNumBackward() < num_read_a) { curr_d = tmp_read_idx; } // move the read_d // read_d = tmp_read_idx; } // exit gracefully // return true;}// end of include file//#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -