OFFIS DCMTK  Version 3.6.0
ofalgo.h
1 /*
2  *
3  * Copyright (C) 1997-2010, OFFIS e.V.
4  * All rights reserved. See COPYRIGHT file for details.
5  *
6  * This software and supporting documentation were developed by
7  *
8  * OFFIS e.V.
9  * R&D Division Health
10  * Escherweg 2
11  * D-26121 Oldenburg, Germany
12  *
13  *
14  * Module: ofstd
15  *
16  * Author: Andreas Barth
17  *
18  * Purpose:
19  * Defines template algorithms for contaimer classes with
20  * interfaces similar to the C++ Standard
21  *
22  * Last Update: $Author: joergr $
23  * Update Date: $Date: 2010-10-14 13:15:49 $
24  * CVS/RCS Revision: $Revision: 1.6 $
25  * Status: $State: Exp $
26  *
27  * CVS/RCS Log at end of file
28  *
29  */
30 
31 #ifndef OFITER_H
32 #define OFITER_H
33 
34 #include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
35 #include "dcmtk/ofstd/oftypes.h"
36 
37 // Usage:
38 // Function_type OFForEach(InputIterator_type, Function_type, first, last ,f)
39 // Applies function f from type Function_type to the result of
40 // derferencing every iterator in the range [first, last) starting
41 // with first and proceeding to last -1 (first, last are of type
42 // InputIterator_type). Returns f.
43 //
44 // InputIterator_type OFFind(InputIterator_type, T_type, first, last, value)
45 // Returns the first Iterator i of type InputIterator_type in the range
46 // [first, last) for which *i == value (of type T_type). Returns last,
47 // if no such iterator is found
48 //
49 // InputIterator_type OFFindIf(InputIterator_type, Predicate_type,
50 // first, last, pred)
51 // Returns the first iterator i of type InputIterator_type in the range
52 // [first, last) for which pred(*i) != false. The function pred is of
53 // type Predicate_type. Returns last, if no such iterator is found
54 //
55 // ForwardIterator OFAdjacentFind(ForwardIterator_type, first, last)
56 // Returns the first iterator i of type ForwardIterator_type such
57 // that both i and i+1 are in the range [first, last) and *i == *(i+1)
58 // returns last, if no such iterator is found. i+1 means the successor
59 // of i.
60 //
61 // ForwardIterator OFAdjacentFindPred(ForwardIterator_type,
62 // BinaryPredicate_type,
63 // first, last, pred)
64 // Returns the first iterator i of type ForwardIterator_type such
65 // that both i and i+1 are in the range [first, last) and
66 // pred (*i, *(i+1)) != false.
67 // Returns last, if no such iterator is found. i+1 means the successor
68 // of i.
69 //
70 // Additional Remarks:
71 // In some template functions one template parameter is another function.
72 // Some compilers cannot determine automatically the type of template
73 // function parameters, so you must give them a hint casting
74 // the parameter function to the correct type (e.g. NeXT gcc 2.5.8)
75 
76 
77 #if defined(HAVE_STL) || defined(HAVE_STL_ALGORITHMS)
78 // It is possible to use the standard template library list class since the
79 // interface is nearly identical.
80 // Important: If you want to use the standard template library (STL), no
81 // variable within a namespace using a class of the STL shall have a name
82 // of one class of the STL
83 #include <algorithm>
84 #define OFForEach(InputIterator_type, Function_type, first, last, f) for_each((first), (last), (f))
85 #define OFFind(InputIterator_type, T_type, first, last, value) find((first), (last), (value))
86 #define OFFindIf(InputIterator_type, Predicate_type, first, last, pred) find_if((first), (last), (pred))
87 #define OFAdjacentFind(ForwardIterator_type, first, last) adjacent_find((first), (last))
88 #define OFAdjacentFindPred(ForwardIterator_type, BinaryPredicate_type, first, last, pred) adjacent_find((first), (last), (pred))
89 #else
90 
91 #ifdef HAVE_FUNCTION_TEMPLATE
92 
93 #define OFForEach(InputIterator_type, Function_type, first, last, f) OF_ForEach((first), (last), (f))
94 
95 #define OFFind(InputIterator_type, T_type, first, last, value) OF_Find((first), (last), (value))
96 
97 #define OFFindIf(InputIterator_type, Predicate_type, first, last, pred) OF_FindIf((first), (last), (pred))
98 
99 #define OFAdjacentFind(ForwardIterator_type, first, last) OF_AdjacentFind((first), (last))
100 
101 #define OFAdjacentFindPred(ForwardIterator_type, BinaryPredicate_type, first, last, pred) OF_AdjacentFind((first), (last), (pred))
102 
103 #elif defined(HAVE_STATIC_TEMPLATE_METHOD)
104 
105 #define OFForEach(InputIterator_type, Function_type, first, last, f) OF_ForEachClass<InputIterator_type, Function_type>::OF_ForEach((first), (last), (f))
106 
107 #define OFFind(InputIterator_type, T_type, first, last, value) OF_FindClass<InputIterator_type, T_type>::OF_Find((first), (last), (value))
108 
109 #define OFFindIf(InputIterator_type, Predicate_type, first, last, pred) OF_FindIfClass<InputIterator_type, Predicate_type>::OF_FindIf((first), (last), (pred))
110 
111 #define OFAdjacentFind(ForwardIterator_type, first, last) OF_AdjacentFindClass<ForwardIterator_type, int>::OF_AdjacentFind((first), (last))
112 
113 #define OFAdjacentFindPred(ForwardIterator_type, BinaryPredicate_type, first, last, pred) OF_AdjacentFindPredClass<ForwardIterator_type, BinaryPredicate_type>::OF_AdjacentFind((first), (last), (pred))
114 #else
115 #error Your C++ Compiler is not capable of compiling this code
116 #endif
117 
118 
119 template <class InputIterator, class Function>
120 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE)
121 class OF_ForEachClass
122 {
123 public:
124 static
125 #endif
126 Function OF_ForEach(InputIterator first, InputIterator last, Function f)
127 {
128  while (first != last)
129  {
130  f(*first);
131  ++first;
132  }
133  return f;
134 }
135 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE)
136 };
137 #endif
138 
139 template <class InputIterator, class T>
140 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE)
141 class OF_FindClass
142 {
143 public:
144 static
145 #endif
146 InputIterator OF_Find(InputIterator first, InputIterator last, const T & value)
147 {
148  while (first != last && *first != value) ++ first;
149  return first;
150 }
151 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE)
152 };
153 #endif
154 
155 
156 template <class InputIterator, class Predicate>
157 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE)
158 class OF_FindIfClass
159 {
160 public:
161 static
162 #endif
163 InputIterator OF_FindIf(InputIterator first, InputIterator last,
164  Predicate pred)
165 {
166  while (first != last && !pred(*first)) ++first;
167  return first;
168 }
169 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE)
170 };
171 #endif
172 
173 template <class ForwardIterator>
174 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE)
175 class OF_AdjacentFindClass
176 {
177 public:
178 static
179 #endif
180 ForwardIterator OF_AdjacentFind(ForwardIterator first, ForwardIterator last)
181 {
182  if (first == last) return last;
183  ForwardIterator next(first);
184  while (++next != last)
185  {
186  if (*first == *next) return *first;
187  ++first;
188  }
189  return last;
190 }
191 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE)
192 };
193 #endif
194 
195 template <class ForwardIterator, class BinaryPredicate>
196 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE)
197 class OF_AdjacentFindPredClass
198 {
199 public:
200 static
201 #endif
202 ForwardIterator OF_AdjacentFind(ForwardIterator first, ForwardIterator last,
203  BinaryPredicate pred)
204 {
205  if (first == last) return last;
206  ForwardIterator next = first;
207  while(++next != last)
208  {
209  if (pred(*first, *last)) return first;
210  ++first;
211  }
212  return last;
213 }
214 
215 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE)
216 };
217 #endif
218 
219 #endif
220 #endif
221 
222 
223 


Generated on Thu Dec 20 2012 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.8.2