-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbuffer_array.cpp
More file actions
150 lines (133 loc) · 2.79 KB
/
buffer_array.cpp
File metadata and controls
150 lines (133 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/cppalliance/capy
//
#include <boost/capy/buffers/buffer_array.hpp>
namespace boost {
namespace capy {
namespace detail {
namespace {
template<class Buffer>
void
do_remove_prefix(
Buffer* arr,
std::size_t* count,
std::size_t* total_size,
std::size_t n) noexcept
{
if(n >= *total_size)
{
while(*count > 0)
arr[--(*count)].~Buffer();
*total_size = 0;
return;
}
std::size_t i = 0;
while(i < *count && n > 0)
{
auto& b = arr[i];
if(n < b.size())
{
b += n;
*total_size -= n;
break;
}
n -= b.size();
*total_size -= b.size();
b.~Buffer();
++i;
}
// Compact: move remaining buffers to front
if(i > 0)
{
std::size_t j = 0;
while(i < *count)
{
arr[j] = arr[i];
arr[i].~Buffer();
++i;
++j;
}
*count = j;
}
}
template<class Buffer>
void
do_keep_prefix(
Buffer* arr,
std::size_t* count,
std::size_t* total_size,
std::size_t n) noexcept
{
if(n >= *total_size)
return;
if(n == 0)
{
while(*count > 0)
arr[--(*count)].~Buffer();
*total_size = 0;
return;
}
std::size_t remaining = n;
std::size_t i = 0;
while(i < *count && remaining > 0)
{
auto& b = arr[i];
if(remaining < b.size())
{
b = Buffer(b.data(), remaining);
++i;
break;
}
remaining -= b.size();
++i;
}
// Destruct elements beyond the new count
while(*count > i)
arr[--(*count)].~Buffer();
*total_size = n;
}
} // anonymous namespace
void
buffer_array_remove_prefix(
const_buffer* arr,
std::size_t* count,
std::size_t* total_size,
std::size_t n) noexcept
{
do_remove_prefix(arr, count, total_size, n);
}
void
buffer_array_remove_prefix(
mutable_buffer* arr,
std::size_t* count,
std::size_t* total_size,
std::size_t n) noexcept
{
do_remove_prefix(arr, count, total_size, n);
}
void
buffer_array_keep_prefix(
const_buffer* arr,
std::size_t* count,
std::size_t* total_size,
std::size_t n) noexcept
{
do_keep_prefix(arr, count, total_size, n);
}
void
buffer_array_keep_prefix(
mutable_buffer* arr,
std::size_t* count,
std::size_t* total_size,
std::size_t n) noexcept
{
do_keep_prefix(arr, count, total_size, n);
}
} // namespace detail
} // namespace capy
} // namespace boost