Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c4c5c50

Browse filesBrowse files
authored
Merge pull request #15 from vinitjames/feature/add_reverse_iterators
added reverser iterator and corresponding tests
2 parents 9632ed8 + 24e162a commit c4c5c50
Copy full SHA for c4c5c50

File tree

2 files changed

+122
-2
lines changed
Filter options

2 files changed

+122
-2
lines changed

‎circular_buffer.h

Copy file name to clipboardExpand all lines: circular_buffer.h
+55-1Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ class CircularBuffer {
109109
const_iterator end() const;
110110
const_iterator cbegin() const noexcept;
111111
const_iterator cend() const noexcept;
112+
iterator rbegin() noexcept;
113+
const_iterator rbegin() const noexcept;
114+
iterator rend() noexcept;
115+
const_iterator rend() const noexcept;
116+
112117

113118
private:
114119
void _increment_bufferstate();
@@ -153,7 +158,7 @@ class CircularBuffer {
153158

154159
reference operator*(){
155160
if(_reverse)
156-
return (*_ptrToBuffer)[(_ptrToBuffer->size() - _index)];
161+
return (*_ptrToBuffer)[(_ptrToBuffer->size() - _index - 1)];
157162
return (*_ptrToBuffer)[_index];
158163
}
159164

@@ -484,4 +489,53 @@ typename CircularBuffer<T>::const_iterator CircularBuffer<T>::cend() const noexc
484489
iter._reverse = false;
485490
return iter;
486491
}
492+
493+
template<typename T>
494+
inline
495+
typename CircularBuffer<T>::iterator CircularBuffer<T>::rbegin() noexcept{
496+
std::lock_guard<std::mutex> _lck(_mtx);
497+
iterator iter;
498+
iter._ptrToBuffer = this;
499+
iter._offset = _tail;
500+
iter._index = 0;
501+
iter._reverse = true;
502+
return iter;
503+
}
504+
505+
template<typename T>
506+
inline
507+
typename CircularBuffer<T>::const_iterator CircularBuffer<T>::rbegin() const noexcept{
508+
std::lock_guard<std::mutex> _lck(_mtx);
509+
const_iterator iter;
510+
iter._ptrToBuffer = this;
511+
iter._offset = _tail;
512+
iter._index = 0;
513+
iter._reverse = true;
514+
return iter;
515+
}
516+
517+
template<typename T>
518+
inline
519+
typename CircularBuffer<T>::iterator CircularBuffer<T>::rend() noexcept{
520+
std::lock_guard<std::mutex> _lck(_mtx);
521+
iterator iter;
522+
iter._ptrToBuffer = this;
523+
iter._offset = _tail;
524+
iter._index = _size;
525+
iter._reverse = true;
526+
return iter;
527+
}
528+
529+
template<typename T>
530+
inline
531+
typename CircularBuffer<T>::const_iterator CircularBuffer<T>::rend() const noexcept{
532+
std::lock_guard<std::mutex> _lck(_mtx);
533+
const_iterator iter;
534+
iter._ptrToBuffer = this;
535+
iter._offset = _tail;
536+
iter._index = _size;
537+
iter._reverse = true;
538+
return iter;
539+
}
540+
487541
#endif /* CIRCULAR_BUFFER_H */

‎tests/member_func_test.cpp

Copy file name to clipboardExpand all lines: tests/member_func_test.cpp
+67-1Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,38 @@ TEST_F(CircularBufferTest, BeginIteratorTest){
316316
}
317317
}
318318

319+
TEST_F(CircularBufferTest, RbeginIteratorTest){
320+
//create full buffer
321+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
322+
test_buff.push_back("string" + std::to_string(i));
323+
//test first element with iterator
324+
CircularBuffer<std::string>::iterator it = test_buff.rbegin();
325+
//access with rbegin iterator
326+
for(int i=TEST_BUFFER_SIZE-1; i>=0; i--)
327+
EXPECT_EQ(*(it++), "string" + std::to_string(i));
328+
329+
//access with const begin iterator
330+
CircularBuffer<std::string>::const_iterator const_it = test_buff.rbegin();
331+
for(int i=TEST_BUFFER_SIZE - 1; i>=0; i--)
332+
EXPECT_EQ(*(const_it++), "string" + std::to_string(i));
333+
//test out of bounds
334+
try {
335+
*it = "test_string";
336+
FAIL() << "Expected std::out_of_range error";
337+
}
338+
catch(const std::out_of_range& err){
339+
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
340+
}
341+
342+
try {
343+
std::string out_of_bound = *(const_it);
344+
FAIL() << "Expected std::out_of_range error";
345+
}
346+
catch(const std::out_of_range& err){
347+
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
348+
}
349+
}
350+
319351
TEST_F(CircularBufferTest, CbeginIteratorTest){
320352
//create full buffer
321353
for(int i=0; i<TEST_BUFFER_SIZE; i++)
@@ -334,7 +366,8 @@ TEST_F(CircularBufferTest, CbeginIteratorTest){
334366
catch(const std::out_of_range& err){
335367
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
336368
}
337-
}
369+
}
370+
338371

339372
TEST_F(CircularBufferTest, EndIteratorTest){
340373
//create full buffer
@@ -369,6 +402,39 @@ TEST_F(CircularBufferTest, EndIteratorTest){
369402
}
370403
}
371404

405+
TEST_F(CircularBufferTest, RendIteratorTest){
406+
//create full buffer
407+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
408+
test_buff.push_back("string" + std::to_string(i));
409+
//test first element with iterator
410+
CircularBuffer<std::string>::iterator it = test_buff.rend() - 1;
411+
//access with rbegin iterator
412+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
413+
EXPECT_EQ(*(it--), "string" + std::to_string(i));
414+
415+
//access with const begin iterator
416+
CircularBuffer<std::string>::const_iterator const_it = test_buff.rend() - 1;
417+
for(int i=0; i<TEST_BUFFER_SIZE; i++)
418+
EXPECT_EQ(*(const_it--), "string" + std::to_string(i));
419+
//test out of bounds
420+
try {
421+
*it = "test_string";
422+
FAIL() << "Expected std::out_of_range error";
423+
}
424+
catch(const std::out_of_range& err){
425+
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
426+
}
427+
428+
try {
429+
std::string out_of_bound = *(const_it);
430+
FAIL() << "Expected std::out_of_range error";
431+
}
432+
catch(const std::out_of_range& err){
433+
EXPECT_EQ(err.what(), std::string("Index is out of Range of buffer size"));
434+
}
435+
}
436+
437+
372438
TEST_F(CircularBufferTest, CendIteratorTest){
373439
//create full buffer
374440
for(int i=0; i<TEST_BUFFER_SIZE; i++)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.