vigra - vigra/random_forest/splices.hxx Source File

[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

splices.hxx |

1 #ifndef SPLICES_HXX
2 #define SPLICES_HXX
3 #include <vigra/multi_array.hxx>
4 #include <iterator>
5
6 namespace vigra
7 {
8 /** Idea for a class to use for easy splicing
9 *
10 * usage (with factory function _spl)
11 *
12 * // copy every even indexed element to a 5 x 5
13 * // sized matrix
14 * Matrix<double> a(10, 10)
15 * MultiArrayView<2,double> b(_spl_shp(_spl(0,2,10),
16 * _spl(0,2,10)));
17 * copy_splice(_spl(0,2,10),_spl(0,2,10), a, b);
18 *
19 * it is also possible to supply iterator ranges
20 * std::vector<int> indices;
21 * indices.push_back(3) (...)
22 *
23 * copy_splice(_spl(indices.begin(), indices.end()),
24 * _spl(a.shape(1)),
25 * a, b)
26 *
27 * if you only have a forward iterator then you must
28 * specify the size of the splice with
29 * _spl(set.begin(), set.end(), set.size());
30 *
31 * ok.. what we actually need is a decent iota iterator
32 * or something like xrange but for now it should suffice.
33 */
34 template<class T>
35 class Splice
36 {
37 int size_;
38 T begin_;
39 T end_;
40 public:
41 Splice(T &begin, T &end)
42 : size_(std::distance(begin, end)),
43 begin_(begin),
44 end_(end)
45 {}
46
47 int operator[](int index)
48 {
49 T ii = begin_;
50 std::advance(ii, index);
51 return *ii;
52 }
53
54 int size()
55 {
56 return size_;
57 }
58 };
59
60 template<>
61 class Splice<int>
62 {
63 int begin_;
64 int interval_;
65 int end_;
66 int size_;
67 public:
68 Splice(int begin, int end)
69 : begin_(begin),
70 interval_(1),
71 end_(end),
72 size_(end - begin)
73 {}
74
75 Splice(int begin, int interval, int end)
76 : begin_(begin),
77 interval_(interval),
78 end_(end),
79 size_(int(std::floor((double(end) -double(begin))/interval)))
80 {}
81
82 int operator[](int index)
83 {
84 int ii = begin_ + index * interval_;
85 return ii;
86 }
87
88 int size()
89 {
90 return size_;
91 }
92 };
93
94 template<class T>
95 Splice<T> _spl(T b, T e)
96 {
97 return Splice<T>(b, e);
98 }
99 template<class T>
100 Splice<T> _spl(T b, int size, T e)
101 {
102 return Splice<T>(b, size, e);
103 }
104
105 inline Splice<int> _spl(int size)
106 {
107 return Splice<int>(0, size);
108 }
109
110
111
112 template<class T, class G>
113 inline MultiArrayShape<2>::type _spl_shp(Splice<T> f,
114 Splice<G> h)
115 {
116 return MultiArrayShape<2>::type(f.size(), h.size());
117 }
118
119
120
121
122 template< class R, class F,
123 class T, class C,
124 class T2, class C2 >
125 void copy_splice( Splice<R> _first,
126 Splice<F> _second,
127 MultiArrayView<2, T, C> src,
128 MultiArrayView<2, T2, C2> dest)
129 {
130 for(int jj = 0 ; jj < _second.size(); ++jj)
131 {
132 for(int ii = 0 ; ii < _first.size(); ++ii)
133 {
134 dest(ii, jj) = src(_first[ii], _second[jj]);
135 }
136 }
137 }
138
139 };
140 #endif //SPLICES_HXX

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.10.0 (Mon Nov 18 2013)