123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- #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(this, m_data.data(), size, 0), m_end(this, m_data.data(), size, 0){
-
- }
- 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 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){
- std::vector<char> newdata(size);
- size_t os = m_end - m_begin;
- if(size >= (m_begin - m_end))
- std::copy(m_begin, m_begin, newdata.begin());
- else
- std::copy(m_begin, m_begin + size, newdata.begin());
- std::swap(m_data, newdata);
- 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);
- }
- 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 == 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{
- 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;
- offset = o.offset;
- }
- circular_buffer::const_iterator& circular_buffer::const_iterator::operator=(const circular_buffer::const_iterator& o){
- source = o.source;
- 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){
-
- }
- socket_ostream& socket_ostream::operator<<(char*){
-
- }
- socket_ostream& socket_ostream::operator<<(const std::string&){
-
- }
- socket_ostream& socket_ostream::operator<<(int){
-
- }
|