Simple Application Framework  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Saf/Algo/Selection/Range/MinMax.h
Go to the documentation of this file.
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_RANGE_MINMAX_H
00029 #define SAF_ALGO_SELECTION_RANGE_MINMAX_H
00030 
00031 #include "../../../Collection/Pair.h"
00032 #include "../Predicate.h"
00033 #include "../../Type/IteratorTraits.h"
00034 
00035 namespace Saf
00036 {
00037     namespace Algo
00038     {
00039         namespace Selection
00040         {
00042             namespace Range
00043             {
00055                 template <class FwdIter, class Comp>
00056                 inline FwdIter Min(FwdIter begin, FwdIter end, Comp comp)
00057                 {
00058                     FwdIter vmin = begin;
00059 
00060                     if (begin != end)
00061                     {
00062                         while (++begin != end)
00063                         {
00064                             if (comp(*begin, *vmin))
00065                             {
00066                                 vmin = begin;
00067                             }
00068                         }
00069                     }
00070 
00071                     return vmin;
00072                 }
00073 
00084                 template <class FwdIter>
00085                 inline FwdIter Min(FwdIter begin, FwdIter end)
00086                 {
00087                     Predicate::Less<typename Type::IteratorTraits<FwdIter>::ValType> pred;
00088                     return Min(begin, end, pred);
00089                 }
00090 
00102                 template <class FwdIter, class Comp>
00103                 inline FwdIter Max(FwdIter begin, FwdIter end, Comp comp)
00104                 {
00105                     FwdIter vmax = begin;
00106 
00107                     if (begin != end)
00108                     {
00109                         while (++begin != end)
00110                         {
00111                             if (comp(*vmax, *begin))
00112                             {
00113                                 vmax = begin;
00114                             }
00115                         }
00116                     }
00117 
00118                     return vmax;
00119                 }
00120 
00131                 template <class FwdIter>
00132                 inline FwdIter Max(FwdIter begin, FwdIter end)
00133                 {
00134                     Predicate::Less<typename Type::IteratorTraits<FwdIter>::ValType> pred;
00135                     return Max(begin, end, pred);
00136                 }
00137 
00152                 template <class FwdIter, class Comp>
00153                 inline Collection::Pair<FwdIter,FwdIter> MinMax(FwdIter begin, FwdIter end, Comp comp)
00154                 {
00155                     Collection::Pair<FwdIter,FwdIter> mm(begin, begin);
00156 
00157                     if (begin != end)
00158                     {
00159                         while (++begin != end)
00160                         {
00161                             if (comp(*begin, *mm.m_first)
00162                             {
00163                                 mm.m_first = begin;
00164                             }
00165                             else if (comp(*mm.m_second, *begin))
00166                             {
00167                                 mm.m_second = begin;
00168                             }
00169                         }
00170                     }
00171 
00172                     return mm;
00173                 }
00174 
00188                 template <class FwdIter>
00189                 inline Collection::Pair<FwdIter,FwdIter> MinMax(FwdIter begin, FwdIter end)
00190                 {
00191                     Predicate::Less<typename Type::IteratorTraits<FwdIter>::ValType> pred;
00192                     return MinMax(begin, end, pred);
00193                 }
00194             }
00195         }
00196     }
00197 }
00198 
00199 #endif