|
@@ -1,14 +1,15 @@
|
|
|
#include <siostream.hpp>
|
|
|
+#include <cassert>
|
|
|
#include <algorithm>
|
|
|
using std::size_t;
|
|
|
socket_ostream::socket_ostream() : std::ostream(), buffer(1024){}
|
|
|
-circular_buffer::circular_buffer(size_t size) : m_data(size), m_begin(m_data.data(), size, 0), m_end(m_data.data(), size, 0){
|
|
|
+circular_buffer::circular_buffer(size_t size) : m_data(size), m_begin(this, m_data.data(), size, 0), m_end(this, m_data.data(), size, 0){
|
|
|
|
|
|
}
|
|
|
-circular_buffer::iterator::iterator(char* _source, size_t _period, size_t _offset):source(_source),period(_period), offset(_offset){
|
|
|
+circular_buffer::iterator::iterator(const circular_buffer* p, char* _source, size_t _period, size_t _offset):parent(p), source(_source),period(_period), offset(_offset){
|
|
|
|
|
|
}
|
|
|
-circular_buffer::const_iterator::const_iterator(const char* _source, size_t _period, size_t _offset):source(_source),period(_period), offset(_offset){
|
|
|
+circular_buffer::const_iterator::const_iterator(const circular_buffer* p, const char* _source, size_t _period, size_t _offset):parent(p), source(_source),period(_period), offset(_offset){
|
|
|
|
|
|
}
|
|
|
void circular_buffer::resize(size_t size){
|
|
@@ -19,8 +20,8 @@ void circular_buffer::resize(size_t size){
|
|
|
else
|
|
|
std::copy(m_begin, m_begin + size, newdata.begin());
|
|
|
std::swap(m_data, newdata);
|
|
|
- m_begin = iterator(m_data.data(),size,0);
|
|
|
- m_end = iterator(m_data.data(),size,os);
|
|
|
+ m_begin = iterator(this, m_data.data(),size,0);
|
|
|
+ m_end = iterator(this, m_data.data(),size,os);
|
|
|
}
|
|
|
char& circular_buffer::iterator::operator*()const{
|
|
|
return *(source + offset);
|
|
@@ -29,32 +30,59 @@ const char& circular_buffer::const_iterator::operator*()const {
|
|
|
return *(source + offset);
|
|
|
}
|
|
|
circular_buffer::iterator& circular_buffer::iterator::operator++(){
|
|
|
+ std::cout << period << std::endl;
|
|
|
++offset;
|
|
|
- if(offset == size)offset = 0;
|
|
|
+ if(offset == period)offset = 0;
|
|
|
return *this;
|
|
|
}
|
|
|
circular_buffer::const_iterator& circular_buffer::const_iterator::operator++(){
|
|
|
-
|
|
|
+ ++offset;
|
|
|
+ if(offset == period)offset = 0;
|
|
|
+ return *this;
|
|
|
}
|
|
|
circular_buffer::iterator circular_buffer::iterator::operator+(size_t leap)const{
|
|
|
-
|
|
|
+ return circular_buffer::iterator(parent, source, period, (offset + leap) % period);
|
|
|
+}
|
|
|
+circular_buffer::const_iterator circular_buffer::const_iterator::operator+(size_t leap)const{
|
|
|
+ return circular_buffer::const_iterator(parent, source, period, (offset + leap) % period);
|
|
|
}
|
|
|
-
|
|
|
std::ptrdiff_t circular_buffer::iterator::operator-(const iterator& o)const{
|
|
|
-
|
|
|
+ assert(source == o.source);
|
|
|
+ if(o.offset > offset)
|
|
|
+ return -(o - *this);
|
|
|
+ if(o.offset == offset)return 0;
|
|
|
+ if(parent->m_begin.offset < parent->m_end.offset)
|
|
|
+ return offset - o.offset;
|
|
|
+ return (offset - o.offset) % period;
|
|
|
}
|
|
|
std::ptrdiff_t circular_buffer::iterator::operator-(const const_iterator& o)const{
|
|
|
-
|
|
|
+ assert(source == o.source);
|
|
|
+ if(o.offset > offset)
|
|
|
+ return -(o - *this);
|
|
|
+ if(o.offset == offset)return 0;
|
|
|
+ if(parent->m_begin.offset < parent->m_end.offset)
|
|
|
+ return offset - o.offset;
|
|
|
+ return (offset - o.offset) % period;
|
|
|
}
|
|
|
std::ptrdiff_t circular_buffer::const_iterator::operator-(const iterator& o)const{
|
|
|
-
|
|
|
+ assert(source == o.source);
|
|
|
+ if(o.offset > offset)
|
|
|
+ return -(o - *this);
|
|
|
+ if(o.offset == offset)return 0;
|
|
|
+ if(parent->m_begin.offset < parent->m_end.offset)
|
|
|
+ return offset - o.offset;
|
|
|
+ return (offset - o.offset) % period;
|
|
|
}
|
|
|
std::ptrdiff_t circular_buffer::const_iterator::operator-(const const_iterator& o)const{
|
|
|
-
|
|
|
-}
|
|
|
-circular_buffer::const_iterator circular_buffer::const_iterator::operator+(size_t leap)const{
|
|
|
-
|
|
|
+ assert(source == o.source);
|
|
|
+ if(o.offset > offset)
|
|
|
+ return -(o - *this);
|
|
|
+ if(o.offset == offset)return 0;
|
|
|
+ if(parent->m_begin.offset < parent->m_end.offset)
|
|
|
+ return offset - o.offset;
|
|
|
+ return (offset - o.offset) % period;
|
|
|
}
|
|
|
+
|
|
|
circular_buffer::iterator& circular_buffer::iterator::operator=(const circular_buffer::iterator& o){
|
|
|
source = o.source;
|
|
|
period = o.period;
|
|
@@ -65,17 +93,20 @@ circular_buffer::const_iterator& circular_buffer::const_iterator::operator=(cons
|
|
|
period = o.period;
|
|
|
offset = o.offset;
|
|
|
}
|
|
|
+circular_buffer::iterator::operator const_iterator(){
|
|
|
+ return const_iterator(parent, source, period, offset);
|
|
|
+}
|
|
|
circular_buffer::iterator circular_buffer::begin(){
|
|
|
-
|
|
|
+ return m_begin;
|
|
|
}
|
|
|
circular_buffer::const_iterator circular_buffer::begin() const{
|
|
|
-
|
|
|
+ return circular_buffer::const_iterator(m_begin.parent, m_begin.source, m_begin.period, m_begin.offset);
|
|
|
}
|
|
|
circular_buffer::iterator circular_buffer::end(){
|
|
|
-
|
|
|
+ return m_end;
|
|
|
}
|
|
|
circular_buffer::const_iterator circular_buffer::end()const{
|
|
|
-
|
|
|
+ return circular_buffer::const_iterator(m_end.parent, m_end.source, m_end.period, m_end.offset);
|
|
|
}
|
|
|
socket_ostream& socket_ostream::put(char_type c){
|
|
|
|