| 1 | /***************************************************************************
 | 
|---|
| 2 |  * blitz/ops.h           Function objects for operators
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  * $Id: ops.h,v 1.1.1.1 1999-11-26 16:37:04 ansari Exp $
 | 
|---|
| 5 |  *
 | 
|---|
| 6 |  * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
 | 
|---|
| 7 |  *
 | 
|---|
| 8 |  * This program is free software; you can redistribute it and/or
 | 
|---|
| 9 |  * modify it under the terms of the GNU General Public License
 | 
|---|
| 10 |  * as published by the Free Software Foundation; either version 2
 | 
|---|
| 11 |  * of the License, or (at your option) any later version.
 | 
|---|
| 12 |  *
 | 
|---|
| 13 |  * This program is distributed in the hope that it will be useful,
 | 
|---|
| 14 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 15 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 16 |  * GNU General Public License for more details.
 | 
|---|
| 17 |  *
 | 
|---|
| 18 |  * Suggestions:          blitz-suggest@cybervision.com
 | 
|---|
| 19 |  * Bugs:                 blitz-bugs@cybervision.com
 | 
|---|
| 20 |  *
 | 
|---|
| 21 |  * For more information, please see the Blitz++ Home Page:
 | 
|---|
| 22 |  *    http://seurat.uwaterloo.ca/blitz/
 | 
|---|
| 23 |  *
 | 
|---|
| 24 |  *************************************************************************
 | 
|---|
| 25 |  * $Log: not supported by cvs2svn $
 | 
|---|
| 26 |  * Revision 1.1.1.1  1999/04/09  17:59:02  ansari
 | 
|---|
| 27 |  * Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99
 | 
|---|
| 28 |  *
 | 
|---|
| 29 |  * Revision 1.2  1998/03/14 00:04:47  tveldhui
 | 
|---|
| 30 |  * 0.2-alpha-05
 | 
|---|
| 31 |  *
 | 
|---|
| 32 |  * Revision 1.1  1997/07/16 14:51:20  tveldhui
 | 
|---|
| 33 |  * Update: Alpha release 0.2 (Arrays)
 | 
|---|
| 34 |  *
 | 
|---|
| 35 |  */
 | 
|---|
| 36 | 
 | 
|---|
| 37 | #ifndef BZ_OPS_H
 | 
|---|
| 38 | #define BZ_OPS_H
 | 
|---|
| 39 | 
 | 
|---|
| 40 | #ifndef BZ_BLITZ_H
 | 
|---|
| 41 |  #include <blitz/blitz.h>
 | 
|---|
| 42 | #endif
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #ifndef BZ_PROMOTE_H
 | 
|---|
| 45 |  #include <blitz/promote.h>
 | 
|---|
| 46 | #endif
 | 
|---|
| 47 | 
 | 
|---|
| 48 | #ifndef BZ_MATHFUNC_H
 | 
|---|
| 49 |  #include <blitz/mathfunc.h>
 | 
|---|
| 50 | #endif
 | 
|---|
| 51 | 
 | 
|---|
| 52 | BZ_NAMESPACE(blitz)
 | 
|---|
| 53 | 
 | 
|---|
| 54 | /*
 | 
|---|
| 55 |  * Originally these function objects had no template arguments, e.g.
 | 
|---|
| 56 |  *
 | 
|---|
| 57 |  * struct Add {
 | 
|---|
| 58 |  *     template<class T_numtype1, class T_numtype2>
 | 
|---|
| 59 |  *     static inline BZ_PROMOTE(T_numtype1, T_numtype2)
 | 
|---|
| 60 |  *     apply(T_numtype1 a, T_numtype2 b)
 | 
|---|
| 61 |  *     { return a + b; }
 | 
|---|
| 62 |  * };
 | 
|---|
| 63 |  *
 | 
|---|
| 64 |  * This made for neater expression templates syntax.  However, there are
 | 
|---|
| 65 |  * some situations in which users may want to override type promotion
 | 
|---|
| 66 |  * for certain operations.  For example, in theoretical physics, there
 | 
|---|
| 67 |  * are U1 objects which when multiplied yield U1 objects, but when added
 | 
|---|
| 68 |  * yield a different type.  To allow for this kind of behaviour, function
 | 
|---|
| 69 |  * objects have been changed to take template parameters:
 | 
|---|
| 70 |  *
 | 
|---|
| 71 |  * template<class T_numtype1, class T_numtype2>
 | 
|---|
| 72 |  * struct Add {
 | 
|---|
| 73 |  *     typedef BZ_PROMOTE(T_numtype1, T_numtype2) T_numtype;
 | 
|---|
| 74 |  *
 | 
|---|
| 75 |  *     static inline T_numtype apply(T_numtype1 a, T_numtype2 b)
 | 
|---|
| 76 |  *     { return a + b; }
 | 
|---|
| 77 |  * };
 | 
|---|
| 78 |  *
 | 
|---|
| 79 |  * Type promotion is performed inside the function object.  The expression
 | 
|---|
| 80 |  * templates code always looks inside the function object to determine
 | 
|---|
| 81 |  * the type promotion, e.g. Add<int,float>::T_numtype
 | 
|---|
| 82 |  *
 | 
|---|
| 83 |  * Users are free to specialize these function objects for their own types.
 | 
|---|
| 84 |  */
 | 
|---|
| 85 | 
 | 
|---|
| 86 | #define BZ_DEFINE_OP(name,op,symbol)                        \
 | 
|---|
| 87 | template<class T_numtype1, class T_numtype2>                \
 | 
|---|
| 88 | struct name {                                               \
 | 
|---|
| 89 |     typedef BZ_PROMOTE(T_numtype1, T_numtype2) T_numtype;   \
 | 
|---|
| 90 |                                                             \
 | 
|---|
| 91 |     static inline T_numtype                                 \
 | 
|---|
| 92 |     apply(T_numtype1 a, T_numtype2 b)                       \
 | 
|---|
| 93 |     { return a op b; }                                      \
 | 
|---|
| 94 |                                                             \
 | 
|---|
| 95 |     template<class T1, class T2>                            \
 | 
|---|
| 96 |     static inline void prettyPrint(string& str,             \
 | 
|---|
| 97 |         prettyPrintFormat& format, const T1& t1,            \
 | 
|---|
| 98 |         const T2& t2)                                       \
 | 
|---|
| 99 |     {                                                       \
 | 
|---|
| 100 |         str += "(";                                         \
 | 
|---|
| 101 |         t1.prettyPrint(str, format);                        \
 | 
|---|
| 102 |         str += symbol;                                      \
 | 
|---|
| 103 |         t2.prettyPrint(str, format);                        \
 | 
|---|
| 104 |         str += ")";                                         \
 | 
|---|
| 105 |     }                                                       \
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | BZ_DEFINE_OP(Add,+,"+");
 | 
|---|
| 109 | BZ_DEFINE_OP(Subtract,-,"-");
 | 
|---|
| 110 | BZ_DEFINE_OP(Multiply,*,"*");
 | 
|---|
| 111 | BZ_DEFINE_OP(Divide,/,"/");
 | 
|---|
| 112 | BZ_DEFINE_OP(Modulo,%,"%");
 | 
|---|
| 113 | BZ_DEFINE_OP(BitwiseXor,^,"^");
 | 
|---|
| 114 | BZ_DEFINE_OP(BitwiseAnd,&,"&");
 | 
|---|
| 115 | BZ_DEFINE_OP(BitwiseOr,|,"|");
 | 
|---|
| 116 | BZ_DEFINE_OP(ShiftRight,>>,">>");
 | 
|---|
| 117 | BZ_DEFINE_OP(ShiftLeft,<<,"<<");
 | 
|---|
| 118 | 
 | 
|---|
| 119 | #define BZ_DEFINE_BOOL_OP(name,op,symbol)                   \
 | 
|---|
| 120 | template<class T_numtype1, class T_numtype2>                \
 | 
|---|
| 121 | struct name {                                               \
 | 
|---|
| 122 |     typedef bool T_numtype;                                 \
 | 
|---|
| 123 |     static inline bool                                      \
 | 
|---|
| 124 |     apply(T_numtype1 a, T_numtype2 b)                       \
 | 
|---|
| 125 |     { return a op b; }                                      \
 | 
|---|
| 126 |                                                             \
 | 
|---|
| 127 |     template<class T1, class T2>                            \
 | 
|---|
| 128 |     static inline void prettyPrint(string& str,             \
 | 
|---|
| 129 |         prettyPrintFormat& format, const T1& t1,            \
 | 
|---|
| 130 |         const T2& t2)                                       \
 | 
|---|
| 131 |     {                                                       \
 | 
|---|
| 132 |         str += "(";                                         \
 | 
|---|
| 133 |         t1.prettyPrint(str, format);                        \
 | 
|---|
| 134 |         str += symbol;                                      \
 | 
|---|
| 135 |         t2.prettyPrint(str, format);                        \
 | 
|---|
| 136 |         str += ")";                                         \
 | 
|---|
| 137 |     }                                                       \
 | 
|---|
| 138 | }
 | 
|---|
| 139 | 
 | 
|---|
| 140 | BZ_DEFINE_BOOL_OP(Greater,>,">");
 | 
|---|
| 141 | BZ_DEFINE_BOOL_OP(Less,<,"<");
 | 
|---|
| 142 | BZ_DEFINE_BOOL_OP(GreaterOrEqual,>=,">=");
 | 
|---|
| 143 | BZ_DEFINE_BOOL_OP(LessOrEqual,<=,"<=");
 | 
|---|
| 144 | BZ_DEFINE_BOOL_OP(Equal,==,"==");
 | 
|---|
| 145 | BZ_DEFINE_BOOL_OP(NotEqual,!=,"!=");
 | 
|---|
| 146 | BZ_DEFINE_BOOL_OP(LogicalAnd,&&,"&&");
 | 
|---|
| 147 | BZ_DEFINE_BOOL_OP(LogicalOr,||,"||");
 | 
|---|
| 148 | 
 | 
|---|
| 149 | template<class T_numtype1, class T_cast>
 | 
|---|
| 150 | struct Cast {
 | 
|---|
| 151 |     typedef T_cast T_numtype;
 | 
|---|
| 152 |     static inline T_cast apply(T_numtype1 a)
 | 
|---|
| 153 |     { return a; }
 | 
|---|
| 154 | 
 | 
|---|
| 155 |     template<class T1>
 | 
|---|
| 156 |     static void prettyPrint(string& str, prettyPrintFormat& format,
 | 
|---|
| 157 |         const T1& a)
 | 
|---|
| 158 |     {
 | 
|---|
| 159 |         str += BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(T_cast);
 | 
|---|
| 160 |         str += "(";
 | 
|---|
| 161 |         a.prettyPrint(str, format);
 | 
|---|
| 162 |         str += ")";
 | 
|---|
| 163 |     }
 | 
|---|
| 164 | };
 | 
|---|
| 165 | 
 | 
|---|
| 166 | template<class T_numtype1>
 | 
|---|
| 167 | struct LogicalNot {
 | 
|---|
| 168 |     typedef bool T_numtype;
 | 
|---|
| 169 |     static inline bool apply(T_numtype1 a)
 | 
|---|
| 170 |     { return !a; }
 | 
|---|
| 171 | 
 | 
|---|
| 172 |     template<class T1>
 | 
|---|
| 173 |     static void prettyPrint(string& str, prettyPrintFormat& format,
 | 
|---|
| 174 |         const T1& a)
 | 
|---|
| 175 |     {
 | 
|---|
| 176 |         str += "!";
 | 
|---|
| 177 |         a.prettyPrint(str, format);
 | 
|---|
| 178 |     }
 | 
|---|
| 179 | };
 | 
|---|
| 180 | 
 | 
|---|
| 181 | template<class T_numtype1>
 | 
|---|
| 182 | struct BitwiseNot {
 | 
|---|
| 183 |     typedef T_numtype1 T_numtype;
 | 
|---|
| 184 |     static inline T_numtype apply(T_numtype1 a)
 | 
|---|
| 185 |     { return ~a; }
 | 
|---|
| 186 | 
 | 
|---|
| 187 |     template<class T1>
 | 
|---|
| 188 |     static void prettyPrint(string& os, prettyPrintFormat& format,
 | 
|---|
| 189 |         const T1& a)
 | 
|---|
| 190 |     {
 | 
|---|
| 191 |         str += "~";
 | 
|---|
| 192 |         a.prettyPrint(os,format);
 | 
|---|
| 193 |     }
 | 
|---|
| 194 | };
 | 
|---|
| 195 | 
 | 
|---|
| 196 | template<class T_numtype1>
 | 
|---|
| 197 | struct Negate {
 | 
|---|
| 198 |     typedef T_numtype1 T_numtype;
 | 
|---|
| 199 |     static inline T_numtype apply(T_numtype1 a)
 | 
|---|
| 200 |     { return -a; }
 | 
|---|
| 201 | 
 | 
|---|
| 202 |     template<class T1>
 | 
|---|
| 203 |     static void prettyPrint(string& str, prettyPrintFormat& format,
 | 
|---|
| 204 |         const T1& a)
 | 
|---|
| 205 |     {
 | 
|---|
| 206 |         str += "-";
 | 
|---|
| 207 |         a.prettyPrint(str, format);
 | 
|---|
| 208 |     }
 | 
|---|
| 209 | };
 | 
|---|
| 210 | BZ_NAMESPACE_END
 | 
|---|
| 211 | 
 | 
|---|
| 212 | #endif
 | 
|---|
| 213 | 
 | 
|---|
| 214 | 
 | 
|---|