1/******************************************************************************************[Sort.h]
2MiniSat -- Copyright (c) 2003-2005, Niklas Een, Niklas Sorensson
3
4Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5associated documentation files (the "Software"), to deal in the Software without restriction,
6including without limitation the rights to use, copy, modify, merge, publish, distribute,
7sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8furnished to do so, subject to the following conditions:
9
10The above copyright notice and this permission notice shall be included in all copies or
11substantial portions of the Software.
12
13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
17OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18**************************************************************************************************/
19
20#ifndef Sort_h
21#define Sort_h
22
23
24//=================================================================================================
25
26
27template<class T>
28struct LessThan_default {
29    bool operator () (T x, T y) { return x < y; }
30};
31
32
33//=================================================================================================
34
35
36template <class T, class LessThan>
37void selectionSort(T* array, int size, LessThan lt)
38{
39    int     i, j, best_i;
40    T       tmp;
41
42    for (i = 0; i < size-1; i++){
43        best_i = i;
44        for (j = i+1; j < size; j++){
45            if (lt(array[j], array[best_i]))
46                best_i = j;
47        }
48        tmp = array[i]; array[i] = array[best_i]; array[best_i] = tmp;
49    }
50}
51template <class T> static inline void selectionSort(T* array, int size) {
52    selectionSort(array, size, LessThan_default<T>()); }
53
54
55template <class T, class LessThan>
56void sort(T* array, int size, LessThan lt, double& seed)
57{
58    if (size <= 15)
59        selectionSort(array, size, lt);
60
61    else{
62        T           pivot = array[irand(seed, size)];
63        T           tmp;
64        int         i = -1;
65        int         j = size;
66
67        for(;;){
68            do i++; while(lt(array[i], pivot));
69            do j--; while(lt(pivot, array[j]));
70
71            if (i >= j) break;
72
73            tmp = array[i]; array[i] = array[j]; array[j] = tmp;
74        }
75
76        sort(array    , i     , lt, seed);
77        sort(&array[i], size-i, lt, seed);
78    }
79}
80template <class T, class LessThan> void sort(T* array, int size, LessThan lt) {
81    double  seed = 91648253; sort(array, size, lt, seed); }
82template <class T> static inline void sort(T* array, int size) {
83    sort(array, size, LessThan_default<T>()); }
84
85
86template <class T, class LessThan>
87void sortUnique(T* array, int& size, LessThan lt)
88{
89    int         i, j;
90    T           last;
91
92    if (size == 0) return;
93
94    sort(array, size, lt);
95
96    i    = 1;
97    last = array[0];
98    for (j = 1; j < size; j++){
99        if (lt(last, array[j])){
100            last = array[i] = array[j];
101            i++; }
102    }
103
104    size = i;
105}
106template <class T> static inline void sortUnique(T* array, int& size) {
107    sortUnique(array, size, LessThan_default<T>()); }
108
109
110//=================================================================================================
111// For 'vec's:
112
113
114template <class T, class LessThan> void sort(vec<T>& v, LessThan lt) {
115    sort((T*)v, v.size(), lt); }
116template <class T> void sort(vec<T>& v) {
117    sort(v, LessThan_default<T>()); }
118
119
120template <class T, class LessThan> void sortUnique(vec<T>& v, LessThan lt) {
121    int     size = v.size();
122    T*      data = v.release();
123    sortUnique(data, size, lt);
124    v.~vec<T>();
125    new (&v) vec<T>(data, size); }
126template <class T> void sortUnique(vec<T>& v) {
127    sortUnique(v, LessThan_default<T>()); }
128
129
130//=================================================================================================
131#endif
132