blob: 3c439741d15f89c9ded3a88afc2a6144611db564 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===----------------------------------------------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <algorithm>
11
Howard Hinnanteb564e72010-08-22 00:08:10 +000012// template<RandomAccessIterator Iter, StrictWeakOrder<auto, Iter::value_type> Compare>
13// requires ShuffleIterator<Iter>
14// && CopyConstructible<Compare>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000015// void
16// stable_sort(Iter first, Iter last, Compare comp);
17
18#include <algorithm>
19#include <functional>
20#include <vector>
21#include <cassert>
Howard Hinnant73d21a42010-09-04 23:28:19 +000022#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000023#include <memory>
24
25struct indirect_less
26{
27 template <class P>
28 bool operator()(const P& x, const P& y)
29 {return *x < *y;}
30};
31
Howard Hinnant73d21a42010-09-04 23:28:19 +000032#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000033
34struct first_only
35{
36 bool operator()(const std::pair<int, int>& x, const std::pair<int, int>& y)
37 {
38 return x.first < y.first;
39 }
40};
41
42void test()
43{
44 typedef std::pair<int, int> P;
45 const int N = 1000;
46 const int M = 10;
47 std::vector<P> v(N);
48 int x = 0;
49 int ver = 0;
50 for (int i = 0; i < N; ++i)
51 {
52 v[i] = P(x, ver);
53 if (++x == M)
54 {
55 x = 0;
56 ++ver;
57 }
58 }
59 for (int i = 0; i < N - M; i += M)
60 {
61 std::random_shuffle(v.begin() + i, v.begin() + i + M);
62 }
63 std::stable_sort(v.begin(), v.end(), first_only());
64 assert(std::is_sorted(v.begin(), v.end()));
65}
66
67int main()
68{
69 test();
70
Howard Hinnant73d21a42010-09-04 23:28:19 +000071#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000072 {
73 std::vector<std::unique_ptr<int> > v(1000);
74 for (int i = 0; i < v.size(); ++i)
75 v[i].reset(new int(i));
76 std::stable_sort(v.begin(), v.end(), indirect_less());
77 assert(std::is_sorted(v.begin(), v.end(), indirect_less()));
78 assert(*v[0] == 0);
79 assert(*v[1] == 1);
80 assert(*v[2] == 2);
81 }
Howard Hinnant73d21a42010-09-04 23:28:19 +000082#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083}