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