internal/vbase.hh

00001 // -*- c++ -*-
00002 
00003 // Copyright (C) 2009 Tom Drummond (twd20@cam.ac.uk),
00004 // Ed Rosten (er258@cam.ac.uk)
00005 //
00006 // This file is part of the TooN Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 namespace TooN {
00032 
00033 namespace Internal{
00034 template<int Size, class Precision, int Stride, class Mem> struct GenericVBase;
00035 
00036 
00038 //
00039 // Slice holding class
00040 //
00041 
00042 template<int Stride>
00043 struct SliceVBase {
00044 
00045     // this class is really just a typedef
00046     template<int Size, typename Precision>
00047     struct VLayout
00048         : public GenericVBase<Size, Precision, Stride, VectorSlice<Size, Precision> > {
00049     
00050         VLayout(Precision* d, int length, int stride)
00051             :GenericVBase<Size, Precision, Stride, VectorSlice<Size, Precision> >(d, length, stride){
00052         }
00053 
00054         template<class Op>
00055         VLayout(const Operator<Op>& op)
00056             :GenericVBase<Size, Precision, Stride, VectorSlice<Size, Precision> >(op) {}
00057     };
00058 
00059 };
00060 
00062 //
00063 // Classes for Vectors owning memory
00064 //
00065 
00066 struct VBase {
00067 
00068     // this class is really just a typedef
00069     template<int Size, class Precision>
00070     struct VLayout 
00071         : public GenericVBase<Size, Precision, 1, VectorAlloc<Size, Precision> > {
00072     
00073         VLayout(){}
00074 
00075         VLayout(int s)
00076             :GenericVBase<Size, Precision, 1, VectorAlloc<Size, Precision> >(s)
00077         {}
00078 
00079         template<class Op>
00080         VLayout(const Operator<Op>& op)
00081             :GenericVBase<Size, Precision, 1, VectorAlloc<Size, Precision> >(op) {}
00082     };
00083 };
00084 
00086 //
00087 // Generic implementation
00088 //
00089 
00090 template<int Size, typename Precision, int Stride, typename Mem> struct GenericVBase: public Mem, public StrideHolder<Stride>
00091 {   
00092     int stride() const{
00093         return StrideHolder<Stride>::stride();
00094     }
00095 
00096     //Optional constuctors
00097     GenericVBase(){}
00098 
00099     GenericVBase(int s)
00100     :Mem(s)
00101     {}
00102 
00103     GenericVBase(Precision* d, int length, int stride)
00104     :Mem(d, length),StrideHolder<Stride>(stride){
00105     }
00106     
00107     template<class Op>
00108     GenericVBase(const Operator<Op> & op) : Mem(op), StrideHolder<Stride>(op) {}
00109 
00110     using Mem::my_data;
00111     using Mem::size;
00112 
00113     Precision& operator[](int i) {
00114         Internal::check_index(size(), i);
00115         return my_data[i * stride()];
00116     }
00117 
00118     const Precision& operator[](int i) const {
00119         Internal::check_index(size(), i);
00120         return my_data[i * stride()];
00121     }
00122 
00123 
00124     template<int Start, int Length> 
00125     Vector<Length, Precision, SliceVBase<Stride> > slice(){
00126         Internal::CheckStaticSlice<Size, Start, Length>::check(size());
00127         return Vector<Length, Precision, SliceVBase<Stride> >(my_data + stride()*Start, Length, stride(), Slicing());
00128     }
00129 
00130     template<int Start, int Length> 
00131     const Vector<Length, Precision, SliceVBase<Stride> > slice() const {
00132         Internal::CheckStaticSlice<Size, Start, Length>::check(size());
00133         return Vector<Length, Precision, SliceVBase<Stride> >(const_cast<Precision*>(my_data + stride()*Start), Length, stride(), Slicing());
00134     }
00135 
00136     Vector<-1, Precision, SliceVBase<Stride> > slice(int start, int length){
00137         Internal::CheckDynamicSlice::check(size(), start, length);
00138         return Vector<-1, Precision, SliceVBase<Stride> >(my_data + stride()*start, length, stride(), Slicing());
00139     }
00140 
00141     const Vector<-1, Precision, SliceVBase<Stride> > slice(int start, int length) const {
00142         Internal::CheckDynamicSlice::check(size(), start, length);
00143         return Vector<-1, Precision, SliceVBase<Stride> >(const_cast<Precision*>(my_data + stride()*start), length, stride(), Slicing());
00144     }
00145 
00146     const Matrix<1, Size, Precision, Slice<1,Stride> > as_row() const{
00147         return Matrix<1, Size, Precision, Slice<1,Stride> >(const_cast<Precision*>(my_data), 1, size(), 1, stride(), Slicing());
00148     }
00149 
00150     Matrix<1, Size, Precision, Slice<1,Stride> > as_row(){
00151         return Matrix<1, Size, Precision, Slice<1,Stride> >(my_data, 1, size(), 1, stride(), Slicing());
00152     }
00153 
00154     const Matrix<Size, 1, Precision, Slice<Stride,1> > as_col() const{
00155         return Matrix<Size, 1, Precision, Slice<Stride,1> >(const_cast<Precision*>(my_data), size(), 1, stride(), 1, Slicing());
00156     }
00157 
00158     Matrix<Size, 1, Precision, Slice<Stride,1> > as_col(){
00159         return Matrix<Size, 1, Precision, Slice<Stride,1> >(my_data, size(), 1, stride(), 1, Slicing());
00160     }
00161     
00162     //This is a hack for in-place functions, so a const version is never required.
00163     Vector<Size, Precision, SliceVBase<Stride> > as_slice(){
00164         return Vector<Size, Precision, SliceVBase<Stride> >(my_data, size(), stride(), Slicing());
00165     }
00166 
00167     typedef Vector<Size, Precision, SliceVBase<Stride> > as_slice_type;
00168 
00169     DiagonalMatrix<Size,Precision, SliceVBase<Stride> > as_diagonal() {
00170         return DiagonalMatrix<Size, Precision, SliceVBase<Stride> > (my_data, size(), stride(), Slicing());
00171     }
00172 
00173 };
00174 
00175 }
00176 
00177 }

Generated on Thu May 7 20:28:40 2009 for TooN by  doxygen 1.5.3