1 /******************************************************************************************[Sort.h]
2 Copyright (c) 2003-2007, Niklas Een, Niklas Sorensson
3 Copyright (c) 2007-2010, Niklas Sorensson
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6 associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute,
8 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all copies or
12 substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
18 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 **************************************************************************************************/
21 #ifndef Minisat_Sort_h
22 #define Minisat_Sort_h
24 #include "libs/minisat/Vec.h"
26 //=================================================================================================
27 // Some sorting algorithms for vec's
33 struct LessThan_default
{
34 bool operator () (T x
, T y
) { return x
< y
; }
38 template <class T
, class LessThan
>
39 void selectionSort(T
* array
, int size
, LessThan lt
)
44 for (i
= 0; i
< size
-1; i
++){
46 for (j
= i
+1; j
< size
; j
++){
47 if (lt(array
[j
], array
[best_i
]))
50 tmp
= array
[i
]; array
[i
] = array
[best_i
]; array
[best_i
] = tmp
;
53 template <class T
> static inline void selectionSort(T
* array
, int size
) {
54 selectionSort(array
, size
, LessThan_default
<T
>()); }
56 template <class T
, class LessThan
>
57 void sort(T
* array
, int size
, LessThan lt
)
60 selectionSort(array
, size
, lt
);
63 T pivot
= array
[size
/ 2];
69 do i
++; while(lt(array
[i
], pivot
));
70 do j
--; while(lt(pivot
, array
[j
]));
74 tmp
= array
[i
]; array
[i
] = array
[j
]; array
[j
] = tmp
;
78 sort(&array
[i
], size
-i
, lt
);
81 template <class T
> static inline void sort(T
* array
, int size
) {
82 sort(array
, size
, LessThan_default
<T
>()); }
85 //=================================================================================================
89 template <class T
, class LessThan
> void sort(vec
<T
>& v
, LessThan lt
) {
90 sort((T
*)v
, v
.size(), lt
); }
91 template <class T
> void sort(vec
<T
>& v
) {
92 sort(v
, LessThan_default
<T
>()); }
95 //=================================================================================================