stlab.adobe.com Adobe Systems Incorporated

copy.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2007 Adobe Systems Incorporated
00003     Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
00004     or a copy at http://stlab.adobe.com/licenses.html)
00005 */
00006 
00007 /*************************************************************************************************/
00008 
00009 #ifndef ADOBE_ALGORITHM_COPY_HPP
00010 #define ADOBE_ALGORITHM_COPY_HPP
00011 
00012 #include <adobe/config.hpp>
00013 
00014 #include <boost/range/begin.hpp>
00015 #include <boost/range/end.hpp>
00016 #include <boost/range/size.hpp>
00017 
00018 #include <algorithm>
00019 #include <iterator>
00020 
00021 /*************************************************************************************************/
00022 
00023 namespace adobe {
00024 
00025 /*************************************************************************************************/
00035 /*************************************************************************************************/
00036 
00042 template <class InputRange, class OutputIterator>
00043 inline OutputIterator copy(const InputRange& range, OutputIterator result)
00044 {
00045     return std::copy(boost::begin(range), boost::end(range), result);
00046 }
00047 
00053 template <class BidirectionalRange1, class BidirectionalIterator2>
00054 inline BidirectionalIterator2 copy_backward(BidirectionalRange1& range1, BidirectionalIterator2 result)
00055 {
00056     return std::copy_backward(boost::begin(range1), boost::end(range1), result);
00057 }
00058 
00064 template <class BidirectionalRange1, class BidirectionalIterator2>
00065 inline BidirectionalIterator2 copy_backward(const BidirectionalRange1& range1, BidirectionalIterator2 result)
00066 {
00067     return std::copy_backward(boost::begin(range1), boost::end(range1), result);
00068 }
00069 
00070 /*************************************************************************************************/
00071 #ifndef ADOBE_NO_DOCUMENTATION
00072 namespace implementation {
00073 
00074 /*************************************************************************************************/
00079 template <class InputIter, class Size, class OutputIter>
00080 std::pair<InputIter, OutputIter> copy_n(InputIter first, Size count,
00081                                         OutputIter result,
00082                                         std::input_iterator_tag) 
00083 {
00084    for ( ; count > 0; --count) {
00085       *result = *first;
00086       ++first;
00087       ++result;
00088    }
00089    return std::pair<InputIter, OutputIter>(first, result);
00090 }
00091 
00097 template <class RAIter, class Size, class OutputIter>
00098 inline std::pair<RAIter, OutputIter>
00099 copy_n(RAIter first, Size count, OutputIter result, std::random_access_iterator_tag) 
00100 {
00101    RAIter last = first + count;
00102    return std::pair<RAIter, OutputIter>(last, std::copy(first, last, result));
00103 }
00104 
00105 /*************************************************************************************************/
00106 
00107 } // namespace implementation
00108 #endif
00109 /*************************************************************************************************/
00110 
00116 template <class InputIter, class Size, class OutputIter>
00117 inline std::pair<InputIter, OutputIter> copy_n(InputIter first, Size count, OutputIter result) 
00118 {
00119     return implementation::copy_n(first, count, result,
00120         typename std::iterator_traits<InputIter>::iterator_category());
00121 }
00122 
00123 /*************************************************************************************************/
00124 
00125 #ifndef ADOBE_NO_DOCUMENTATION
00126 namespace implementation {
00127 
00128 /*************************************************************************************************/
00135 template <typename I, // I models RandomAccessIterator
00136           typename F> // F models RandomAccessIterator
00137 inline std::pair<I, F> copy_bounded(I first, I last,
00138                                   F result_first, F result_last,
00139                                   std::random_access_iterator_tag, std::random_access_iterator_tag)
00140 {
00141     return adobe::copy_n(first, std::min(last - first, result_last - result_first), result_first);
00142 }
00143 
00149 template <typename I, // I models InputIterator
00150           typename F> // F models ForwardIterator
00151 inline std::pair<I, F> copy_bounded(I first, I last,
00152                                     F result_first, F result_last,
00153                                     std::input_iterator_tag, std::forward_iterator_tag)
00154 {
00155     while (first != last && result_first != result_last)
00156     {
00157         *result_first = *first;
00158         ++first; ++result_first;
00159     }
00160     
00161     return std::make_pair(first, result_first);
00162 }
00163 
00164 /*************************************************************************************************/
00165 
00166 } // namespace implementation
00167 #endif
00168 
00169 /*************************************************************************************************/
00175 template <typename I, // I models InputIterator
00176           typename F> // F models ForwardIterator
00177 inline std::pair<I, F> copy_bounded(I first, I last, F result_first, F result_last)
00178 {
00179     return implementation::copy_bounded(first, last, result_first, result_last,
00180         typename std::iterator_traits<I>::iterator_category(),
00181         typename std::iterator_traits<F>::iterator_category());
00182 }
00183 
00184 /*************************************************************************************************/
00185 
00191 template <typename I, // I models InputIterator
00192           typename O, // O models OutputIterator
00193           typename T> // T == value_type(I)
00194 inline std::pair<I, O> copy_sentinal(I f, O o, const T& x)
00195 {
00196     while (*f != x) {
00197         *o = *f;
00198         ++f, ++o;
00199     }
00200     return std::make_pair(f, o);
00201 }
00202 
00203 /*************************************************************************************************/
00204 
00210 template <typename I, // I models InputIterator
00211           typename O> // O models OutputIterator
00212 inline std::pair<I, O> copy_sentinal(I f, O o)
00213 {
00214     return copy_sentinal(f, o, typename std::iterator_traits<I>::value_type());
00215 }
00216 
00217 /*************************************************************************************************/
00218 
00219 } // namespace adobe
00220 
00221 /*************************************************************************************************/
00222 
00223 #endif
00224 
00225 /*************************************************************************************************/

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google