SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
Sort.h
Go to the documentation of this file.
1 /******************************************************************************************[Sort.h]
2 Copyright (c) 2003-2007, Niklas Een, Niklas Sorensson
3 Copyright (c) 2007-2010, Niklas Sorensson
4 
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:
10 
11 The above copyright notice and this permission notice shall be included in all copies or
12 substantial portions of the Software.
13 
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 **************************************************************************************************/
20 
21 #ifndef Minisat_Sort_h
22 #define Minisat_Sort_h
23 
24 #include "Vec.h"
25 
26 //=================================================================================================
27 // Some sorting algorithms for vec's
28 
29 namespace Minisat
30 {
31  template<class T>
33  {
34  bool operator ()( T x, T y )
35  {
36  return x < y;
37  }
38  };
39 
40  template<class T, class LessThan>
41  void selectionSort( T* array, int size, LessThan lt )
42  {
43  int i, j, best_i;
44  T tmp;
45 
46  for( i = 0; i < size - 1; i++ )
47  {
48  best_i = i;
49  for( j = i + 1; j < size; j++ )
50  {
51  if( lt( array[j], array[best_i] ) )
52  best_i = j;
53  }
54  tmp = array[i];
55  array[i] = array[best_i];
56  array[best_i] = tmp;
57  }
58  }
59 
60  template<class T>
61  static inline void selectionSort( T* array, int size )
62  {
63  selectionSort( array, size, LessThan_default<T>() );
64  }
65 
66  template<class T, class LessThan>
67  void sort( T* array, int size, LessThan lt )
68  {
69  if( size <= 15 )
70  selectionSort( array, size, lt );
71 
72  else
73  {
74  T pivot = array[size / 2];
75  T tmp;
76  int i = -1;
77  int j = size;
78 
79  for( ; ; )
80  {
81  do
82  i++;
83  while( lt( array[i], pivot ) );
84  do
85  j--;
86  while( lt( pivot, array[j] ) );
87 
88  if( i >= j )
89  break;
90 
91  tmp = array[i];
92  array[i] = array[j];
93  array[j] = tmp;
94  }
95 
96  sort( array, i, lt );
97  sort( &array[i], size - i, lt );
98  }
99  }
100 
101  template<class T>
102  static inline void sort( T* array, int size )
103  {
104  sort( array, size, LessThan_default<T>() );
105  }
106 
107  //=================================================================================================
108  // For 'vec's:
109 
110  template<class T, class LessThan>
111  void sort( vec<T>& v, LessThan lt )
112  {
113  sort( (T*)v, v.size(), lt );
114  }
115 
116  template<class T>
117  void sort( vec<T>& v )
118  {
119  sort( v, LessThan_default<T>() );
120  }
121 
122  //=================================================================================================
123 }
124 
125 #endif
int size(void) const
Definition: Vec.h:118
Definition: Alg.h:27
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:67
void selectionSort(T *array, int size, LessThan lt)
Definition: Sort.h:41
bool operator()(T x, T y)
Definition: Sort.h:34