stlab.adobe.com Adobe Systems Incorporated

mismatch.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_MISMATCH_HPP
00010 #define ADOBE_MISMATCH_HPP
00011 
00012 #include <adobe/config.hpp>
00013 
00014 #include <iterator>
00015 #include <utility>
00016 
00017 /*************************************************************************************************/
00018 
00019 namespace adobe {
00020 
00021 /*************************************************************************************************/
00022 
00023 namespace implementation {
00024 
00025 /*************************************************************************************************/
00026 
00027 template <typename C1, // Category of I1
00028           typename C2> // Category of I2
00029 struct mismatch_t;
00030 
00031 /*************************************************************************************************/
00032 
00033 } // namespace implementation
00034 
00035 /*************************************************************************************************/
00036 
00037 /*
00038     REVISIT (sparent) : With mismatch_n() we are throwing away the resulting value of n - this is
00039     only because it complciates using it in the implementation of mismatch() but I'm not
00040     convinced that the interface to mismatch() is correct either.
00041 */
00042 
00043 template <typename I1,  // I1 models InputIterator
00044           typename I2,  // I2 models InputIterator
00045           typename N>   // N models Integer
00046 std::pair<I1, I2> mismatch_n(I1 first1, I2 first2, N n)
00047 {
00048     while (n && *first1 == *first2)
00049     {
00050         --n;
00051         ++first1;
00052         ++first2;
00053     }
00054     
00055     return std::make_pair(first1, first2);
00056 }
00057 
00058 template <typename I1, // I1 models InputIterator
00059           typename I2> // I2 models InputIterator
00060 std::pair<I1, I2> mismatch(I1 first1, I1 last1, I2 first2, I2 last2)
00061 {
00062     return implementation::mismatch_t<typename std::iterator_traits<I1>::iterator_category,
00063         typename std::iterator_traits<I2>::iterator_category>()(first1, last1, first2, last2);
00064 }
00065 
00066 /*************************************************************************************************/
00067 
00068 namespace implementation {
00069 
00070 /*************************************************************************************************/
00071 
00072 template <typename C1, // Category of I1
00073           typename C2> // Category of I2
00074 struct mismatch_t
00075 {
00076     template <typename I1, // I1 models InputIterator
00077               typename I2> // I2 models InputIterator
00078     std::pair<I1, I2> operator()(I1 first1, I1 last1, I2 first2, I2 last2) const
00079     {
00080         while (first1 != last1 && first2 != last2 && *first1 == * first2)
00081         {
00082             ++first1;
00083             ++first2;
00084         }
00085         return std::make_pair(first1, first2);
00086     }
00087 };
00088 
00089 template <>
00090 struct mismatch_t <std::random_access_iterator_tag, std::random_access_iterator_tag>
00091 {
00092     template <typename I1, // I1 models RandomAccessIterator
00093               typename I2> // I2 models RandomAccessIterator
00094     std::pair<I1, I2> operator()(I1 first1, I1 last1, I2 first2, I2 last2) const
00095     {
00096         if ((last1 - first1) < (last2 - first2))
00097         {
00098             return adobe::mismatch_n(first1, first2, last1 - first1);
00099         }
00100 
00101         return adobe::mismatch_n(first1, first2, last2 - first2);
00102     }
00103 };
00104 
00105 /*************************************************************************************************/
00106 
00107 } // namespace implementation
00108 
00109 } // namespace adobe
00110 
00111 /*************************************************************************************************/
00112 
00113 #endif
00114 // ADOBE_MISMATCH_HPP
00115 
00116 /*************************************************************************************************/

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