00001 /* 00002 * 00003 * Copyright (C) 1997-2010, OFFIS e.V. 00004 * All rights reserved. See COPYRIGHT file for details. 00005 * 00006 * This software and supporting documentation were developed by 00007 * 00008 * OFFIS e.V. 00009 * R&D Division Health 00010 * Escherweg 2 00011 * D-26121 Oldenburg, Germany 00012 * 00013 * 00014 * Module: ofstd 00015 * 00016 * Author: Andreas Barth 00017 * 00018 * Purpose: 00019 * Defines template algorithms for contaimer classes with 00020 * interfaces similar to the C++ Standard 00021 * 00022 * Last Update: $Author: joergr $ 00023 * Update Date: $Date: 2010-10-14 13:15:49 $ 00024 * CVS/RCS Revision: $Revision: 1.6 $ 00025 * Status: $State: Exp $ 00026 * 00027 * CVS/RCS Log at end of file 00028 * 00029 */ 00030 00031 #ifndef OFITER_H 00032 #define OFITER_H 00033 00034 #include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */ 00035 #include "dcmtk/ofstd/oftypes.h" 00036 00037 // Usage: 00038 // Function_type OFForEach(InputIterator_type, Function_type, first, last ,f) 00039 // Applies function f from type Function_type to the result of 00040 // derferencing every iterator in the range [first, last) starting 00041 // with first and proceeding to last -1 (first, last are of type 00042 // InputIterator_type). Returns f. 00043 // 00044 // InputIterator_type OFFind(InputIterator_type, T_type, first, last, value) 00045 // Returns the first Iterator i of type InputIterator_type in the range 00046 // [first, last) for which *i == value (of type T_type). Returns last, 00047 // if no such iterator is found 00048 // 00049 // InputIterator_type OFFindIf(InputIterator_type, Predicate_type, 00050 // first, last, pred) 00051 // Returns the first iterator i of type InputIterator_type in the range 00052 // [first, last) for which pred(*i) != false. The function pred is of 00053 // type Predicate_type. Returns last, if no such iterator is found 00054 // 00055 // ForwardIterator OFAdjacentFind(ForwardIterator_type, first, last) 00056 // Returns the first iterator i of type ForwardIterator_type such 00057 // that both i and i+1 are in the range [first, last) and *i == *(i+1) 00058 // returns last, if no such iterator is found. i+1 means the successor 00059 // of i. 00060 // 00061 // ForwardIterator OFAdjacentFindPred(ForwardIterator_type, 00062 // BinaryPredicate_type, 00063 // first, last, pred) 00064 // Returns the first iterator i of type ForwardIterator_type such 00065 // that both i and i+1 are in the range [first, last) and 00066 // pred (*i, *(i+1)) != false. 00067 // Returns last, if no such iterator is found. i+1 means the successor 00068 // of i. 00069 // 00070 // Additional Remarks: 00071 // In some template functions one template parameter is another function. 00072 // Some compilers cannot determine automatically the type of template 00073 // function parameters, so you must give them a hint casting 00074 // the parameter function to the correct type (e.g. NeXT gcc 2.5.8) 00075 00076 00077 #if defined(HAVE_STL) || defined(HAVE_STL_ALGORITHMS) 00078 // It is possible to use the standard template library list class since the 00079 // interface is nearly identical. 00080 // Important: If you want to use the standard template library (STL), no 00081 // variable within a namespace using a class of the STL shall have a name 00082 // of one class of the STL 00083 #include <algorithm> 00084 #define OFForEach(InputIterator_type, Function_type, first, last, f) for_each((first), (last), (f)) 00085 #define OFFind(InputIterator_type, T_type, first, last, value) find((first), (last), (value)) 00086 #define OFFindIf(InputIterator_type, Predicate_type, first, last, pred) find_if((first), (last), (pred)) 00087 #define OFAdjacentFind(ForwardIterator_type, first, last) adjacent_find((first), (last)) 00088 #define OFAdjacentFindPred(ForwardIterator_type, BinaryPredicate_type, first, last, pred) adjacent_find((first), (last), (pred)) 00089 #else 00090 00091 #ifdef HAVE_FUNCTION_TEMPLATE 00092 00093 #define OFForEach(InputIterator_type, Function_type, first, last, f) OF_ForEach((first), (last), (f)) 00094 00095 #define OFFind(InputIterator_type, T_type, first, last, value) OF_Find((first), (last), (value)) 00096 00097 #define OFFindIf(InputIterator_type, Predicate_type, first, last, pred) OF_FindIf((first), (last), (pred)) 00098 00099 #define OFAdjacentFind(ForwardIterator_type, first, last) OF_AdjacentFind((first), (last)) 00100 00101 #define OFAdjacentFindPred(ForwardIterator_type, BinaryPredicate_type, first, last, pred) OF_AdjacentFind((first), (last), (pred)) 00102 00103 #elif defined(HAVE_STATIC_TEMPLATE_METHOD) 00104 00105 #define OFForEach(InputIterator_type, Function_type, first, last, f) OF_ForEachClass<InputIterator_type, Function_type>::OF_ForEach((first), (last), (f)) 00106 00107 #define OFFind(InputIterator_type, T_type, first, last, value) OF_FindClass<InputIterator_type, T_type>::OF_Find((first), (last), (value)) 00108 00109 #define OFFindIf(InputIterator_type, Predicate_type, first, last, pred) OF_FindIfClass<InputIterator_type, Predicate_type>::OF_FindIf((first), (last), (pred)) 00110 00111 #define OFAdjacentFind(ForwardIterator_type, first, last) OF_AdjacentFindClass<ForwardIterator_type, int>::OF_AdjacentFind((first), (last)) 00112 00113 #define OFAdjacentFindPred(ForwardIterator_type, BinaryPredicate_type, first, last, pred) OF_AdjacentFindPredClass<ForwardIterator_type, BinaryPredicate_type>::OF_AdjacentFind((first), (last), (pred)) 00114 #else 00115 #error Your C++ Compiler is not capable of compiling this code 00116 #endif 00117 00118 00119 template <class InputIterator, class Function> 00120 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE) 00121 class OF_ForEachClass 00122 { 00123 public: 00124 static 00125 #endif 00126 Function OF_ForEach(InputIterator first, InputIterator last, Function f) 00127 { 00128 while (first != last) 00129 { 00130 f(*first); 00131 ++first; 00132 } 00133 return f; 00134 } 00135 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE) 00136 }; 00137 #endif 00138 00139 template <class InputIterator, class T> 00140 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE) 00141 class OF_FindClass 00142 { 00143 public: 00144 static 00145 #endif 00146 InputIterator OF_Find(InputIterator first, InputIterator last, const T & value) 00147 { 00148 while (first != last && *first != value) ++ first; 00149 return first; 00150 } 00151 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE) 00152 }; 00153 #endif 00154 00155 00156 template <class InputIterator, class Predicate> 00157 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE) 00158 class OF_FindIfClass 00159 { 00160 public: 00161 static 00162 #endif 00163 InputIterator OF_FindIf(InputIterator first, InputIterator last, 00164 Predicate pred) 00165 { 00166 while (first != last && !pred(*first)) ++first; 00167 return first; 00168 } 00169 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE) 00170 }; 00171 #endif 00172 00173 template <class ForwardIterator> 00174 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE) 00175 class OF_AdjacentFindClass 00176 { 00177 public: 00178 static 00179 #endif 00180 ForwardIterator OF_AdjacentFind(ForwardIterator first, ForwardIterator last) 00181 { 00182 if (first == last) return last; 00183 ForwardIterator next(first); 00184 while (++next != last) 00185 { 00186 if (*first == *next) return *first; 00187 ++first; 00188 } 00189 return last; 00190 } 00191 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE) 00192 }; 00193 #endif 00194 00195 template <class ForwardIterator, class BinaryPredicate> 00196 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE) 00197 class OF_AdjacentFindPredClass 00198 { 00199 public: 00200 static 00201 #endif 00202 ForwardIterator OF_AdjacentFind(ForwardIterator first, ForwardIterator last, 00203 BinaryPredicate pred) 00204 { 00205 if (first == last) return last; 00206 ForwardIterator next = first; 00207 while(++next != last) 00208 { 00209 if (pred(*first, *last)) return first; 00210 ++first; 00211 } 00212 return last; 00213 } 00214 00215 #if defined(HAVE_STATIC_TEMPLATE_METHOD) && !defined(HAVE_FUNCTION_TEMPLATE) 00216 }; 00217 #endif 00218 00219 #endif 00220 #endif 00221 00222 00223