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