Skip to content

Commit 7ec679d

Browse files
committed
Use segudo instead of reference parameters
1 parent a12422b commit 7ec679d

10 files changed

Lines changed: 262 additions & 322 deletions

include/boost/container/experimental/segmented_copy.hpp

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ namespace detail_algo {
3434
//////////////////////////////////////////////////////////////////////////////
3535
// Bounded destination helper: copies from source into [dst_first, dst_last),
3636
// stopping when source or destination is exhausted.
37-
// Advances first_out (by reference) so the caller knows how far we got.
38-
// Recursively walks destination segments when dst is segmented.
37+
// Returns segduo<SrcIter, DstIter> with the final positions of
38+
// both iterators. Recursively walks destination segments when dst is
39+
// segmented.
3940
//
4041
// When dst_last is unreachable_sentinel_t the destination-full check
4142
// is optimised away, giving the same code as an unbounded loop.
@@ -44,12 +45,11 @@ namespace detail_algo {
4445
#if defined(BOOST_CONTAINER_SEGMENTED_LOOP_UNROLLING)
4546

4647
template <class RASrcIter, class DstIter, class DstSent>
47-
DstIter segmented_copy_dst_bounded
48-
(RASrcIter& first_out, RASrcIter last, DstIter dst_first, DstSent dst_last,
48+
segduo<RASrcIter, DstIter> segmented_copy_dst_bounded
49+
(RASrcIter first, RASrcIter last, DstIter dst_first, DstSent dst_last,
4950
const non_segmented_iterator_tag &, const std::random_access_iterator_tag &)
5051
{
5152
typedef typename iterator_traits<RASrcIter>::difference_type difference_type;
52-
RASrcIter first = first_out;
5353

5454
difference_type n = last - first;
5555

@@ -75,33 +75,29 @@ DstIter segmented_copy_dst_bounded
7575
break;
7676
}
7777
out_path:
78-
first_out = first;
79-
return dst_first;
78+
return segduo<RASrcIter, DstIter>(first, dst_first);
8079
}
8180

8281
#endif //BOOST_CONTAINER_SEGMENTED_LOOP_UNROLLING
8382

8483
template <class SrcIter, class Sent, class DstIter, class DstSent, class DstTag, class SrcCat>
85-
typename algo_enable_if_c<!DstTag::value, DstIter>::type
84+
typename algo_enable_if_c<!DstTag::value, segduo<SrcIter, DstIter> >::type
8685
segmented_copy_dst_bounded
87-
(SrcIter& first_out, Sent last, DstIter dst_first, DstSent dst_last, DstTag, SrcCat)
86+
(SrcIter first, Sent last, DstIter dst_first, DstSent dst_last, DstTag, SrcCat)
8887
{
89-
SrcIter first = first_out;
90-
9188
for(; first != last; ++first) {
9289
if(dst_first == dst_last)
9390
goto out_path;
9491
*dst_first = *first;
9592
++dst_first;
9693
}
9794
out_path:
98-
first_out = first;
99-
return dst_first;
95+
return segduo<SrcIter, DstIter>(first, dst_first);
10096
}
10197

10298
template <class SrcIter, class Sent, class SegDstIter, class SrcCat>
103-
SegDstIter segmented_copy_dst_bounded
104-
(SrcIter& first, Sent last, SegDstIter dst_first, SegDstIter dst_last,
99+
segduo<SrcIter, SegDstIter> segmented_copy_dst_bounded
100+
(SrcIter first, Sent last, SegDstIter dst_first, SegDstIter dst_last,
105101
segmented_iterator_tag, SrcCat)
106102
{
107103
typedef segmented_iterator_traits<SegDstIter> dst_traits;
@@ -113,26 +109,28 @@ SegDstIter segmented_copy_dst_bounded
113109
const dst_segment_iterator slast = dst_traits::segment(dst_last);
114110

115111
if(sfirst == slast) {
116-
dst_local_iterator r = (segmented_copy_dst_bounded)
112+
segduo<SrcIter, dst_local_iterator> r = (segmented_copy_dst_bounded)
117113
(first, last, dst_traits::local(dst_first), dst_traits::local(dst_last), dst_is_local_seg_t(), SrcCat());
118-
return dst_traits::compose(sfirst, r);
114+
return segduo<SrcIter, SegDstIter>(r.first, dst_traits::compose(sfirst, r.second));
119115
}
120116
else {
121-
dst_local_iterator dst_local = (segmented_copy_dst_bounded)
117+
segduo<SrcIter, dst_local_iterator> r = (segmented_copy_dst_bounded)
122118
(first, last, dst_traits::local(dst_first), dst_traits::end(sfirst), dst_is_local_seg_t(), SrcCat());
119+
first = r.first;
123120
if(first == last)
124-
return dst_traits::compose(sfirst, dst_local);
121+
return segduo<SrcIter, SegDstIter>(first, dst_traits::compose(sfirst, r.second));
125122

126123
for(++sfirst; sfirst != slast; ++sfirst) {
127-
dst_local = (segmented_copy_dst_bounded)
124+
r = (segmented_copy_dst_bounded)
128125
(first, last, dst_traits::begin(sfirst), dst_traits::end(sfirst), dst_is_local_seg_t(), SrcCat());
126+
first = r.first;
129127
if(first == last)
130-
return dst_traits::compose(sfirst, dst_local);
128+
return segduo<SrcIter, SegDstIter>(first, dst_traits::compose(sfirst, r.second));
131129
}
132130

133-
dst_local = (segmented_copy_dst_bounded)
131+
r = (segmented_copy_dst_bounded)
134132
(first, last, dst_traits::begin(slast), dst_traits::local(dst_last), dst_is_local_seg_t(), SrcCat());
135-
return dst_traits::compose(sfirst, dst_local);
133+
return segduo<SrcIter, SegDstIter>(r.first, dst_traits::compose(sfirst, r.second));
136134
}
137135
}
138136

@@ -148,7 +146,7 @@ BOOST_CONTAINER_FORCEINLINE DstIter segmented_copy_dst_dispatch
148146
const non_segmented_iterator_tag &, Cat)
149147
{
150148
return (segmented_copy_dst_bounded)
151-
(first, last, result, unreachable_sentinel_t(), non_segmented_iterator_tag(), Cat());
149+
(first, last, result, unreachable_sentinel_t(), non_segmented_iterator_tag(), Cat()).second;
152150
}
153151

154152
template <class SrcIter, class Sent, class SegDstIter, class Cat>
@@ -168,13 +166,16 @@ SegDstIter segmented_copy_dst_dispatch
168166
dst_local_iterator dst_local = dst_traits::local(result);
169167

170168
while(first != last) {
171-
dst_local_iterator dst_end = dst_traits::end(dst_seg);
172-
dst_local = (segmented_copy_dst_bounded)
169+
const dst_local_iterator dst_end = dst_traits::end(dst_seg);
170+
const segduo<SrcIter, dst_local_iterator> r = (segmented_copy_dst_bounded)
173171
(first, last, dst_local, dst_end, dst_is_local_seg_t(), Cat());
172+
first = r.first;
174173
if(first != last) {
175174
++dst_seg;
176175
dst_local = dst_traits::begin(dst_seg);
177176
}
177+
else
178+
dst_local = r.second;
178179
}
179180
return dst_traits::compose(dst_seg, dst_local);
180181
}

include/boost/container/experimental/segmented_copy_n.hpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ namespace detail_algo {
3535
//////////////////////////////////////////////////////////////////////////////
3636
// Bounded destination helper: copies from source count elements into
3737
// [dst_first, dst_last), stopping when count reaches zero or destination
38-
// is full. Advances first_out and count (by reference) so the caller
39-
// knows how far we got.
38+
// is full. Returns segduo<SrcIter, DstIter> with the final positions of
39+
// both iterators. count is advanced by reference so the caller knows
40+
// how many elements remain.
4041
// Recursively walks destination segments when dst is segmented.
4142
//
4243
// When dst_last is unreachable_sentinel_t the destination-full check
@@ -46,12 +47,10 @@ namespace detail_algo {
4647
#if defined(BOOST_CONTAINER_SEGMENTED_LOOP_UNROLLING)
4748

4849
template <class RASrcIter, class Size, class DstIter, class DstSent>
49-
DstIter segmented_copy_n_dst_bounded
50-
(RASrcIter& first_out, Size& count, DstIter dst_first, DstSent dst_last,
50+
segduo<RASrcIter, DstIter> segmented_copy_n_dst_bounded
51+
(RASrcIter first, Size& count, DstIter dst_first, DstSent dst_last,
5152
const non_segmented_iterator_tag &, const std::random_access_iterator_tag &)
5253
{
53-
RASrcIter first = first_out;
54-
5554
while(count >= Size(4)) {
5655
if(dst_first == dst_last) goto out_path; *dst_first = *first; ++first; ++dst_first; --count;
5756
if(dst_first == dst_last) goto out_path; *dst_first = *first; ++first; ++dst_first; --count;
@@ -73,33 +72,29 @@ DstIter segmented_copy_n_dst_bounded
7372
break;
7473
}
7574
out_path:
76-
first_out = first;
77-
return dst_first;
75+
return segduo<RASrcIter, DstIter>(first, dst_first);
7876
}
7977

8078
#endif //BOOST_CONTAINER_SEGMENTED_LOOP_UNROLLING
8179

8280
template <class SrcIter, class Size, class DstIter, class DstSent, class DstTag, class SrcCat>
83-
typename algo_enable_if_c<!DstTag::value, DstIter>::type
81+
typename algo_enable_if_c<!DstTag::value, segduo<SrcIter, DstIter> >::type
8482
segmented_copy_n_dst_bounded
85-
(SrcIter& first_out, Size& count, DstIter dst_first, DstSent dst_last, DstTag, SrcCat)
83+
(SrcIter first, Size& count, DstIter dst_first, DstSent dst_last, DstTag, SrcCat)
8684
{
87-
SrcIter first = first_out;
88-
8985
for(; count > 0; ++first, --count) {
9086
if(dst_first == dst_last)
9187
goto out_path;
9288
*dst_first = *first;
9389
++dst_first;
9490
}
9591
out_path:
96-
first_out = first;
97-
return dst_first;
92+
return segduo<SrcIter, DstIter>(first, dst_first);
9893
}
9994

10095
template <class SrcIter, class Size, class SegDstIter, class SrcCat>
101-
SegDstIter segmented_copy_n_dst_bounded
102-
(SrcIter& first, Size& count, SegDstIter dst_first, SegDstIter dst_last,
96+
segduo<SrcIter, SegDstIter> segmented_copy_n_dst_bounded
97+
(SrcIter first, Size& count, SegDstIter dst_first, SegDstIter dst_last,
10398
segmented_iterator_tag, SrcCat)
10499
{
105100
typedef segmented_iterator_traits<SegDstIter> dst_traits;
@@ -111,26 +106,28 @@ SegDstIter segmented_copy_n_dst_bounded
111106
const dst_segment_iterator slast = dst_traits::segment(dst_last);
112107

113108
if(sfirst == slast) {
114-
dst_local_iterator r = (segmented_copy_n_dst_bounded)
109+
segduo<SrcIter, dst_local_iterator> r = (segmented_copy_n_dst_bounded)
115110
(first, count, dst_traits::local(dst_first), dst_traits::local(dst_last), dst_is_local_seg_t(), SrcCat());
116-
return dst_traits::compose(sfirst, r);
111+
return segduo<SrcIter, SegDstIter>(r.first, dst_traits::compose(sfirst, r.second));
117112
}
118113
else {
119-
dst_local_iterator dst_local = (segmented_copy_n_dst_bounded)
114+
segduo<SrcIter, dst_local_iterator> r = (segmented_copy_n_dst_bounded)
120115
(first, count, dst_traits::local(dst_first), dst_traits::end(sfirst), dst_is_local_seg_t(), SrcCat());
116+
first = r.first;
121117
if(count == 0)
122-
return dst_traits::compose(sfirst, dst_local);
118+
return segduo<SrcIter, SegDstIter>(first, dst_traits::compose(sfirst, r.second));
123119

124120
for(++sfirst; sfirst != slast; ++sfirst) {
125-
dst_local = (segmented_copy_n_dst_bounded)
121+
r = (segmented_copy_n_dst_bounded)
126122
(first, count, dst_traits::begin(sfirst), dst_traits::end(sfirst), dst_is_local_seg_t(), SrcCat());
123+
first = r.first;
127124
if(count == 0)
128-
return dst_traits::compose(sfirst, dst_local);
125+
return segduo<SrcIter, SegDstIter>(first, dst_traits::compose(sfirst, r.second));
129126
}
130127

131-
dst_local = (segmented_copy_n_dst_bounded)
128+
r = (segmented_copy_n_dst_bounded)
132129
(first, count, dst_traits::begin(slast), dst_traits::local(dst_last), dst_is_local_seg_t(), SrcCat());
133-
return dst_traits::compose(sfirst, dst_local);
130+
return segduo<SrcIter, SegDstIter>(r.first, dst_traits::compose(sfirst, r.second));
134131
}
135132
}
136133

@@ -146,7 +143,7 @@ BOOST_CONTAINER_FORCEINLINE DstIter segmented_copy_n_dst_dispatch
146143
const non_segmented_iterator_tag &, Cat)
147144
{
148145
return (segmented_copy_n_dst_bounded)
149-
(first, count, result, unreachable_sentinel_t(), non_segmented_iterator_tag(), Cat());
146+
(first, count, result, unreachable_sentinel_t(), non_segmented_iterator_tag(), Cat()).second;
150147
}
151148

152149
template <class SrcIter, class Size, class SegDstIter, class Cat>
@@ -166,13 +163,16 @@ SegDstIter segmented_copy_n_dst_dispatch
166163
dst_local_iterator dst_local = dst_traits::local(result);
167164

168165
while(count > 0) {
169-
dst_local_iterator dst_end = dst_traits::end(dst_seg);
170-
dst_local = (segmented_copy_n_dst_bounded)
166+
const dst_local_iterator dst_end = dst_traits::end(dst_seg);
167+
const segduo<SrcIter, dst_local_iterator> r = (segmented_copy_n_dst_bounded)
171168
(first, count, dst_local, dst_end, dst_is_local_seg_t(), Cat());
169+
first = r.first;
172170
if(count > 0) {
173171
++dst_seg;
174172
dst_local = dst_traits::begin(dst_seg);
175173
}
174+
else
175+
dst_local = r.second;
176176
}
177177
return dst_traits::compose(dst_seg, dst_local);
178178
}

0 commit comments

Comments
 (0)