1 | //$Id: utility.h 14 2005-12-30 14:57:47Z peter $ |
---|
2 | |
---|
3 | #ifndef _theplu_svnstat_utility_ |
---|
4 | #define _theplu_svnstat_utility_ |
---|
5 | |
---|
6 | #include <algorithm> |
---|
7 | #include <functional> |
---|
8 | #include <utility> |
---|
9 | #include <vector> |
---|
10 | |
---|
11 | namespace theplu{ |
---|
12 | namespace svnstat{ |
---|
13 | |
---|
14 | /// |
---|
15 | /// Calculating sum of two vectors. |
---|
16 | /// |
---|
17 | /// @return resulting vector |
---|
18 | /// |
---|
19 | template <class T > |
---|
20 | struct VectorPlus : |
---|
21 | public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> > |
---|
22 | { |
---|
23 | std::vector<T> operator()(const std::vector<T>& u, |
---|
24 | const std::vector<T>& v) const |
---|
25 | { |
---|
26 | if ( u.size() > v.size() ){ |
---|
27 | std::vector<T> res(u.size()); |
---|
28 | transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>()); |
---|
29 | copy(u.begin()+v.size(), u.end(), res.begin()+v.size()); |
---|
30 | return res; |
---|
31 | } |
---|
32 | |
---|
33 | std::vector<T> res(v.size()); |
---|
34 | transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>()); |
---|
35 | if ( v.size() > u.size() ) |
---|
36 | copy(u.begin()+v.size(), u.end(), res.begin()+v.size()); |
---|
37 | return res; |
---|
38 | } |
---|
39 | |
---|
40 | }; |
---|
41 | |
---|
42 | /// |
---|
43 | /// @return resulting vector |
---|
44 | /// |
---|
45 | template <class Key, class T> |
---|
46 | struct PairValuePlus : |
---|
47 | public std::binary_function<std::vector<T>, |
---|
48 | std::pair<const Key, std::vector<T> >, |
---|
49 | std::vector<T> > |
---|
50 | { |
---|
51 | std::vector<T> operator()(const std::vector<T>& sum, |
---|
52 | const std::pair<const Key,std::vector<T> >& p) |
---|
53 | { |
---|
54 | return VectorPlus<T>()(sum, p.second); |
---|
55 | } |
---|
56 | }; |
---|
57 | }} |
---|
58 | |
---|
59 | #endif // end of namespace svnstat end of namespace theplu |
---|