/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock

« back to all changes in this revision

Viewing changes to src/utility/numeric

  • Committer: edam
  • Date: 2011-11-04 13:48:15 UTC
  • Revision ID: edam@waxworlds.org-20111104134815-kj7qdhqfbxaj1tng
finished writing up electronics notes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*      Copyright (C) 2004 Garrett A. Kajmowicz
2
 
 
3
 
        This file is part of the uClibc++ Library.
4
 
 
5
 
        This library is free software; you can redistribute it and/or
6
 
        modify it under the terms of the GNU Lesser General Public
7
 
        License as published by the Free Software Foundation; either
8
 
        version 2.1 of the License, or (at your option) any later version.
9
 
 
10
 
        This library is distributed in the hope that it will be useful,
11
 
        but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
        Lesser General Public License for more details.
14
 
 
15
 
        You should have received a copy of the GNU Lesser General Public
16
 
        License along with this library; if not, write to the Free Software
17
 
        Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
*/
19
 
 
20
 
#include <basic_definitions>
21
 
#include <exception>
22
 
 
23
 
#ifndef __STD_NUMERIC_HEADER
24
 
#define __STD_NUMERIC_HEADER 1
25
 
 
26
 
#pragma GCC visibility push(default)
27
 
 
28
 
namespace std{
29
 
        template <class InputIterator, class T> _UCXXEXPORT 
30
 
                T accumulate(InputIterator first, InputIterator last, T init)
31
 
        {
32
 
                while(first != last){
33
 
                        init = init + *first;
34
 
                        ++first;
35
 
                }
36
 
                return init;
37
 
        }
38
 
 
39
 
        template <class InputIterator, class T, class BinaryOperation> _UCXXEXPORT 
40
 
                T accumulate(InputIterator first, InputIterator last, T init, BinaryOperation binary_op)
41
 
        {
42
 
                while(first != last){
43
 
                        init = binary_op(init, *first);
44
 
                        ++first;
45
 
                }
46
 
                return init;
47
 
        }
48
 
 
49
 
 
50
 
        template <class InputIterator1, class InputIterator2, class T> _UCXXEXPORT 
51
 
                T inner_product(InputIterator1 first1, InputIterator1 last1,
52
 
                        InputIterator2 first2, T init)
53
 
        {
54
 
                while(first1 != last1){
55
 
                        init = init + *first1 * *first2;
56
 
                        ++first1;
57
 
                        ++first2;
58
 
                }
59
 
                return init;
60
 
        }
61
 
 
62
 
        template <class InputIterator1, class InputIterator2, class T,
63
 
                class BinaryOperation1, class BinaryOperation2> _UCXXEXPORT 
64
 
                T inner_product(InputIterator1 first1, InputIterator1 last1,
65
 
                        InputIterator2 first2, T init,
66
 
                        BinaryOperation1 binary_op1,
67
 
                        BinaryOperation2 binary_op2)
68
 
        {
69
 
                while(first1 != last1){
70
 
                        init = binary_op1(init, binary_op2(*first1, *first2));
71
 
                        ++first1;
72
 
                        ++first2;
73
 
                }
74
 
                return init;
75
 
        }
76
 
 
77
 
        template <class InputIterator, class OutputIterator> _UCXXEXPORT 
78
 
                OutputIterator partial_sum(InputIterator first, InputIterator last,
79
 
                OutputIterator result)
80
 
        {
81
 
                OutputIterator temp(result);
82
 
                *result = *first;
83
 
                ++first;
84
 
                ++result;
85
 
 
86
 
                while(first != last){
87
 
                        *result = *first + *temp;
88
 
                        temp = result;
89
 
                        ++first;
90
 
                        ++result;
91
 
                }
92
 
                return result;
93
 
        }
94
 
 
95
 
 
96
 
        template <class InputIterator, class OutputIterator, class BinaryOperation> _UCXXEXPORT 
97
 
                OutputIterator partial_sum(InputIterator first, InputIterator last,
98
 
                OutputIterator result, BinaryOperation binary_op)
99
 
        {
100
 
                OutputIterator temp(result);
101
 
                *result = *first;
102
 
                ++first;
103
 
                ++result;
104
 
 
105
 
                while(first != last){
106
 
                        *result = binary_op(*first, *temp);
107
 
                        temp = result;
108
 
                        ++first;
109
 
                        ++result;
110
 
                }
111
 
                return result;
112
 
        }
113
 
 
114
 
 
115
 
        template <class InputIterator, class OutputIterator> _UCXXEXPORT 
116
 
                OutputIterator
117
 
                adjacent_difference(InputIterator first, InputIterator last,
118
 
                        OutputIterator result)
119
 
        {
120
 
                OutputIterator temp(first);
121
 
                *result = *first;
122
 
                ++first;
123
 
                ++result;
124
 
 
125
 
                while(first != last){
126
 
                        *result = *first - *temp;
127
 
                        temp = first;
128
 
                        ++first;
129
 
                        ++result;
130
 
                }
131
 
 
132
 
                return result;
133
 
        }
134
 
 
135
 
 
136
 
        template <class InputIterator, class OutputIterator, class BinaryOperation> _UCXXEXPORT 
137
 
                OutputIterator
138
 
                adjacent_difference(InputIterator first, InputIterator last,
139
 
                        OutputIterator result, BinaryOperation binary_op)
140
 
        {
141
 
                OutputIterator temp(first);
142
 
                *result = *first;
143
 
                ++first;
144
 
                ++result;
145
 
 
146
 
                while(first != last){
147
 
                        *result = binary_op(*first, *temp);
148
 
                        temp = first;
149
 
                        ++first;
150
 
                        ++result;
151
 
                }
152
 
 
153
 
                return result;
154
 
        }
155
 
 
156
 
}
157
 
 
158
 
#pragma GCC visibility pop
159
 
 
160
 
#endif
161