contrib/mul/mbl/mbl_stl.h
Go to the documentation of this file.
00001 // This is mul/mbl/mbl_stl.h
00002 #ifndef mbl_stl_h_
00003 #define mbl_stl_h_
00004 //:
00005 // \file
00006 // \brief Useful things missing from vcl_algorithm, etc.
00007 // \author iscott
00008 // \date  Dec 2001
00009 // Actually, this is mostly an opportunity to mess around in STL to produce code
00010 // which would be much simpler in ordinary C++. Stroustrup assures us that
00011 // this approach is faster in general - which I don't really believe.
00012 //
00013 // \verbatim
00014 //  Modifications
00015 //   30 April 2004 - Martin Roberts -
00016 //    Added quite a few little functors mainly to do with iterating through maps
00017 //    for example a version of the non-standard select1st and select2nd
00018 
00019 #include <vcl_functional.h>
00020 #include <vcl_vector.h>
00021 #include <vcl_ostream.h>
00022 #include <vcl_utility.h>
00023 
00024 //: Fill an output sequence with incrementing values.
00025 // A bit like vcl_fill, but after each assignment, the value is incremented.
00026 // \return the next value in the sequence.
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 //: Fill the first n values of an output sequence with incrementing values.
00035 // A bit like vcl_fill_n, but after each assignment,
00036 // the value is incremented.
00037 // \return the next value in the sequence.
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 //: Produces a first order sequence from the supplied unary function.
00046 // The value produced at a given step is a function of the previous value.
00047 // E.g. the following is equivalent to using mbl_stl_increments
00048 // \code
00049 // mbl_stl_sequence(A.begin(), A.end(), vcl_bind1st(vcl_plus<unsigned>(), 1u), 0u);
00050 // \endcode
00051 // \return the next value in the sequence.
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 //: Produces a first order sequence of size n from the supplied function.
00060 // The value produced at a given step is a function of the previous value.
00061 // E.g. the following is equivalent to using mbl_stl_increments
00062 // \return the next value in the sequence.
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 //: Clean out a range of pointers
00071 // NB the dereferenced iterator must be a pointer
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 //: Copy elements in input range for which the supplied predicate is true
00083 //Note bizarely although the STL provides remove_copy if etc etc
00084 //the simple copy_if was dropped from the C++ standard
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 //Now some map related functors
00105 //
00106 //: select 1st element of a pair (e.g. for map iterators)
00107 //NB something like this is in the SGI extension to the STL but is not included in the standard VCL
00108 //However this is very useful with map iterators so include it here
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 //: select 2nd element of a pair (e.g. for map iterators)
00119 //NB something like this is in the SGI extension to the STL but is not included in the standard VCL
00120 //However this is very useful with map iterators so include it here
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 //Accumulate the second elements of a pair (e.g. for accumulating values through a map)
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 // End of map/pair related functors
00142 //------------------------------------------------------------------------------------
00143 //: Given a vector of things, select an indexed element
00144 //For use in eg STL transform algorithm to extract out required subset of (indexed) objects into a working vector
00145 //e.g. given vector of indices and vector of values, copy out the required subset thus
00146 // \code
00147 // vcl_vector<T> subset
00148 // subset.reserve(indices.size());
00149 // vcl_transform(indices.begin(),indices.end(),
00150 //               vcl_back_inserter(subset),
00151 //               mbl_stl_index_functor(values));
00152 // \endcode
00153 template <class T>
00154 class mbl_stl_index_functor
00155 {
00156   //This functor copies out  element vec[index]
00157   //For use in eg STL transform algorithm to extract out required subset of (indexed) points into a working vector
00158   //No bounds checking is done
00159  private:
00160   //:const reference to vector used to store the objects indexed
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 //: implementation class for use with mbl_stl_output
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 //: implementation function for use with mbl_stl_output
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 //: Allow easy stream output of STL container contents.
00194 // \verbatim
00195 // vcl_vector<int> c;
00196 // ...
00197 // vcl_cout << "The contents of c using normal << notation" <<
00198 //   mbl_stl_output(c) << vcl_endl;
00199 // \endverbatim
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 //: Find first instance of common value in two sorted sequences.
00208 // \return pair. Either *pair.first == *pair.second, or pair.first == finish1 && pair.second == finish2 if
00209 // no matches are found.
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 //: Find first instance of common value in two sequences sorted by specified comparator.
00229 // \return pair. Either *pair.first == *pair.second, or pair.first == finish1 && pair.second == finish2 if
00230 // no matches are found.
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_