toon-members
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Toon-members] TooN operators.h internal/vector.hh


From: Tom Drummond
Subject: [Toon-members] TooN operators.h internal/vector.hh
Date: Fri, 09 Jan 2009 14:45:02 +0000

CVSROOT:        /cvsroot/toon
Module name:    TooN
Changes by:     Tom Drummond <twd20>    09/01/09 14:45:02

Modified files:
        .              : operators.h 
        internal       : vector.hh 

Log message:
        inserted arbitrary precision for vectors

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/operators.h?cvsroot=toon&r1=1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/vector.hh?cvsroot=toon&r1=1.1&r2=1.2

Patches:
Index: operators.h
===================================================================
RCS file: /cvsroot/toon/TooN/operators.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- operators.h 7 Jan 2009 14:23:52 -0000       1.1
+++ operators.h 9 Jan 2009 14:45:01 -0000       1.2
@@ -6,8 +6,10 @@
 
 
 // Dot product Vector * Vector
-template<int Size1, typename Base1, int Size2, typename Base2>
-double operator*(const Vector<Size1, Base1>& v1, const Vector<Size2, Base2>& 
v2){
+template<int Size1, typename Precision1, typename Base1,
+        int Size2, typename Precision2, typename Base2>
+double operator*(const Vector<Size1, Precision1, Base1>& v1,
+                const Vector<Size2, Precision2, Base2>& v2){
   SizeMismatch<Size1, Size2>:: test(v1.size(),v2.size());
   const int s=v1.size();
   double result=0;
@@ -18,8 +20,10 @@
 }
 
 // operator += Vector
-template<int Size1, typename Base1, int Size2, typename Base2>
-Vector<Size1, Base1>& operator += (Vector<Size1, Base1>& lhs, const 
Vector<Size2, Base2>& rhs){
+template<int Size1, typename Precision1, typename Base1,
+        int Size2, typename Precision2, typename Base2>
+Vector<Size1, Precision1, Base1>& operator += (Vector<Size1, Precision1, 
Base1>& lhs,
+                                              const Vector<Size2, Precision2, 
Base2>& rhs){
   SizeMismatch<Size1,Size2>::test(lhs.size(),rhs.size());
   const int s=lhs.size();
   for(int i=0; i<s; i++){
@@ -29,8 +33,10 @@
 }
 
 // operator -= Vector
-template<int Size1, typename Base1, int Size2, typename Base2>
-Vector<Size1, Base1>& operator -= (Vector<Size1, Base1>& lhs, const 
Vector<Size2, Base2>& rhs){
+template<int Size1, typename Base1, typename Precision1,
+        int Size2, typename Precision2, typename Base2>
+Vector<Size1, Precision1, Base1>& operator -= (Vector<Size1, Precision1, 
Base1>& lhs,
+                                              const Vector<Size2, Precision2, 
Base2>& rhs){
   SizeMismatch<Size1,Size2>::test(lhs.size(),rhs.size());
   const int s=lhs.size();
   for(int i=0; i<s; i++){
@@ -40,8 +46,8 @@
 }
 
 // operator *= double
-template<int Size, typename Base>
-Vector<Size, Base>& operator *= (Vector<Size, Base>& lhs, double rhs){
+template<int Size, typename Precision, typename Base>
+Vector<Size, Precision, Base>& operator *= (Vector<Size, Precision, Base>& 
lhs, double rhs){
   const int s=lhs.size();
   for(int i=0; i<s; i++){
     lhs[i]*=rhs;
@@ -50,8 +56,8 @@
 }
 
 // operator /= double
-template<int Size, typename Base>
-Vector<Size, Base>& operator /= (Vector<Size, Base>& lhs, double rhs){
+template<int Size, typename Precision, typename Base>
+Vector<Size, Precision, Base>& operator /= (Vector<Size, Precision, Base>& 
lhs, double rhs){
   const int s=lhs.size();
   for(int i=0; i<s; i++){
     lhs[i]/=rhs;
@@ -60,8 +66,8 @@
 }
 
 // output operator <<
-template <int Size, typename Base>
-inline std::ostream& operator<< (std::ostream& os, const Vector<Size,Base>& v){
+template <int Size, typename Precision, typename Base>
+inline std::ostream& operator<< (std::ostream& os, const 
Vector<Size,Precision,Base>& v){
   for(int i=0; i<v.size(); i++){
     os << v[i] << " ";
   }

Index: internal/vector.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/vector.hh,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- internal/vector.hh  9 Jan 2009 14:24:03 -0000       1.1
+++ internal/vector.hh  9 Jan 2009 14:45:02 -0000       1.2
@@ -1,9 +1,10 @@
 //-*- c++ -*-
+
 // forward declarations
-template<int Size, typename Base>
+template<int Size, typename Precision, typename Base>
 class Vector;
 
-template<int Size, int Stride>
+template<int Size, int Stride, typename Precision>
 class SVBase;
 
 // forward declaration
@@ -16,14 +17,15 @@
 // Type is a hack so that x?y:z expressions work
 // Type=0 means the data is internal to the object
 // Type=1 means the data is external to the object
-template <int Size, int Type>
+template <int Size, int Type, typename Precision>
 class VBase;
 
+template <typename Precision>
 class SDVBase;
 
 
-template<int Size>
-class VBase<Size, 0> {
+template<int Size, typename Precision>
+class VBase<Size, 0, Precision> {
 public:
   inline VBase(){}
   inline VBase(const VBase& from){}
@@ -40,105 +42,104 @@
   template<int Size2, class Base2>
   inline VBase(const Vector<Size2,Base2>& from){}
 
-  double* data(){return my_data;}
-  const double* data() const {return my_data;}
+  Precision* data(){return my_data;}
+  const Precision* data() const {return my_data;}
   
   static int size(){return Size;}
   static int stride(){return 1;}
 
-  double& operator[](int i){
+  Precision& operator[](int i){
     Internal::check_index(Size, i);
     return my_data[i];
   }
-  const double& operator[](int i) const {
+  const Precision& operator[](int i) const {
     Internal::check_index(Size, i);
     return my_data[i];
   }
 
   template <int Start, int Length>
-  Vector<Length, SVBase<Length,1> >
+  Vector<Length, Precision, SVBase<Length,1> >
   slice(){
     Internal::CheckSlice<Size, Start, Length>::check();
-    return Vector<Length, SVBase<Length,1> >(&(my_data[Start]));
+    return Vector<Length, Precision, SVBase<Length,1,Precision> 
>(&(my_data[Start]));
   }
 
-  Vector<-1, SDVBase>
+  Vector<-1, Precision, SDVBase>
   slice(int start, int length);
 
 private:
-  double my_data[Size];
+  Precision my_data[Size];
 };
 
-template<int Size>
+template<int Size, typename Precision>
 class VBase<Size,1>{
 public:
+
+  // Constructors
+  // For all these, the owning vector class is responsible
+  // for filling in any data that needs to go into the vector elements
+
   VBase()
-    : my_data(new double[Size]){
+    : my_data(new Precision[Size]){
   }
 
   VBase(const VBase& from)
-    : my_data(new double[Size]){
+    : my_data(new Precision[Size]){
     }
 
   VBase(int size_in)
-    : my_data(new double[Size]){
+    : my_data(new Precision[Size]){
   }
 
   // construction from 1-ary operator
   template <class T, class Op>
   inline VBase(const T&, const Operator<Op>&):
-    my_data(new double[Size]){}
+    my_data(new Precision[Size]){}
 
   // constructor from 2-ary operator
   template <class LHS, class RHS, class Op>
   inline VBase(const LHS& lhs, const RHS& rhs, const Operator<Op>&):
-    my_data(new double[Size]){}
+    my_data(new Precision[Size]){}
 
   // constructor from arbitrary vector
-  template<int Size2, class Base2>
-  inline VBase(const Vector<Size2,Base2>& from):
-    my_data(new double[from.size()]) {}
+  template<int Size2, typename Precision2, typename Base2>
+  inline VBase(const Vector<Size2,Precision2,Base2>& from):
+    my_data(new Precision[from.size()]) {}
 
   ~VBase(){
     delete[] my_data;
   }
 
-  double* data(){return my_data;}
-  const double* data() const {return my_data;}
+  Precision* data(){return my_data;}
+  const Precsion* data() const {return my_data;}
   
   static int size(){return Size;}
   static int stride(){return 1;}
 
-  double& operator[](int i){
-    Internal::check_index(Size, i);
+  Precision& operator[](int i){
     return my_data[i];
   }
-  const double& operator[](int i) const {
-    Internal::check_index(Size, i);
+  const Precision& operator[](int i) const {
     return my_data[i];
   }
 
   template <int Start, int Length>
-  Vector<Length, SVBase<Length,1> >
+  Vector<Length, Precision, SVBase<Length,1> >
   slice(){
-    Internal::CheckSlice<Size, Start, Length>::check();
-    return Vector<Length, SVBase<Length,1> >(&(my_data[Start]));
+    return Vector<Length, Precision, SVBase<Length,1,Precision> 
>(&(my_data[Start]));
   }
 
-  Vector<-1, SDVBase>
-  slice(int start, int length);
-
 private:
-  double* const my_data;
+  Precision* const my_data;
 };
 
 
 // SVBase does not own its data
 // and has a template stride parameter
-template<int Size, int Stride>
+template<int Size, int Stride, typename Precision>
 class SVBase{
 public:
-  SVBase(double* data_in):
+  SVBase(Precision* data_in):
     my_data(data_in){
   }
 
@@ -146,27 +147,27 @@
     my_data(from.my_data){
   }
 
-  double* data(){return my_data;}
-  const double* data() const {return my_data;}
+  Precision* data(){return my_data;}
+  const Precision* data() const {return my_data;}
   
   int size() const {return Size;}
   int stride() const {return Stride;}
 
-  double& operator[](int i){
+  Precision& operator[](int i){
     return my_data[i*Stride];
   }
-  const double& operator[](int i) const {
+  const Precision& operator[](int i) const {
     return my_data[i*Stride];
   }
 
   template <int Start, int Length>
-  Vector<Length, SVBase<Length,Stride> >
+  Vector<Length, Precision, SVBase<Length,Stride> >
   slice(){
-    return Vector<Length, SVBase<Length,1> >(&(my_data[Start*Stride]));
+    return Vector<Length, Precision, SVBase<Length,1,Precision> 
>(&(my_data[Start*Stride]));
   }
 
 private:
-  double* const my_data;
+  Precision* const my_data;
 };
 
 
@@ -174,36 +175,37 @@
 
 // DVBase is for vectors whose size is determined dynamically at runtime
 // They own their data
+template <typename Precision>
 class DVBase{
 public:
   DVBase(int size_in):
-    my_data(new double[size_in]),
+    my_data(new Precision[size_in]),
     my_size(size_in){
   }
 
   DVBase(const DVBase& from):
-    my_data(new double[from.my_size]),
+    my_data(new Precision[from.my_size]),
     my_size(from.my_size) {
   }
 
   // construction from 1-ary operator
   template <class T, class Op>
   inline DVBase(const T& arg, const Operator<Op>&):
-    my_data(new double[Op::size(arg)]),
+    my_data(new Precision[Op::size(arg)]),
     my_size(Op::size(arg)) {
   }
 
   // constructor from 2-ary operator
   template <class LHS, class RHS, class Op>
   inline DVBase(const LHS& lhs, const RHS& rhs, const Operator<Op>&):
-    my_data(new double[Op::size(lhs,rhs)]),
+    my_data(new Precision[Op::size(lhs,rhs)]),
     my_size(Op::size(lhs,rhs)) {
   }
 
   // constructor from arbitrary vector
   template<int Size2, class Base2>
   inline DVBase(const Vector<Size2,Base2>& from):
-    my_data(new double[from.size()]),
+    my_data(new Precision[from.size()]),
     my_size(from.size()) {
   }
 
@@ -211,69 +213,106 @@
     delete[] my_data;
   }
 
-  double* data(){return my_data;}
-  const double* data() const {return my_data;}
+  Precision* data(){return my_data;}
+  const Precision* data() const {return my_data;}
   
   int size() const {return my_size;}
   int stride() const {return 1;}
 
-  double& operator[](int i){
+  Precision& operator[](int i){
     return my_data[i];
   }
-  const double& operator[](int i) const {
+  const Precision& operator[](int i) const {
     return my_data[i];
   }
+
+  // TODO slice still to go in here
+
 private:
-  double* const my_data;
+  Precision* const my_data;
   int my_size;
 };
 
 // SDVBase is for dynamically sized vectors that do not own their data
-// They have an additional stride member
+// They have an additional templated stride
+template <int Stride, typename Precision>
 class SDVBase{
 public:
-  SDVBase(double* data_in, int size_in):
+  SDVBase(Precision* data_in, int size_in):
     my_data(data_in) {
     my_size=size_in;
   };
 
   SDVBase(const SDVBase& from)
     : my_data(from.my_data),
-      my_size(from.my_size){
+      my_size(from.my_size),
   }
 
-  ~SDVBase(){
+  Precision* data(){return my_data;}
+  const Precision* data() const {return my_data;}
+  
+  int size() const {return my_size;}
+  int stride() const {return Stride;}
+
+  Precision& operator[](int i){
+    return my_data[i*Stride];
+  }
+  const Precision& operator[](int i) const {
+    return my_data[i*Stride];
+  }
+private:
+  Precision* const my_data;
+  int my_size;
+};
+
+// SDVBase is for dynamically sized vectors that do not own their data
+// They have an additional stride member
+template <typename Precision>
+class SSDVBase{
+public:
+  SSDVBase(Precision* data_in, int size_in, int stride_in):
+    my_data(data_in) {
+    my_size=size_in;
+    my_stride=stride_in;
+  };
+
+  SSDVBase(const SDVBase& from)
+    : my_data(from.my_data),
+      my_size(from.my_size),
+      my_stride(from.my_stride){
   }
 
-  double* data(){return my_data;}
-  const double* data() const {return my_data;}
+  Precision* data(){return my_data;}
+  const Precision* data() const {return my_data;}
   
   int size() const {return my_size;}
-  int stride() const {return 1;}
+  int stride() const {return my_stride;}
 
-  double& operator[](int i){
-    return my_data[i];
+  Precision& operator[](int i){
+    return my_data[i*my_stride];
   }
-  const double& operator[](int i) const {
-    return my_data[i];
+  const Precision& operator[](int i) const {
+    return my_data[i*my_stride];
   }
 private:
-  double* const my_data;
+  Precision* const my_data;
   int my_size;
+  int my_stride;
 };
 
+
 // traits classes that help with building the vectors you actually
 // construct in code
 static const int MAX_SIZE=10;
 
-template<int Size>
+template<int Size, typename Precision>
 struct VectorSelector{
-  typedef VBase<Size, (Size>MAX_SIZE)?1:0 > Type;
+  typedef VBase<Size, (Size>MAX_SIZE)?1:0, Precision > Type;
 };
 
-template<>
-struct VectorSelector<-1>{
-  typedef DVBase Type;
+template<typename Precision>
+struct VectorSelector<-1, Precision>{
+  typedef DVBase<Precision> Type;
 };
 
 
@@ -283,17 +322,18 @@
 
 
 
-template<int Size=-1, typename Base= typename VectorSelector<Size>::Type>
+template<int Size=-1, typename Precision=double,
+        typename Base=typename VectorSelector<Size,Precision>::Type>
 class Vector : public Base {
 public:
   // sneaky hack: only one of these constructors will work with any given base
   // class but they don't generate errors unless the user tries to use one of 
them
   // although the error message may be less than helpful - maybe this can be 
changed?
   inline Vector(){}
-  inline Vector(double* data) : Base (data) {}
+  inline Vector(Precision* data) : Base (data) {}
   inline Vector(int size_in) : Base(size_in) {}
-  inline Vector(double* data_in, int size_in) : Base(data_in, size_in) {}
-  inline Vector(double* data_in, int size_in, int stride_in) : Base(data_in, 
size_in, stride_in) {}
+  inline Vector(Precision* data_in, int size_in, int stride_in) : 
Base(data_in, size_in, stride_in) {}
+  inline Vector(Precision* data_in, int size_in) : Base(data_in, size_in) {}
 
 
   // constructors to allow return value optimisations
@@ -311,14 +351,14 @@
   }
 
   // copy constructor listed explicitly
-  inline Vector(const Vector<Size,Base>& from)
+  inline Vector(const Vector<Size,Precision,Base>& from)
     : Base(from) {
     (*this)=from;
   }
 
   // constructor from arbitrary vector
-  template<int Size2, class Base2>
-  inline Vector(const Vector<Size2,Base2>& from):
+  template<int Size2, typename Precision2, typename Base2>
+  inline Vector(const Vector<Size2,Precision2,Base2>& from):
     Base(from) {
     operator=(from);
   }
@@ -334,8 +374,8 @@
   }
 
   // operator =
-  template<int Size2, typename Base2>
-  Vector<Size,Base >& operator= (const Vector<Size2, Base2>& from){
+  template<int Size2, typename Precision2, typename Base2>
+  Vector<Size,Precision,Base >& operator= (const Vector<Size2, Precision2, 
Base2>& from){
     SizeMismatch<Size,Size2>::test(Base::size(), from.size());
     const int s=Base::size();
     for(int i=0; i<s; i++){
@@ -346,20 +386,4 @@
 
 };
 
-
-
-////////////////////////////////////////////////////////////////////////////////
-//
-// Fill in function calls, now everything is visible
-
-template<int Size>
-Vector<-1, SDVBase> VBase<Size, 0>:: slice(int start, int length){
-  Internal::CheckSlice<>::check(Size, start, length);
-  return Vector<-1, SDVBase>(my_data + start, length);
-}
-
-template<int Size>
-Vector<-1, SDVBase> VBase<Size, 1>:: slice(int start, int length){
-  Internal::CheckSlice<>::check(Size, start, length);
-  return Vector<-1, SDVBase>(my_data + start, length);
-}
+#endif




reply via email to

[Prev in Thread] Current Thread [Next in Thread]