Statistics
| Branch: | Tag: | Revision:

root / host / include / uhd / utils / algorithm.hpp @ ce5940f8

History | View | Annotate | Download (5.5 KB)

1
//
2
// Copyright 2010 Ettus Research LLC
3
//
4
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

    
18
#ifndef INCLUDED_UHD_UTILS_ALGORITHM_HPP
19
#define INCLUDED_UHD_UTILS_ALGORITHM_HPP
20

    
21
#include <algorithm>
22
#include <boost/range/begin.hpp>
23
#include <boost/range/end.hpp>
24
#include <boost/range/size.hpp>
25

    
26
/*! \file algorithm.hpp
27
 * Useful templated functions and classes that I like to pretend are part of stl.
28
 * Many of the range wrapper functions come with recent versions of boost (1.43).
29
 */
30
namespace std{
31

    
32
    /*!
33
     * A wrapper around std::copy that takes ranges instead of iterators.
34
     *
35
     * Copy the elements of the source range into the destination range.
36
     * The destination range should be at least as large as the source range.
37
     *
38
     * \param src the range of elements to copy from
39
     * \param dst the range of elements to be filled
40
     */
41
    template<typename RangeSrc, typename RangeDst> inline
42
    void copy(const RangeSrc &src, RangeDst &dst){
43
        std::copy(boost::begin(src), boost::end(src), boost::begin(dst));
44
    }
45

    
46
    /*!
47
     * A wrapper around std::sort that takes a range instead of an iterator.
48
     *
49
     * The elements are sorted into ascending order using the less-than operator.
50
     *
51
     * \param range the range of elements to be sorted
52
     */
53
    template<typename Range> inline void sort(Range &range){
54
        return std::sort(boost::begin(range), boost::end(range));
55
    }
56

    
57
    /*!
58
     * A wrapper around std::sort that takes a range instead of an iterator.
59
     *
60
     * The elements are sorted into ascending order using the less-than operator.
61
     * This wrapper sorts the elements non-destructively into a new range.
62
     * Based on the builtin python function sorted(...)
63
     *
64
     * \param range the range of elements to be sorted
65
     * \return a new range with the elements sorted
66
     */
67
    template<typename Range> inline Range sorted(const Range &range){
68
        Range srange(range); std::sort(srange); return srange;
69
    }
70

    
71
    /*!
72
     * A wrapper around std::reverse that takes a range instead of an iterator.
73
     *
74
     * The elements are reversed into descending order using the less-than operator.
75
     *
76
     * \param range the range of elements to be reversed
77
     */
78
    template<typename Range> inline void reverse(Range &range){
79
        return std::reverse(boost::begin(range), boost::end(range));
80
    }
81

    
82
    /*!
83
     * A wrapper around std::reverse that takes a range instead of an iterator.
84
     *
85
     * The elements are reversed into descending order using the less-than operator.
86
     * This wrapper reverses the elements non-destructively into a new range.
87
     * Based on the builtin python function reversed(...)
88
     *
89
     * \param range the range of elements to be reversed
90
     * \return a new range with the elements reversed
91
     */
92
    template<typename Range> inline Range reversed(const Range &range){
93
        Range srange(range); std::reverse(srange); return srange;
94
    }
95

    
96
    /*!
97
     * Is the value found within the elements in this range?
98
     *
99
     * Uses std::find to search the iterable for an element.
100
     *
101
     * \param range the elements to search through
102
     * \param value the match to look for in the range
103
     * \return true when the value is found in the range
104
     */
105
    template<typename Range, typename T> inline
106
    bool has(const Range &range, const T &value){
107
        return boost::end(range) != std::find(boost::begin(range), boost::end(range), value);
108
    }
109

    
110
    /*!
111
     * Count the number of appearances of a value in a range.
112
     *
113
     * Uses std::count to count the appearances in the range.
114
     *
115
     * \param range the elements to iterate through
116
     * \param value the value to count in the range
117
     * \return the number of appearances of the value
118
     */
119
    template<typename Range, typename T> inline
120
    size_t count(const Range &range, const T &value){
121
        return std::count(boost::begin(range), boost::end(range), value);
122
    }
123

    
124
    /*!
125
     * Are the ranges equal (are their elements equivalent)?
126
     *
127
     * Uses std::equal to search the iterable for an element.
128
     *
129
     * \param range1 the first range of elements
130
     * \param range2 the second range of elements
131
     * \return true when the elements are equivalent
132
     */
133
    template<typename Range> inline
134
    bool equal(const Range &range1, const Range &range2){
135
        return (boost::size(range1) == boost::size(range2)) and
136
        std::equal(boost::begin(range1), boost::end(range1), boost::begin(range2));
137
    }
138

    
139
    /*!
140
     * A templated clip implementation.
141
     * \param val the value to clip between an upper and lower limit
142
     * \param bound1 the upper or lower bound
143
     * \param bound2 the upper or lower bound
144
     * \return the value clipped at the bounds
145
     */
146
    template<typename T> inline T clip(T val, T bound1, T bound2){
147
        return std::min(std::max(val, std::min(bound1, bound2)), std::max(bound1, bound2));
148
    }
149

    
150
}//namespace std
151

    
152
#endif /* INCLUDED_UHD_UTILS_ALGORITHM_HPP */