00001
00002 #ifndef mbl_stl_h_
00003 #define mbl_stl_h_
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <vcl_functional.h>
00020 #include <vcl_vector.h>
00021 #include <vcl_ostream.h>
00022 #include <vcl_utility.h>
00023
00024
00025
00026
00027 template<class Out, class T>
00028 inline T mbl_stl_increments(Out first, Out last, T init)
00029 {
00030 for (; first != last; ++first, ++init) *first = init;
00031 return init;
00032 }
00033
00034
00035
00036
00037
00038 template<class Out, class Size, class T>
00039 inline T mbl_stl_increments_n(Out first, Size n, T init)
00040 {
00041 for (; 0 < n; ++first, --n, ++init) *first = init;
00042 return init;
00043 }
00044
00045
00046
00047
00048
00049
00050
00051
00052 template<class Out, class T, class UnOp>
00053 inline T mbl_stl_sequence(Out first, Out last, UnOp op, T init)
00054 {
00055 for (;first != last; ++first, init = op(init)) *first = init;
00056 return init;
00057 }
00058
00059
00060
00061
00062
00063 template<class Out, class Size, class T, class UnOp>
00064 inline T mbl_stl_sequence_n(Out first, Size n, UnOp op, T init)
00065 {
00066 for (; 0 < n; ++first, --n, init = op(init)) *first = init;
00067 return init;
00068 }
00069
00070
00071
00072 template<class iterType>
00073 inline void mbl_stl_clean(iterType first, iterType last)
00074 {
00075 for (; first != last; ++first)
00076 {
00077 delete *first;
00078 *first=0;
00079 }
00080 }
00081
00082
00083
00084
00085 template<typename InputIterator,
00086 typename OutputIterator,
00087 typename Predicate>
00088 inline OutputIterator mbl_stl_copy_if(InputIterator begin, InputIterator end,
00089 OutputIterator destBegin,
00090 Predicate pred)
00091 {
00092 while (begin != end)
00093 {
00094 if (pred(*begin))
00095 {
00096 *destBegin++ = *begin;
00097 }
00098 ++begin;
00099 }
00100 return destBegin;
00101 }
00102
00103
00104
00105
00106
00107
00108
00109 template <class Pair>
00110 struct mbl_stl_select1st : public vcl_unary_function<Pair, typename Pair::first_type>
00111 {
00112 inline typename Pair::first_type const & operator()(Pair const & pair) const
00113 {
00114 return pair.first;
00115 }
00116 };
00117
00118
00119
00120
00121 template <class Pair>
00122 struct mbl_stl_select2nd : public vcl_unary_function<Pair, typename Pair::second_type>
00123 {
00124 inline typename Pair::second_type const & operator()(Pair const & pair) const
00125 {
00126 return pair.second;
00127 }
00128 };
00129
00130
00131 template <class Pair>
00132 struct mbl_stl_add2nd : public vcl_binary_function<typename Pair::second_type, Pair, typename Pair::second_type>
00133 {
00134 inline typename Pair::second_type operator()(typename Pair::second_type partSum, Pair const & x2 ) const
00135 {
00136 return partSum + x2.second;
00137 }
00138 };
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 template <class T>
00154 class mbl_stl_index_functor
00155 {
00156
00157
00158
00159 private:
00160
00161 const vcl_vector<T >& vec_;
00162
00163 public:
00164 mbl_stl_index_functor(const vcl_vector<T >& vec): vec_(vec) {}
00165 inline const T& operator()(unsigned index) const { return vec_[index]; }
00166 };
00167
00168
00169
00170
00171 template <class Cont>
00172 class mbl_stl_output_t1
00173 {
00174 public:
00175 const Cont &c;
00176 const char *sep;
00177 mbl_stl_output_t1(const Cont& c, const char * sep): c(c), sep(sep) {}
00178 };
00179
00180
00181 template <class Cont> inline
00182 vcl_ostream& operator<<(vcl_ostream& s, const mbl_stl_output_t1<Cont>& t)
00183 {
00184 if (t.c.empty()) return s;
00185 VCL_DISAPPEARING_TYPENAME Cont::const_iterator it=t.c.begin(), end=t.c.end();
00186 s << *it;
00187 ++it;
00188 for (; it!=end; ++it)
00189 s << t.sep << *it;
00190 return s;
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200 template <class Cont> inline
00201 mbl_stl_output_t1<Cont> mbl_stl_output(const Cont &c, const char * sep=" ")
00202 {
00203 return mbl_stl_output_t1<Cont>(c, sep);
00204 }
00205
00206
00207
00208
00209
00210 template <class IT1, class IT2>
00211 inline vcl_pair<IT1, IT2>
00212 mbl_stl_find_common_value(IT1 start1, IT1 finish1, IT2 start2, IT2 finish2)
00213 {
00214 vcl_pair<IT1, IT2> its(start1, start2);
00215 while (true)
00216 {
00217 if (its.first == finish1 || its.second == finish2) return make_pair(finish1, finish2);
00218
00219 else if (*its.first < *its.second)
00220 ++its.first;
00221 else if (*its.second < *its.first)
00222 ++its.second;
00223 else return its;
00224 }
00225 }
00226
00227
00228
00229
00230
00231 template <class IT1, class IT2, class CMP>
00232 inline vcl_pair<IT1, IT2>
00233 mbl_stl_find_common_value(IT1 start1, IT1 finish1, IT2 start2, IT2 finish2, CMP comp = CMP())
00234 {
00235 vcl_pair<IT1, IT2> its(start1, start2);
00236 while (true)
00237 {
00238 if (its.first == finish1 || its.second == finish2) return make_pair(finish1, finish2);
00239
00240 else if (comp(*its.first, *its.second))
00241 ++its.first;
00242 else if (comp(*its.second, *its.first))
00243 ++its.second;
00244 else return its;
00245 }
00246 }
00247
00248 #endif // mbl_stl_h_