100.00% Lines (58/58) 100.00% Functions (6/6)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) 2   // Copyright (c) 2025 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/buffer_array.hpp> 10   #include <boost/capy/buffers/buffer_array.hpp>
11   11  
12   namespace boost { 12   namespace boost {
13   namespace capy { 13   namespace capy {
14   namespace detail { 14   namespace detail {
15   15  
16   namespace { 16   namespace {
17   17  
18   template<class Buffer> 18   template<class Buffer>
19   void 19   void
HITCBC 20   1024 do_remove_prefix( 20   1024 do_remove_prefix(
21   Buffer* arr, 21   Buffer* arr,
22   std::size_t* count, 22   std::size_t* count,
23   std::size_t* total_size, 23   std::size_t* total_size,
24   std::size_t n) noexcept 24   std::size_t n) noexcept
25   { 25   {
HITCBC 26   1024 if(n >= *total_size) 26   1024 if(n >= *total_size)
27   { 27   {
HITCBC 28   276 while(*count > 0) 28   276 while(*count > 0)
HITCBC 29   180 arr[--(*count)].~Buffer(); 29   180 arr[--(*count)].~Buffer();
HITCBC 30   96 *total_size = 0; 30   96 *total_size = 0;
HITCBC 31   96 return; 31   96 return;
32   } 32   }
33   33  
HITCBC 34   928 std::size_t i = 0; 34   928 std::size_t i = 0;
HITCBC 35   1348 while(i < *count && n > 0) 35   1348 while(i < *count && n > 0)
36   { 36   {
HITCBC 37   1260 auto& b = arr[i]; 37   1260 auto& b = arr[i];
HITCBC 38   1260 if(n < b.size()) 38   1260 if(n < b.size())
39   { 39   {
HITCBC 40   840 b += n; 40   840 b += n;
HITCBC 41   840 *total_size -= n; 41   840 *total_size -= n;
HITCBC 42   840 break; 42   840 break;
43   } 43   }
HITCBC 44   420 n -= b.size(); 44   420 n -= b.size();
HITCBC 45   420 *total_size -= b.size(); 45   420 *total_size -= b.size();
HITCBC 46   420 b.~Buffer(); 46   420 b.~Buffer();
HITCBC 47   420 ++i; 47   420 ++i;
48   } 48   }
49   49  
50   // Compact: move remaining buffers to front 50   // Compact: move remaining buffers to front
HITCBC 51   928 if(i > 0) 51   928 if(i > 0)
52   { 52   {
HITCBC 53   420 std::size_t j = 0; 53   420 std::size_t j = 0;
HITCBC 54   840 while(i < *count) 54   840 while(i < *count)
55   { 55   {
HITCBC 56   420 arr[j] = arr[i]; 56   420 arr[j] = arr[i];
HITCBC 57   420 arr[i].~Buffer(); 57   420 arr[i].~Buffer();
HITCBC 58   420 ++i; 58   420 ++i;
HITCBC 59   420 ++j; 59   420 ++j;
60   } 60   }
HITCBC 61   420 *count = j; 61   420 *count = j;
62   } 62   }
63   } 63   }
64   64  
65   template<class Buffer> 65   template<class Buffer>
66   void 66   void
HITCBC 67   1056 do_keep_prefix( 67   1056 do_keep_prefix(
68   Buffer* arr, 68   Buffer* arr,
69   std::size_t* count, 69   std::size_t* count,
70   std::size_t* total_size, 70   std::size_t* total_size,
71   std::size_t n) noexcept 71   std::size_t n) noexcept
72   { 72   {
HITCBC 73   1056 if(n >= *total_size) 73   1056 if(n >= *total_size)
HITCBC 74   64 return; 74   64 return;
75   75  
HITCBC 76   992 if(n == 0) 76   992 if(n == 0)
77   { 77   {
HITCBC 78   276 while(*count > 0) 78   276 while(*count > 0)
HITCBC 79   180 arr[--(*count)].~Buffer(); 79   180 arr[--(*count)].~Buffer();
HITCBC 80   96 *total_size = 0; 80   96 *total_size = 0;
HITCBC 81   96 return; 81   96 return;
82   } 82   }
83   83  
HITCBC 84   896 std::size_t remaining = n; 84   896 std::size_t remaining = n;
HITCBC 85   896 std::size_t i = 0; 85   896 std::size_t i = 0;
HITCBC 86   1316 while(i < *count && remaining > 0) 86   1316 while(i < *count && remaining > 0)
87   { 87   {
HITCBC 88   1260 auto& b = arr[i]; 88   1260 auto& b = arr[i];
HITCBC 89   1260 if(remaining < b.size()) 89   1260 if(remaining < b.size())
90   { 90   {
HITCBC 91   840 b = Buffer(b.data(), remaining); 91   840 b = Buffer(b.data(), remaining);
HITCBC 92   840 ++i; 92   840 ++i;
HITCBC 93   840 break; 93   840 break;
94   } 94   }
HITCBC 95   420 remaining -= b.size(); 95   420 remaining -= b.size();
HITCBC 96   420 ++i; 96   420 ++i;
97   } 97   }
98   98  
99   // Destruct elements beyond the new count 99   // Destruct elements beyond the new count
HITCBC 100   1316 while(*count > i) 100   1316 while(*count > i)
HITCBC 101   420 arr[--(*count)].~Buffer(); 101   420 arr[--(*count)].~Buffer();
102   102  
HITCBC 103   896 *total_size = n; 103   896 *total_size = n;
104   } 104   }
105   105  
106   } // anonymous namespace 106   } // anonymous namespace
107   107  
108   void 108   void
HITCBC 109   512 buffer_array_remove_prefix( 109   512 buffer_array_remove_prefix(
110   const_buffer* arr, 110   const_buffer* arr,
111   std::size_t* count, 111   std::size_t* count,
112   std::size_t* total_size, 112   std::size_t* total_size,
113   std::size_t n) noexcept 113   std::size_t n) noexcept
114   { 114   {
HITCBC 115   512 do_remove_prefix(arr, count, total_size, n); 115   512 do_remove_prefix(arr, count, total_size, n);
HITCBC 116   512 } 116   512 }
117   117  
118   void 118   void
HITCBC 119   512 buffer_array_remove_prefix( 119   512 buffer_array_remove_prefix(
120   mutable_buffer* arr, 120   mutable_buffer* arr,
121   std::size_t* count, 121   std::size_t* count,
122   std::size_t* total_size, 122   std::size_t* total_size,
123   std::size_t n) noexcept 123   std::size_t n) noexcept
124   { 124   {
HITCBC 125   512 do_remove_prefix(arr, count, total_size, n); 125   512 do_remove_prefix(arr, count, total_size, n);
HITCBC 126   512 } 126   512 }
127   127  
128   void 128   void
HITCBC 129   528 buffer_array_keep_prefix( 129   528 buffer_array_keep_prefix(
130   const_buffer* arr, 130   const_buffer* arr,
131   std::size_t* count, 131   std::size_t* count,
132   std::size_t* total_size, 132   std::size_t* total_size,
133   std::size_t n) noexcept 133   std::size_t n) noexcept
134   { 134   {
HITCBC 135   528 do_keep_prefix(arr, count, total_size, n); 135   528 do_keep_prefix(arr, count, total_size, n);
HITCBC 136   528 } 136   528 }
137   137  
138   void 138   void
HITCBC 139   528 buffer_array_keep_prefix( 139   528 buffer_array_keep_prefix(
140   mutable_buffer* arr, 140   mutable_buffer* arr,
141   std::size_t* count, 141   std::size_t* count,
142   std::size_t* total_size, 142   std::size_t* total_size,
143   std::size_t n) noexcept 143   std::size_t n) noexcept
144   { 144   {
HITCBC 145   528 do_keep_prefix(arr, count, total_size, n); 145   528 do_keep_prefix(arr, count, total_size, n);
HITCBC 146   528 } 146   528 }
147   147  
148   } // namespace detail 148   } // namespace detail
149   } // namespace capy 149   } // namespace capy
150   } // namespace boost 150   } // namespace boost