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

vigra/random_forest/splices.hxx VIGRA

00001 #ifndef SPLICES_HXX
00002 #define SPLICES_HXX
00003 #include <vigra/multi_array.hxx>
00004 #include <iterator>
00005 
00006 namespace vigra
00007 {
00008 /** Idea for a class to use for easy splicing
00009  *
00010  * usage (with factory function _spl)
00011  *
00012  * // copy every even indexed element to a 5 x 5 
00013  * // sized matrix
00014  * Matrix<double> a(10, 10)
00015  * MultiArrayView<2,double> b(_spl_shp(_spl(0,2,10),
00016  *                                     _spl(0,2,10)));
00017  * copy_splice(_spl(0,2,10),_spl(0,2,10), a, b);
00018  * 
00019  * it is also possible to supply iterator ranges
00020  * std::vector<int> indices;
00021  * indices.push_back(3) (...)
00022  * 
00023  * copy_splice(_spl(indices.begin(), indices.end()),
00024  *             _spl(a.shape(1)),
00025  *             a, b)
00026  *
00027  * if you only have a forward iterator then you must
00028  * specify the size of the splice with 
00029  * _spl(set.begin(), set.end(), set.size());
00030  *
00031  * ok.. what we actually need is a decent iota iterator
00032  * or something like xrange but for now it should suffice.
00033  */
00034 template<class T>
00035 class Splice
00036 {
00037     int size_;
00038     T begin_;
00039     T end_;
00040 public:
00041     Splice(T  &begin, T &end)
00042         : size_(std::distance(begin, end)),
00043           begin_(begin),
00044           end_(end)
00045     {}
00046 
00047     int operator[](int index)
00048     {
00049         T ii = begin_;
00050         std::advance(ii, index);
00051         return *ii;
00052     }
00053 
00054     int size()
00055     {
00056         return size_;
00057     }
00058 };
00059 
00060 template<>
00061 class Splice<int>
00062 {
00063     int begin_;
00064     int interval_;
00065     int end_;
00066     int size_;
00067     public:
00068     Splice(int begin, int end)
00069     : begin_(begin), 
00070       interval_(1),
00071       end_(end),
00072       size_(end - begin)
00073     {}
00074 
00075     Splice(int begin, int interval, int end) 
00076     : begin_(begin), 
00077       interval_(interval),
00078       end_(end),
00079       size_(int(std::floor((double(end) -double(begin))/interval)))
00080     {}
00081     
00082     int operator[](int index)
00083     {
00084         int ii = begin_ + index * interval_;
00085         return ii;
00086     }
00087 
00088     int size()
00089     {
00090         return size_;
00091     }
00092 };
00093 
00094 template<class T>
00095 Splice<T> _spl(T b, T e)
00096 {
00097     return Splice<T>(b, e);
00098 }
00099 template<class T>
00100 Splice<T> _spl(T b, int size, T e)
00101 {
00102     return Splice<T>(b, size, e);
00103 }
00104 
00105 inline Splice<int> _spl(int size)
00106 {
00107     return Splice<int>(0, size);
00108 }
00109 
00110 
00111 
00112 template<class T, class G>
00113 inline MultiArrayShape<2>::type _spl_shp(Splice<T> f,
00114                                                   Splice<G> h)
00115 {
00116     return MultiArrayShape<2>::type(f.size(), h.size());
00117 }
00118 
00119 
00120 
00121 
00122 template<   class R, class F, 
00123             class T, class C,
00124             class T2, class C2 >
00125 void copy_splice(  Splice<R> _first,
00126                    Splice<F> _second,
00127                    MultiArrayView<2, T, C>  src,
00128                    MultiArrayView<2, T2, C2> dest)
00129 {
00130     for(int jj = 0 ; jj < _second.size(); ++jj)
00131     {
00132         for(int ii = 0 ; ii < _first.size(); ++ii)
00133         {
00134             dest(ii, jj) = src(_first[ii], _second[jj]);
00135         }
00136     }
00137 }
00138 
00139 };
00140 #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.9.0 (Tue Nov 6 2012)