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