100.00% Lines (33/33) 100.00% Functions (4/4)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com) 2   // Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
3   // 3   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 4   // Distributed under the Boost Software License, Version 1.0. (See accompanying
5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 6   //
7   // Official repository: https://github.com/cppalliance/capy 7   // Official repository: https://github.com/cppalliance/capy
8   // 8   //
9   9  
10   #include <boost/capy/buffers/circular_dynamic_buffer.hpp> 10   #include <boost/capy/buffers/circular_dynamic_buffer.hpp>
11   #include <boost/capy/detail/except.hpp> 11   #include <boost/capy/detail/except.hpp>
12   12  
13   namespace boost { 13   namespace boost {
14   namespace capy { 14   namespace capy {
15   15  
16   auto 16   auto
HITCBC 17   1514 circular_dynamic_buffer:: 17   1514 circular_dynamic_buffer::
18   data() const noexcept -> 18   data() const noexcept ->
19   const_buffers_type 19   const_buffers_type
20   { 20   {
HITCBC 21   1514 if(in_pos_ + in_len_ <= cap_) 21   1514 if(in_pos_ + in_len_ <= cap_)
22   return {{ 22   return {{
HITCBC 23   1001 const_buffer{ base_ + in_pos_, in_len_ }, 23   1001 const_buffer{ base_ + in_pos_, in_len_ },
HITCBC 24   1001 const_buffer{ base_, 0} }}; 24   1001 const_buffer{ base_, 0} }};
25   return {{ 25   return {{
HITCBC 26   513 const_buffer{ base_ + in_pos_, cap_ - in_pos_}, 26   513 const_buffer{ base_ + in_pos_, cap_ - in_pos_},
HITCBC 27   513 const_buffer{ base_, in_len_- (cap_ - in_pos_)} }}; 27   513 const_buffer{ base_, in_len_- (cap_ - in_pos_)} }};
28   } 28   }
29   29  
30   auto 30   auto
HITCBC 31   866 circular_dynamic_buffer:: 31   866 circular_dynamic_buffer::
32   prepare(std::size_t n) -> 32   prepare(std::size_t n) ->
33   mutable_buffers_type 33   mutable_buffers_type
34   { 34   {
35   // Buffer is too small for n 35   // Buffer is too small for n
HITCBC 36   866 if(n > cap_ - in_len_) 36   866 if(n > cap_ - in_len_)
HITCBC 37   3 detail::throw_length_error(); 37   3 detail::throw_length_error();
38   38  
HITCBC 39   863 out_size_ = n; 39   863 out_size_ = n;
HITCBC 40   863 auto const pos = ( 40   863 auto const pos = (
HITCBC 41   863 in_pos_ + in_len_) % cap_; 41   863 in_pos_ + in_len_) % cap_;
HITCBC 42   863 if(pos + n <= cap_) 42   863 if(pos + n <= cap_)
43   return {{ 43   return {{
HITCBC 44   731 mutable_buffer{ base_ + pos, n }, 44   731 mutable_buffer{ base_ + pos, n },
HITCBC 45   731 mutable_buffer{ base_, 0 } }}; 45   731 mutable_buffer{ base_, 0 } }};
46   return {{ 46   return {{
HITCBC 47   132 mutable_buffer{ base_ + pos, cap_ - pos }, 47   132 mutable_buffer{ base_ + pos, cap_ - pos },
HITCBC 48   132 mutable_buffer{ base_, n - (cap_ - pos) } }}; 48   132 mutable_buffer{ base_, n - (cap_ - pos) } }};
49   } 49   }
50   50  
51   void 51   void
HITCBC 52   823 circular_dynamic_buffer:: 52   823 circular_dynamic_buffer::
53   commit( 53   commit(
54   std::size_t n) noexcept 54   std::size_t n) noexcept
55   { 55   {
HITCBC 56   823 if(n < out_size_) 56   823 if(n < out_size_)
HITCBC 57   52 in_len_ += n; 57   52 in_len_ += n;
58   else 58   else
HITCBC 59   771 in_len_ += out_size_; 59   771 in_len_ += out_size_;
HITCBC 60   823 out_size_ = 0; 60   823 out_size_ = 0;
HITCBC 61   823 } 61   823 }
62   62  
63   void 63   void
HITCBC 64   809 circular_dynamic_buffer:: 64   809 circular_dynamic_buffer::
65   consume( 65   consume(
66   std::size_t n) noexcept 66   std::size_t n) noexcept
67   { 67   {
HITCBC 68   809 if(n < in_len_) 68   809 if(n < in_len_)
69   { 69   {
HITCBC 70   555 in_pos_ = (in_pos_ + n) % cap_; 70   555 in_pos_ = (in_pos_ + n) % cap_;
HITCBC 71   555 in_len_ -= n; 71   555 in_len_ -= n;
72   } 72   }
73   else 73   else
74   { 74   {
75   // preserve in_pos_ if there is 75   // preserve in_pos_ if there is
76   // a prepared buffer 76   // a prepared buffer
HITCBC 77   254 if(out_size_ != 0) 77   254 if(out_size_ != 0)
78   { 78   {
HITCBC 79   1 in_pos_ = (in_pos_ + in_len_) % cap_; 79   1 in_pos_ = (in_pos_ + in_len_) % cap_;
HITCBC 80   1 in_len_ = 0; 80   1 in_len_ = 0;
81   } 81   }
82   else 82   else
83   { 83   {
84   // make prepare return a 84   // make prepare return a
85   // bigger single buffer 85   // bigger single buffer
HITCBC 86   253 in_pos_ = 0; 86   253 in_pos_ = 0;
HITCBC 87   253 in_len_ = 0; 87   253 in_len_ = 0;
88   } 88   }
89   } 89   }
HITCBC 90   809 } 90   809 }
91   91  
92   } // capy 92   } // capy
93   } // boost 93   } // boost