2
// mlib/range/my_adaptors.h
3
// This file is part of Bombono DVD project.
5
// Copyright (c) 2010 Ilya Murav'jov
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
17
// You should have received a copy of the GNU General Public License
18
// along with this program; if not, write to the Free Software
19
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#ifndef __MLIB_RANGE_MY_ADAPTORS_H__
23
#define __MLIB_RANGE_MY_ADAPTORS_H__
25
#include <boost/range/iterator_range.hpp>
26
#include <boost/iterator/transform_iterator.hpp>
27
#include <boost/iterator/filter_iterator.hpp>
29
namespace RangeAdaptor {
35
// :TODO: рефакторинг range_transform & range_filter
36
template <class Range, class UnaryFunc>
37
struct range_transform
39
typedef typename boost::range_iterator<Range>::type iterator;
40
typedef boost::transform_iterator<UnaryFunc, iterator> op_iterator;
41
typedef boost::iterator_range<op_iterator> type;
44
template <class Range, class UnaryFunc>
45
typename range_transform<Range, UnaryFunc>::type
46
_transform(Range& r, const UnaryFunc& f)
48
typedef typename boost::range_iterator<Range>::type iterator;
49
iterator beg = boost::begin(r), end = boost::end(r);
51
typedef boost::transform_iterator<UnaryFunc, iterator> op_iterator;
52
return boost::iterator_range<op_iterator>(op_iterator(beg, f), op_iterator(end, f));
55
template <class Range, class UnaryFunc>
56
typename range_transform<Range, UnaryFunc>::type
57
transform(Range& r, const UnaryFunc& f)
59
return _transform(r, f);
62
template <class Range, class UnaryFunc>
63
typename range_transform<const Range, UnaryFunc>::type
64
transform(const Range& r, const UnaryFunc& f)
66
return _transform(r, f);
73
template <class Range, class Predicate>
76
typedef typename boost::range_iterator<Range>::type iterator;
77
typedef boost::filter_iterator<Predicate, iterator> op_iterator;
78
typedef boost::iterator_range<op_iterator> type;
81
template <class Range, class Predicate>
82
typename range_filter<Range, Predicate>::type
83
_filter(Range& r, const Predicate& f)
85
typedef typename boost::range_iterator<Range>::type iterator;
86
iterator beg = boost::begin(r), end = boost::end(r);
88
typedef boost::filter_iterator<Predicate, iterator> op_iterator;
89
return boost::iterator_range<op_iterator>(op_iterator(f, beg, end), op_iterator(f, end, end));
92
template <class Range, class Predicate>
93
typename range_filter<Range, Predicate>::type
94
filter(Range& r, const Predicate& f)
99
template <class Range, class Predicate>
100
typename range_filter<const Range, Predicate>::type
101
filter(const Range& r, const Predicate& f)
103
return _filter(r, f);
106
} // namespace RangeAdaptor
108
#endif // #ifndef __MLIB_RANGE_MY_ADAPTORS_H__