ITK  4.4.0
Insight Segmentation and Registration Toolkit
itkGenericUtilities.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkGenericUtilities_h
19 #define __itkGenericUtilities_h
20 
21 #include <utility>
22 
23 /*
24  * This code was contributed in the Insight Journal paper:
25  * "A Streaming IO Base Class and Support for Streaming the MRC and VTK File Format"
26  * by Lowekamp B., Chen D.
27  * http://www.insight-journal.org/browse/publication/729
28  * http://hdl.handle.net/10380/3171
29  *
30  */
31 
32 namespace itk
33 {
35 
37 // Generic Programming Algorithms
39 
45 template< class TInputIter >
46 std::pair< TInputIter, TInputIter > min_max_element(TInputIter first, TInputIter last)
47 {
48  std::pair< TInputIter, TInputIter > result(first, first);
49 
50  if ( first == last )
51  {
52  return result;
53  }
54 
55  while ( ++first != last )
56  {
57  TInputIter prev = first;
58  if ( ++first == last )
59  {
60  if ( *prev < *( result.first ) )
61  {
62  result.first = prev;
63  }
64  if ( *( result.second ) < *prev )
65  {
66  result.second = prev;
67  }
68  break;
69  }
70  else if ( *first < *prev )
71  {
72  if ( *first < *( result.first ) )
73  {
74  result.first = first;
75  }
76  if ( *( result.second ) < *prev )
77  {
78  result.second = prev;
79  }
80  }
81  else
82  {
83  if ( *prev < *( result.first ) )
84  {
85  result.first = prev;
86  }
87  if ( *( result.second ) < *first )
88  {
89  result.second = first;
90  }
91  }
92  }
93  return result;
94 }
95 
101 template< class TInputIter, class TCompare >
102 std::pair< TInputIter, TInputIter > min_max_element(TInputIter first, TInputIter last, TCompare comp)
103 {
104  std::pair< TInputIter, TInputIter > result(first, first);
105 
106  if ( first == last )
107  {
108  return result;
109  }
110 
111  while ( ++first != last )
112  {
113  TInputIter prev = first;
114  if ( ++first == last )
115  {
116  if ( comp( *prev, *( result.first ) ) )
117  {
118  result.first = prev;
119  }
120  if ( comp(*( result.second ), *prev) )
121  {
122  result.second = prev;
123  }
124  break;
125  }
126  else if ( comp(*first, *prev) )
127  {
128  if ( comp( *first, *( result.first ) ) )
129  {
130  result.first = first;
131  }
132  if ( comp(*( result.second ), *prev) )
133  {
134  result.second = prev;
135  }
136  }
137  else
138  {
139  if ( comp( *prev, *( result.first ) ) )
140  {
141  result.first = prev;
142  }
143  if ( comp(*( result.second ), *first) )
144  {
145  result.second = first;
146  }
147  }
148  }
149  return result;
150 }
151 } // end itk namespace
152 
153 #endif //__itkGenericUtilities_h
154