mirror of
https://github.com/boostorg/compose.git
synced 2026-01-19 04:02:16 +00:00
This commit was generated by cvs2svn to compensate for changes in r4,
which included commits to RCS files with non-trunk default branches. [SVN r7621]
This commit is contained in:
96
.gitattributes
vendored
Normal file
96
.gitattributes
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
* text=auto !eol svneol=native#text/plain
|
||||
*.gitattributes text svneol=native#text/plain
|
||||
|
||||
# Scriptish formats
|
||||
*.bat text svneol=native#text/plain
|
||||
*.bsh text svneol=native#text/x-beanshell
|
||||
*.cgi text svneol=native#text/plain
|
||||
*.cmd text svneol=native#text/plain
|
||||
*.js text svneol=native#text/javascript
|
||||
*.php text svneol=native#text/x-php
|
||||
*.pl text svneol=native#text/x-perl
|
||||
*.pm text svneol=native#text/x-perl
|
||||
*.py text svneol=native#text/x-python
|
||||
*.sh eol=lf svneol=LF#text/x-sh
|
||||
configure eol=lf svneol=LF#text/x-sh
|
||||
|
||||
# Image formats
|
||||
*.bmp binary svneol=unset#image/bmp
|
||||
*.gif binary svneol=unset#image/gif
|
||||
*.ico binary svneol=unset#image/ico
|
||||
*.jpeg binary svneol=unset#image/jpeg
|
||||
*.jpg binary svneol=unset#image/jpeg
|
||||
*.png binary svneol=unset#image/png
|
||||
*.tif binary svneol=unset#image/tiff
|
||||
*.tiff binary svneol=unset#image/tiff
|
||||
*.svg text svneol=native#image/svg%2Bxml
|
||||
|
||||
# Data formats
|
||||
*.pdf binary svneol=unset#application/pdf
|
||||
*.avi binary svneol=unset#video/avi
|
||||
*.doc binary svneol=unset#application/msword
|
||||
*.dsp text svneol=crlf#text/plain
|
||||
*.dsw text svneol=crlf#text/plain
|
||||
*.eps binary svneol=unset#application/postscript
|
||||
*.gz binary svneol=unset#application/gzip
|
||||
*.mov binary svneol=unset#video/quicktime
|
||||
*.mp3 binary svneol=unset#audio/mpeg
|
||||
*.ppt binary svneol=unset#application/vnd.ms-powerpoint
|
||||
*.ps binary svneol=unset#application/postscript
|
||||
*.psd binary svneol=unset#application/photoshop
|
||||
*.rdf binary svneol=unset#text/rdf
|
||||
*.rss text svneol=unset#text/xml
|
||||
*.rtf binary svneol=unset#text/rtf
|
||||
*.sln text svneol=native#text/plain
|
||||
*.swf binary svneol=unset#application/x-shockwave-flash
|
||||
*.tgz binary svneol=unset#application/gzip
|
||||
*.vcproj text svneol=native#text/xml
|
||||
*.vcxproj text svneol=native#text/xml
|
||||
*.vsprops text svneol=native#text/xml
|
||||
*.wav binary svneol=unset#audio/wav
|
||||
*.xls binary svneol=unset#application/vnd.ms-excel
|
||||
*.zip binary svneol=unset#application/zip
|
||||
|
||||
# Text formats
|
||||
.htaccess text svneol=native#text/plain
|
||||
*.bbk text svneol=native#text/xml
|
||||
*.cmake text svneol=native#text/plain
|
||||
*.css text svneol=native#text/css
|
||||
*.dtd text svneol=native#text/xml
|
||||
*.htm text svneol=native#text/html
|
||||
*.html text svneol=native#text/html
|
||||
*.ini text svneol=native#text/plain
|
||||
*.log text svneol=native#text/plain
|
||||
*.mak text svneol=native#text/plain
|
||||
*.qbk text svneol=native#text/plain
|
||||
*.rst text svneol=native#text/plain
|
||||
*.sql text svneol=native#text/x-sql
|
||||
*.txt text svneol=native#text/plain
|
||||
*.xhtml text svneol=native#text/xhtml%2Bxml
|
||||
*.xml text svneol=native#text/xml
|
||||
*.xsd text svneol=native#text/xml
|
||||
*.xsl text svneol=native#text/xml
|
||||
*.xslt text svneol=native#text/xml
|
||||
*.xul text svneol=native#text/xul
|
||||
*.yml text svneol=native#text/plain
|
||||
boost-no-inspect text svneol=native#text/plain
|
||||
CHANGES text svneol=native#text/plain
|
||||
COPYING text svneol=native#text/plain
|
||||
INSTALL text svneol=native#text/plain
|
||||
Jamfile text svneol=native#text/plain
|
||||
Jamroot text svneol=native#text/plain
|
||||
Jamfile.v2 text svneol=native#text/plain
|
||||
Jamrules text svneol=native#text/plain
|
||||
Makefile* text svneol=native#text/plain
|
||||
README text svneol=native#text/plain
|
||||
TODO text svneol=native#text/plain
|
||||
|
||||
# Code formats
|
||||
*.c text svneol=native#text/plain
|
||||
*.cpp text svneol=native#text/plain
|
||||
*.h text svneol=native#text/plain
|
||||
*.hpp text svneol=native#text/plain
|
||||
*.ipp text svneol=native#text/plain
|
||||
*.tpp text svneol=native#text/plain
|
||||
*.jam text svneol=native#text/plain
|
||||
*.java text svneol=native#text/plain
|
||||
442
compose.hpp
Normal file
442
compose.hpp
Normal file
@@ -0,0 +1,442 @@
|
||||
/* The following code example is taken from the book
|
||||
* "The C++ Standard Library - A Tutorial and Reference"
|
||||
* by Nicolai M. Josuttis, Addison-Wesley, 1999
|
||||
*
|
||||
* (C) Copyright Nicolai M. Josuttis 1999.
|
||||
* Permission to copy, use, modify, sell and distribute this software
|
||||
* is granted provided this copyright notice appears in all copies.
|
||||
* This software is provided "as is" without express or implied
|
||||
* warranty, and with no claim as to its suitability for any purpose.
|
||||
*/
|
||||
/* supplementing compose function objects
|
||||
* Son Dez 26 22:11:12 MET 1999
|
||||
*/
|
||||
#ifndef BOOST_COMPOSE_HPP
|
||||
#define BOOST_COMPOSE_HPP
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace boost {
|
||||
|
||||
/**********************************************************
|
||||
* type nullary_function
|
||||
* - as supplement to unary_function and binary_function
|
||||
**********************************************************/
|
||||
template <class Result>
|
||||
struct nullary_function {
|
||||
typedef Result result_type;
|
||||
};
|
||||
|
||||
/**********************************************************
|
||||
* ptr_fun for functions with no argument
|
||||
**********************************************************/
|
||||
template <class Result>
|
||||
class pointer_to_nullary_function : public nullary_function<Result>
|
||||
{
|
||||
protected:
|
||||
Result (*ptr)();
|
||||
public:
|
||||
pointer_to_nullary_function() {
|
||||
}
|
||||
explicit pointer_to_nullary_function(Result (*x)()) : ptr(x) {
|
||||
}
|
||||
Result operator()() const {
|
||||
return ptr();
|
||||
}
|
||||
};
|
||||
|
||||
template <class Result>
|
||||
inline pointer_to_nullary_function<Result> ptr_fun(Result (*x)())
|
||||
{
|
||||
return pointer_to_nullary_function<Result>(x);
|
||||
}
|
||||
|
||||
/*********** compose_f_gx_t and compose_f_gx **********************/
|
||||
|
||||
/* class for the compose_f_gx adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
class compose_f_gx_t
|
||||
: public std::unary_function<typename OP2::argument_type,
|
||||
typename OP1::result_type>
|
||||
{
|
||||
private:
|
||||
OP1 op1; // process: op1(op2(x))
|
||||
OP2 op2;
|
||||
public:
|
||||
// constructor
|
||||
compose_f_gx_t(const OP1& o1, const OP2& o2)
|
||||
: op1(o1), op2(o2) {
|
||||
}
|
||||
|
||||
// function call
|
||||
typename OP1::result_type
|
||||
operator()(const typename OP2::argument_type& x) const {
|
||||
return op1(op2(x));
|
||||
}
|
||||
};
|
||||
|
||||
/* convenience functions for the compose_f_gx adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
inline compose_f_gx_t<OP1,OP2>
|
||||
compose_f_gx (const OP1& o1, const OP2& o2) {
|
||||
return compose_f_gx_t<OP1,OP2>(o1,o2);
|
||||
}
|
||||
|
||||
/*********** compose_f_gx_hx_t and compose_f_gx_hx **********************/
|
||||
|
||||
/* class for the compose_f_gx_hx adapter
|
||||
*/
|
||||
template <class OP1, class OP2, class OP3>
|
||||
class compose_f_gx_hx_t
|
||||
: public std::unary_function<typename OP2::argument_type,
|
||||
typename OP1::result_type>
|
||||
{
|
||||
private:
|
||||
OP1 op1; // process: op1(op2(x),op3(x))
|
||||
OP2 op2;
|
||||
OP3 op3;
|
||||
public:
|
||||
// constructor
|
||||
compose_f_gx_hx_t (const OP1& o1, const OP2& o2, const OP3& o3)
|
||||
: op1(o1), op2(o2), op3(o3) {
|
||||
}
|
||||
|
||||
// function call
|
||||
typename OP1::result_type
|
||||
operator()(const typename OP2::argument_type& x) const {
|
||||
return op1(op2(x),op3(x));
|
||||
}
|
||||
};
|
||||
|
||||
/* convenience functions for the compose_f_gx_hx adapter
|
||||
*/
|
||||
template <class OP1, class OP2, class OP3>
|
||||
inline compose_f_gx_hx_t<OP1,OP2,OP3>
|
||||
compose_f_gx_hx (const OP1& o1, const OP2& o2, const OP3& o3) {
|
||||
return compose_f_gx_hx_t<OP1,OP2,OP3>(o1,o2,o3);
|
||||
}
|
||||
|
||||
/*********** compose_f_gxy_t and compose_f_gxy **********************/
|
||||
|
||||
/* class for the compose_f_gxy adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
class compose_f_gxy_t
|
||||
: public std::binary_function<typename OP2::first_argument_type,
|
||||
typename OP2::second_argument_type,
|
||||
typename OP1::result_type>
|
||||
{
|
||||
private:
|
||||
OP1 op1; // process: op1(op2(x,y))
|
||||
OP2 op2;
|
||||
public:
|
||||
// constructor
|
||||
compose_f_gxy_t (const OP1& o1, const OP2& o2)
|
||||
: op1(o1), op2(o2) {
|
||||
}
|
||||
|
||||
// function call
|
||||
typename OP1::result_type
|
||||
operator()(const typename OP2::first_argument_type& x,
|
||||
const typename OP2::second_argument_type& y) const {
|
||||
return op1(op2(x,y));
|
||||
}
|
||||
};
|
||||
|
||||
/* convenience function for the compose_f_gxy adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
inline compose_f_gxy_t<OP1,OP2>
|
||||
compose_f_gxy (const OP1& o1, const OP2& o2) {
|
||||
return compose_f_gxy_t<OP1,OP2>(o1,o2);
|
||||
}
|
||||
|
||||
/*********** compose_f_gx_hy_t and compose_f_gx_hy **********************/
|
||||
|
||||
/* class for the compose_f_gx_hy adapter
|
||||
*/
|
||||
template <class OP1, class OP2, class OP3>
|
||||
class compose_f_gx_hy_t
|
||||
: public std::binary_function<typename OP2::argument_type,
|
||||
typename OP3::argument_type,
|
||||
typename OP1::result_type>
|
||||
{
|
||||
private:
|
||||
OP1 op1; // process: op1(op2(x),op3(y))
|
||||
OP2 op2;
|
||||
OP3 op3;
|
||||
public:
|
||||
// constructor
|
||||
compose_f_gx_hy_t (const OP1& o1, const OP2& o2, const OP3& o3)
|
||||
: op1(o1), op2(o2), op3(o3) {
|
||||
}
|
||||
|
||||
// function call
|
||||
typename OP1::result_type
|
||||
operator()(const typename OP2::argument_type& x,
|
||||
const typename OP3::argument_type& y) const {
|
||||
return op1(op2(x),op3(y));
|
||||
}
|
||||
};
|
||||
|
||||
/* convenience function for the compose_f_gx_hy adapter
|
||||
*/
|
||||
template <class OP1, class OP2, class OP3>
|
||||
inline compose_f_gx_hy_t<OP1,OP2,OP3>
|
||||
compose_f_gx_hy (const OP1& o1, const OP2& o2, const OP3& o3) {
|
||||
return compose_f_gx_hy_t<OP1,OP2,OP3>(o1,o2,o3);
|
||||
}
|
||||
|
||||
/*********** compose_f_g_t and compose_f_g **********************/
|
||||
|
||||
/* class for the compose_f_g adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
class compose_f_g_t
|
||||
: public boost::nullary_function<typename OP1::result_type>
|
||||
{
|
||||
private:
|
||||
OP1 op1; // process: op1(op2())
|
||||
OP2 op2;
|
||||
public:
|
||||
// constructor
|
||||
compose_f_g_t(const OP1& o1, const OP2& o2)
|
||||
: op1(o1), op2(o2) {
|
||||
}
|
||||
|
||||
// function call
|
||||
typename OP1::result_type
|
||||
operator()() const {
|
||||
return op1(op2());
|
||||
}
|
||||
};
|
||||
|
||||
/* convenience functions for the compose_f_g adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
inline compose_f_g_t<OP1,OP2>
|
||||
compose_f_g (const OP1& o1, const OP2& o2) {
|
||||
return compose_f_g_t<OP1,OP2>(o1,o2);
|
||||
}
|
||||
|
||||
} /* namespace boost */
|
||||
|
||||
#endif /*BOOST_COMPOSE_HPP*/
|
||||
/* supplementing compose function objects
|
||||
* Son Dez 26 22:14:55 MET 1999
|
||||
*/
|
||||
#ifndef BOOST_COMPOSE_HPP
|
||||
#define BOOST_COMPOSE_HPP
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace boost {
|
||||
|
||||
/**********************************************************
|
||||
* type nullary_function
|
||||
* - as supplement to unary_function and binary_function
|
||||
**********************************************************/
|
||||
template <class Result>
|
||||
struct nullary_function {
|
||||
typedef Result result_type;
|
||||
};
|
||||
|
||||
/**********************************************************
|
||||
* ptr_fun for functions with no argument
|
||||
**********************************************************/
|
||||
template <class Result>
|
||||
class pointer_to_nullary_function : public nullary_function<Result>
|
||||
{
|
||||
protected:
|
||||
Result (*ptr)();
|
||||
public:
|
||||
pointer_to_nullary_function() {
|
||||
}
|
||||
explicit pointer_to_nullary_function(Result (*x)()) : ptr(x) {
|
||||
}
|
||||
Result operator()() const {
|
||||
return ptr();
|
||||
}
|
||||
};
|
||||
|
||||
template <class Result>
|
||||
inline pointer_to_nullary_function<Result> ptr_fun(Result (*x)())
|
||||
{
|
||||
return pointer_to_nullary_function<Result>(x);
|
||||
}
|
||||
|
||||
/*********** compose_f_gx_t and compose_f_gx **********************/
|
||||
|
||||
/* class for the compose_f_gx adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
class compose_f_gx_t
|
||||
: public std::unary_function<typename OP2::argument_type,
|
||||
typename OP1::result_type>
|
||||
{
|
||||
private:
|
||||
OP1 op1; // process: op1(op2(x))
|
||||
OP2 op2;
|
||||
public:
|
||||
// constructor
|
||||
compose_f_gx_t(const OP1& o1, const OP2& o2)
|
||||
: op1(o1), op2(o2) {
|
||||
}
|
||||
|
||||
// function call
|
||||
typename OP1::result_type
|
||||
operator()(const typename OP2::argument_type& x) const {
|
||||
return op1(op2(x));
|
||||
}
|
||||
};
|
||||
|
||||
/* convenience functions for the compose_f_gx adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
inline compose_f_gx_t<OP1,OP2>
|
||||
compose_f_gx (const OP1& o1, const OP2& o2) {
|
||||
return compose_f_gx_t<OP1,OP2>(o1,o2);
|
||||
}
|
||||
|
||||
/*********** compose_f_gx_hx_t and compose_f_gx_hx **********************/
|
||||
|
||||
/* class for the compose_f_gx_hx adapter
|
||||
*/
|
||||
template <class OP1, class OP2, class OP3>
|
||||
class compose_f_gx_hx_t
|
||||
: public std::unary_function<typename OP2::argument_type,
|
||||
typename OP1::result_type>
|
||||
{
|
||||
private:
|
||||
OP1 op1; // process: op1(op2(x),op3(x))
|
||||
OP2 op2;
|
||||
OP3 op3;
|
||||
public:
|
||||
// constructor
|
||||
compose_f_gx_hx_t (const OP1& o1, const OP2& o2, const OP3& o3)
|
||||
: op1(o1), op2(o2), op3(o3) {
|
||||
}
|
||||
|
||||
// function call
|
||||
typename OP1::result_type
|
||||
operator()(const typename OP2::argument_type& x) const {
|
||||
return op1(op2(x),op3(x));
|
||||
}
|
||||
};
|
||||
|
||||
/* convenience functions for the compose_f_gx_hx adapter
|
||||
*/
|
||||
template <class OP1, class OP2, class OP3>
|
||||
inline compose_f_gx_hx_t<OP1,OP2,OP3>
|
||||
compose_f_gx_hx (const OP1& o1, const OP2& o2, const OP3& o3) {
|
||||
return compose_f_gx_hx_t<OP1,OP2,OP3>(o1,o2,o3);
|
||||
}
|
||||
|
||||
/*********** compose_f_gxy_t and compose_f_gxy **********************/
|
||||
|
||||
/* class for the compose_f_gxy adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
class compose_f_gxy_t
|
||||
: public std::binary_function<typename OP2::first_argument_type,
|
||||
typename OP2::second_argument_type,
|
||||
typename OP1::result_type>
|
||||
{
|
||||
private:
|
||||
OP1 op1; // process: op1(op2(x,y))
|
||||
OP2 op2;
|
||||
public:
|
||||
// constructor
|
||||
compose_f_gxy_t (const OP1& o1, const OP2& o2)
|
||||
: op1(o1), op2(o2) {
|
||||
}
|
||||
|
||||
// function call
|
||||
typename OP1::result_type
|
||||
operator()(const typename OP2::first_argument_type& x,
|
||||
const typename OP2::second_argument_type& y) const {
|
||||
return op1(op2(x,y));
|
||||
}
|
||||
};
|
||||
|
||||
/* convenience function for the compose_f_gxy adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
inline compose_f_gxy_t<OP1,OP2>
|
||||
compose_f_gxy (const OP1& o1, const OP2& o2) {
|
||||
return compose_f_gxy_t<OP1,OP2>(o1,o2);
|
||||
}
|
||||
|
||||
/*********** compose_f_gx_hy_t and compose_f_gx_hy **********************/
|
||||
|
||||
/* class for the compose_f_gx_hy adapter
|
||||
*/
|
||||
template <class OP1, class OP2, class OP3>
|
||||
class compose_f_gx_hy_t
|
||||
: public std::binary_function<typename OP2::argument_type,
|
||||
typename OP3::argument_type,
|
||||
typename OP1::result_type>
|
||||
{
|
||||
private:
|
||||
OP1 op1; // process: op1(op2(x),op3(y))
|
||||
OP2 op2;
|
||||
OP3 op3;
|
||||
public:
|
||||
// constructor
|
||||
compose_f_gx_hy_t (const OP1& o1, const OP2& o2, const OP3& o3)
|
||||
: op1(o1), op2(o2), op3(o3) {
|
||||
}
|
||||
|
||||
// function call
|
||||
typename OP1::result_type
|
||||
operator()(const typename OP2::argument_type& x,
|
||||
const typename OP3::argument_type& y) const {
|
||||
return op1(op2(x),op3(y));
|
||||
}
|
||||
};
|
||||
|
||||
/* convenience function for the compose_f_gx_hy adapter
|
||||
*/
|
||||
template <class OP1, class OP2, class OP3>
|
||||
inline compose_f_gx_hy_t<OP1,OP2,OP3>
|
||||
compose_f_gx_hy (const OP1& o1, const OP2& o2, const OP3& o3) {
|
||||
return compose_f_gx_hy_t<OP1,OP2,OP3>(o1,o2,o3);
|
||||
}
|
||||
|
||||
/*********** compose_f_g_t and compose_f_g **********************/
|
||||
|
||||
/* class for the compose_f_g adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
class compose_f_g_t
|
||||
: public boost::nullary_function<typename OP1::result_type>
|
||||
{
|
||||
private:
|
||||
OP1 op1; // process: op1(op2())
|
||||
OP2 op2;
|
||||
public:
|
||||
// constructor
|
||||
compose_f_g_t(const OP1& o1, const OP2& o2)
|
||||
: op1(o1), op2(o2) {
|
||||
}
|
||||
|
||||
// function call
|
||||
typename OP1::result_type
|
||||
operator()() const {
|
||||
return op1(op2());
|
||||
}
|
||||
};
|
||||
|
||||
/* convenience functions for the compose_f_g adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
inline compose_f_g_t<OP1,OP2>
|
||||
compose_f_g (const OP1& o1, const OP2& o2) {
|
||||
return compose_f_g_t<OP1,OP2>(o1,o2);
|
||||
}
|
||||
|
||||
} /* namespace boost */
|
||||
|
||||
#endif /*BOOST_COMPOSE_HPP*/
|
||||
37
compose1.cpp
Normal file
37
compose1.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
/* The following code example is taken from the book
|
||||
* "The C++ Standard Library - A Tutorial and Reference"
|
||||
* by Nicolai M. Josuttis, Addison-Wesley, 1999
|
||||
*
|
||||
* (C) Copyright Nicolai M. Josuttis 1999.
|
||||
* Permission to copy, use, modify, sell and distribute this software
|
||||
* is granted provided this copyright notice appears in all copies.
|
||||
* This software is provided "as is" without express or implied
|
||||
* warranty, and with no claim as to its suitability for any purpose.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <iterator>
|
||||
#include "print.hpp"
|
||||
#include "compose.hpp"
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> coll;
|
||||
|
||||
// insert elements from 1 to 9
|
||||
for (int i=1; i<=9; ++i) {
|
||||
coll.push_back(i);
|
||||
}
|
||||
PRINT_ELEMENTS(coll);
|
||||
|
||||
// for each element add 10 and multiply by 5
|
||||
transform (coll.begin(),coll.end(),
|
||||
ostream_iterator<int>(cout," "),
|
||||
compose_f_gx(bind2nd(multiplies<int>(),5),
|
||||
bind2nd(plus<int>(),10)));
|
||||
cout << endl;
|
||||
}
|
||||
42
compose2.cpp
Normal file
42
compose2.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
/* The following code example is taken from the book
|
||||
* "The C++ Standard Library - A Tutorial and Reference"
|
||||
* by Nicolai M. Josuttis, Addison-Wesley, 1999
|
||||
*
|
||||
* (C) Copyright Nicolai M. Josuttis 1999.
|
||||
* Permission to copy, use, modify, sell and distribute this software
|
||||
* is granted provided this copyright notice appears in all copies.
|
||||
* This software is provided "as is" without express or implied
|
||||
* warranty, and with no claim as to its suitability for any purpose.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include "print.hpp"
|
||||
#include "compose.hpp"
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> coll;
|
||||
|
||||
// insert elements from 1 to 9
|
||||
for (int i=1; i<=9; ++i) {
|
||||
coll.push_back(i);
|
||||
}
|
||||
PRINT_ELEMENTS(coll);
|
||||
|
||||
// remove all elements that are greater than four and less than seven
|
||||
// - retain new end
|
||||
vector<int>::iterator pos;
|
||||
pos = remove_if (coll.begin(),coll.end(),
|
||||
compose_f_gx_hx(logical_and<bool>(),
|
||||
bind2nd(greater<int>(),4),
|
||||
bind2nd(less<int>(),7)));
|
||||
|
||||
// remove ``removed'' elements in coll
|
||||
coll.erase(pos,coll.end());
|
||||
|
||||
PRINT_ELEMENTS(coll);
|
||||
}
|
||||
37
compose3.cpp
Normal file
37
compose3.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
/* The following code example is taken from the book
|
||||
* "The C++ Standard Library - A Tutorial and Reference"
|
||||
* by Nicolai M. Josuttis, Addison-Wesley, 1999
|
||||
*
|
||||
* (C) Copyright Nicolai M. Josuttis 1999.
|
||||
* Permission to copy, use, modify, sell and distribute this software
|
||||
* is granted provided this copyright notice appears in all copies.
|
||||
* This software is provided "as is" without express or implied
|
||||
* warranty, and with no claim as to its suitability for any purpose.
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
#include "compose.hpp"
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
int main()
|
||||
{
|
||||
string s("Internationalization");
|
||||
string sub("Nation");
|
||||
|
||||
// search substring case insensitive
|
||||
string::iterator pos;
|
||||
pos = search (s.begin(),s.end(), // string to search in
|
||||
sub.begin(),sub.end(), // substring to search
|
||||
compose_f_gx_hy(equal_to<int>(), // compar. criterion
|
||||
ptr_fun(::toupper),
|
||||
ptr_fun(::toupper)));
|
||||
|
||||
if (pos != s.end()) {
|
||||
cout << "\"" << sub << "\" is part of \"" << s << "\""
|
||||
<< endl;
|
||||
}
|
||||
}
|
||||
37
compose4.cpp
Normal file
37
compose4.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
/* The following code example is taken from the book
|
||||
* "The C++ Standard Library - A Tutorial and Reference"
|
||||
* by Nicolai M. Josuttis, Addison-Wesley, 1999
|
||||
*
|
||||
* (C) Copyright Nicolai M. Josuttis 1999.
|
||||
* Permission to copy, use, modify, sell and distribute this software
|
||||
* is granted provided this copyright notice appears in all copies.
|
||||
* This software is provided "as is" without express or implied
|
||||
* warranty, and with no claim as to its suitability for any purpose.
|
||||
*/
|
||||
#include <list>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <cstdlib>
|
||||
#include "print.hpp"
|
||||
#include "compose.hpp"
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
list<int> coll;
|
||||
|
||||
// insert five random numbers
|
||||
generate_n (back_inserter(coll), // beginning of destination range
|
||||
5, // count
|
||||
rand); // new value generator
|
||||
PRINT_ELEMENTS(coll);
|
||||
|
||||
// overwrite with five new random numbers
|
||||
// in the range between 0 (including) and 10 (excluding)
|
||||
generate (coll.begin(), coll.end(), // destination range
|
||||
compose_f_g(bind2nd(modulus<int>(),10),
|
||||
ptr_fun(rand)));
|
||||
PRINT_ELEMENTS(coll);
|
||||
}
|
||||
226
include/boost/compose.hpp
Normal file
226
include/boost/compose.hpp
Normal file
@@ -0,0 +1,226 @@
|
||||
/* supplementing compose function objects
|
||||
* Fri Jul 16 21:01:58 MEST 1999
|
||||
*/
|
||||
/* The following code example is taken from the book
|
||||
* "The C++ Standard Library - A Tutorial and Reference"
|
||||
* by Nicolai M. Josuttis, Addison-Wesley, 1999
|
||||
*
|
||||
* (C) Copyright Nicolai M. Josuttis 1999.
|
||||
* Permission to copy, use, modify, sell and distribute this software
|
||||
* is granted provided this copyright notice appears in all copies.
|
||||
* This software is provided "as is" without express or implied
|
||||
* warranty, and with no claim as to its suitability for any purpose.
|
||||
*/
|
||||
#ifndef BOOST_COMPOSE_HPP
|
||||
#define BOOST_COMPOSE_HPP
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace boost {
|
||||
|
||||
/**********************************************************
|
||||
* type nullary_function
|
||||
* - as supplement to unary_function and binary_function
|
||||
**********************************************************/
|
||||
template <class Result>
|
||||
struct nullary_function {
|
||||
typedef Result result_type;
|
||||
};
|
||||
|
||||
/**********************************************************
|
||||
* ptr_fun for functions with no argument
|
||||
**********************************************************/
|
||||
template <class Result>
|
||||
class pointer_to_nullary_function : public nullary_function<Result>
|
||||
{
|
||||
protected:
|
||||
Result (*ptr)();
|
||||
public:
|
||||
pointer_to_nullary_function() {
|
||||
}
|
||||
explicit pointer_to_nullary_function(Result (*x)()) : ptr(x) {
|
||||
}
|
||||
Result operator()() const {
|
||||
return ptr();
|
||||
}
|
||||
};
|
||||
|
||||
template <class Result>
|
||||
inline pointer_to_nullary_function<Result> ptr_fun(Result (*x)())
|
||||
{
|
||||
return pointer_to_nullary_function<Result>(x);
|
||||
}
|
||||
|
||||
/*********** compose_f_gx_t and compose_f_gx **********************/
|
||||
|
||||
/* class for the compose_f_gx adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
class compose_f_gx_t
|
||||
: public std::unary_function<typename OP2::argument_type,
|
||||
typename OP1::result_type>
|
||||
{
|
||||
private:
|
||||
OP1 op1; // process: op1(op2(x))
|
||||
OP2 op2;
|
||||
public:
|
||||
// constructor
|
||||
compose_f_gx_t(const OP1& o1, const OP2& o2)
|
||||
: op1(o1), op2(o2) {
|
||||
}
|
||||
|
||||
// function call
|
||||
typename OP1::result_type
|
||||
operator()(const typename OP2::argument_type& x) const {
|
||||
return op1(op2(x));
|
||||
}
|
||||
};
|
||||
|
||||
/* convenience functions for the compose_f_gx adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
inline compose_f_gx_t<OP1,OP2>
|
||||
compose_f_gx (const OP1& o1, const OP2& o2) {
|
||||
return compose_f_gx_t<OP1,OP2>(o1,o2);
|
||||
}
|
||||
|
||||
/*********** compose_f_gx_hx_t and compose_f_gx_hx **********************/
|
||||
|
||||
/* class for the compose_f_gx_hx adapter
|
||||
*/
|
||||
template <class OP1, class OP2, class OP3>
|
||||
class compose_f_gx_hx_t
|
||||
: public std::unary_function<typename OP2::argument_type,
|
||||
typename OP1::result_type>
|
||||
{
|
||||
private:
|
||||
OP1 op1; // process: op1(op2(x),op3(x))
|
||||
OP2 op2;
|
||||
OP3 op3;
|
||||
public:
|
||||
// constructor
|
||||
compose_f_gx_hx_t (const OP1& o1, const OP2& o2, const OP3& o3)
|
||||
: op1(o1), op2(o2), op3(o3) {
|
||||
}
|
||||
|
||||
// function call
|
||||
typename OP1::result_type
|
||||
operator()(const typename OP2::argument_type& x) const {
|
||||
return op1(op2(x),op3(x));
|
||||
}
|
||||
};
|
||||
|
||||
/* convenience functions for the compose_f_gx_hx adapter
|
||||
*/
|
||||
template <class OP1, class OP2, class OP3>
|
||||
inline compose_f_gx_hx_t<OP1,OP2,OP3>
|
||||
compose_f_gx_hx (const OP1& o1, const OP2& o2, const OP3& o3) {
|
||||
return compose_f_gx_hx_t<OP1,OP2,OP3>(o1,o2,o3);
|
||||
}
|
||||
|
||||
/*********** compose_f_gxy_t and compose_f_gxy **********************/
|
||||
|
||||
/* class for the compose_f_gxy adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
class compose_f_gxy_t
|
||||
: public std::binary_function<typename OP2::first_argument_type,
|
||||
typename OP2::second_argument_type,
|
||||
typename OP1::result_type>
|
||||
{
|
||||
private:
|
||||
OP1 op1; // process: op1(op2(x,y))
|
||||
OP2 op2;
|
||||
public:
|
||||
// constructor
|
||||
compose_f_gxy_t (const OP1& o1, const OP2& o2)
|
||||
: op1(o1), op2(o2) {
|
||||
}
|
||||
|
||||
// function call
|
||||
typename OP1::result_type
|
||||
operator()(const typename OP2::first_argument_type& x,
|
||||
const typename OP2::second_argument_type& y) const {
|
||||
return op1(op2(x,y));
|
||||
}
|
||||
};
|
||||
|
||||
/* convenience function for the compose_f_gxy adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
inline compose_f_gxy_t<OP1,OP2>
|
||||
compose_f_gxy (const OP1& o1, const OP2& o2) {
|
||||
return compose_f_gxy_t<OP1,OP2>(o1,o2);
|
||||
}
|
||||
|
||||
/*********** compose_f_gx_hy_t and compose_f_gx_hy **********************/
|
||||
|
||||
/* class for the compose_f_gx_hy adapter
|
||||
*/
|
||||
template <class OP1, class OP2, class OP3>
|
||||
class compose_f_gx_hy_t
|
||||
: public std::binary_function<typename OP2::argument_type,
|
||||
typename OP3::argument_type,
|
||||
typename OP1::result_type>
|
||||
{
|
||||
private:
|
||||
OP1 op1; // process: op1(op2(x),op3(y))
|
||||
OP2 op2;
|
||||
OP3 op3;
|
||||
public:
|
||||
// constructor
|
||||
compose_f_gx_hy_t (const OP1& o1, const OP2& o2, const OP3& o3)
|
||||
: op1(o1), op2(o2), op3(o3) {
|
||||
}
|
||||
|
||||
// function call
|
||||
typename OP1::result_type
|
||||
operator()(const typename OP2::argument_type& x,
|
||||
const typename OP3::argument_type& y) const {
|
||||
return op1(op2(x),op3(y));
|
||||
}
|
||||
};
|
||||
|
||||
/* convenience function for the compose_f_gx_hy adapter
|
||||
*/
|
||||
template <class OP1, class OP2, class OP3>
|
||||
inline compose_f_gx_hy_t<OP1,OP2,OP3>
|
||||
compose_f_gx_hy (const OP1& o1, const OP2& o2, const OP3& o3) {
|
||||
return compose_f_gx_hy_t<OP1,OP2,OP3>(o1,o2,o3);
|
||||
}
|
||||
|
||||
/*********** compose_f_g_t and compose_f_g **********************/
|
||||
|
||||
/* class for the compose_f_g adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
class compose_f_g_t
|
||||
: public boost::nullary_function<typename OP1::result_type>
|
||||
{
|
||||
private:
|
||||
OP1 op1; // process: op1(op2())
|
||||
OP2 op2;
|
||||
public:
|
||||
// constructor
|
||||
compose_f_g_t(const OP1& o1, const OP2& o2)
|
||||
: op1(o1), op2(o2) {
|
||||
}
|
||||
|
||||
// function call
|
||||
typename OP1::result_type
|
||||
operator()() const {
|
||||
return op1(op2());
|
||||
}
|
||||
};
|
||||
|
||||
/* convenience functions for the compose_f_g adapter
|
||||
*/
|
||||
template <class OP1, class OP2>
|
||||
inline compose_f_g_t<OP1,OP2>
|
||||
compose_f_g (const OP1& o1, const OP2& o2) {
|
||||
return compose_f_g_t<OP1,OP2>(o1,o2);
|
||||
}
|
||||
|
||||
} /* namespace boost */
|
||||
|
||||
#endif /*BOOST_COMPOSE_HPP*/
|
||||
28
print.hpp
Normal file
28
print.hpp
Normal file
@@ -0,0 +1,28 @@
|
||||
/* The following code example is taken from the book
|
||||
* "The C++ Standard Library - A Tutorial and Reference"
|
||||
* by Nicolai M. Josuttis, Addison-Wesley, 1999
|
||||
*
|
||||
* (C) Copyright Nicolai M. Josuttis 1999.
|
||||
* Permission to copy, use, modify, sell and distribute this software
|
||||
* is granted provided this copyright notice appears in all copies.
|
||||
* This software is provided "as is" without express or implied
|
||||
* warranty, and with no claim as to its suitability for any purpose.
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
/* PRINT_ELEMENTS()
|
||||
* - prints optional C-string optcstr followed by
|
||||
* - all elements of the collection coll
|
||||
* - separated by spaces
|
||||
*/
|
||||
template <class T>
|
||||
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
|
||||
{
|
||||
typename T::const_iterator pos;
|
||||
|
||||
std::cout << optcstr;
|
||||
for (pos=coll.begin(); pos!=coll.end(); ++pos) {
|
||||
std::cout << *pos << ' ';
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
Reference in New Issue
Block a user