44 #ifdef CEXMC_USE_CUSTOM_FILTER
50 #include <boost/variant/get.hpp>
51 #include <boost/format.hpp>
60 static const std::string opId[] =
61 {
"UNINITIALIZED",
"TOP",
"u -",
"!",
"*",
"/",
"+",
"-",
"<",
"<=",
62 ">",
">=",
"=",
"!=",
"&",
"|" };
64 std::stringstream value;
65 const Operator * op( boost::get< Operator >( &type ) );
69 value <<
"-op- " << opId[ op->type ];
73 const Function * fun( boost::get< Function >( &type ) );
74 value <<
"-fun- " << *fun;
77 std::stringstream format;
78 format <<
"%|" << level * printIndent <<
"t|";
79 std::cout << boost::format( format.str() ) << value.str() << std::endl;
81 for ( std::vector< Node >::const_iterator k( children.begin() );
82 k != children.end(); ++k )
84 const Subtree * subtree( boost::get< Subtree >( &*k ) );
88 subtree->Print( level + 1 );
92 const Leaf * leaf( boost::get< Leaf >( &*k ) );
94 PrintLeaf( leaf, level + 1 );
100 void Subtree::PrintLeaf(
const Leaf * leaf,
int level )
const
103 std::stringstream value;
105 if ( (
variable = boost::get< Variable >( leaf ) ) )
118 const Constant * constant( boost::get< Constant >( leaf ) );
119 const int * intConstant( boost::get< int >( constant ) );
120 const double * doubleConstant( boost::get< double >(
123 value << ( intConstant ? *intConstant : *doubleConstant );
126 std::stringstream format;
127 format <<
"%|" << level * printIndent <<
"t|";
128 std::cout << boost::format( format.str() ) << value.str() << std::endl;
132 BasicEval::~BasicEval()
137 bool BasicEval::operator()(
const Subtree & ast )
const
139 ScalarValueType retval( GetScalarValue( ast ) );
140 int * intRetval( NULL );
141 double * doubleRetval( NULL );
143 intRetval = boost::get< int >( &retval );
146 doubleRetval = boost::get< double >( &retval );
148 return doubleRetval ? bool( *doubleRetval ) : bool( *intRetval );
152 BasicEval::ScalarValueType BasicEval::GetScalarValue(
153 const Node & node )
const
155 const Subtree * ast( boost::get< Subtree >( &node ) );
159 const Operator * op( boost::get< Operator >( &ast->type ) );
162 ScalarValueType
left( 0 );
163 ScalarValueType
right( 0 );
164 int * intLeft( NULL );
165 double * doubleLeft( NULL );
166 int * intRight( NULL );
167 double * doubleRight( NULL );
168 bool isDoubleRetval(
false );
170 if ( ast->children.size() > 0 )
172 left = GetScalarValue( ast->children[ 0 ] );
173 intLeft = boost::get< int >( &
left );
176 doubleLeft = boost::get< double >( &
left );
188 if ( ast->children.size() > 1 )
190 right = GetScalarValue( ast->children[ 1 ] );
191 intRight = boost::get< int >( &
right );
194 doubleRight = boost::get< double >( &
right );
197 CexmcCFUnexpectedContext );
200 isDoubleRetval = doubleLeft || doubleRight;
212 return - *doubleLeft;
217 return ! *doubleLeft;
221 if ( isDoubleRetval )
222 return ( doubleLeft ? *doubleLeft : *intLeft ) *
223 ( doubleRight ? *doubleRight : *intRight );
225 return *intLeft * *intRight;
227 if ( isDoubleRetval )
228 return ( doubleLeft ? *doubleLeft : *intLeft ) /
229 ( doubleRight ? *doubleRight : *intRight );
231 return *intLeft / *intRight;
233 if ( isDoubleRetval )
234 return ( doubleLeft ? *doubleLeft : *intLeft ) +
235 ( doubleRight ? *doubleRight : *intRight );
237 return *intLeft + *intRight;
239 if ( isDoubleRetval )
240 return ( doubleLeft ? *doubleLeft : *intLeft ) -
241 ( doubleRight ? *doubleRight : *intRight );
243 return *intLeft - *intRight;
245 if ( isDoubleRetval )
246 return ( doubleLeft ? *doubleLeft : *intLeft ) <
247 ( doubleRight ? *doubleRight : *intRight );
249 return *intLeft < *intRight;
251 if ( isDoubleRetval )
252 return ( doubleLeft ? *doubleLeft : *intLeft ) <=
253 ( doubleRight ? *doubleRight : *intRight );
255 return *intLeft <= *intRight;
257 if ( isDoubleRetval )
258 return ( doubleLeft ? *doubleLeft : *intLeft ) >
259 ( doubleRight ? *doubleRight : *intRight );
261 return *intLeft > *intRight;
263 if ( isDoubleRetval )
264 return ( doubleLeft ? *doubleLeft : *intLeft ) >=
265 ( doubleRight ? *doubleRight : *intRight );
267 return *intLeft >= *intRight;
269 if ( isDoubleRetval )
270 return ( doubleLeft ? *doubleLeft : *intLeft ) ==
271 ( doubleRight ? *doubleRight : *intRight );
273 return *intLeft == *intRight;
275 if ( isDoubleRetval )
276 return ( doubleLeft ? *doubleLeft : *intLeft ) !=
277 ( doubleRight ? *doubleRight : *intRight );
279 return *intLeft != *intRight;
291 right = GetScalarValue( ast->children[ 1 ] );
292 intRight = boost::get< int >( &
right );
295 doubleRight = boost::get< double >( &
right );
321 right = GetScalarValue( ast->children[ 1 ] );
322 intRight = boost::get< int >( &
right );
325 doubleRight = boost::get< double >( &
right );
346 return GetFunScalarValue( *ast );
351 const Leaf & leaf( boost::get< Leaf >( node ) );
352 const Constant * constant( boost::get< Constant >( &leaf ) );
360 const Variable &
variable( boost::get< Variable >( leaf ) );
361 return GetVarScalarValue(
variable );
369 BasicEval::ScalarValueType BasicEval::GetFunScalarValue(
370 const Subtree & ast )
const
372 bool evalResult(
false );
373 ScalarValueType result( GetBasicFunScalarValue( ast, evalResult ) );
384 BasicEval::ScalarValueType BasicEval::GetVarScalarValue(
385 const Variable & )
const
393 BasicEval::ScalarValueType BasicEval::GetBasicFunScalarValue(
394 const Subtree & ast,
bool & result )
const
396 const Function & fun( boost::get< Function >( ast.type ) );
400 ScalarValueType arg( GetScalarValue( ast.children[ 0 ] ) );
401 int * intArg( NULL );
402 double * doubleArg( NULL );
404 intArg = boost::get< int >( &arg );
406 doubleArg = boost::get< double >( &arg );
411 return *doubleArg * *doubleArg;
413 return *intArg * *intArg;
418 return std::sqrt( *doubleArg );
420 return std::sqrt( *intArg );
static int variable(const string &name, double &result, const dic_type &dictionary)
void Print(G4Element &ele)