| [221] | 1 | /*************************************************************************** | 
|---|
|  | 2 | * blitz/ops.h           Function objects for operators | 
|---|
|  | 3 | * | 
|---|
|  | 4 | * $Id: ops.h,v 1.1.1.1 1999-04-09 17:59:02 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.2  1998/03/14 00:04:47  tveldhui | 
|---|
|  | 27 | * 0.2-alpha-05 | 
|---|
|  | 28 | * | 
|---|
|  | 29 | * Revision 1.1  1997/07/16 14:51:20  tveldhui | 
|---|
|  | 30 | * Update: Alpha release 0.2 (Arrays) | 
|---|
|  | 31 | * | 
|---|
|  | 32 | */ | 
|---|
|  | 33 |  | 
|---|
|  | 34 | #ifndef BZ_OPS_H | 
|---|
|  | 35 | #define BZ_OPS_H | 
|---|
|  | 36 |  | 
|---|
|  | 37 | #ifndef BZ_BLITZ_H | 
|---|
|  | 38 | #include <blitz/blitz.h> | 
|---|
|  | 39 | #endif | 
|---|
|  | 40 |  | 
|---|
|  | 41 | #ifndef BZ_PROMOTE_H | 
|---|
|  | 42 | #include <blitz/promote.h> | 
|---|
|  | 43 | #endif | 
|---|
|  | 44 |  | 
|---|
|  | 45 | #ifndef BZ_MATHFUNC_H | 
|---|
|  | 46 | #include <blitz/mathfunc.h> | 
|---|
|  | 47 | #endif | 
|---|
|  | 48 |  | 
|---|
|  | 49 | BZ_NAMESPACE(blitz) | 
|---|
|  | 50 |  | 
|---|
|  | 51 | /* | 
|---|
|  | 52 | * Originally these function objects had no template arguments, e.g. | 
|---|
|  | 53 | * | 
|---|
|  | 54 | * struct Add { | 
|---|
|  | 55 | *     template<class T_numtype1, class T_numtype2> | 
|---|
|  | 56 | *     static inline BZ_PROMOTE(T_numtype1, T_numtype2) | 
|---|
|  | 57 | *     apply(T_numtype1 a, T_numtype2 b) | 
|---|
|  | 58 | *     { return a + b; } | 
|---|
|  | 59 | * }; | 
|---|
|  | 60 | * | 
|---|
|  | 61 | * This made for neater expression templates syntax.  However, there are | 
|---|
|  | 62 | * some situations in which users may want to override type promotion | 
|---|
|  | 63 | * for certain operations.  For example, in theoretical physics, there | 
|---|
|  | 64 | * are U1 objects which when multiplied yield U1 objects, but when added | 
|---|
|  | 65 | * yield a different type.  To allow for this kind of behaviour, function | 
|---|
|  | 66 | * objects have been changed to take template parameters: | 
|---|
|  | 67 | * | 
|---|
|  | 68 | * template<class T_numtype1, class T_numtype2> | 
|---|
|  | 69 | * struct Add { | 
|---|
|  | 70 | *     typedef BZ_PROMOTE(T_numtype1, T_numtype2) T_numtype; | 
|---|
|  | 71 | * | 
|---|
|  | 72 | *     static inline T_numtype apply(T_numtype1 a, T_numtype2 b) | 
|---|
|  | 73 | *     { return a + b; } | 
|---|
|  | 74 | * }; | 
|---|
|  | 75 | * | 
|---|
|  | 76 | * Type promotion is performed inside the function object.  The expression | 
|---|
|  | 77 | * templates code always looks inside the function object to determine | 
|---|
|  | 78 | * the type promotion, e.g. Add<int,float>::T_numtype | 
|---|
|  | 79 | * | 
|---|
|  | 80 | * Users are free to specialize these function objects for their own types. | 
|---|
|  | 81 | */ | 
|---|
|  | 82 |  | 
|---|
|  | 83 | #define BZ_DEFINE_OP(name,op,symbol)                        \ | 
|---|
|  | 84 | template<class T_numtype1, class T_numtype2>                \ | 
|---|
|  | 85 | struct name {                                               \ | 
|---|
|  | 86 | typedef BZ_PROMOTE(T_numtype1, T_numtype2) T_numtype;   \ | 
|---|
|  | 87 | \ | 
|---|
|  | 88 | static inline T_numtype                                 \ | 
|---|
|  | 89 | apply(T_numtype1 a, T_numtype2 b)                       \ | 
|---|
|  | 90 | { return a op b; }                                      \ | 
|---|
|  | 91 | \ | 
|---|
|  | 92 | template<class T1, class T2>                            \ | 
|---|
|  | 93 | static inline void prettyPrint(string& str,             \ | 
|---|
|  | 94 | prettyPrintFormat& format, const T1& t1,            \ | 
|---|
|  | 95 | const T2& t2)                                       \ | 
|---|
|  | 96 | {                                                       \ | 
|---|
|  | 97 | str += "(";                                         \ | 
|---|
|  | 98 | t1.prettyPrint(str, format);                        \ | 
|---|
|  | 99 | str += symbol;                                      \ | 
|---|
|  | 100 | t2.prettyPrint(str, format);                        \ | 
|---|
|  | 101 | str += ")";                                         \ | 
|---|
|  | 102 | }                                                       \ | 
|---|
|  | 103 | } | 
|---|
|  | 104 |  | 
|---|
|  | 105 | BZ_DEFINE_OP(Add,+,"+"); | 
|---|
|  | 106 | BZ_DEFINE_OP(Subtract,-,"-"); | 
|---|
|  | 107 | BZ_DEFINE_OP(Multiply,*,"*"); | 
|---|
|  | 108 | BZ_DEFINE_OP(Divide,/,"/"); | 
|---|
|  | 109 | BZ_DEFINE_OP(Modulo,%,"%"); | 
|---|
|  | 110 | BZ_DEFINE_OP(BitwiseXor,^,"^"); | 
|---|
|  | 111 | BZ_DEFINE_OP(BitwiseAnd,&,"&"); | 
|---|
|  | 112 | BZ_DEFINE_OP(BitwiseOr,|,"|"); | 
|---|
|  | 113 | BZ_DEFINE_OP(ShiftRight,>>,">>"); | 
|---|
|  | 114 | BZ_DEFINE_OP(ShiftLeft,<<,"<<"); | 
|---|
|  | 115 |  | 
|---|
|  | 116 | #define BZ_DEFINE_BOOL_OP(name,op,symbol)                   \ | 
|---|
|  | 117 | template<class T_numtype1, class T_numtype2>                \ | 
|---|
|  | 118 | struct name {                                               \ | 
|---|
|  | 119 | typedef bool T_numtype;                                 \ | 
|---|
|  | 120 | static inline bool                                      \ | 
|---|
|  | 121 | apply(T_numtype1 a, T_numtype2 b)                       \ | 
|---|
|  | 122 | { return a op b; }                                      \ | 
|---|
|  | 123 | \ | 
|---|
|  | 124 | template<class T1, class T2>                            \ | 
|---|
|  | 125 | static inline void prettyPrint(string& str,             \ | 
|---|
|  | 126 | prettyPrintFormat& format, const T1& t1,            \ | 
|---|
|  | 127 | const T2& t2)                                       \ | 
|---|
|  | 128 | {                                                       \ | 
|---|
|  | 129 | str += "(";                                         \ | 
|---|
|  | 130 | t1.prettyPrint(str, format);                        \ | 
|---|
|  | 131 | str += symbol;                                      \ | 
|---|
|  | 132 | t2.prettyPrint(str, format);                        \ | 
|---|
|  | 133 | str += ")";                                         \ | 
|---|
|  | 134 | }                                                       \ | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
|  | 137 | BZ_DEFINE_BOOL_OP(Greater,>,">"); | 
|---|
|  | 138 | BZ_DEFINE_BOOL_OP(Less,<,"<"); | 
|---|
|  | 139 | BZ_DEFINE_BOOL_OP(GreaterOrEqual,>=,">="); | 
|---|
|  | 140 | BZ_DEFINE_BOOL_OP(LessOrEqual,<=,"<="); | 
|---|
|  | 141 | BZ_DEFINE_BOOL_OP(Equal,==,"=="); | 
|---|
|  | 142 | BZ_DEFINE_BOOL_OP(NotEqual,!=,"!="); | 
|---|
|  | 143 | BZ_DEFINE_BOOL_OP(LogicalAnd,&&,"&&"); | 
|---|
|  | 144 | BZ_DEFINE_BOOL_OP(LogicalOr,||,"||"); | 
|---|
|  | 145 |  | 
|---|
|  | 146 | template<class T_numtype1, class T_cast> | 
|---|
|  | 147 | struct Cast { | 
|---|
|  | 148 | typedef T_cast T_numtype; | 
|---|
|  | 149 | static inline T_cast apply(T_numtype1 a) | 
|---|
|  | 150 | { return a; } | 
|---|
|  | 151 |  | 
|---|
|  | 152 | template<class T1> | 
|---|
|  | 153 | static void prettyPrint(string& str, prettyPrintFormat& format, | 
|---|
|  | 154 | const T1& a) | 
|---|
|  | 155 | { | 
|---|
|  | 156 | str += BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(T_cast); | 
|---|
|  | 157 | str += "("; | 
|---|
|  | 158 | a.prettyPrint(str, format); | 
|---|
|  | 159 | str += ")"; | 
|---|
|  | 160 | } | 
|---|
|  | 161 | }; | 
|---|
|  | 162 |  | 
|---|
|  | 163 | template<class T_numtype1> | 
|---|
|  | 164 | struct LogicalNot { | 
|---|
|  | 165 | typedef bool T_numtype; | 
|---|
|  | 166 | static inline bool apply(T_numtype1 a) | 
|---|
|  | 167 | { return !a; } | 
|---|
|  | 168 |  | 
|---|
|  | 169 | template<class T1> | 
|---|
|  | 170 | static void prettyPrint(string& str, prettyPrintFormat& format, | 
|---|
|  | 171 | const T1& a) | 
|---|
|  | 172 | { | 
|---|
|  | 173 | str += "!"; | 
|---|
|  | 174 | a.prettyPrint(str, format); | 
|---|
|  | 175 | } | 
|---|
|  | 176 | }; | 
|---|
|  | 177 |  | 
|---|
|  | 178 | template<class T_numtype1> | 
|---|
|  | 179 | struct BitwiseNot { | 
|---|
|  | 180 | typedef T_numtype1 T_numtype; | 
|---|
|  | 181 | static inline T_numtype apply(T_numtype1 a) | 
|---|
|  | 182 | { return ~a; } | 
|---|
|  | 183 |  | 
|---|
|  | 184 | template<class T1> | 
|---|
|  | 185 | static void prettyPrint(string& os, prettyPrintFormat& format, | 
|---|
|  | 186 | const T1& a) | 
|---|
|  | 187 | { | 
|---|
|  | 188 | str += "~"; | 
|---|
|  | 189 | a.prettyPrint(os,format); | 
|---|
|  | 190 | } | 
|---|
|  | 191 | }; | 
|---|
|  | 192 |  | 
|---|
|  | 193 | template<class T_numtype1> | 
|---|
|  | 194 | struct Negate { | 
|---|
|  | 195 | typedef T_numtype1 T_numtype; | 
|---|
|  | 196 | static inline T_numtype apply(T_numtype1 a) | 
|---|
|  | 197 | { return -a; } | 
|---|
|  | 198 |  | 
|---|
|  | 199 | template<class T1> | 
|---|
|  | 200 | static void prettyPrint(string& str, prettyPrintFormat& format, | 
|---|
|  | 201 | const T1& a) | 
|---|
|  | 202 | { | 
|---|
|  | 203 | str += "-"; | 
|---|
|  | 204 | a.prettyPrint(str, format); | 
|---|
|  | 205 | } | 
|---|
|  | 206 | }; | 
|---|
|  | 207 | BZ_NAMESPACE_END | 
|---|
|  | 208 |  | 
|---|
|  | 209 | #endif | 
|---|
|  | 210 |  | 
|---|
|  | 211 |  | 
|---|