stlab.adobe.com Adobe Systems Incorporated
mismatch.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3  Distributed under the MIT License (see accompanying file LICENSE_1_0_0.txt
4  or a copy at http://stlab.adobe.com/licenses.html)
5 */
6 
7 /*************************************************************************************************/
8 
9 #ifndef ADOBE_MISMATCH_HPP
10 #define ADOBE_MISMATCH_HPP
11 
12 #include <adobe/config.hpp>
13 
14 #include <iterator>
15 #include <utility>
16 
17 /*************************************************************************************************/
18 
19 namespace adobe {
20 
21 /*************************************************************************************************/
22 
23 namespace implementation {
24 
25 /*************************************************************************************************/
26 
27 template <typename C1, // Category of I1
28  typename C2> // Category of I2
29 struct mismatch_t;
30 
31 /*************************************************************************************************/
32 
33 } // namespace implementation
34 
35 /*************************************************************************************************/
36 
37 /*
38  REVISIT (sparent) : With mismatch_n() we are throwing away the resulting value of n - this is
39  only because it complciates using it in the implementation of mismatch() but I'm not
40  convinced that the interface to mismatch() is correct either.
41 */
42 
43 template <typename I1, // I1 models InputIterator
44  typename I2, // I2 models InputIterator
45  typename N> // N models Integer
46 std::pair<I1, I2> mismatch_n(I1 first1, I2 first2, N n)
47 {
48  while (n && *first1 == *first2)
49  {
50  --n;
51  ++first1;
52  ++first2;
53  }
54 
55  return std::make_pair(first1, first2);
56 }
57 
58 template <typename I1, // I1 models InputIterator
59  typename I2> // I2 models InputIterator
60 std::pair<I1, I2> mismatch(I1 first1, I1 last1, I2 first2, I2 last2)
61 {
62  return implementation::mismatch_t<typename std::iterator_traits<I1>::iterator_category,
63  typename std::iterator_traits<I2>::iterator_category>()(first1, last1, first2, last2);
64 }
65 
66 /*************************************************************************************************/
67 
68 namespace implementation {
69 
70 /*************************************************************************************************/
71 
72 template <typename C1, // Category of I1
73  typename C2> // Category of I2
74 struct mismatch_t
75 {
76  template <typename I1, // I1 models InputIterator
77  typename I2> // I2 models InputIterator
78  std::pair<I1, I2> operator()(I1 first1, I1 last1, I2 first2, I2 last2) const
79  {
80  while (first1 != last1 && first2 != last2 && *first1 == * first2)
81  {
82  ++first1;
83  ++first2;
84  }
85  return std::make_pair(first1, first2);
86  }
87 };
88 
89 template <>
90 struct mismatch_t <std::random_access_iterator_tag, std::random_access_iterator_tag>
91 {
92  template <typename I1, // I1 models RandomAccessIterator
93  typename I2> // I2 models RandomAccessIterator
94  std::pair<I1, I2> operator()(I1 first1, I1 last1, I2 first2, I2 last2) const
95  {
96  if ((last1 - first1) < (last2 - first2))
97  {
98  return adobe::mismatch_n(first1, first2, last1 - first1);
99  }
100 
101  return adobe::mismatch_n(first1, first2, last2 - first2);
102  }
103 };
104 
105 /*************************************************************************************************/
106 
107 } // namespace implementation
108 
109 } // namespace adobe
110 
111 /*************************************************************************************************/
112 
113 #endif
114 // ADOBE_MISMATCH_HPP
115 
116 /*************************************************************************************************/
std::pair< I1, I2 > mismatch(I1 first1, I1 last1, I2 first2, I2 last2)
Definition: mismatch.hpp:60
std::pair< I1, I2 > mismatch_n(I1 first1, I2 first2, N n)
Definition: mismatch.hpp:46
pair< T1, T2 > make_pair(T1 x, T2 y)
Definition: pair.hpp:109

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