sat-solver/Sort.h
00001 /******************************************************************************************[Sort.h]
00002 MiniSat -- Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
00005 associated documentation files (the "Software"), to deal in the Software without restriction,
00006 including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00008 furnished to do so, subject to the following conditions:
00009 
00010 The above copyright notice and this permission notice shall be included in all copies or
00011 substantial portions of the Software.
00012 
00013 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
00014 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
00017 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018 **************************************************************************************************/
00019 
00020 #ifndef Sort_h
00021 #define Sort_h
00022 
00023 #include "Vec.h"
00024 
00025 //=================================================================================================
00026 // Some sorting algorithms for vec's
00027 
00028 
00029 template<class T>
00030 struct LessThan_default {
00031     bool operator () (T x, T y) { return x < y; }
00032 };
00033 
00034 
00035 template <class T, class LessThan>
00036 void selectionSort(T* array, int size, LessThan lt)
00037 {
00038     int     i, j, best_i;
00039     T       tmp;
00040 
00041     for (i = 0; i < size-1; i++){
00042         best_i = i;
00043         for (j = i+1; j < size; j++){
00044             if (lt(array[j], array[best_i]))
00045                 best_i = j;
00046         }
00047         tmp = array[i]; array[i] = array[best_i]; array[best_i] = tmp;
00048     }
00049 }
00050 template <class T> static inline void selectionSort(T* array, int size) {
00051     selectionSort(array, size, LessThan_default<T>()); }
00052 
00053 template <class T, class LessThan>
00054 void sort(T* array, int size, LessThan lt)
00055 {
00056     if (size <= 15)
00057         selectionSort(array, size, lt);
00058 
00059     else{
00060         T           pivot = array[size / 2];
00061         T           tmp;
00062         int         i = -1;
00063         int         j = size;
00064 
00065         for(;;){
00066             do i++; while(lt(array[i], pivot));
00067             do j--; while(lt(pivot, array[j]));
00068 
00069             if (i >= j) break;
00070 
00071             tmp = array[i]; array[i] = array[j]; array[j] = tmp;
00072         }
00073 
00074         sort(array    , i     , lt);
00075         sort(&array[i], size-i, lt);
00076     }
00077 }
00078 template <class T> static inline void sort(T* array, int size) {
00079     sort(array, size, LessThan_default<T>()); }
00080 
00081 
00082 //=================================================================================================
00083 // For 'vec's:
00084 
00085 
00086 template <class T, class LessThan> void sort(vec<T>& v, LessThan lt) {
00087     sort((T*)v, v.size(), lt); }
00088 template <class T> void sort(vec<T>& v) {
00089     sort(v, LessThan_default<T>()); }
00090 
00091 
00092 //=================================================================================================
00093 #endif