FEDRA emulsion software from the OPERA Collaboration
Expression.hh
Go to the documentation of this file.
1 #ifndef __EXPRESSION_HH
2 #define __EXPRESSION_HH
3 // ********************************************************************
4 //
5 // source:
6 //
7 // type: source code
8 //
9 // created: 19. Mar 2001
10 //
11 // author: Thorsten Glebe
12 // HERA-B Collaboration
13 // Max-Planck-Institut fuer Kernphysik
14 // Saupfercheckweg 1
15 // 69117 Heidelberg
16 // Germany
17 // E-mail: T.Glebe@mpi-hd.mpg.de
18 //
19 // Description: Expression Template Elements for SVector
20 //
21 // changes:
22 // 19 Mar 2001 (TG) creation
23 // 20 Mar 2001 (TG) added rows(), cols() to Expr
24 // 21 Mar 2001 (TG) added Expr::value_type
25 // 11 Apr 2001 (TG) rows(), cols() replaced by rows, cols
26 // 10 Okt 2001 (TG) added print() and operator<<() for Expr class
27 //
28 // ********************************************************************
29 
36 //==============================================================================
37 // Expr: class representing SVector expressions
38 //==============================================================================
39 #include "Riostream.h"
40 using namespace std;
41 
42 template <class ExprType, class T, unsigned int D, unsigned int D2 = 0>
43 class Expr {
44 public:
45  typedef T value_type;
46 
48  Expr(const ExprType& rhs) :
49  rhs_(rhs) {}
50 
52  ~Expr() {}
53 
55  inline T apply(unsigned int i) const {
56  return rhs_.apply(i);
57  }
58 
60  static const unsigned int rows = D;
62  static const unsigned int cols = D2;
63 
65  std::ostream& print(std::ostream& os) const {
66  os.setf(ios::right, ios::adjustfield) ;
67 
68  if(D2 == 0) {
69  unsigned int i=0;
70  for(; i<D-1; ++i) {
71  os << apply(i) << ", ";
72  }
73  os << apply(i);
74  } else {
75  os << "[ ";
76  for (unsigned int i=0; i < D; ++i) {
77  for (unsigned int j=0; j < D2; ++j) {
78  os << setw(12) << apply(i*D2+j);
79  if ((!((j+1)%12)) && (j < D2-1))
80  os << endl << " ...";
81  }
82  if (i != D - 1)
83  os << endl << " ";
84  }
85  os << " ]";
86  } // if D2==0
87 
88  return os;
89  }
90 
91 private:
92  ExprType rhs_; // cannot be a reference!
93 };
94 
95 //==============================================================================
96 // operator<<
97 //==============================================================================
98 template <class A, class T, unsigned int D1, unsigned int D2>
99 inline std::ostream& operator<<(std::ostream& os, const Expr<A,T,D1,D2>& rhs) {
100  return rhs.print(os);
101 }
102 
109 //==============================================================================
110 // BinaryOp
111 //==============================================================================
112 template <class Operator, class LHS, class RHS, class T>
113 class BinaryOp {
114 public:
116  BinaryOp( Operator op, const LHS& lhs, const RHS& rhs) :
117  lhs_(lhs), rhs_(rhs) {}
118 
121 
123  inline T apply(unsigned int i) const {
124  return Operator::apply(lhs_.apply(i), rhs_.apply(i));
125  }
126 
127 protected:
128  const LHS& lhs_;
129  const RHS& rhs_;
130 };
131 
132 
139 //==============================================================================
140 // UnaryOp
141 //==============================================================================
142 template <class Operator, class RHS, class T>
143 class UnaryOp {
144 public:
146  UnaryOp( Operator op, const RHS& rhs) :
147  rhs_(rhs) {}
148 
150  ~UnaryOp() {}
151 
153  inline T apply(unsigned int i) const {
154  return Operator::apply(rhs_.apply(i));
155  }
156 
157 protected:
158  const RHS& rhs_;
159 };
160 
161 
168 //==============================================================================
169 // Constant
170 //==============================================================================
171 template <class T>
172 class Constant {
173 public:
175  Constant( const T& rhs ) :
176  rhs_(rhs) {}
177 
180 
182  inline T apply(unsigned int i) const { return rhs_; }
183 
184 protected:
185  const T& rhs_;
186 };
187 #endif
std::ostream & operator<<(std::ostream &os, const Expr< A, T, D1, D2 > &rhs)
Definition: Expression.hh:99
Definition: Expression.hh:113
const RHS & rhs_
Definition: Expression.hh:129
T apply(unsigned int i) const
Definition: Expression.hh:123
const LHS & lhs_
Definition: Expression.hh:128
BinaryOp(Operator op, const LHS &lhs, const RHS &rhs)
Definition: Expression.hh:116
~BinaryOp()
Definition: Expression.hh:120
Definition: Expression.hh:172
const T & rhs_
Definition: Expression.hh:185
Constant(const T &rhs)
Definition: Expression.hh:175
T apply(unsigned int i) const
Definition: Expression.hh:182
~Constant()
Definition: Expression.hh:179
Definition: Expression.hh:43
T value_type
Definition: Expression.hh:45
T apply(unsigned int i) const
Definition: Expression.hh:55
ExprType rhs_
Definition: Expression.hh:92
std::ostream & print(std::ostream &os) const
used by operator<<()
Definition: Expression.hh:65
~Expr()
Definition: Expression.hh:52
Expr(const ExprType &rhs)
Definition: Expression.hh:48
Definition: Expression.hh:143
T apply(unsigned int i) const
Definition: Expression.hh:153
UnaryOp(Operator op, const RHS &rhs)
Definition: Expression.hh:146
const RHS & rhs_
Definition: Expression.hh:158
~UnaryOp()
Definition: Expression.hh:150