Skip to content

Commit 12d1957

Browse files
author
paolo
committed
2009-08-24 Chris Jefferson <[email protected]>
* include/stl_algo.h (__unguarded_partition_pivot, __move_median_first): New. (__insertion_sort, __unguarded_insertion_sort): Adjust for move-only types. (__unguarded_linear_insert): Assume always inserting value at __last. (__unguarded_partition): Take pivot by reference. (__introsort_loop, __introselect) : Use __unguarded_partition_pivot. * testsuite/25_algorithms/nth_element/moveable.cc : Enable. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@151055 138bc75d-0d04-0410-961f-82ee72b054a4
1 parent 23f19d1 commit 12d1957

File tree

3 files changed

+116
-65
lines changed

3 files changed

+116
-65
lines changed

libstdc++-v3/ChangeLog

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
2009-08-24 Chris Jefferson <[email protected]>
2+
3+
* include/stl_algo.h (__unguarded_partition_pivot,
4+
__move_median_first): New.
5+
(__insertion_sort, __unguarded_insertion_sort): Adjust for move-only
6+
types.
7+
(__unguarded_linear_insert): Assume always inserting value at __last.
8+
(__unguarded_partition): Take pivot by reference.
9+
(__introsort_loop, __introselect) : Use __unguarded_partition_pivot.
10+
* testsuite/25_algorithms/nth_element/moveable.cc : Enable.
11+
112
2009-08-23 Ralf Wildenhues <[email protected]>
213

314
* libsupc++/Makefile.am (LTCOMPILE): Expand $(LIBTOOLFLAGS)

libstdc++-v3/include/bits/stl_algo.h

Lines changed: 105 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,56 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
136136
return __b;
137137
}
138138

139+
/// Swaps the median value of *__a, *__b and *__c to *__a
140+
template<typename _Iterator>
141+
void
142+
__move_median_first(_Iterator __a, _Iterator __b, _Iterator __c)
143+
{
144+
// concept requirements
145+
__glibcxx_function_requires(_LessThanComparableConcept<
146+
typename iterator_traits<_Iterator>::value_type>)
147+
148+
if (*__a < *__b)
149+
{
150+
if (*__b < *__c)
151+
std::iter_swap(__a, __b);
152+
else if (*__a < *__c)
153+
std::iter_swap(__a, __c);
154+
}
155+
else if (*__a < *__c)
156+
return;
157+
else if (*__b < *__c)
158+
std::iter_swap(__a, __c);
159+
else
160+
std::iter_swap(__a, __b);
161+
}
162+
163+
/// Swaps the median value of *__a, *__b and *__c under __comp to *__a
164+
template<typename _Iterator, typename _Compare>
165+
void
166+
__move_median_first(_Iterator __a, _Iterator __b, _Iterator __c,
167+
_Compare __comp)
168+
{
169+
// concept requirements
170+
__glibcxx_function_requires(_BinaryFunctionConcept<_Compare, bool,
171+
typename iterator_traits<_Iterator>::value_type,
172+
typename iterator_traits<_Iterator>::value_type>)
173+
174+
if (__comp(*__a, *__b))
175+
{
176+
if (__comp(*__b, *__c))
177+
std::iter_swap(__a, __b);
178+
else if (__comp(*__a, *__c))
179+
std::iter_swap(__a, __c);
180+
}
181+
else if (__comp(*__a, *__c))
182+
return;
183+
else if (__comp(*__b, *__c))
184+
std::iter_swap(__a, __c);
185+
else
186+
std::iter_swap(__a, __b);
187+
}
188+
139189
// for_each
140190

141191
/// This is an overload used by find() for the Input Iterator case.
@@ -2058,36 +2108,40 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
20582108
}
20592109

20602110
/// This is a helper function for the sort routine.
2061-
template<typename _RandomAccessIterator, typename _Tp>
2111+
template<typename _RandomAccessIterator>
20622112
void
2063-
__unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val)
2113+
__unguarded_linear_insert(_RandomAccessIterator __last)
20642114
{
2115+
typename iterator_traits<_RandomAccessIterator>::value_type
2116+
__val = _GLIBCXX_MOVE(*__last);
20652117
_RandomAccessIterator __next = __last;
20662118
--__next;
20672119
while (__val < *__next)
20682120
{
2069-
*__last = *__next;
2121+
*__last = _GLIBCXX_MOVE(*__next);
20702122
__last = __next;
20712123
--__next;
20722124
}
2073-
*__last = __val;
2125+
*__last = _GLIBCXX_MOVE(__val);
20742126
}
20752127

20762128
/// This is a helper function for the sort routine.
2077-
template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
2129+
template<typename _RandomAccessIterator, typename _Compare>
20782130
void
2079-
__unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val,
2131+
__unguarded_linear_insert(_RandomAccessIterator __last,
20802132
_Compare __comp)
20812133
{
2134+
typename iterator_traits<_RandomAccessIterator>::value_type
2135+
__val = _GLIBCXX_MOVE(*__last);
20822136
_RandomAccessIterator __next = __last;
20832137
--__next;
20842138
while (__comp(__val, *__next))
20852139
{
2086-
*__last = *__next;
2140+
*__last = _GLIBCXX_MOVE(*__next);
20872141
__last = __next;
20882142
--__next;
20892143
}
2090-
*__last = __val;
2144+
*__last = _GLIBCXX_MOVE(__val);
20912145
}
20922146

20932147
/// This is a helper function for the sort routine.
@@ -2101,15 +2155,15 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
21012155

21022156
for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
21032157
{
2104-
typename iterator_traits<_RandomAccessIterator>::value_type
2105-
__val = *__i;
2106-
if (__val < *__first)
2158+
if (*__i < *__first)
21072159
{
2108-
std::copy_backward(__first, __i, __i + 1);
2109-
*__first = __val;
2160+
typename iterator_traits<_RandomAccessIterator>::value_type
2161+
__val = _GLIBCXX_MOVE(*__i);
2162+
_GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
2163+
*__first = _GLIBCXX_MOVE(__val);
21102164
}
21112165
else
2112-
std::__unguarded_linear_insert(__i, __val);
2166+
std::__unguarded_linear_insert(__i);
21132167
}
21142168
}
21152169

@@ -2123,15 +2177,15 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
21232177

21242178
for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
21252179
{
2126-
typename iterator_traits<_RandomAccessIterator>::value_type
2127-
__val = *__i;
2128-
if (__comp(__val, *__first))
2180+
if (__comp(*__i, *__first))
21292181
{
2130-
std::copy_backward(__first, __i, __i + 1);
2131-
*__first = __val;
2182+
typename iterator_traits<_RandomAccessIterator>::value_type
2183+
__val = _GLIBCXX_MOVE(*__i);
2184+
_GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
2185+
*__first = _GLIBCXX_MOVE(__val);
21322186
}
21332187
else
2134-
std::__unguarded_linear_insert(__i, __val, __comp);
2188+
std::__unguarded_linear_insert(__i, __comp);
21352189
}
21362190
}
21372191

@@ -2145,7 +2199,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
21452199
_ValueType;
21462200

21472201
for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2148-
std::__unguarded_linear_insert(__i, _ValueType(*__i));
2202+
std::__unguarded_linear_insert(__i);
21492203
}
21502204

21512205
/// This is a helper function for the sort routine.
@@ -2158,7 +2212,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
21582212
_ValueType;
21592213

21602214
for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
2161-
std::__unguarded_linear_insert(__i, _ValueType(*__i), __comp);
2215+
std::__unguarded_linear_insert(__i, __comp);
21622216
}
21632217

21642218
/**
@@ -2202,7 +2256,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
22022256
template<typename _RandomAccessIterator, typename _Tp>
22032257
_RandomAccessIterator
22042258
__unguarded_partition(_RandomAccessIterator __first,
2205-
_RandomAccessIterator __last, _Tp __pivot)
2259+
_RandomAccessIterator __last, const _Tp& __pivot)
22062260
{
22072261
while (true)
22082262
{
@@ -2223,7 +2277,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
22232277
_RandomAccessIterator
22242278
__unguarded_partition(_RandomAccessIterator __first,
22252279
_RandomAccessIterator __last,
2226-
_Tp __pivot, _Compare __comp)
2280+
const _Tp& __pivot, _Compare __comp)
22272281
{
22282282
while (true)
22292283
{
@@ -2239,16 +2293,36 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
22392293
}
22402294
}
22412295

2296+
/// This is a helper function...
2297+
template<typename _RandomAccessIterator>
2298+
inline _RandomAccessIterator
2299+
__unguarded_partition_pivot(_RandomAccessIterator __first,
2300+
_RandomAccessIterator __last)
2301+
{
2302+
_RandomAccessIterator __mid = __first + (__last - __first) / 2;
2303+
std::__move_median_first(__first, __mid, (__last - 1));
2304+
return std::__unguarded_partition(__first + 1, __last, *__first);
2305+
}
2306+
2307+
2308+
/// This is a helper function...
2309+
template<typename _RandomAccessIterator, typename _Compare>
2310+
inline _RandomAccessIterator
2311+
__unguarded_partition_pivot(_RandomAccessIterator __first,
2312+
_RandomAccessIterator __last, _Compare __comp)
2313+
{
2314+
_RandomAccessIterator __mid = __first + (__last - __first) / 2;
2315+
std::__move_median_first(__first, __mid, (__last - 1), __comp);
2316+
return std::__unguarded_partition(__first + 1, __last, *__first, __comp);
2317+
}
2318+
22422319
/// This is a helper function for the sort routine.
22432320
template<typename _RandomAccessIterator, typename _Size>
22442321
void
22452322
__introsort_loop(_RandomAccessIterator __first,
22462323
_RandomAccessIterator __last,
22472324
_Size __depth_limit)
22482325
{
2249-
typedef typename iterator_traits<_RandomAccessIterator>::value_type
2250-
_ValueType;
2251-
22522326
while (__last - __first > int(_S_threshold))
22532327
{
22542328
if (__depth_limit == 0)
@@ -2258,14 +2332,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
22582332
}
22592333
--__depth_limit;
22602334
_RandomAccessIterator __cut =
2261-
std::__unguarded_partition(__first, __last,
2262-
_ValueType(std::__median(*__first,
2263-
*(__first
2264-
+ (__last
2265-
- __first)
2266-
/ 2),
2267-
*(__last
2268-
- 1))));
2335+
std::__unguarded_partition_pivot(__first, __last);
22692336
std::__introsort_loop(__cut, __last, __depth_limit);
22702337
__last = __cut;
22712338
}
@@ -2278,9 +2345,6 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
22782345
_RandomAccessIterator __last,
22792346
_Size __depth_limit, _Compare __comp)
22802347
{
2281-
typedef typename iterator_traits<_RandomAccessIterator>::value_type
2282-
_ValueType;
2283-
22842348
while (__last - __first > int(_S_threshold))
22852349
{
22862350
if (__depth_limit == 0)
@@ -2290,15 +2354,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
22902354
}
22912355
--__depth_limit;
22922356
_RandomAccessIterator __cut =
2293-
std::__unguarded_partition(__first, __last,
2294-
_ValueType(std::__median(*__first,
2295-
*(__first
2296-
+ (__last
2297-
- __first)
2298-
/ 2),
2299-
*(__last - 1),
2300-
__comp)),
2301-
__comp);
2357+
std::__unguarded_partition_pivot(__first, __last, __comp);
23022358
std::__introsort_loop(__cut, __last, __depth_limit, __comp);
23032359
__last = __cut;
23042360
}
@@ -2349,14 +2405,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
23492405
}
23502406
--__depth_limit;
23512407
_RandomAccessIterator __cut =
2352-
std::__unguarded_partition(__first, __last,
2353-
_ValueType(std::__median(*__first,
2354-
*(__first
2355-
+ (__last
2356-
- __first)
2357-
/ 2),
2358-
*(__last
2359-
- 1))));
2408+
std::__unguarded_partition_pivot(__first, __last);
23602409
if (__cut <= __nth)
23612410
__first = __cut;
23622411
else
@@ -2385,15 +2434,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
23852434
}
23862435
--__depth_limit;
23872436
_RandomAccessIterator __cut =
2388-
std::__unguarded_partition(__first, __last,
2389-
_ValueType(std::__median(*__first,
2390-
*(__first
2391-
+ (__last
2392-
- __first)
2393-
/ 2),
2394-
*(__last - 1),
2395-
__comp)),
2396-
__comp);
2437+
std::__unguarded_partition_pivot(__first, __last, __comp);
23972438
if (__cut <= __nth)
23982439
__first = __cut;
23992440
else

libstdc++-v3/testsuite/25_algorithms/nth_element/moveable.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// { dg-require-rvalref "" }
21
// { dg-options "-std=gnu++0x" }
32

43
// Copyright (C) 2005, 2007, 2009 Free Software Foundation, Inc.

0 commit comments

Comments
 (0)