Skip to content

Commit 46cc95f

Browse files
committed
Formatting fixups.
1 parent c44c2a2 commit 46cc95f

File tree

4 files changed

+131
-133
lines changed

4 files changed

+131
-133
lines changed

libcxx/benchmarks/algorithms/set_intersection.bench.cpp

Lines changed: 101 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,14 @@ using AllContainerTypes = std::tuple<VectorContainer, SetContainer, ForwardListC
4242

4343
// set_intersection performance may depend on where matching values lie
4444
enum class OverlapPosition {
45-
Nowhere,
46-
Front,
47-
Back,
48-
Interlaced,
45+
None,
46+
Front,
47+
Back,
48+
Interlaced,
4949
};
5050

5151
struct AllOverlapPositions : EnumValuesAsTuple<AllOverlapPositions, OverlapPosition, 4> {
52-
static constexpr const char* Names[] = {
53-
"Nowhere", "Front", "Back", "Interlaced"};
52+
static constexpr const char* Names[] = {"None", "Front", "Back", "Interlaced"};
5453
};
5554

5655
// functor that moves elements from an iterator range into a new Container instance
@@ -59,166 +58,168 @@ struct MoveInto {};
5958

6059
template <typename T>
6160
struct MoveInto<std::vector<T>> {
62-
template <class It>
63-
[[nodiscard]] static std::vector<T> operator()(It first, It last) {
64-
std::vector<T> out;
65-
std::move(first, last, std::back_inserter(out));
66-
return out;
67-
}
61+
template <class It>
62+
[[nodiscard]] static std::vector<T> operator()(It first, It last) {
63+
std::vector<T> out;
64+
std::move(first, last, std::back_inserter(out));
65+
return out;
66+
}
6867
};
6968

7069
template <typename T>
7170
struct MoveInto<std::forward_list<T>> {
72-
template <class It>
73-
[[nodiscard]] static std::forward_list<T> operator()(It first, It last) {
74-
std::forward_list<T> out;
75-
std::move(first, last, std::front_inserter(out));
76-
out.reverse();
77-
return out;
78-
}
71+
template <class It>
72+
[[nodiscard]] static std::forward_list<T> operator()(It first, It last) {
73+
std::forward_list<T> out;
74+
std::move(first, last, std::front_inserter(out));
75+
out.reverse();
76+
return out;
77+
}
7978
};
8079

8180
template <typename T>
8281
struct MoveInto<std::set<T>> {
83-
template <class It>
84-
[[nodiscard]] static std::set<T> operator()(It first, It last) {
85-
std::set<T> out;
86-
std::move(first, last, std::inserter(out, out.begin()));
87-
return out;
88-
}
82+
template <class It>
83+
[[nodiscard]] static std::set<T> operator()(It first, It last) {
84+
std::set<T> out;
85+
std::move(first, last, std::inserter(out, out.begin()));
86+
return out;
87+
}
8988
};
9089

9190
// lightweight wrapping around fillValues() which puts a little effort into
9291
// making that would be contiguous when sorted non-contiguous in memory
9392
template <typename T>
9493
std::vector<T> getVectorOfRandom(size_t N) {
95-
std::vector<T> V;
96-
fillValues(V, N, Order::Random);
97-
sortValues(V, Order::Random);
98-
return std::vector<T>(V);
94+
std::vector<T> v;
95+
fillValues(v, N, Order::Random);
96+
sortValues(v, Order::Random);
97+
return std::vector<T>(v);
9998
}
10099

101100
// forward_iterator wrapping which, for each increment, moves the underlying iterator forward Stride elements
102101
template <typename Wrapped>
103102
struct StridedFwdIt {
104-
Wrapped Base;
105-
unsigned Stride;
103+
Wrapped base_;
104+
unsigned stride_;
106105

107106
using iterator_category = std::forward_iterator_tag;
108-
using difference_type = typename Wrapped::difference_type;
109-
using value_type = typename Wrapped::value_type;
110-
using pointer = typename Wrapped::pointer;
111-
using reference = typename Wrapped::reference;
112-
113-
StridedFwdIt(Wrapped B, unsigned Stride_) : Base(B), Stride(Stride_) { assert(Stride != 0); }
114-
115-
StridedFwdIt operator++() { for (unsigned I=0; I<Stride; ++I) ++Base; return *this; }
116-
StridedFwdIt operator++(int) { auto Tmp = *this; ++*this; return Tmp; }
117-
value_type& operator*() { return *Base; }
118-
const value_type& operator*() const { return *Base; }
119-
value_type& operator->() { return *Base; }
120-
const value_type& operator->() const { return *Base; }
121-
bool operator==(const StridedFwdIt& o) const { return Base==o.Base; }
107+
using difference_type = typename Wrapped::difference_type;
108+
using value_type = typename Wrapped::value_type;
109+
using pointer = typename Wrapped::pointer;
110+
using reference = typename Wrapped::reference;
111+
112+
StridedFwdIt(Wrapped base, unsigned stride) : base_(base), stride_(stride) { assert(stride_ != 0); }
113+
114+
StridedFwdIt operator++() {
115+
for (unsigned i = 0; i < stride_; ++i)
116+
++base_;
117+
return *this;
118+
}
119+
StridedFwdIt operator++(int) {
120+
auto tmp = *this;
121+
++*this;
122+
return tmp;
123+
}
124+
value_type& operator*() { return *base_; }
125+
const value_type& operator*() const { return *base_; }
126+
value_type& operator->() { return *base_; }
127+
const value_type& operator->() const { return *base_; }
128+
bool operator==(const StridedFwdIt& o) const { return base_ == o.base_; }
122129
bool operator!=(const StridedFwdIt& o) const { return !operator==(o); }
123130
};
124-
template <typename Wrapped> StridedFwdIt(Wrapped, unsigned) -> StridedFwdIt<Wrapped>;
125-
131+
template <typename Wrapped>
132+
StridedFwdIt(Wrapped, unsigned) -> StridedFwdIt<Wrapped>;
126133

127134
// realistically, data won't all be nicely contiguous in a container
128135
// we'll go through some effort to ensure that it's shuffled through memory
129136
template <class Container>
130-
std::pair<Container, Container> genCacheUnfriendlyData(size_t Size1, size_t Size2, OverlapPosition Pos) {
137+
std::pair<Container, Container> genCacheUnfriendlyData(size_t size1, size_t size2, OverlapPosition pos) {
131138
using ValueType = typename Container::value_type;
132-
const MoveInto<Container> moveInto;
133-
const auto SrcSize = Pos == OverlapPosition::Nowhere ? Size1 + Size2 : std::max(Size1, Size2);
134-
std::vector<ValueType> Src = getVectorOfRandom<ValueType>(SrcSize);
135-
136-
if (Pos == OverlapPosition::Nowhere) {
137-
std::sort(Src.begin(), Src.end());
138-
return std::make_pair(
139-
moveInto(Src.begin(), Src.begin() + Size1),
140-
moveInto(Src.begin() + Size1, Src.end()));
139+
const MoveInto<Container> move_into;
140+
const auto src_size = pos == OverlapPosition::None ? size1 + size2 : std::max(size1, size2);
141+
std::vector<ValueType> src = getVectorOfRandom<ValueType>(src_size);
142+
143+
if (pos == OverlapPosition::None) {
144+
std::sort(src.begin(), src.end());
145+
return std::make_pair(move_into(src.begin(), src.begin() + size1), move_into(src.begin() + size1, src.end()));
141146
}
142147

143148
// all other overlap types will have to copy some part of the data, but if
144149
// we copy after sorting it will likely have high cache locality, so we sort
145150
// each copy separately
146-
auto Copy = Src;
147-
std::sort(Src.begin(), Src.end());
148-
std::sort(Copy.begin(), Copy.end());
149-
150-
switch(Pos) {
151-
case OverlapPosition::Nowhere:
152-
break;
153-
154-
case OverlapPosition::Front:
155-
return std::make_pair(
156-
moveInto(Src.begin(), Src.begin() + Size1),
157-
moveInto(Copy.begin(), Copy.begin() + Size2));
158-
159-
case OverlapPosition::Back:
160-
return std::make_pair(
161-
moveInto(Src.begin() + (Src.size() - Size1), Src.end()),
162-
moveInto(Copy.begin() + (Copy.size() - Size2), Copy.end()));
163-
164-
case OverlapPosition::Interlaced:
165-
const auto Stride1 = Size1 < Size2 ? Size2/Size1 : 1;
166-
const auto Stride2 = Size2 < Size1 ? Size1/Size2 : 1;
167-
return std::make_pair(
168-
moveInto(StridedFwdIt(Src.begin(), Stride1), StridedFwdIt(Src.end(), Stride1)),
169-
moveInto(StridedFwdIt(Copy.begin(), Stride2), StridedFwdIt(Copy.end(), Stride2)));
151+
auto copy = src;
152+
std::sort(src.begin(), src.end());
153+
std::sort(copy.begin(), copy.end());
154+
155+
switch (pos) {
156+
case OverlapPosition::None:
157+
break;
158+
159+
case OverlapPosition::Front:
160+
return std::make_pair(move_into(src.begin(), src.begin() + size1), move_into(copy.begin(), copy.begin() + size2));
161+
162+
case OverlapPosition::Back:
163+
return std::make_pair(move_into(src.begin() + (src.size() - size1), src.end()),
164+
move_into(copy.begin() + (copy.size() - size2), copy.end()));
165+
166+
case OverlapPosition::Interlaced:
167+
const auto stride1 = size1 < size2 ? size2 / size1 : 1;
168+
const auto stride2 = size2 < size1 ? size1 / size2 : 1;
169+
return std::make_pair(move_into(StridedFwdIt(src.begin(), stride1), StridedFwdIt(src.end(), stride1)),
170+
move_into(StridedFwdIt(copy.begin(), stride2), StridedFwdIt(copy.end(), stride2)));
170171
}
171172
abort();
172173
return std::pair<Container, Container>();
173174
}
174175

175-
176176
template <class ValueType, class Container, class Overlap>
177177
struct SetIntersection {
178178
using ContainerType = typename Container::template type<Value<ValueType>>;
179-
size_t Size1;
180-
size_t Size2;
179+
size_t size1_;
180+
size_t size2_;
181181

182-
SetIntersection(size_t M, size_t N) : Size1(M), Size2(N) {}
182+
SetIntersection(size_t size1, size_t size2) : size1_(size1), size2_(size2) {}
183183

184184
void run(benchmark::State& state) const {
185185
state.PauseTiming();
186-
auto Input = genCacheUnfriendlyData<ContainerType>(Size1, Size2, Overlap());
187-
std::vector<Value<ValueType>> out(std::min(Size1, Size2));
186+
auto input = genCacheUnfriendlyData<ContainerType>(size1_, size2_, Overlap());
187+
std::vector<Value<ValueType>> out(std::min(size1_, size2_));
188188

189189
size_t cmp;
190-
auto trackingLess = [&cmp](const Value<ValueType>& lhs, const Value<ValueType>& rhs) {
191-
++cmp;
192-
return std::less<Value<ValueType>>{}(lhs, rhs);
190+
auto tracking_less = [&cmp](const Value<ValueType>& lhs, const Value<ValueType>& rhs) {
191+
++cmp;
192+
return std::less<Value<ValueType>>{}(lhs, rhs);
193193
};
194194

195-
const auto BatchSize = std::max(size_t{16}, (2*TestSetElements) / (Size1+Size2));
195+
const auto BATCH_SIZE = std::max(size_t{16}, (2 * TestSetElements) / (size1_ + size2_));
196196
state.ResumeTiming();
197197

198198
for (const auto& _ : state) {
199-
while (state.KeepRunningBatch(BatchSize)) {
200-
for (unsigned i=0; i<BatchSize; ++i) {
201-
const auto& [C1, C2] = Input;
202-
auto outIter = std::set_intersection(C1.begin(), C1.end(), C2.begin(), C2.end(), out.begin(), trackingLess);
203-
benchmark::DoNotOptimize(outIter);
199+
while (state.KeepRunningBatch(BATCH_SIZE)) {
200+
for (unsigned i = 0; i < BATCH_SIZE; ++i) {
201+
const auto& [c1, c2] = input;
202+
auto res = std::set_intersection(c1.begin(), c1.end(), c2.begin(), c2.end(), out.begin(), tracking_less);
203+
benchmark::DoNotOptimize(res);
204204
state.counters["Comparisons"] = cmp;
205205
}
206206
}
207207
}
208208
}
209209

210210
std::string name() const {
211-
return std::string("SetIntersection") + Overlap::name() + '_' + Container::Name +
212-
ValueType::name() + '_' + std::to_string(Size1) + '_' + std::to_string(Size2);
211+
return std::string("SetIntersection") + Overlap::name() + '_' + Container::Name + ValueType::name() + '_' +
212+
std::to_string(size1_) + '_' + std::to_string(size2_);
213213
}
214214
};
215215

216216
} // namespace
217217

218-
int main(int argc, char** argv) {/**/
218+
int main(int argc, char** argv) { /**/
219219
benchmark::Initialize(&argc, argv);
220220
if (benchmark::ReportUnrecognizedArguments(argc, argv))
221221
return 1;
222-
makeCartesianProductBenchmark<SetIntersection, AllValueTypes, AllContainerTypes, AllOverlapPositions>(Quantities, Quantities);
222+
makeCartesianProductBenchmark<SetIntersection, AllValueTypes, AllContainerTypes, AllOverlapPositions>(
223+
Quantities, Quantities);
223224
benchmark::RunSpecifiedBenchmarks();
224225
}

libcxx/include/__algorithm/iterator_operations.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ struct _IterOps<_ClassicAlgPolicy> {
9191
// it's unclear whether _Iter has a difference_type and whether that's signed, so we play it safe:
9292
// use the incoming type for returning and steer clear of negative overflows
9393
template <class _Iter, class _Distance>
94-
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 static _Distance advance(_Iter& __iter, _Distance __count, const _Iter& __sentinel) {
94+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 static _Distance
95+
advance(_Iter& __iter, _Distance __count, const _Iter& __sentinel) {
9596
return _IterOps::__advance(__iter, __count, __sentinel, typename iterator_traits<_Iter>::iterator_category());
9697
}
9798

libcxx/include/__algorithm/lower_bound.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,8 @@ __lower_bound_onesided(_Iter __first, _Sent __last, const _Type& __value, _Comp&
7979
}
8080

8181
template <class _AlgPolicy, class _RandIter, class _Sent, class _Type, class _Proj, class _Comp>
82-
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _RandIter __lower_bound(
83-
_RandIter __first,
84-
_Sent __last,
85-
const _Type& __value,
86-
_Comp& __comp,
87-
_Proj& __proj) {
82+
_LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _RandIter
83+
__lower_bound(_RandIter __first, _Sent __last, const _Type& __value, _Comp& __comp, _Proj& __proj) {
8884
const auto __dist = _IterOps<_AlgPolicy>::distance(__first, __last);
8985
return std::__lower_bound_bisecting<_AlgPolicy>(__first, __value, __dist, __comp, __proj);
9086
}

libcxx/include/__algorithm/set_intersection.h

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ struct _LIBCPP_NODISCARD_EXT __set_intersector {
6060
__comp_(__comp) {}
6161

6262
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI
63-
_LIBCPP_CONSTEXPR_SINCE_CXX20 __set_intersection_result<_InIter1, _InIter2, _OutIter>
64-
operator()() && {
63+
_LIBCPP_CONSTEXPR_SINCE_CXX20 __set_intersection_result<_InIter1, _InIter2, _OutIter>
64+
operator()() && {
6565
while (__first2_ != __last2_) {
6666
__advance1_and_maybe_add_result();
6767
if (__first1_ == __last1_)
@@ -84,7 +84,7 @@ struct _LIBCPP_NODISCARD_EXT __set_intersector {
8484
__advance_and_maybe_add_result(_Iter& __iter, const _Sent& __sentinel, const _Value& __value) {
8585
// use one-sided lower bound for improved algorithmic complexity bounds
8686
const auto __tmp = std::move(__iter);
87-
__iter = std::__lower_bound_onesided<_AlgPolicy>(__iter, __sentinel, __value, __comp_, __proj_);
87+
__iter = std::__lower_bound_onesided<_AlgPolicy>(__iter, __sentinel, __value, __comp_, __proj_);
8888
__add_output_unless(__tmp != __iter);
8989
}
9090

@@ -122,16 +122,16 @@ template <class _AlgPolicy,
122122
class _Sent2,
123123
class _OutIter>
124124
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI
125-
_LIBCPP_CONSTEXPR_SINCE_CXX20 __set_intersection_result<_InForwardIter1, _InForwardIter2, _OutIter>
126-
__set_intersection(
127-
_InForwardIter1 __first1,
128-
_Sent1 __last1,
129-
_InForwardIter2 __first2,
130-
_Sent2 __last2,
131-
_OutIter __result,
132-
_Compare&& __comp,
133-
std::forward_iterator_tag,
134-
std::forward_iterator_tag) {
125+
_LIBCPP_CONSTEXPR_SINCE_CXX20 __set_intersection_result<_InForwardIter1, _InForwardIter2, _OutIter>
126+
__set_intersection(
127+
_InForwardIter1 __first1,
128+
_Sent1 __last1,
129+
_InForwardIter2 __first2,
130+
_Sent2 __last2,
131+
_OutIter __result,
132+
_Compare&& __comp,
133+
std::forward_iterator_tag,
134+
std::forward_iterator_tag) {
135135
std::__set_intersector<_AlgPolicy, _Compare, _InForwardIter1, _Sent1, _InForwardIter2, _Sent2, _OutIter>
136136
__intersector(__first1, __last1, __first2, __last2, __result, __comp);
137137
return std::move(__intersector)();
@@ -146,16 +146,16 @@ template <class _AlgPolicy,
146146
class _Sent2,
147147
class _OutIter>
148148
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI
149-
_LIBCPP_CONSTEXPR_SINCE_CXX20 __set_intersection_result<_InInputIter1, _InInputIter2, _OutIter>
150-
__set_intersection(
151-
_InInputIter1 __first1,
152-
_Sent1 __last1,
153-
_InInputIter2 __first2,
154-
_Sent2 __last2,
155-
_OutIter __result,
156-
_Compare&& __comp,
157-
std::input_iterator_tag,
158-
std::input_iterator_tag) {
149+
_LIBCPP_CONSTEXPR_SINCE_CXX20 __set_intersection_result<_InInputIter1, _InInputIter2, _OutIter>
150+
__set_intersection(
151+
_InInputIter1 __first1,
152+
_Sent1 __last1,
153+
_InInputIter2 __first2,
154+
_Sent2 __last2,
155+
_OutIter __result,
156+
_Compare&& __comp,
157+
std::input_iterator_tag,
158+
std::input_iterator_tag) {
159159
while (__first1 != __last1 && __first2 != __last2) {
160160
if (__comp(*__first1, *__first2))
161161
++__first1;
@@ -190,9 +190,9 @@ class __set_intersection_iter_category {
190190

191191
template <class _AlgPolicy, class _Compare, class _InIter1, class _Sent1, class _InIter2, class _Sent2, class _OutIter>
192192
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI
193-
_LIBCPP_CONSTEXPR_SINCE_CXX20 __set_intersection_result<_InIter1, _InIter2, _OutIter>
194-
__set_intersection(
195-
_InIter1 __first1, _Sent1 __last1, _InIter2 __first2, _Sent2 __last2, _OutIter __result, _Compare&& __comp) {
193+
_LIBCPP_CONSTEXPR_SINCE_CXX20 __set_intersection_result<_InIter1, _InIter2, _OutIter>
194+
__set_intersection(
195+
_InIter1 __first1, _Sent1 __last1, _InIter2 __first2, _Sent2 __last2, _OutIter __result, _Compare&& __comp) {
196196
return std::__set_intersection<_AlgPolicy>(
197197
std::move(__first1),
198198
std::move(__last1),

0 commit comments

Comments
 (0)