Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkGenericUtilities.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkGenericUtilities.h,v $
00005   Language:  C++
00006   Date:      $Date: 2010-06-16 18:23:47 $
00007   Version:   $Revision: 1.2 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================*/
00017 
00018 #ifndef __itkGenericUtilities_h
00019 #define __itkGenericUtilities_h
00020 
00021 namespace itk
00022 {
00023 
00024 
00026 
00027 
00029 // Generic Programming Algorithms
00031 
00037 template <class TInputIter>
00038 std::pair<TInputIter, TInputIter> min_max_element(TInputIter first, TInputIter last)
00039 {
00040   std::pair<TInputIter,TInputIter> result(first,first);
00041 
00042   if (first == last)
00043     return result;
00044 
00045   while (++first != last)
00046     {
00047     TInputIter prev = first;
00048     if (++first == last)
00049       {
00050       if (*prev < *(result.first))
00051         result.first = prev;
00052       if (*(result.second) < *prev)
00053         result.second = prev;
00054       break;
00055       }
00056     else if (*first < *prev)
00057       {
00058       if (*first < *(result.first))
00059         result.first = first;
00060       if (*(result.second) < *prev)
00061         result.second = prev;
00062       }
00063     else
00064       {
00065       if (*prev < *(result.first))
00066         result.first = prev;
00067       if (*(result.second) < *first)
00068         result.second = first;
00069       }
00070     }
00071   return result;
00072 }
00073 
00074 
00080 template <class TInputIter, class TCompare>
00081 std::pair<TInputIter, TInputIter> min_max_element(TInputIter first, TInputIter last, TCompare comp)
00082 {
00083   std::pair<TInputIter,TInputIter> result(first,first);
00084 
00085   if (first == last)
00086     {
00087     return result;
00088     }
00089 
00090   while (++first != last)
00091     {
00092     TInputIter prev = first;
00093     if (++first == last)
00094       {
00095       if (comp(*prev,*(result.first)))
00096         {
00097         result.first = prev;
00098         }
00099       if (comp(*(result.second),*prev))
00100         {
00101         result.second = prev;
00102         }
00103       break;
00104       }
00105     else if (comp(*first,*prev))
00106       {
00107       if (comp(*first,*(result.first)))
00108         {
00109         result.first = first;
00110         }
00111       if (comp(*(result.second),*prev))
00112         {
00113         result.second = prev;
00114         }
00115       }
00116     else
00117       {
00118       if (comp(*prev,*(result.first)))
00119         {
00120         result.first = prev;
00121         }
00122       if (comp(*(result.second),*first))
00123         {
00124         result.second = first;
00125         }
00126       }
00127     }
00128   return result;
00129 }
00130 
00131 } // end itk namespace
00132 
00133 #endif //__itkGenericUtilities_h
00134 

Generated at Mon Jul 12 2010 18:24:08 for ITK by doxygen 1.7.1 written by Dimitri van Heesch, © 1997-2000