Topic: Generalized standard algorithms?


Author: NDos Dannyu <ndospark320@naver.com>
Date: Sat, 19 Sep 2015 17:29:52 -0700 (PDT)
Raw View
------=_Part_3695_1843898497.1442708992663
Content-Type: multipart/alternative;
 boundary="----=_Part_3696_2082478782.1442708992664"

------=_Part_3696_2082478782.1442708992664
Content-Type: text/plain; charset=UTF-8

Just for generality...

mismatch: finds the first position where two *or more* ranges differ.
*template <class InputIt1, class ...InputIt>*
*tuple<InputIt1, InputIt...> mismatch(InputIt1 first1, InputIt1 last1,
InputIt ...first) {*
*    while (first1 != last1 && ((*first1 == *first) && ... )) {*
*        ++first1;*
*        ++first...;*
*    } **return make_tuple(first1, first...);*
*}*
*template <class InputIt1, class ...InputIt, class N_aryPredicate>*
*tuple<InputIt1, InputIt...> mismatch(InputIt1 first1, InputIt1 last1,
InputIt ...first, N_aryPredicate p) {*
*    while (first1 != last1 && p(*first1, *first...) {*
*        ++first1;*
*        ++first...;*
*    } **return make_tuple(first1, first...);*
*}*

equal: determines if two *or more* sets of elements are the same.
*template <class InputIt1, class ...InputIt>*
*bool equal(InputIt1 first1, InputIt1 last1, InputIt ...first) {*
*    return last1 == get<0>(mismatch(first1, last1, first...));*
*}*
*template <class InputIt1, class ...InputIt, class N_aryPredicate>*
*bool mismatch(InputIt1 first1, InputIt1 last1, InputIt ...first,
N_aryPredicate p) {*
*    return last1 == get<0>(mismatch(first1, last1, first..., p));*
*}*

*find_first_n_of: searches for any n consecutive elements of a set of
elements.*
*template <class InputIt, class ForwardIt>*
*InputIt find_first_n_of(InputIt first, InputIt last, ForwardIt s_first,
ForwardIt s_last, size_t n) {*
*    size_t match(0);*
*    while (first != last) {*
*        ForwardIt it(s_first);*
*        while (it != s_last) {*
*            if (*first == *it) ** {*
*                ++match;*
*                if (match == n) return first;*
*                break;*
*            } **++it;*
*        } **if (it == s_last) match = 0;*
*        ++first;*
*    } **return* *last;*
*}*
*template <class InputIt, class ForwardIt, class BinaryPredicate>*
*InputIt find_first_n_of(InputIt first, InputIt last, ForwardIt s_first,
ForwardIt s_last, BinaryPredicate p, size_t n) {*
*    size_t match(0);*
*    while (first != last) {*
*        ForwardIt it(s_first);*
*        while (it != s_last) {*
*            if (p(*first, *it)) ** {*
*                ++match;*
*                if (match == n) return first;*
*                break;*
*            } **++it;*
*        } **if (it == s_last) match = 0;*
*        ++first;*
*    } **return* *last;*
*}*

transform: applies a function to a range of elements with given operation, *which
is n_ary*.
*template<class* *InputIt1, class ...InputIt, class OutputIt, class
N_aryOperation>*
*OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt ...first,
OutputIt d_first,  N_aryOperation N_ary_op) {*
*    while (first1 != last1)*
*        *d_first++ = N_ary_op(*first1++, *first++...);*
*    return d_first;*
*}*

max: returns *the largest of three or more elements*.
*template<class T>*
*const T &max(const T init, const T &...args) {*
*    return max(init, max(args...));*
*}*
*template<class T, class Compare>*
*const T &max(const T init, const T &...args, Compare comp) {*
*    return max(init, max(args..., comp), comp);*
*}*

min: returns *the smallest of **three or more **elements*.
*template<class T>*
*const T &min(const T init, const T &...args) {*
*    return min(init, min(args...));*
*}*
*template<class T, class Compare>*
*const T &min(const T init, const T &...args, Compare comp) {*
*    return min(init, min(args..., comp), comp);*
*}*

minmax: returns *the smallest and the largest of three of more elements*.
*template<class T>*
*pair<const T &, const T &> minmax(const T &...args) {*
*    return make_pair(const T (min(args...), max(args...));*
*}*
*template<class T, class Compare>*
*pair<const T &, const T &> minmax(const T &...args, Compare comp) {*
*    return make_pair(const T (min(args..., comp), max(args..., comp));*
*}*

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_3696_2082478782.1442708992664
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>Just for generality...</div><div><br></div><div>misma=
tch: finds the first position where=C2=A0two <u>or more</u>=C2=A0ranges dif=
fer.</div><div><strong>template &lt;class InputIt1, class ...InputIt&gt;</s=
trong></div><div><strong>tuple&lt;InputIt1, InputIt...&gt; mismatch(InputIt=
1 first1, InputIt1 last1, InputIt ...first) {</strong></div><div><strong>=
=C2=A0=C2=A0=C2=A0 while (first1 !=3D last1 &amp;&amp; ((*first1 =3D=3D=C2=
=A0*first)=C2=A0&amp;&amp; ... )) {</strong></div><div><strong>=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ++first1;</strong></div><div><strong>=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ++first...;</strong></div><div><strong=
>=C2=A0=C2=A0=C2=A0 } </strong><strong>return make_tuple(first1, first...);=
</strong></div><div><strong>}</strong></div><div><strong>template &lt;class=
 InputIt1, class ...InputIt, class N_aryPredicate&gt;</strong></div><div><d=
iv><strong>tuple&lt;InputIt1, InputIt...&gt; mismatch(InputIt1 first1, Inpu=
tIt1 last1, InputIt ...first, N_aryPredicate p) {</strong></div><div><stron=
g>=C2=A0=C2=A0=C2=A0 while (first1 !=3D last1 &amp;&amp; p(*first1, *first.=
...) {</strong></div><div><strong>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
 ++first1;</strong></div><div><strong>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0 ++first...;</strong></div><div><strong>=C2=A0=C2=A0=C2=A0 } </strong=
><strong>return make_tuple(first1, first...);</strong></div><div><strong>}<=
/strong></div><div><br></div><div>equal: determines if two <u>or more</u> s=
ets of elements are the same.</div><div><div><strong>template &lt;class Inp=
utIt1, class ...InputIt&gt;</strong></div><div><strong>bool=C2=A0equal(Inpu=
tIt1 first1, InputIt1 last1, InputIt ...first) {</strong></div><div><strong=
>=C2=A0=C2=A0=C2=A0 return last1 =3D=3D get&lt;0&gt;(mismatch(first1, last1=
, first...));</strong></div><div><strong>}</strong></div><div><strong>templ=
ate &lt;class InputIt1, class ...InputIt, class N_aryPredicate&gt;</strong>=
</div><div><div><strong>bool=C2=A0mismatch(InputIt1 first1, InputIt1 last1,=
 InputIt ...first, N_aryPredicate p) {</strong></div><div><strong>=C2=A0=C2=
=A0=C2=A0 return last1 =3D=3D get&lt;0&gt;(mismatch(first1, last1, first...=
, p));</strong></div><div><strong>}</strong></div><div><br></div><div><u>fi=
nd_first_n_of: searches for any n consecutive elements of a set of elements=
..</u></div><div><strong>template &lt;class InputIt, class ForwardIt&gt;</st=
rong></div><div><strong>InputIt find_first_n_of(InputIt first, InputIt last=
, ForwardIt s_first, ForwardIt s_last, size_t n) {</strong><div><strong>=C2=
=A0=C2=A0=C2=A0=C2=A0size_t match(0);</strong></div></div><div><strong>=C2=
=A0=C2=A0=C2=A0 while (first !=3D last) {</strong><div><strong>=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ForwardIt it(s_first);</strong></div></div><=
div><strong>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0while (it !=3D =
s_last) {</strong></div><div><strong>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (*first =3D=3D *it)=C2=A0</strong><strong=
> {</strong></div><div><strong>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ++match;</strong></div><di=
v><strong>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (match =3D=3D n) return first;</strong></div=
><div><strong>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 break;</strong></div><div><strong>=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } </strong><stron=
g>++it;</strong></div><div><strong>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0 } </strong><strong>if=C2=A0(it =3D=3D s_last) match =3D 0;</strong></di=
v><div><strong>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ++first;</strong>=
</div><div><strong>=C2=A0=C2=A0=C2=A0 } </strong><strong>return</strong>=C2=
=A0<strong>last;</strong></div><div><strong>}</strong></div><div><div><stro=
ng>template &lt;class InputIt, class ForwardIt, class BinaryPredicate&gt;</=
strong></div><div><strong>InputIt find_first_n_of(InputIt first, InputIt la=
st, ForwardIt s_first, ForwardIt s_last, BinaryPredicate p, size_t n) {</st=
rong><div><strong>=C2=A0=C2=A0=C2=A0=C2=A0size_t match(0);</strong></div></=
div><div><strong>=C2=A0=C2=A0=C2=A0 while (first !=3D last) {</strong><div>=
<strong>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ForwardIt it(s_first);</=
strong></div></div><div><strong>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0while (it !=3D s_last) {</strong></div><div><strong>=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (p(*first, *it))=C2=
=A0</strong><strong> {</strong></div><div><strong>=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ++match;=
</strong></div><div><strong>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (match =3D=3D n) return fi=
rst;</strong></div><div><strong>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 break;</strong></div><div>=
<strong>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 =
} </strong><strong>++it;</strong></div><div><strong>=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0 } </strong><strong>if=C2=A0(it =3D=3D s_last) match =
=3D 0;</strong></div><div><strong>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0 ++first;</strong></div><div><strong>=C2=A0=C2=A0=C2=A0 } </strong><stro=
ng>return</strong>=C2=A0<strong>last;</strong></div><div><strong>}</strong>=
</div><div><strong><br></strong></div></div><div>transform: applies a funct=
ion to a range of elements with given operation, <u>which is n_ary</u>.</di=
v><div><strong>template&lt;class</strong> <strong>InputIt1, class ...InputI=
t, class OutputIt, class N_aryOperation&gt;</strong></div><div><strong>Outp=
utIt transform(InputIt1 first1,=C2=A0InputIt1 last1,=C2=A0InputIt ...first,=
 OutputIt d_first,=C2=A0 N_aryOperation N_ary_op) {</strong></div><div><str=
ong>=C2=A0=C2=A0=C2=A0 while (first1 !=3D last1)</strong></div><div><strong=
>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 *d_first++ =3D N_ary_op(*first1=
++, *first++...);</strong></div><div><strong>=C2=A0=C2=A0=C2=A0=C2=A0return=
 d_first;</strong></div><div><strong>}</strong></div><div><br>max: returns =
<u>the largest of=C2=A0three or more=C2=A0elements</u>.</div><div><strong>t=
emplate&lt;class T&gt;</strong></div><div><strong>const T &amp;max(const T =
init, const T &amp;...args) {</strong></div><div><strong>=C2=A0=C2=A0=C2=A0=
 return max(init, max(args...));</strong></div><div><strong>}</strong></div=
><div><div><strong>template&lt;class T, class Compare&gt;</strong></div><di=
v><strong>const T &amp;max(const T init, const T &amp;...args, Compare comp=
) {</strong></div><div><strong>=C2=A0=C2=A0=C2=A0 return max(init, max(args=
...., comp), comp);</strong></div><div><strong>}</strong></div><div><br></di=
v><div>min: returns <u>the smallest of </u><u>three or more=C2=A0</u><u>ele=
ments</u>.</div><div><div><strong>template&lt;class T&gt;</strong></div><di=
v><strong>const T &amp;min(const T init, const T &amp;...args) {</strong></=
div><div><strong>=C2=A0=C2=A0=C2=A0 return min(init, min(args...));</strong=
></div><div><strong>}</strong></div><div><div><strong>template&lt;class T, =
class Compare&gt;</strong></div><div><strong>const T &amp;min(const T init,=
 const T &amp;...args, Compare comp) {</strong></div><div><strong>=C2=A0=C2=
=A0=C2=A0 return min(init, min(args..., comp), comp);</strong></div><div><s=
trong>}</strong></div><div><br></div><div>minmax: returns <u>the smallest a=
nd the largest of three of more elements</u>.</div><div><strong>template&lt=
;class T&gt;</strong></div><div><strong>pair&lt;const T &amp;, const T &amp=
;&gt;=C2=A0minmax(const T &amp;...args) {</strong></div><div><strong>=C2=A0=
=C2=A0=C2=A0 return make_pair(const T (min(args...), max(args...));</strong=
></div><div><strong>}</strong></div><div><div><strong>template&lt;class T, =
class Compare&gt;</strong></div><div><strong>pair&lt;const=C2=A0T &amp;, co=
nst T &amp;&gt;=C2=A0minmax(const T &amp;...args, Compare comp) {</strong><=
/div><div><strong>=C2=A0=C2=A0=C2=A0 return make_pair(const T (min(args...,=
 comp), max(args..., comp));</strong></div><div><strong>}</strong></div></d=
iv></div></div></div></div></div></div></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_3696_2082478782.1442708992664--
------=_Part_3695_1843898497.1442708992663--

.