@@ -24,12 +24,13 @@ template < typename I1, typename I2 >
2424inline void iter_swap (I1 a, I2 b) {
2525 swap (*a, *b);
2626}
27+ template < class T , class Cmp >
28+ void __sort3 (T& a, T& b, T& c, Cmp comp); // TODO
2729
2830template < typename It, class Cmp >
2931void __insertion_sort (It first, It last, Cmp cmp);
3032
31- template < class T , class Cmp >
32- void __sort3 (T& a, T& b, T& c, Cmp comp); // TODO
33+
3334
3435template < typename It, class Cmp >
3536void sort (It first, It last, Cmp cmp); // TODO
@@ -38,20 +39,22 @@ void sort(It first, It last, Cmp cmp); // TODO
3839
3940template < typename It, class Cmp >
4041void __insertion_sort (It first, It last, Cmp cmp) {
41- for (It next = first + 1 ; next < last; ++next) {
42+ It next = first;
43+ for (++next; next < last; ++next) {
4244 typename iterator_traits< It >::value_type value = *next;
4345
4446 It t1 = next - 1 ;
4547 It t2 = next;
4648 while (first < t2 && cmp (value, *t1)) {
47- *t2-- = *t1--;
49+ *t2-- = *t1;
50+ --t1;
4851 }
4952 *t2 = value;
5053 }
5154}
5255
5356template < typename T, class Cmp >
54- void __sort3 (T& a, T& b, T& c, Cmp comp) {
57+ void __sort3 (T& a, T& b, T& c, const Cmp comp) {
5558 if (comp (b, a)) {
5659 swap (a, b);
5760 }
@@ -70,28 +73,30 @@ void __sort3(T& a, T& b, T& c, Cmp comp) {
7073template < typename It, class Cmp >
7174void sort (It first, It last, Cmp cmp) {
7275 int count = last - first; // use distance?
73- if (count > 1 ) {
74- if (count <= 20 ) {
75- __insertion_sort (first, last, cmp);
76- } else {
77- It pivot = first + count / 2 ;
78- It end = last;
79- __sort3 (* first, *pivot, *--end, cmp) ;
80- typename iterator_traits< It >::value_type value = *pivot ;
81- It it = first + 1 ;
82- --end ;
83- while ( true ) {
84- while ( cmp (*it, value))
85- ++it;
86- while (cmp (value, *end ))
87- --end ;
88- if (it >= end)
89- break ;
90- iter_swap (it++, end--);
91- }
92- sort (first, it, cmp );
93- sort (it, last, cmp) ;
76+ if (count <= 1 ) {
77+ return ;
78+ }
79+ if ( 20 >= count) {
80+ __insertion_sort ( first, last, cmp) ;
81+ } else {
82+ It pivot = first + count / 2 ;
83+ It end = last ;
84+ __sort3 (* first, *pivot, *--end, cmp) ;
85+ typename iterator_traits< It >::value_type value = *pivot ;
86+ It it = first + 1 ;
87+ --end;
88+ while ( true ) {
89+ while (cmp (*it, value ))
90+ ++it ;
91+ while ( cmp (value, * end) )
92+ --end ;
93+ if (it >= end)
94+ break ;
95+ iter_swap ( it, end-- );
96+ ++it ;
9497 }
98+ sort (first, it, cmp);
99+ sort (it, last, cmp);
95100 }
96101}
97102
@@ -147,18 +152,21 @@ class pair_sorter_finder< pair< K, V >, Cmp > {
147152 bool operator ()(const pair< K, V >& a, const pair< K, V >& b) const ;
148153};
149154
150- template <typename T>
151- inline pair_sorter_finder< typename T::value_type, less< typename select1st< typename T::value_type >::value_type > > default_pair_sorter_finder ()
152- {
153- less< typename select1st< typename T::value_type >::value_type > l;
154- pair_sorter_finder< typename T::value_type, less< typename select1st< typename T::value_type >::value_type > > a (l);
155- return a;
155+ template < typename T >
156+ inline pair_sorter_finder< typename T::value_type,
157+ less< typename select1st< typename T::value_type >::value_type > >
158+ default_pair_sorter_finder () {
159+ less< typename select1st< typename T::value_type >::value_type > l;
160+ pair_sorter_finder< typename T::value_type,
161+ less< typename select1st< typename T::value_type >::value_type > >
162+ a (l);
163+ return a;
156164}
157165
158166template < typename K, typename V, typename Cmp >
159167inline bool pair_sorter_finder< pair< K, V >, Cmp >::operator ()(const K& a,
160168 const pair< K, V >& b) const {
161- return cmp (a, b.first );
169+ return !! cmp (a, b.first );
162170}
163171
164172template < typename K, typename V, typename Cmp >
@@ -181,8 +189,15 @@ find_by_key(const T& container,
181189template < typename T >
182190typename T::const_iterator inline find_by_key (
183191 const T& container, const typename select1st< typename T::value_type >::value_type& key) {
192+ return binary_find (container.begin (), container.end (), key, default_pair_sorter_finder< T >());
193+ }
194+
195+ template < typename T, class Cmp >
196+ typename T::const_iterator inline find_by_key (
197+ const T& container, const typename select1st< typename T::value_type >::value_type& key,
198+ Cmp cmp) {
184199 return binary_find (container.begin (), container.end (), key,
185- default_pair_sorter_finder<T>( ));
200+ pair_sorter_finder< typename T::value_type, Cmp >(cmp ));
186201}
187202
188203template < typename T >
@@ -192,8 +207,12 @@ find_by_key_nc(T& container, const typename select1st< typename T::value_type >:
192207template < typename T >
193208typename T::iterator inline find_by_key_nc (
194209 T& container, const typename select1st< typename T::value_type >::value_type& key) {
195- return binary_find (container.begin (), container.end (), key,
196- default_pair_sorter_finder<T>());
210+ return binary_find (container.begin (), container.end (), key, default_pair_sorter_finder< T >());
211+ }
212+
213+ template < typename T, class Cmp >
214+ inline void sort_by_key (T& container, const Cmp& cmp) {
215+ sort (container.begin (), container.end (), pair_sorter_finder< typename T::value_type, Cmp >(cmp));
197216}
198217
199218} // namespace rstl
0 commit comments