ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkGenericUtilities.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkGenericUtilities_h
00019 #define __itkGenericUtilities_h
00020 
00021 #include <utility>
00022 
00023 /*
00024  *  This code was contributed in the Insight Journal paper:
00025  *  "A Streaming IO Base Class and Support for Streaming the MRC and VTK File Format"
00026  *  by Lowekamp B., Chen D.
00027  *  http://www.insight-journal.org/browse/publication/729
00028  *  http://hdl.handle.net/10380/3171
00029  *
00030  */
00031 
00032 namespace itk
00033 {
00035 
00037 // Generic Programming Algorithms
00039 
00045 template< class TInputIter >
00046 std::pair< TInputIter, TInputIter > min_max_element(TInputIter first, TInputIter last)
00047 {
00048   std::pair< TInputIter, TInputIter > result(first, first);
00049 
00050   if ( first == last )
00051     {
00052     return result;
00053     }
00054 
00055   while ( ++first != last )
00056     {
00057     TInputIter prev = first;
00058     if ( ++first == last )
00059       {
00060       if ( *prev < *( result.first ) )
00061         {
00062         result.first = prev;
00063         }
00064       if ( *( result.second ) < *prev )
00065         {
00066         result.second = prev;
00067         }
00068       break;
00069       }
00070     else if ( *first < *prev )
00071       {
00072       if ( *first < *( result.first ) )
00073         {
00074         result.first = first;
00075         }
00076       if ( *( result.second ) < *prev )
00077         {
00078         result.second = prev;
00079         }
00080       }
00081     else
00082       {
00083       if ( *prev < *( result.first ) )
00084         {
00085         result.first = prev;
00086         }
00087       if ( *( result.second ) < *first )
00088         {
00089         result.second = first;
00090         }
00091       }
00092     }
00093   return result;
00094 }
00095 
00101 template< class TInputIter, class TCompare >
00102 std::pair< TInputIter, TInputIter > min_max_element(TInputIter first, TInputIter last, TCompare comp)
00103 {
00104   std::pair< TInputIter, TInputIter > result(first, first);
00105 
00106   if ( first == last )
00107     {
00108     return result;
00109     }
00110 
00111   while ( ++first != last )
00112     {
00113     TInputIter prev = first;
00114     if ( ++first == last )
00115       {
00116       if ( comp( *prev, *( result.first ) ) )
00117         {
00118         result.first = prev;
00119         }
00120       if ( comp(*( result.second ), *prev) )
00121         {
00122         result.second = prev;
00123         }
00124       break;
00125       }
00126     else if ( comp(*first, *prev) )
00127       {
00128       if ( comp( *first, *( result.first ) ) )
00129         {
00130         result.first = first;
00131         }
00132       if ( comp(*( result.second ), *prev) )
00133         {
00134         result.second = prev;
00135         }
00136       }
00137     else
00138       {
00139       if ( comp( *prev, *( result.first ) ) )
00140         {
00141         result.first = prev;
00142         }
00143       if ( comp(*( result.second ), *first) )
00144         {
00145         result.second = first;
00146         }
00147       }
00148     }
00149   return result;
00150 }
00151 } // end itk namespace
00152 
00153 #endif //__itkGenericUtilities_h
00154