Simple Application Framework
1
|
00001 /* 00002 This file is part of Simple Application Framework (Saf) library. 00003 Copyright (C) 2010 - 2012 Ondrej Danek <ondrej.danek@gmail.com> 00004 00005 This library is free software: you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published 00007 by the Free Software Foundation, either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 Saf is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with Simple Application Framework library. If not, 00017 see <http://www.gnu.org/licenses/>. 00018 */ 00019 00028 #ifndef SAF_ALGO_SELECTION_MINMAX_H 00029 #define SAF_ALGO_SELECTION_MINMAX_H 00030 00031 #include "../../Collection/Pair.h" 00032 #include "../Predicate.h" 00033 00034 namespace Saf 00035 { 00036 namespace Algo 00037 { 00039 namespace Selection 00040 { 00042 template <class T> 00043 inline T Max(const T& x, const T& y) 00044 { 00045 return x < y ? y : x; 00046 } 00047 00049 template <class T, class Pred> 00050 inline T Max(const T& x, const T& y, Pred pred) 00051 { 00052 return pred(x, y) ? y : x; 00053 } 00054 00056 template <class T> 00057 inline T Max(const T& x, const T& y, const T& z) 00058 { 00059 return (x < y) ? Max(y, z) : Max(x, z); 00060 } 00061 00063 template <class T, class Pred> 00064 inline T Max(const T& x, const T& y, const T& z, Pred pred) 00065 { 00066 return pred(x, y) ? Max(y, z, pred) : Max(x, z, pred); 00067 } 00068 00070 template <class T> 00071 inline T Min(const T& x, const T& y) 00072 { 00073 return x < y ? x : y; 00074 } 00075 00077 template <class T, class Pred> 00078 inline T Min(const T& x, const T& y, Pred pred) 00079 { 00080 return pred(x, y) ? x : y; 00081 } 00082 00084 template <class T> 00085 inline T Min(const T& x, const T& y, const T& z) 00086 { 00087 return (x < y) ? Min(x, z) : Min(y, z); 00088 } 00089 00091 template <class T, class Pred> 00092 inline T Min(const T& x, const T& y, const T& z, Pred pred) 00093 { 00094 return pred(x, y) ? Min(x, z, pred) : Min(y, z, pred); 00095 } 00096 00098 template <class T, class Pred> 00099 inline Collection::Pair<T,T> MinMax(const T& x, const T& y, Pred pred) 00100 { 00101 Collection::Pair<T,T> mm; 00102 00103 if (pred(x, y)) 00104 { 00105 mm.m_first = x; 00106 mm.m_first = y; 00107 } 00108 else 00109 { 00110 mm.m_first = y; 00111 mm.m_first = x; 00112 } 00113 00114 return mm; 00115 } 00116 00118 template <class T> 00119 inline Collection::Pair<T,T> MinMax(const T& x, const T& y) 00120 { 00121 Predicate::Less<T> pred; 00122 return MinMax(x, y, pred); 00123 } 00124 00126 template <class T, class Pred> 00127 inline Collection::Pair<T,T> MinMax(const T& x, const T& y, const T& z, Pred pred) 00128 { 00129 Collection::Pair<T,T> mm(x, x); 00130 00131 if (pred(y, x)) 00132 { 00133 mm.m_first = y; 00134 } 00135 else 00136 { 00137 mm.m_second = y; 00138 } 00139 00140 if (pred(z, mm.m_first)) 00141 { 00142 mm.m_first = z; 00143 } 00144 else if (pred(mm.m_second, z)) 00145 { 00146 mm.m_second = z; 00147 } 00148 00149 return mm; 00150 } 00151 00153 template <class T> 00154 inline Collection::Pair<T,T> MinMax(const T& x, const T& y, const T& z) 00155 { 00156 Predicate::Less<T> pred; 00157 return MinMax(x, y, z, pred); 00158 } 00159 } 00160 } 00161 } 00162 00163 #endif